B - Ignatius and the Princess IV【DP】
2017-10-29 20:26
267 查看
“OK, you are not too bad, em… But you can never pass the next test.” feng5166 says.
“I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers.” feng5166 says.
“But what is the characteristic of the special integer?” Ignatius asks.
“The integer will appear at least (N+1)/2 times. If you can’t find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha…..” feng5166 says.
Can you find the special integer for Ignatius?
Input
The input contains several test cases. Each test case contains two lines. The first line consists of an odd integer N(1<=N<=999999) which indicate the number of the integers feng5166 will tell our hero. The second line contains the N integers. The input is terminated by the end of file.
Output
For each test case, you have to output only one line which contains the special number you have found.
Sample Input
5
1 3 2 3 3
11
1 1 1 1 1 5 5 5 5 5 5
7
1 1 1 1 1 1 1
Sample Output
3
5
1
思维DP,
开两个变量进行维护。
顺手可以介绍一下scanf/cin的效率
前者256ms 后者998ms【差点血崩!】
scanf:
cin
“I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers.” feng5166 says.
“But what is the characteristic of the special integer?” Ignatius asks.
“The integer will appear at least (N+1)/2 times. If you can’t find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha…..” feng5166 says.
Can you find the special integer for Ignatius?
Input
The input contains several test cases. Each test case contains two lines. The first line consists of an odd integer N(1<=N<=999999) which indicate the number of the integers feng5166 will tell our hero. The second line contains the N integers. The input is terminated by the end of file.
Output
For each test case, you have to output only one line which contains the special number you have found.
Sample Input
5
1 3 2 3 3
11
1 1 1 1 1 5 5 5 5 5 5
7
1 1 1 1 1 1 1
Sample Output
3
5
1
思维DP,
开两个变量进行维护。
顺手可以介绍一下scanf/cin的效率
前者256ms 后者998ms【差点血崩!】
scanf:
#include<cstdio> #include<iostream> using namespace std; int main() { int n, now_num, cnt, i, tmp; while (scanf("%d", &n) != EOF) { cnt = 0; for (i = 0; i < n; i++) { scanf("%d", &tmp); if (cnt == 0) { now_num = tmp; cnt++; continue; } if (tmp == now_num) { cnt++; } else { cnt--; } } printf("%d\n", now_num); } return 0; }
cin
//#include<cstdio> #include<iostream> using namespace std; int main() { int n, now_num, cnt, i, tmp; while (cin >> n) { cnt = 0; for (i = 0; i < n; i++) { cin >> tmp; if (cnt == 0) { now_num = tmp; cnt++; continue; } if (tmp == now_num) { cnt++; } else { cnt--; } } cout << now_num << endl; } return 0; }
相关文章推荐
- HDU - 1029 Ignatius and the Princess IV(DP)
- kuangbin求带飞DP1 Ignatius and the Princess IV( 漂亮的解法!!)
- HDU 1029 Ignatius and the Princess IV(基础dp(思维))
- 【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 B - Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV DP
- [kuangbin带你飞]专题十二 基础DP1 B - Ignatius and the Princess IV
- B - Ignatius and the Princess IV DP
- HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)
- HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)
- 【map/桶记录/基础dp】Ignatius and the Princess IV HDU - 1029
- hdu1029 Ignatius and the Princess IV(DP求数列中出现次数过半的数)
- HDU 1029 Ignatius and the Princess IV (DP,水题)
- HDOJ 1029 Ignatius and the Princess IV(数位dp)
- B - Ignatius and the Princess IV DP
- dp-基础3 B - Ignatius and the Princess IV HDU - 1029
- HDU-1029 Ignatius and the Princess IV(DP?)
- [kuangbin带你飞]专题十二 基础DP1 -B - Ignatius and the Princess IV
- HDU1029:Ignatius and the Princess IV -DP求一列数中出现次数最多的数的次数
- Ignatius and the Princess IV
- HDU 1028 Ignatius and the Princess III (递归,dp)