这个题应该是个优先队列的模版题 当时比赛的时候没时间做先贴一下大神的代码好好学习学习

B - Gandalf vs the Balrog

Time Limit:2000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status Practice SPOJ AMR12B

Description

'We fought far under the living earth, where time is not counted. Ever he clutched me, and ever I hewed him, till at last he fled into dark tunnels. Ever up now we went, until we came to the Endless Stair. Out he sprang, and even as I came behind, he burst into new flame. Those that looked up from afar thought that the mountain was crowned with storm. Thunder they heard, and lightning, they said, smote upon Celebdil, and leaped back broken into tongues of fire.' - Gandalf, describing his fight against the Balrog.
Although Gandalf would not go into the details of his battle, they can be summarized into the following simplified form: both Gandalf and the Balrog have a set of N attacks they can use (spells, swords, brute-force strength etc.). These attacks are numbered from 1 to N in increasing order of Power. When each has chosen an attack, in general, the one with the higher power wins. However, there are a few ("M") anomalous pairs of attacks, in which the lesser-powered attack wins.
Initially, Gandalf picks an attack. Then the Balrog counters it with one of the remaining attacks. If the Balrog's counter does not defeat Gandalf's, then we say Gandalf receives a score of 2. If however it does, then Gandalf has exactly one more opportunity to pick an attack that will defeat the Balrog's. If he manages to defeat him now, his score will be 1, whereas if he is still unable to defeat him, his score will be 0.
Your task is to determine, given N and the M anomalous pairs of attacks, what will be Gandalf's score, given that both play optimally. Further, in case Gandalf gets a score of 2, you must also determine which attack he could have chosen as his first choice.
Note 1: The Balrog can choose only one attack, whereas Gandalf can choose upto two.
Note 2: The relation A defeats B is not transitive within the attacks. For example, attack A can defeat attack B, attack B can defeat attack C, and attack C can defeat attack A.
Note 3: Between any two attacks A and B, either attack A defeats attack B or attack B defeats attack A.
Input (STDIN):
The first line will consist of the integer T, the number of test-cases.
Each test case begins with a single line containing two integers N and M.
This is followed by M lines consisting of 2 integers each x and y, denoting that x and y are an anomalous pair.
Output (STDOUT):
For each test-case, output a single line either
2 A, if Gandalf can defeat any attack the Balrog chooses if he picks attack A,
1, if Gandalf can choose an attack such that even if the Balrog chooses an attack to defeat him, he can choose an attack to defeat the Balrog's chosen card,
0, if none of the above two options are possible for all possible choices of Gandalf's attack(s).
Between successive test cases, there should not be any blank lines in the output.
Constraints:
1 <= T <= 15
3 <= N <= 1,000,000
0 <= M <= min(N(N-1)/2, 300,000)
1 <= x < y <= N for all the anomalous pairs (x,y)
The sum of M over all test-cases will not exceed 300,000.
Time Limit: 4s
Memory Limit: 64MB
Sample Input:
2
3 0
3 1
1 3 
Sample Output:
2 3
1
Notes/Explanation of Sample Input:
In the first case, attack 3 can beat both attacks 1 and 2. So Gandalf just chooses attack 3.
In the second case, attack 1 beats 3 which beats 2 which beats 1. No matter which attack Gandalf chooses, the Balrog can pick the one which defeats his, but then he can pick the remaining attack and defeat the Balrog's.

'We fought far under the living earth, where time is not counted. Ever he clutched me, and ever I hewed him, till at last he fled into dark tunnels. Ever up now we went, until we came to the Endless Stair. Out he sprang, and even as I came behind, he burst into new flame. Those that looked up from afar thought that the mountain was crowned with storm. Thunder they heard, and lightning, they said, smote upon Celebdil, and leaped back broken into tongues of fire.' - Gandalf, describing his fight against the Balrog.

Although Gandalf would not go into the details of his battle, they can be summarized into the following simplified form: both Gandalf and the Balrog have a set of N attacks they can use (spells, swords, brute-force strength etc.). These attacks are numbered from 1 to N in increasing order of Power. When each has chosen an attack, in general, the one with the higher power wins. However, there are a few ("M") anomalous pairs of attacks, in which the lesser-powered attack wins.

Initially, Gandalf picks an attack. Then the Balrog counters it with one of the remaining attacks. If the Balrog's counter does not defeat Gandalf's, then we say Gandalf receives a score of 2. If however it does, then Gandalf has exactly one more opportunity to pick an attack that will defeat the Balrog's. If he manages to defeat him now, his score will be 1, whereas if he is still unable to defeat him, his score will be 0.

Your task is to determine, given N and the M anomalous pairs of attacks, what will be Gandalf's score, given that both play optimally. Further, in case Gandalf gets a score of 2, you must also determine which attack he could have chosen as his first choice.

Note 1: The Balrog can choose only one attack, whereas Gandalf can choose upto two.

