传送门:A. Hulk
思路:Just alternatively print "I hate that" and "I love that", and in the last level change "that" to "it".
#include <bits/stdc++.h>
using namespace std;
int n;
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(0);
string s1="I hate ";
string s2="I love ";
string itt="it ";
string thatt="that ";
cin>>n;
int x=0;
while(x<n){
x++;
if(x%2==1)cout<<s1;
else cout<<s2;
if(x==n)cout<<itt<<endl;
else cout<<thatt;
}
return 0;
}
传送门:B. Spider Man
题意:对于一系列由若干个点组成的环进行游戏,规则如下:若所有环的结点数都为1,则游戏结束;否则选择一个节点数大于1的环拆成两个环,无法进行操作者失败。输入一个序列,对于序列的每个前缀,依次求当初始各环的结点数分别为序列中的数字时先手必胜还是必败。
思路:找规律结果和奇偶数有关,我当时想的是当新增的环节点数x为偶数,那么需要拆x-1奇数次,而奇数次会使胜负结果改变,所以只需要统计偶数的次数即可
可参考官方题解:点击打开链接
#include <bits/stdc++.h>
using namespace std;
int n;
int main(){
while(~scanf("%d",&n)){
int cnt=0,ans,x;
for(int i=0; i<n; i++){
ans=0;
scanf("%d",&x);
if(!(x&1))cnt++;
ans=cnt%2;
if(ans)puts("1");
else puts("2");
}
}
return 0;
}
传送门:C. Thor
题意:手机里有n个不同的应用,每个应用不时的会产生通知消息。现在有q个查询,分别为:
1:x号应用产生了一个新的通知
或:2:阅读所有x已产生的通知
或:3:阅读前t个产生的通知(任何种类的应用产生的都算)
问每次查询后未读的通知有多少?
思路:用一个数组存放所有的通知,然后遍历数组进行查询--> n2会超时,不宜直接用数组进行模拟。
那么就用needread数组记录各个应用还有多少条消息未读,readed数组记录各个应用截止目前已经被读的消息数目。
对于1对needread数组加一。
对于2要把总数减去needread数组的值,但是因为有些消息可能之前已经被读过了,所以要减的应该是needread数组减readed数组的值。
对于3我们需要按消息产生的顺序来处理,因此我们设置一个队列,每次1产生新消息时候将消息入队。每次要处理前t个消息,那么我们从队列中一个个出列然后处理消息就可以了。需要注意的是,不是每次都要处理队列中的前t个消息,因为我们前面处理完一些消息后这些消息已经出队了,所以每次处理的其实比t个消息少,前pop_cnt个消息已经出队了,处理过了。这就是设置一个pop_cnt变量记录已出队消息数量的原因。当处理到某个应用的消息数大于了这个应用已经处理的消息数后,接下来的消息就是未读的消息了,此时处理未读消息,更新记录。
#include <bits/stdc++.h>
using namespace std;
const int N=3e5+10;
int needread[N];
int readed[N];
int cnt[N];
int main(){
int n,q,type,num,pop_cnt=0;
queue<int>que;
scanf("%d%d",&n,&q);
int ans=0;
while(q--){
scanf("%d%d",&type,&num);
if(type==1){
ans++;
needread[num]++;
que.push(num);//产生新消息时候将消息入队
}
else if(type==2){
ans-=needread[num]-readed[num];
readed[num]=needread[num];
}
else{
int t=num-pop_cnt;//前pop_cnt个消息已经出队了,处理过了。
while(!que.empty()&&t>0){
t--;
int now=que.front();
que.pop();
pop_cnt++;
cnt[now]++;
if (cnt[now] > readed[now]){//当处理到某个应用的消息数大于了这个应用已经处理的消息数后,接下来的消息就是未读的消息了,此时处理未读消息,更新记录。
ans-=cnt[now]-readed[now];
readed[now]=cnt[now];
}
}
}
printf("%d\n",ans);
}
return 0;
}