codeforces 1216 A(模拟)B(思维) D(简单思维) E1(思维加模拟)

A. Prefixes(模拟)

题目链接:codeforces 1216A

题意:

    给一个字符串(只包含字符 'a', 'b'),要求a 和 b 相邻,问最少改变多少次,改变后的字符串为什么

题解:

     模拟

#include 
using namespace std;
typedef long long ll;
const int maxn = 1e4+5;
int a[maxn];
int main(){
	int n, ans = 0;
	cin >> n;
	string s;
	cin >> s;
	for(int i = 0; i < n;i = i + 2 ){
		if(s[i] == 'a' && s[i+1] == 'a'){
			s[i+1] = 'b';
			ans++;
		}
		if(s[i] == 'b' && s[i+1] == 'b'){
			s[i+1] = 'a';
			ans++;
		}
	}
	cout << ans << endl << s << endl;
	return 0;
}

B. Shooting(思维)

题目链接:codeforces 1216B

题意:

    射击一排瓶子,射倒第一个罐子需要 1 次,击倒之后的需要 a[i] * x + 1 次,顺序自选,x从1递增

题解:

   直接排序,从大到小,最大的a[i] * 最小的x

#include 
using namespace std;
typedef long long ll;
const int maxn = 1e4+5;
struct node{
	int k, u;
}a[maxn];
bool cmp(node p, node y){
	return p.u > y.u;
}
int main(){
	int n;
	cin >> n; 
	for(int i = 1; i <= n; i++){
		cin >> a[i].u;
		a[i].k = i;
	}
	sort(a+1, a+1+n, cmp);
	int ans = n;
	for(int i = 2; i <= n; i++){
		ans = ans + a[i].u * (i-1);
	}
	cout << ans << endl << a[1].k;
	for(int i = 2; i <= n; i++){
		cout <<" " << a[i].k;
	}
	return 0;
}

D. Swords(简单思维)

题目链接:codeforces 1216D

题意:

    剧院有n种剑,每种都有x把, y 个人来到剧院拿了相同的剑(每个人拿的剑的种类相同),只有1种剑没有被拿过,然后给出每种剑剩余的个数,求最少有多少个人,拿了多少把剑

题解:

    直接可以看出 x 等于剩余剑数最多的那种剑,每个人拿的剑相同,所以应该是差值的最大公约数

#include 
using namespace std;
typedef long long ll;
const int maxn = 2e5+500;
long long gcd(long long a, long long b){
	return b == 0 ? a : gcd(b, a%b);
}
ll a[maxn];
int main(){
	int n;
	cin >> n;
	ll sum = 0, ma = 0;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		ma = max(ma, a[i]);
	}
	ll ans = ma - a[1];
	sum = ma - a[1];
	for(int i = 2; i <= n; i++){
		sum = sum + (ma - a[i]);
		ans = gcd(ma-a[i], ans);
		
	}
	cout << sum / ans << " " << ans << endl;
	return 0;
}

E1. Numerical Sequence (easy version) (思维扩展)

题目链接:codeforces 1216E1

题意:

    一个字符串,112123123412345123456.....问第n位是几 (n最大为 10^9)

题解:

    这道题,首先不能存字符串,爆内存,爆时间

我的思路为:先判断n属于哪个区间,将字符串划分为 1    12    123    1234   12345 ....

比如 n=4属于区间3, n = 8属于区间 4

然后判断它属于区间的第几个

#include 
using namespace std;
typedef long long ll;
const int maxn = 3e5+500;
ll a[maxn], b[maxn];
int main(){
	for(int i = 1; i <= 9; i++){
		a[i] = a[i-1] + i;
		b[i] = i;
	}
	b[10] = 11;
	for(int i = 10; i <= 99; i++){
		a[i] = a[i-1] + b[i];
		b[i+1] = b[i] + 2;
	}
	b[100] = b[99] + 3;
	for(int i = 100; i <= 999; i++){
		a[i] = a[i-1] + b[i];
		b[i+1] = b[i] + 3;
	}
	b[1000] = b[999] + 4;
	for(int i = 1000; i <= 9999; i++){
		a[i] = a[i-1] + b[i];
		b[i+1] = b[i] + 4;
	}
	b[10000] = b[9999] + 5;
	for(int i = 10000; i <= 22000; i++){
		a[i] = a[i-1] + b[i];
		b[i+1] = b[i] + 5;
	}
	int q;
	cin >> q;
	while(q--){
		int n, ans = 0, f = 0;
		cin >> n;
		for(int i = 1; i <= 22000; i++){
			if(n < a[i]){
				ans = i; // 判断区间
				break;
			}
			if(n == a[i]){
				cout << i % 10 << endl;
				f = 1;
				break;
			}
		}
		if(f == 0){
			int ans1 = n - a[ans-1];
			for(int i = 1; i <= 22000; i++){
				if(ans1 < b[i]){
					ans = i;  // 判断属于第几个
					break;
				}
				if(ans1 == b[i]){
					cout << i % 10 << endl;
					f = 1;
					break;
				}
			}
			if(f == 0){
				int sum = b[ans] - ans1;
				string s = "";
				stringstream ss;
				ss << ans;
				s = ss.str();
				cout << s[s.size()-sum-1] << endl;
			}
		}
	}
	return 0;
}

 

你可能感兴趣的:(codeforces)