ZOJ 3879 Capture the Flag

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5527


题面:



Capture the Flag

Time Limit: 2 Seconds       Memory Limit: 65536 KB       Special 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:

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.


解题:

    省赛时,没有看的一道题,当时还觉得挺可惜的,但写了之后才知道,现场很难写出来。后来写了将近2个小时,被各种小错误坑死。

   讲一下几个点吧。

   1.攻击次数重复时,只算1次。

   2.不要重复减去分数

   3.每次checkpoint都是延续之前的,不要重置

   4.小心没有人成功幸存的情况

   5.处理排名需注意,但精度只要0.00001就够了

   6.各种下标要注意,被坑死了...



代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
struct team  //小组 
{
    double score;
    int no;
}store[105];
bool attack[11][105][105];//记录攻击 
int attacklog[110];//记录谁攻击成功了 
bool maintain[11][105];//保持的状态 
int rank[105];//排名数组 
bool cmp(team a,team b)
{
    return a.score>b.score;
}
int main()
{
	int n,q,s,c,t,a,fm,to,service,cnt,tmp,u;
	double check;
	scanf("%d",&t);
	while(t--)
	{
	   scanf("%d%d%d%d",&n,&q,&s,&c);       
	   //记录序号 
	   for(int i=1;i<=n;i++)
       {
         store[i].score=s;
         store[i].no=i;
       }
       for(int i=1;i<=c;i++)
       {
          scanf("%d",&a);
          memset(attack,0,sizeof(attack));
          for(int j=1;j<=a;j++)
          {
             scanf("%d%d%d",&fm,&to,&service);
             //避免了重复 
             attack[service][to][fm]=1;
          }
          for(int j=1;j<=q;j++)
          {
             for(int k=1;k<=n;k++)
             {
                cnt=0;
                for(int m=1;m<=n;m++)
                {
                    if(attack[j][k][m])
                    {
                      attacklog[cnt++]=m;
                    }
                }
                if(cnt)
                {
                    for(int i=1;i<=n;i++)
                    {
                        if(store[i].no==k)
                        {
                        	//不要减多了 
                           store[i].score-=(n-1);
                           break; 
                        }
                    }
                    double addscore=1.0*(n-1)/cnt;//每人加的分 
                    for(int m=0;m<cnt;m++)
                    {
                        for(int x=1;x<=n;x++)
                        {
                        	//攻击成功加分 
                            if(store[x].no==attacklog[m])
                            store[x].score+=addscore;
                        }
                    }
                }
             }
          }
          memset(maintain,0,sizeof(maintain));
          for(int j=1;j<=q;j++)
          {
             for(int k=1;k<=n;k++)
             {
                 scanf("%d",&tmp);
                 if(tmp)
                 {
                    maintain[j][k]=1;
                 }
             }
          }
          for(int j=1;j<=q;j++)
          {
             cnt=0;
             for(int k=1;k<=n;k++)
             {
                if(maintain[j][k])
                cnt++;
                else
                {
                    for(int x=1;x<=n;x++)
                    {
                        if(store[x].no==k)
                        {
                            store[x].score-=(n-1);
                        }
                    }
                }
             }
             if(cnt)
             {
             	//幸存每人加的分 
                double addscore=1.0*(n-cnt)*(n-1)/cnt;
                for(int k=1;k<=n;k++)
                {
                   if(maintain[j][k])
                   {
                     for(int x=1;x<=n;x++)
                     {
                        if(store[x].no==k)
                        store[x].score+=addscore;
                     }
                   }
                }
             }
          }
          sort(store+1,store+n+1,cmp);
          scanf("%d",&u);
          //排名处理 
          rank[1]=1;
          for(int z=2;z<=n;z++)
          {
          	if((store[z-1].score-store[z].score)<=0.00001)
          	  rank[z]=rank[z-1];
          	else rank[z]=z;
          }
          for(int k=1;k<=u;k++)
          {
             scanf("%d",&tmp);
             for(int x=1;x<=n;x++)
             {
                if(store[x].no==tmp)
                {
				  printf("%.8lf %d\n",store[x].score,rank[x]);
                }
             }
          }
       }
    }
	return 0;
}


Capture the Flag

Time Limit: 2 Seconds       Memory Limit: 65536 KB       Special 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:

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.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值