Question link: Click the open link
Question: n teams, M games
The n lines below indicate the names of n teams.
The following M-game shows the score of the two teams in the game.
-1 indicates that we can enter the value as needed.
The number of such games cannot exceed 12.
Please:
Winning a team scored 2 points, scored 1 point, and scored 0 points.
Find the best ranking and worst name for each team.
Each game has only three States, and there are only 12 at most. Therefore, the status may be 3 ^ 12.
#include <cstdio>#include <algorithm>#include <cstring>#include <string>#include <map>#include <vector>#include <iostream>using namespace std;const int N = 20 + 2;map<string, int> id;vector<int> l, r;char s[200], ss[200];int n;int base[N], tob[N], put[N], rank[N];int mx[N], mi[N];string name[N];bool cmp(int x, int y) {return tob[x] > tob[y];}void upmx(int& z, int x) {if (z == -1)z = x;else if (z < x)z = x;}void upmi(int& z, int x) {if (z == -1)z = x;else if (z > x)z = x;}void dfs(int dep) {if (dep == l.size()) {for (int i = 1; i <= n; ++i) {rank[i] = i;tob[i] = base[i];}for (int i = 0; i < l.size(); ++i) {if (put[i] == 0) {++ tob[l[i]];++ tob[r[i]];} else if (put[i] == 1) {tob[l[i]] += 3;} else tob[r[i]] += 3;}sort(rank + 1, rank + 1 + n, cmp);int cur = 1;upmx(mx[rank[1]], 1);upmi(mi[rank[1]], 1);for (int i = 2; i <= n; ++i) {if (tob[rank[i]] != tob[rank[i - 1]])cur = i;upmx(mx[rank[i]], cur);upmi(mi[rank[i]], cur);}} else {put[dep] = 0;dfs(dep + 1);put[dep] = 1;dfs(dep + 1);put[dep] = 2;dfs(dep + 1);}}void pu(int x) {if (x == 1)printf("1st");else if (x == 2) {printf("2nd");} else if (x == 3) {printf("3rd");} else {printf("%dth", x);}}int main() {int m, a, b, x, y, len, cas = 0;while (~scanf("%d%d", &n, &m)) {if (0 == n && 0 == m)break;if (cas == 0)cas = 1;elseputs("");id.clear();for (int i = 1; i <= n; ++i) {cin >> name[i];id[name[i]] = i;}memset(base, 0, sizeof base);l.clear();r.clear();while (m -- > 0) {scanf("%s vs %s %d %d", s, ss, &a, &b);len = strlen(ss);ss[--len] = '\0';x = id[s];y = id[ss];if (a == -1) {l.push_back(x);r.push_back(y);} else {if (a == b) {++ base[x];++ base[y];} else if (a > b)base[x] += 3;else if (b > a)base[y] += 3;}}memset(mx, -1, sizeof mx);memset(mi, -1, sizeof mi);dfs(0);for (int i = 1; i <= n; ++i) {printf("Team ");cout << name[i];printf(" can finish as high as ");pu(mi[i]);printf(" place and as low as ");pu(mx[i]);puts(" place.");}}return 0;}
Uvalive 4887 soccer pressure + Simulation