Note 2: The relation A defeats B is not transitive within the attacks. For example, attack A can defeat attack B, attack B can defeat attack C, and attack C can defeat attack A.

Note 3: Between any two attacks A and B, either attack A defeats attack B or attack B defeats attack A.

Input (STDIN):

The first line will consist of the integer T, the number of test-cases.

Each test case begins with a single line containing two integers N and M.

This is followed by M lines consisting of 2 integers each x and y, denoting that x and y are an anomalous pair.

Output (STDOUT):

For each test-case, output a single line either

2 A, if Gandalf can defeat any attack the Balrog chooses if he picks attack A,

1, if Gandalf can choose an attack such that even if the Balrog chooses an attack to defeat him, he can choose an attack to defeat the Balrog's chosen card,

0, if none of the above two options are possible for all possible choices of Gandalf's attack(s).

Between successive test cases, there should not be any blank lines in the output.

Constraints:

1 <= T <= 15

3 <= N <= 1,000,000

0 <= M <= min(N(N-1)/2, 300,000)

1 <= x < y <= N for all the anomalous pairs (x,y)

The sum of M over all test-cases will not exceed 300,000.

Sample Input:

2

3 0

3 1

1 3

Sample Output:

2 3

1

Notes/Explanation of Sample Input:

In the first case, attack 3 can beat both attacks 1 and 2. So Gandalf just chooses attack 3.

In the second case, attack 1 beats 3 which beats 2 which beats 1. No matter which attack Gandalf chooses, the Balrog can pick the one which defeats his, but then he can pick the remaining attack and defeat the Balrog's.

 1 #include <iostream>
 2
 3 using namespace std;
 4
 5 int array[1000001];
 6
 7 int main()
 8 {
 9     int t, n, m, x, y;
10     cin >> t;
11
12     while (t--)
13     {
14         cin >> n >> m;
15
16         for (int i = 1; i <= n; i++)
17         {
18             array[i] = n - i;
19         }
20
21         for (int i = 0; i < m; i++)
22         {
23             cin >> x >> y;
24             array[x]--;
25             array[y]++;
26         }
27
28         int ok = 0;
29         for (int i = 1; i <= n; i++)
30         {
31             if (array[i] == 0)
32             {
33                 ok = i;
34                 break;
35             }
36         }
37
38         if (ok)
39         {
40             cout << 2 << ' ' << ok << endl;
41         }
42         else
43         {
44             cout << 1 << endl;
45         }
46     }
47     return 0;
48 }

View Code

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<math.h>
 4 #include<algorithm>
 5 #include<string>
 6 #include<iostream>
 7 #include<vector>
 8 #define N 1001000
 9
10 using namespace std;
11
12 int in[N];
13 int out[N];
14 int n,m;
15 int main()
16 {
17     int cas;
18     scanf("%d",&cas);
19     while (cas--){
20         scanf("%d%d",&n,&m);
21         memset(in,0,sizeof(in));
22         memset(out,0,sizeof(out));
23
24         for (int i=1;i<=m;i++){
25             int x,y;
26             scanf("%d%d",&x,&y);
27             in[y]++;
28             out[x]++;
29         }
30         int i = n;
31         while (i>=1 && !(in[i]==0 && out[i]>=(n - i))){
32             i--;
33
34         }
35         if (i>=1){
36             printf("2 %d\n",i);
37         }else{
38             if (m == (n-1)*n /2)
39                 printf("0\n");
40             else
41                 printf("1\n");
42         }
43     }
44     return 0;
45 }

View Code

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstdlib>
 5 #include <cmath>
 6 #include <cstring>
 7 #include <cctype>
 8 #include <climits>
 9 #include <ctime>
10 #include <vector>
11 #include <set>
12 #include <stack>
13 #include <sstream>
14 #include <iomanip>
15 #define MAX 1000010
16 #define CLR(arr,val) memset(arr,val,sizeof(arr))
17
18 using namespace std;
19
20 int cards[MAX], defeated[MAX];
21
22 int main()
23 {
24     std::ios::sync_with_stdio(false);
25 #ifndef ONLINE_JUDGE
26     freopen( "in.txt", "r", stdin );
27     //freopen( "out.txt", "w", stdout );
28     clock_t program_start, program_end;
29     program_start = clock();
30 #endif
31     int T, N, M, x, y, max_card;
32     cin >> T;
33     while ( T-- )
34     {
35         cin >> N >> M;
36         for ( int i = 1; i <= N; ++i )
37         {
38             cards[i] = 0;
39             defeated[i] = 0;
40         }
41         for ( int i = 0; i < M; ++i )
42         {
43             cin >> x >> y;
44             cards[x]++; defeated[y]++;
45         }
46         max_card = -1;
47         for ( int i = N; i >= 1; --i )
48             if ( cards[i] == N - i && defeated[i] == 0 )
49             {
50                 max_card = i;
51                 break;
52             }
53         if ( max_card != -1 )
54             cout << "2 " << max_card << endl;
55         else
56             cout << "1" << endl;
57     }
58
59 #ifndef ONLINE_JUDGE
60     program_end = clock();
61     cerr << "Time consumed: " << endl << ( program_end - program_start ) << " MS" << endl;
62 #endif
63 }

