-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathpoj_1030_Rating.cpp
More file actions
169 lines (169 loc) · 5.18 KB
/
poj_1030_Rating.cpp
File metadata and controls
169 lines (169 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//把只参加一场的另一场名次赋值为同一场同名次队伍另一场的名次,但是不直接赋值,而
//先缓存(nex),并统计缓存次数(cgcnt),如果缓存仅一次,即没有总名次不同队伍的
//冲突,则真正把另一场赋值为nex。之后进行第一次排序。
//对于只参加一场而无其他参加两场的同名次队伍时,判断是否可合理插入,记录插入位置
//之后进行第二次排序,针对这次处理的这类队伍进行比较。
//输出,终名次相同者输出在同一行。
//单独写了完整的名次比较函数Compab(),函数中顺带处理了前两点所需数据。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
const int maxn = 101;
typedef struct
{
int rt[2];
int num;
int nex;
int cgcnt;
int share;
} TEAM;
TEAM te[maxn];
int n, m;
char buf[10001];
void ReadData(int n, int ith)
{
int i, j, rank, cnt, tnum;
for (i = rank = 1; i <= n; ++i, rank += cnt)
{
gets(buf);
for (j = cnt = 0; buf[j]; ++j)
{
if (isdigit(buf[j]))
{
sscanf(buf + j, "%d", &tnum);
te[tnum].rt[ith] = rank, ++cnt;
te[tnum].num = tnum;
while (isdigit(buf[j]))
++j;
--j;
}
}
}
}
int Compab(TEAM &a, TEAM &b)
{
if ((a.rt[0] || a.rt[1]) && !(b.rt[0] || b.rt[1]))
return -1;
if (!(a.rt[0] || a.rt[1]) && (b.rt[0] || b.rt[1]))
return 1;
if (a.cgcnt == -1 && b.cgcnt != -1)
return a.rt[0] ? a.rt[0] - b.rt[0] : a.rt[1] - b.rt[1];
if (b.cgcnt == -1 && a.cgcnt != -1)
return b.rt[0] ? a.rt[0] - b.rt[0] : a.rt[1] - b.rt[1];
if (a.cgcnt == -1 && b.cgcnt == -1)
{
if (a.share == b.share)
return a.rt[0] ? a.rt[0] - (b.rt[0] ? b.rt[0] : b.rt[1]) : a.rt[1] - (b.rt[1] ? b.rt[1] : b.rt[0]);
return a.share - b.share;
}
if (a.rt[0] && a.rt[1] && !(b.rt[0] && b.rt[1]))
{
if (b.rt[0] == a.rt[0])
{
if (!b.nex || b.nex + b.rt[0] == a.rt[0] + a.rt[1])
b.nex = a.rt[1], b.share = a.num;
else
++b.cgcnt;
}
else if (b.rt[1] == a.rt[1])
{
if (!b.nex || b.nex + b.rt[1] == a.rt[0] + a.rt[1])
b.nex = a.rt[0], b.share = a.num;
else
++b.cgcnt;
}
return -1;
}
if (b.rt[0] && b.rt[1] && !(a.rt[0] && a.rt[1]))
{
if (a.rt[0] == b.rt[0])
{
if (!a.nex || a.nex + a.rt[0] == b.rt[0] + b.rt[1])
a.nex = b.rt[1], a.share = b.num;
else
++a.cgcnt;
}
else if (a.rt[1] == b.rt[1])
{
if (!a.nex || a.nex + a.rt[1] == b.rt[0] + b.rt[1])
a.nex = b.rt[0], a.share = b.num;
else
++a.cgcnt;
}
return 1;
}
if ((a.rt[0] && a.rt[1]) && (b.rt[0] && b.rt[1]))
return a.rt[0] + a.rt[1] - b.rt[0] - b.rt[1];
if ((a.rt[0] || a.rt[1]) && (b.rt[0] || b.rt[1]))
{
if (a.rt[0] && b.rt[0])
return a.rt[0] - b.rt[0];
if (a.rt[1] && b.rt[1])
return a.rt[1] - b.rt[1];
return 0;
}
}
int comp(const void *a, const void *b)
{
return Compab(*(TEAM *)a, *(TEAM *)b) ? Compab(*(TEAM *)a, *(TEAM *)b) : (*(TEAM *)a).num - (*(TEAM *)b).num;
}
int Judge(TEAM &a, int ith)
{
int i, j;
for (i = 1; te[i].rt[0] && te[i].rt[1] && i < maxn; ++i)
if (te[i].rt[ith] > a.rt[ith])
break;
j = i;
for (; te[i].rt[0] && te[i].rt[1] && i < maxn; ++i)
if (te[i].rt[ith] <= a.rt[ith])
break;
if (j < maxn && (Compab(te[j - 1], te[j]) == 0 || te[i].rt[0] && te[i].rt[1]))
return a.rt[0] = a.rt[1] = a.cgcnt = 0;
return a.cgcnt = -1, j;
}
int main()
{
int i, j, k;
while (scanf("%d\n", &n) != EOF)
{
memset(te, 0, sizeof(te));
for (i = 0; i < maxn; ++i)
te[i].num = i;
ReadData(n, 0);
scanf("%d\n", &m);
ReadData(m, 1);
for (i = 1; i < maxn; ++i)
for (j = i + 1; j < maxn; ++j)
Compab(te[i], te[j]);
for (i = 1; i < maxn; ++i)
{
if (te[i].cgcnt)
te[i].rt[0] = te[i].rt[1] = 0;
else if (te[i].nex)
{
if (te[i].rt[0])
te[i].rt[1] = te[i].nex;
else
te[i].rt[0] = te[i].nex;
}
}
qsort(te + 1, 100, sizeof(TEAM), comp);
for (i = 1; i < maxn; ++i)
{
if (te[i].rt[0] && !te[i].rt[1])
te[i].share = Judge(te[i], 0);
else if (!te[i].rt[0] && te[i].rt[1])
te[i].share = Judge(te[i], 1);
}
qsort(te + 1, 100, sizeof(TEAM), comp);
for (i = 1; (te[i].rt[0] || te[i].rt[1]) && i < maxn; i = j)
{
printf("%d", te[i].num);
for (j = i + 1; (te[j].rt[0] || te[j].rt[1]) && !Compab(te[i], te[j]); ++j)
printf(" %d", te[j].num);
printf("\n");
}
}
return 0;
}