Codeforces Round #366 (Div. 2) 題解

題目鏈接:http://codeforces.com/contest/705


A.思路:找規律。仔細看一下樣例,大概就知道規律了。先輸出"I hate ",然後將計數減一,再每次輸出"that ",然後交替輸出"I love "、"I hate ",直到計數結束,最後再輸出"it"。詳見代碼。


附上AC代碼:

#include <bits/stdc++.h>
using namespace std;
const char s1[] = "I hate ";
const char s2[] = "I love ";
int n;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	cout << s1;
	--n;
	for (int i=0; i<n; ++i){
		cout << "that ";
		if ((i&1) == 0)
			cout << s2;
		else
			cout << s1;
	}
	cout << "it" << endl;
	return 0;
}

B.思路:簡單的博弈。每次都要拆環成兩段,而且每段必須要有至少一個點,不能拆就輸了。所以只需要求和判斷一下奇偶即可。詳見代碼。


附上AC代碼:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	ll sum = 0;
	int num;
	for (int i=0; i<n; ++i){
		cin >> num;
		sum += num-1;
		if (sum%2 == 0)
			cout << 2 << endl;
		else
			cout << 1 << endl;
	}
	return 0;
}

C.思路:就是模擬一下就好了,但肯定不能太暴力了,稍微有技巧一下就可以了。用vector數組存儲每個應用的所有信息編號,set存儲未讀消息,一個計數器記錄是第幾條消息,然後開始模擬,代碼較簡單,會用STL就很容易,所以不詳述。詳見代碼。


附上AC代碼:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 300005;
vector<int> v[maxn];
set<int> s;
set<int>::iterator it;
int n, q;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> q;
	int cnt=0, a, b;
	while (q--){
		cin >> a >> b;
		if (1 == a){
			v[b].push_back(++cnt);
			s.insert(cnt);
		}
		else if (2 == a){
			for (int i=0; i<v[b].size(); ++i){
				int id = v[b][i];
				s.erase(id);
			}
			v[b].clear();
		}
		else{
			while (!s.empty()){
				if (*s.begin() > b)
					break;
				s.erase(*s.begin());
			}
		}
		cout << s.size() << endl;
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章