ZOJ|ZOJ-3879-Capture the Flag【模拟】【12th浙江省赛】

ZOJ-3879-Capture the Flag

Time Limit: 2 SecondsMemory Limit: 65536 KBSpecial Judge

In computer security, Capture the Flag (CTF) is a computer security competition. CTF contests are usually designed to serve as an educational exercise to give participants experience in securing a machine, as well as conducting and reacting to the sort of attacks found in the real world. Reverse-engineering, network sniffing, protocol analysis, system administration, programming, and cryptanalysis are all skills which have been required by prior CTF contests at DEF CON. There are two main styles of capture the flag competitions: attack/defense and jeopardy.
In an attack/defense style competition, each team is given a machine (or a small network) to defend on an isolated network. Teams are scored on both their success in defending their assigned machine and on their success in attacking other team’s machines. Depending on the nature of the particular CTF game, teams may either be attempting to take an opponent’s flag from their machine or teams may be attempting to plant their own flag on their opponent’s machine.
Recently, an attack/defense style competition called MCTF held by Marjar University is coming, and there are N teams which participate in the competition. In the beginning, each team has S points as initial score; during the competition, there are some checkpoints which will renew scores for all teams. The rules of the competition are as follows:
If a team has been attacked for a service P, they will lose N - 1 points. The lost points will be split equally and be added to the team(s) which attacks successfully. For example, there are 4 teams and Team A has been attacked by Team B and Team C, so Team A will lose 3 points, while Team B and Team C each will get 1.5 points.
If a team cannot maintain their service well, they will lose N - 1 points, which will be split equally too and be added to the team(s) which maintains the service well.
The score will be calculated at the checkpoints and then all attacks will be re-calculated. Edward is the organizer of the competition and he needs to write a program to display the scoreboard so the teams can see their scores instantly. But he doesn’t know how to write. Please help him!
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
【ZOJ|ZOJ-3879-Capture the Flag【模拟】【12th浙江省赛】】The first line contains four integers N (2 <= N <= 100) - the number of teams, Q - the number of services (1 <= Q <= 10), S - the initial points (0 <= S <= 100000) and C - the number of checkpoints (1 <= C <= 100).
For each checkpoint, there are several parts:
The first line contains an integer A - the number of the successful attacks. Then A lines follow and each line contains a message:
[The No. of the attacker] [The No. of the defender] [The No. of the service]
For example, “1 2 3” means the 1st team attacks the 2nd team in service 3 successfully. The No. of teams and services are indexed from 1. You should notice that duplicate messages are invalid because of the rules. Just ignore them.
Then there are Q lines and each line contains N integers. The jth number of the ith line indicating the jth team’s maintaining status of the ith service, where 1 means well and 0 means not well.
Finally there is an integer U (0 <= U <= 100), which describing the number of the queries. The following line contains U integers, which means Edward wants to know the score and the ranking of these teams.
Output
For each query L, output the score and the ranking of the Lth team. The relative error or absolute error of the score should be less than 10-5. The team with higher score gets higher rank; the teams with the same scores should have the same rank. It is guaranteed that the scores of any two teams are either the same or with a difference greater than 10-5.
Sample Input
1
4 2 2500 5
0
1 1 1 1
1 1 1 1
4
1 2 3 4
2
1 2 1
3 2 1
1 1 1 1
1 1 1 1
4
1 2 3 4
1
1 2 2
1 1 1 1
1 1 1 0
4
1 2 3 4
0
0 0 0 0
0 0 0 0
4
1 2 3 4
0
1 1 1 1
1 1 1 1
2
1 4
Sample Output
2500.00000000 1
2500.00000000 1
2500.00000000 1
2500.00000000 1
2501.50000000 1
2497.00000000 4
2501.50000000 1
2500.00000000 3
2505.50000000 1
2495.00000000 4
2502.50000000 2
2497.00000000 3
2499.50000000 1
2489.00000000 4
2496.50000000 2
2491.00000000 3
2499.50000000 1
2491.00000000 3
Hint
For C++ users, kindly use scanf to avoid TLE for huge inputs.
题目链接:ZOJ-3879
题目思路:
  • n: 电脑数量
  • q: 服务器种类
  • s: 初始分数
  • c: 操作次数
对于每个操作:
1.首先输入a:表示攻击成功次数;每次攻击有三个数值,att, def, ser 分别表示攻击方电脑编号,被攻击方电脑编号,以及被攻击的服务器编号。
注意:可能会有重复的相同的att, def, ser,只计算一次,需要去重。(eg:4 3 2出现过又出现4 3 2,则第二次不要。)
遍历服务器,防守方,攻击方。 如果 j 防守方的 k 服务器被攻击。防守方需要减去n-1的分数,n - 1的分数平均分给成功攻击 j 防守方 k 服务器的这几个攻击方。
2. 输入一个q * n的状态矩阵。0代表 i 号服务器 j 号电脑维护失败。1则代表成功
对于每个服务器i,开两个数组,zero:记录在当前服务器下,维护失败的电脑编号。one:记录成功。
维护失败:分数减去 n - 1
维护成功:分数加上 (n - 1)* zero.size() / one.size() //每个失败的电脑都分发出去n - 1的分数
最后:排序
注意: 排序从sort(p+ 1, p + 1 + n, cmp); 因为电脑从1开始存
计算rank,如果当前分数和前一项相同,则rank等于前一项,不然等于rank等于 正常排序结果。
注意: 计算完rank之后,要根据name,电脑编号重新排回来,不然会对之后结果产生影响
以下是代码:
#include #include #include #include #include #include #include #include #include #include #include #define ll long long #define inf 0xffffff using namespace std; #define EPS 1e-5 struct node { double score; int rank; int name; }p[105]; bool cmp(node a,node b) { return a.score > b.score; } bool comp2(node a,node b) { return a.name < b.name; } int main() { int t; scanf("%d",&t); while(t--) { int n,q,c; double s; scanf("%d%d%lf%d",&n,&q,&s,&c); for(int i = 0; i <= n; i++) { p[i].score = s; p[i].name = i; p[i].rank = 1; } while(c--) { int a; scanf("%d",&a); int vis[105][105][15] = {0}; for (int i = 0; i < a; i++) { int att,def,ser; scanf("%d%d%d",&att,&def,&ser); if (vis[att][def][ser]) continue; vis[att][def][ser] = 1; } for (int i = 1; i <= q; i++)//服务器 { for (int j = 1; j <= n; j++)//防守方 { int cnt = 0; for (int k = 1; k <= n; k++) //攻击方 { if(vis[k][j][i]) cnt++; } if (!cnt) continue; double add_score = (n - 1) * 1.0 / cnt; p[j].score -= n - 1; for (int k = 1; k <= n; k++) { if (vis[k][j][i]) p[k].score += add_score; } } } for (int i = 1; i <= q; i++) { vector zero; vector one; for (int j = 1; j <= n; j++) { int statu; scanf("%d",&statu); if (statu == 0) zero.push_back(j); else one.push_back(j); } for (int j = 0; j < zero.size(); j++) { p[zero[j]].score -= n - 1; } double add_score2 = ((n - 1) * zero.size() * 1.0 ) / one.size(); for (int j = 0; j

    推荐阅读