Задача A. Robot
type vector =array[0..11] of integer;
var x,y,p :vector;
tests,t,i,j,kk,res :integer;
procedure Work(a :vector);
var i, s : integer;
begin
s:=0;
for i:=1 to kk do
s:=s+abs(x[a[i]]-x[a[i-1]])+abs(y[a[i]]-y[a[i-1]]);
s:=s+abs(x[a[0]]-x[a[kk]])+abs(y[a[0]]-y[a[kk]]);
if s<res then res:=s;
end;
procedure Permulations(n :integer);
procedure Perm(i :integer);
var j,k:integer;
begin
if i=n then begin Work(p) {for j:=1 to kk do write(p[j],' ');writeln};end
else begin for j:=i+1 to n do
begin
Perm(i+1);
k:=p[i]; p[i]:=p[j]; p[j]:=k
end;
Perm(i+1);
k:=p[i];
for j:=i to n-1 do p[j]:=p[j+1];
p[n]:=k
end
end;
begin
Perm(1)
end;
begin
readln(tests);
for t:=1 to tests do begin
readln(x[0],y[0]);
readln(kk);
for i:=1 to kk do begin
readln(x[i],y[i]);
p[i]:=i;
end;
x[kk+1]:=x[0];y[kk+1]:=y[0];
res:=10000;
Permulations(kk);
writeln(res);
end;
end.
Задача B. Robot-2
var n, i, t, res, tests, k : longint;
way, w : string[11];
code : integer;
begin
readln(tests);
for t:=1 to tests do begin
readln(k);
n:=0;res:=0;
for i:=1 to k do begin
readln(way);
w:=way;delete(w,1,pos(' ',w));val(w,n,code);
if code>0 then n:=90;
if pos('LEFT',way)>0 then dec(res,n) else
if pos('RIGHT',way)>0 then inc(res,n) else
if pos('TURN',way)>0 then inc(res,180) else
if pos('HALT',way)>0 then break;
end;
writeln((res+3600000) mod 360);
end
end.
Задача C. Hacker vs Olympians
#include <stdio.h>
#include <string.h>
using namespace std;
#define MAXN 10000
#define MAXM 100000
int start[MAXN+1];
typedef struct { int from, to; } ARC;
ARC arc[MAXM];
int otherend[MAXM];
int cnt[MAXN];
char visited[MAXN];
void visit(int p)
{
int i;
if (visited[p]) return;
visited[p] = 1;
for (i = start[p]; i < start[p+1]; i++)
visit(otherend[i]);
}
int main()
{
int n, m, i, j, k;
scanf("%d %d", &n, &m);
memset(start, 0, sizeof(start));
for (i=0; i<m; i++)
{
scanf("%d %d", &arc[i].to, &arc[i].from);
arc[i].to--;
arc[i].from--;
start[arc[i].from]++;
}
for (i=0; i<n; i++) start[i+1] += start[i];
for (i=0; i<m; i++)
otherend[--start[arc[i].from]] = arc[i].to;
for (i=0; i<n; i++)
{
memset(visited, 0, sizeof(visited));
visit(i);
cnt[i] = 0;
for (j=0; j<n; j++)
if (visited[j]) cnt[i]++;
}
m = 0;
for (i=0; i<n; i++)
if (cnt[i] > m) m = cnt[i];
for (i=0; i<n; i++)
if (cnt[i] == m)
printf("%d ", i+1);
printf("\n");
return 0;
}