ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1041. Nikifor

I get WA on test 7. Could someone give me any hint?
Posted by tantian 20 Nov 2007 16:27
My mail: ttxxli@163.com
My code
Posted by tantian 20 Nov 2007 16:33
program ex;

const
  maxn=2000;
  maxm=50;
  zero=1e-15;

var
  g: Array[0..maxn, 1..maxm] of extended;
  cost: Array[1..maxn] of longint;
  from: Array[1..maxn] of longint;
  n, m: longint;
  ans: longint;
  num: Array[1..maxm] of longint;
  tot: longint;
  kk: Array[1..maxn] of longint;

procedure init;
var
  i, j: longint;
begin
  read(n, m);
  for i:=1 to n do
    for j:=1 to m do
      read(g[i, j]);
  for i:=1 to n do begin
    read(cost[i]);
    from[i]:=i;
  end;
end;

procedure qsort(l, r: longint);
var
  i, j: longint;
  x, y: longint;
  t: longint;
begin
  i:=l;
  j:=r;
  x:=cost[(l+r) shr 1];
  y:=from[(l+r) shr 1];
  while i<=j do begin
    while (cost[i]<x)or(cost[i]=x)and(from[i]<y) do inc(i);
    while (cost[j]>x)or(cost[j]=x)and(from[j]>y) do dec(j);
    if i<=j then begin
      g[0]:=g[i];
      g[i]:=g[j];
      g[j]:=g[0];
      t:=cost[i];
      cost[i]:=cost[j];
      cost[j]:=t;
      t:=from[i];
      from[i]:=from[j];
      from[j]:=t;
      inc(i);
      dec(j);
    end;
  end;
  if j>l then qsort(l, j);
  if i<r then qsort(i, t);
end;

function can(x: longint): boolean;
var
  i, j: longint;
  len1, len2: extended;
  sum: extended;
  co: extended;
  tt: extended;
  temp: Array[1..maxn] of extended;
begin
  for i:=1 to tot do begin
    sum:=0;
    len1:=0;
    len2:=0;
    for j:=1 to m do begin
      sum:=sum+g[x, j]*g[i, j];
      len1:=len1+g[x, j]*g[x, j];
      len2:=len2+g[kk[i], j]*g[kk[i], j];
    end;
    len1:=sqrt(len1);
    len2:=sqrt(len2);
    if abs(len1)<=zero then
      exit(false);
    co:=sum/len1/len2;
    tt:=len1*co;
    for j:=1 to m do
      temp[j]:=g[kk[i], j]*tt/len2;
    for j:=1 to m do
      g[x, j]:=g[x, j]-temp[j];
  end;
  for j:=1 to m do
    if abs(g[x, j])>zero then
      exit(true);
  exit(false);
end;

procedure solve;
var
  i: longint;
begin
  qsort(1, n);
  tot:=1;
  num[1]:=from[1];
  ans:=ans+cost[1];
  kk[1]:=1;
  for i:=2 to n do
    if can(i) then begin
      inc(tot);
      kk[tot]:=i;
      num[tot]:=from[i];
      ans:=ans+cost[i];
      if tot=m then
        exit;
    end;
end;

procedure print;
var
  i, j: longint;
  t: longint;
begin
  if tot<m then
    writeln(0)
  else begin
    writeln(ans);
    for i:=1 to m do
      for j:=i+1 to m do
        if num[i]>num[j] then begin
          t:=num[i];
          num[i]:=num[j];
          num[j]:=t;
        end;
    for i:=1 to m do
      writeln(num[i]);
  end;
end;

begin
  init;
  solve;
  print;
end.