View Code

转载于:https://www.cnblogs.com/lwy-kitty/p/3203311.html

SPOJ AMR12B 720相关推荐

  1. bzoj 2588 Spoj 10628. Count on a tree (可持久化线段树)

    Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MB Submit: 7669  Solved: 1894 [Sub ...

  2. BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster( 后缀数组 + 二分 + RMQ + 树状数组 )

    全部串起来做SA, 在按字典序排序的后缀中, 包含每个询问串必定是1段连续的区间, 对每个询问串s二分+RMQ求出包含s的区间. 然后就是求区间的不同的数的个数(经典问题), sort queries ...

  3. SPOJ GSS3-Can you answer these queries III-分治+线段树区间合并

    Can you answer these queries III SPOJ - GSS3 这道题和洛谷的小白逛公园一样的题目. 传送门: 洛谷 P4513 小白逛公园-区间最大子段和-分治+线段树区间 ...

  4. SPOJ ATOMS - Atoms in the Lab

    题目链接:http://www.spoj.com/problems/ATOMS/ 题目大意:有N个原子,他们每秒分裂成K个新原子,新原子也能继续分裂.问如果要控制他的数量为M以内,应在什么时候使其停止 ...

  5. PTA 基础编程题目集 7-20 打印九九口诀表 C语言

    PTA 基础编程题目集 7-20 打印九九口诀表 C语言 下面是一个完整的下三角九九口诀表: 本题要求对任意给定的一位正整数N,输出从11到NN的部分口诀表. 输入格式: 输入在一行中给出一个正整数N ...

  6. 7-20 打印九九口诀表 (C语言)

    7-20 打印九九口诀表 (15 分)下面是一个完整的下三角九九口诀表: 11=1 12=2 22=4 13=3 23=6 33=9 14=4 24=8 34=12 44=16 15=5 25=10 ...

  7. SPOJ 375. Query on a tree (树链剖分)

    题目链接: http://www.spoj.com/problems/QTREE/ 375. Query on a tree Problem code: QTREE You are given a t ...

  8. SPOJ 694 Distinct Substrings(后缀数组)

    题目链接:http://www.spoj.com/problems/DISUBSTR/ 题意:给定一个串,求不同的字串的个数. 思路:每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相 ...

  9. spoj Pattern Find(kmp)

    解法1 先计算模式串的前缀函数,然后通过前缀函数来搜索文本串 代码参考: https://github.com/wuli2496/OJ/blob/master/spoj/Pattern%20Find/ ...

最新文章

  1. 对Linux内核中进程上下文和中断上下文的理解
  2. 2017苏州太湖国际马拉松 半程成绩单
  3. Pandas 基础(9) - 组合方法 merge
  4. Linux内核将用Nftables替代iptables
  5. Mac电脑上的Safari运行缓慢,卡的要死,该怎么解决?
  6. 神州信息盘活农村三资,以金融科技服务三农助推乡村振兴
  7. 800*480bmp图片显示
  8. linux在服务器上下载文件,上传下载文件到Linux服务器
  9. 淘宝、天猫按关键词搜索商品API接口返回数据展示
  10. 如何将报表附件组件FastReport VCL旧版本升级到最新版本?
  11. Python2/3的中、英文字符编码与解码输出: UnicodeDecodeError: 'ascii' codec can't decode/encode...
  12. 本悟法师:信仰,让孤独走开
  13. MTK芯片处理器对比资料图
  14. 微软数据分析服务 - Microsoft Clarity
  15. ps去掉图片上的文字的6种方法
  16. focaltech(敦泰)触摸屏驱动Ft5306.c学习记录
  17. 2017电子设计大赛-光电科技协会板球控制系统
  18. commander入门教程及高级用法
  19. 双模sa_买5G手机一定要选5G双模手机,SA/NSA你都了解吗?
  20. 标准流程Invalid bound statement (not found)出现原因和最特殊的解决方法

热门文章

  1. 优雅的使用Js或CSS处理文本的截断与展示
  2. php svn客户端
  3. 【数据校验杂谈】循环冗余检验 (CRC) 算法原理
  4. Developer Express右键菜单显示汉化
  5. 国产手机可以复制苹果的成功吗?
  6. windows WEB 服务器安全策略
  7. 通过脚本下派WsusAgent3.0.exe(续)
  8. 我国企业对开源社区的贡献度_开源社区对我意味着什么
  9. ajax请求成功和失败方法_创新需要反馈和失败的新方法
  10. Bootstrap 插件的事件