[SGU]138. Games of Chess

来源:互联网 发布:用户行为数据分析 编辑:程序博客网 时间:2024/05/16 12:17

Analysis

    又是一道构造题。这道题想了很久没想出来……最后根据网上别人的题解构造了一下就AC了……总的比赛场数为每个人的相加除以二,构造方法是,先把人按比赛场次由多到少排序,然后按这个顺序,把每个人除最后一场外安排为胜场,最后一场为负场,直到胜场全部排完,剩下的人都安排负场。

Accepted Code

var    tot,n,i,j,bag,tmp:longint;    win,lost,num,a:array[0..10100] of longint;begin    readln(n);    tot:=0;    for i:=1 to n do    begin        read(a[i]);        num[i]:=i;        tot:=tot+a[i];    end;    tot:=tot shr 1;    writeln(tot);    for i:=1 to n-1 do        for j:=i+1 to n do            if a[i]<a[j] then            begin                tmp:=a[i];                a[i]:=a[j];                a[j]:=tmp;                tmp:=num[i];                num[i]:=num[j];                num[j]:=tmp;            end;    win[1]:=num[1];    j:=1;    bag:=1;    for i:=2 to tot do    begin        if bag=a[j]-1 then        begin            lost[i]:=num[j];            inc(j);            bag:=1;            win[i]:=num[j];        end        else            if bag=a[j] then            begin                inc(j);                bag:=1;                win[i]:=num[j];            end            else            begin                win[i]:=num[j];                inc(bag);            end;    end;    for i:=1 to tot do    begin        if lost[i]>0 then            continue;        if bag=a[j] then        begin            inc(j);            lost[i]:=num[j];            bag:=1;        end        else        begin            lost[i]:=num[j];            inc(bag);        end;    end;    for i:=1 to tot do        writeln(win[i],' ',lost[i]);end.


原创粉丝点击