Codeforces Round 913 (Div. 3)

Codeforces Round 913 (Div. 3)_第1张图片

Codeforces Round 913 (Div. 3)

Codeforces Round 913 (Div. 3)

A. Rook

思路:记录当前位置的字母和数字,然后遍历a-h以及1-8组合输出未记录的位置即可。

AC code:

void solve(){
    char a, b; cin >> a >> b;
    map mp;
    mp[a] ++;
    mp[b] ++;
    for(char i = 'a'; i <= 'h'; i ++){
        if(!mp[i]) cout << i << b << endl;
    }
    for(char i = '1'; i <= '8'; i ++){
        if(!mp[i]) cout << a << i << endl;
    }
}

B. YetnotherrokenKeoard

思路:模拟,从后往前进行遍历,当遇到删除字符b和B时,记录下来,当遇到其他字符时根据记录情况进行删除。

AC code:

void solve(){
    string s;
    cin >> s;
    string ans = "";
    int B = 0, b = 0;
    for(int i = s.size() - 1; i >= 0; i --){
    	if(s[i] == 'B') B ++;
    	else if(s[i] == 'b') b ++;
    	else if(s[i] >= 'a' && s[i] <= 'z'){
    		if(b){
    			b --;
    			continue;
			}else ans += s[i];
		}else{
			if(B){
				B --;
				continue;
			}else ans += s[i];
		}
	}
	reverse(ans.begin(), ans.end());
	cout << ans << endl;
}

C. Removal of Unattractive Pairs

题意:给出一个字符串,相邻字符不同可以删除,删除后剩下的字符按原顺序拼接,操作可以进行无数次,字符串最少剩余多少。

思路:记录数量最多的字符数量,若大于全部字符的一半,则最少剩余(mx - (n - mx))个字符,即最多字符的数量减去其他字符数量;

若小于全部字符一半,则奇数剩一个,偶数可以全删。

AC code:

void solve(){
    map mp;
    cin >> n >> s;
    int mx = 0;
    for(char x : s) {
        mp[x] ++;
        mx = max(mx, mp[x]);
    }
    if(mx <= n / 2)
        cout << (n % 2 != 0) << endl;
    else
        cout << mx - (n - mx) << endl;
}

D. Jumping Through Segments

题意:游戏从0开始移动,有n段区间,每次移动最多不超过k的单位距离,需要移动n次,第i次移动必须在第i个区间内,最终需要到达第n个区间为游戏胜利,游戏胜利k值最小是多少。

思路:二分,每次记录当前最近/最远可移动到的距离,即最大的左边界和最小的右边界。

AC code:

#include
#define endl '\n'
#define int long long
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

typedef long long LL;
typedef pair PII;
const int N = 3e5+10, M = 2001;
const int INF = 0x3f3f3f3f3f3f3f3f, mod = 998244353;
int T, n;
PII q[N];

bool check(int k){
    int mn = 0, mx = 0;
    for(int i = 1; i <= n; i ++){
        mn = max(q[i].first, mn - k);
        mx = min(q[i].second, mx + k);
        if(mn > mx) return false;
    }
    return true;
}

void solve(){
    cin >> n;
    for(int i = 1; i <= n; i ++)
        cin >> q[i].first >> q[i].second;
    int l = 0, r = 3e9;
    while(l < r){
        int mid = l + r >> 1;
        if(check(mid)) r = mid;
        else l = mid + 1;
    }
    cout << l << endl;
}

signed main(){
    fast();
    
    cin >> T;
    while(T --){
        solve();
    }
    return 0;
}

E. Good Triples

题意:将给出的n分解成三个数相加,且三数每位相加的和等于n每一位相加的和。

思路:每一位相加不能有进位,每一次进位至少会损失单位贡献十而只增加一,所以枚举三个数每一位可能的组合情况即可。

AC code:

#include
#define endl '\n'
#define int long long
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

typedef long long LL;
typedef pair PCI;
typedef pair PII;
const int N = 3e5+10, M = 2001;
const int INF = 0x3f3f3f3f3f, mod = 998244353;
int T, n, k;
map mp;

void init(){
    for(int i = 0; i <= 9; i ++){
        for(int j = 0; j <= 9; j ++){
            for(int u = 0; u <= 9; u ++)
                mp[i + j + u] ++;
        }
    }
}

void solve(){
    cin >> n;
    int cnt = 1, u = n;
    while(u){
        cnt *= mp[u % 10];
        u /= 10;
    }
    cout << cnt << endl;
}

signed main(){
    fast();
    
    init();
    T = 1;
    cin >> T;
    while(T --){
        solve();
    }
    return 0;
}

你可能感兴趣的:(算法,c++)