在第一周的训练过后,学习到了一些较基础的算法,但是相对应的问题仍有很多。比如,一些题相对应的算法知道了,但是实践不出来,导致题也做不出来。
当然,这一周是选拔赛打的比较多,所以还是有一定的感悟。
超时问题,比较难以解决。比如
Alice and Bob are playing a game on a sequence a1,a2⋯an�1,�2⋯�� of length n�. They move in turns, and Alice moves first.
At each player's turn, they should select an integer and remove it from the sequence. The game ends when there is no integer left in the sequence.
Assume the sum of integers selected by Alice is S1�1, and the sum of integers selected by Bob is S2�2.
If the difference between S1�1 and S2 (ie.|S1−S2|)�2 (��.|�1−�2|) is odd, Alice wins; Otherwise, Bob wins.
Your task is to determine who will win the game if both players play optimally.
Input
The first line contains an integer n (1≤n≤106)� (1≤�≤106), indicating the length of the sequence.
The second line contains n� integers a1,a2⋯an (1≤ai≤109)�1,�2⋯�� (1≤��≤109), indicating the elements of the sequence.
Output
The first line output "Alice" (without quotes) if Alice wins and "Bob" (without quotes) otherwise. It is obvious that there is no draw.
#include<bits/stdc++.h>
using namespace std;
int a[1000005];
int main(){
int n,sum=0;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(int i=0;i<n;i++){
if(a[i]%2==1) sum++;
}
if(sum%2==1){
printf("Alice");
}
else printf("Bob");
return 0;
}
按照题上给的一步一步算的思路,暴力会超时,所以就需要一个取巧的方法去解决。
这周的感悟差不多就是这些,更多是去学习更多的算法并加以实践。