在線筆試的時(shí)候只AC了第一道,還做復(fù)雜了,第二題暴力僅在最小規(guī)模數(shù)據(jù)下成功院刁,得到20分。做得非常不好...
這兩天又重新看了一下這些題粪狼,本著練習(xí)一下C++的目的把這四道題都做了一遍⊥诵龋現(xiàn)在回頭看感覺(jué)沒(méi)有當(dāng)時(shí)覺(jué)得的那么難!當(dāng)時(shí)一定是太緊張和害怕了再榄!希望自己以后做題能夠放平心態(tài)狡刘,發(fā)揮出自己的水平就可以了。
1399 : Shortening Sequence
Description
There is an integer array A1, A2 ...AN. Each round you may choose two adjacent integers. If their sum is an odd number, the two adjacent integers can be deleted.
Can you work out the minimum length of the final array after elaborate deletions?
Input
The first line contains one integer N, indicating the length of the initial array.
The second line contains N integers, indicating A1, A2 ...AN.
For 30% of the data:1 ≤ N ≤ 10
For 60% of the data:1 ≤ N ≤ 1000
For 100% of the data:1 ≤ N ≤ 1000000, 0 ≤ Ai ≤ 1000000000
Output
One line with an integer indicating the minimum length of the final array.
Sample Hint
(1,2) (3,4) (4,5) are deleted.
Sample Input
7
1 1 2 3 4 4 5
Sample Output
1
思路分析
如果列表中同時(shí)存在奇數(shù)和偶數(shù)困鸥,那么一定存在可消除的數(shù)對(duì)嗅蔬。因此,在消除的最后,列表里要么只有奇數(shù)购城,要么只有偶數(shù)吕座,而數(shù)對(duì)是成對(duì)消去的虐译,因此最后列表中剩下的數(shù)字個(gè)數(shù)就是原列表中奇數(shù)項(xiàng)和偶數(shù)項(xiàng)的差值的絕對(duì)值瘪板。
AC代碼
#encoding=utf-8
import sys
num = int(sys.stdin.readline())
s = [int(x) for x in sys.stdin.readline().strip().split()]
count = 0
for i in s:
if i % 2 == 0:
count += 1
else:
count -= 1
if count < 0:
count = -count
print count
#include <iostream>
using namespace std;
int main() {
int sum;
cin >> sum;
int count_odd = 0;
int count_even = 0;
int tmp;
for (int i = 0; i < sum; i++) {
cin >> tmp;
if (tmp % 2 == 0)
count_even++;
else
count_odd++;
}
int rst = count_odd - count_even > 0 ? count_odd - count_even : count_even - count_odd;
cout << rst;
}