在线笔试的时候只AC了第一道,还做复杂了,第二题暴力仅在最小规模数据下成功,得到20分。做得非常不好...
这两天又重新看了一下这些题,本着练习一下C++的目的把这四道题都做了一遍。现在回头看感觉没有当时觉得的那么难!当时一定是太紧张和害怕了!希望自己以后做题能够放平心态,发挥出自己的水平就可以了。
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
思路分析
如果列表中同时存在奇数和偶数,那么一定存在可消除的数对。因此,在消除的最后,列表里要么只有奇数,要么只有偶数,而数对是成对消去的,因此最后列表中剩下的数字个数就是原列表中奇数项和偶数项的差值的绝对值。
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;
}