Codeforces Round 905 (Div. 3)

在这里插入图片描述

Dashboard - Codeforces Round 905 (Div. 3) - Codeforces

A. Morning

题意:输入一个0到9组成的密码,密码由字符串组成,最初光标指向1,一秒可以进行两种操作,移动到相邻数字或者显示当前数字,输入密码最少的秒数是多少。

思路:将字符串转换为int数组,记录当前光标的位置,每次移动到下一位置的秒数为两位置的绝对值。

AC code:

#include<bits/stdc++.h>
#define endl '\n'
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
 
typedef long long LL;
typedef pair<char, int> PCI;
typedef pair<int, int> PII;
const int N = 2e5+10, M = 1001;
const int INF = 0x3f3f3f3f, mod = 998244353;
int T, n, m;
 
int gcd(int a, int b){
	if(b) while((a %= b) && (b %= a));
	return a + b;
}
 
int main(){
	fast();
	
	T = 1;
	cin >> T;
	while(T --){
		string s; cin >> s;
		int a[N];
		for(int i = 0; i < s.size(); i ++){
			a[i] = s[i] - '0';
			if(s[i] == '0') a[i] = 10;
		}
		int cnt = 0, tt = 1;
		for(int i = 0; i < s.size(); i ++){
			cnt += abs(a[i] - tt) + 1;
			tt = a[i];
		}
		cout << cnt << endl;
	}
	return 0;
}

B. Chemistry

题意:给定字符串,在删除任意k个字符后,能否重新排列为回文字符串

思路:统计同一字符的个数,优先删除奇数的字符,只有剩余字符全为偶数,或者只存在一个奇数字符的情况能组成回文

AC code:

#include<bits/stdc++.h>
#define endl '\n'
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
 
typedef long long LL;
typedef pair<char, int> PCI;
typedef pair<int, int> PII;
const int N = 2e5+10, M = 1001;
const int INF = 0x3f3f3f3f, mod = 998244353;
int T, n, k;
 
int gcd(int a, int b){
	if(b) while((a %= b) && (b %= a));
	return a + b;
}
 
int main(){
	fast();
	
	T = 1;
	cin >> T;
	while(T --){
		cin >> n >> k;
		string s; cin >> s;
		if(s.size() == 1) cout << "YES" << endl;
		else if(s.size() - k == 1) cout << "YES" << endl;
		else{
			map<char, int> mp;
			for(int i = 0; i < s.size(); i ++)
			mp[s[i]] ++;
			
			int x = 0, y = 0;
			for(auto i : mp){
				if(i.second % 2 == 0) x ++;
				else y ++;
			}
			if(y >= k){
				if(y - k <= 1) cout <<"YES" << endl;
				else cout << "NO" << endl;
			}
			else{
			cout << "YES" << endl;
				
			}
			
		}
	}
	return 0;
}

C. Raspberries

题意:给出整数数组a和整数k,一次操作可以对任意一个a中的元素+1,最少多少次操作可以令数组a所有元素的积能被k整除

思路:

  • k为2,3,5时只要数组中存在能被k整除的元素,或者对a中元素遍历操作变成k取最小值;
  • k为4时:
    • 偶数数量大于1,操作为0
    • 无偶数,操作为2
    • 一偶数,操作为1

AC code:

#include<bits/stdc++.h>
#define endl '\n'
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
 
typedef long long LL;
typedef pair<char, int> PCI;
typedef pair<int, int> PII;
const int N = 2e5+10, M = 1001;
const int INF = 0x3f3f3f3f, mod = 998244353;
int T, n, k;
int a[N];
 
int gcd(int a, int b){
	if(b) while((a %= b) && (b %= a));
	return a + b;
}
 
signed main(){
	fast();
	
	T = 1;
	cin >> T;
	while(T --){
		int k3 = 0, k2 = 0, kk = 0, k5 = 0, k4 = 0;
		map<int, int> mp;
		cin >> n >> k;
		int ans = INF, cnt = 1;
		int u = 0, v = 0;
		for(int i = 0; i < n; i ++){
			int x; cin >> x;
			if(x % 2 == 0) k2 = 1;
			if(x % 3 == 0) kk = 1;
			if(x% 4 == 0) k4 = 1;
			if(x % 5 == 0) k5 = 1;
			mp[x] ++;
			if(x % 2) u ++;
			else v ++;
			int tt = 0;
			while(x % k){
				x ++;
				tt ++;
			}
			ans = min(ans, tt);
		}
		if(k == 4 && v > 1) ans = 0;
		if(k== 4){
			if(v != 0 && u != 0) ans = min(1, ans);
			if(v == 0) ans = min(2, ans);
		}
		if(k == 2 && k2 == 1 || k == 3 && kk == 1 || k == 4 && k4 == 1 || k == 5 && k5 == 1) cout << 0 <<endl;
		else cout << ans << endl;
	}
	return 0;
}

D. In Love

题意:给出q次询问,每次询问前有两种操作,+号时添加线段[l,r],-号是删去线段[l,r],每次操作后询问是否存在两线段不相交

思路:若最大的左边界大于最小的右边界,则存在两线段不相交,set会自动去重,需要用数据结构里的multiset来实现,存入每段线段的两边界,根据first元素自动排序,开两个set,一个存入[l,r],一个存入[r,l]

AC code:

#include<bits/stdc++.h>
#define endl '\n'
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

typedef long long LL;
typedef pair<char, int> PCI;
typedef pair<int, int> PII;
const int N = 2e5+10, M = 1001;
const int INF = 0x3f3f3f3f, mod = 998244353;
int T, n, m;

int gcd(int a, int b){
   if(b) while((a %= b) && (b %= a));
   return a + b;
}

int main(){
   fast();
   
   multiset<PII> stl;
   multiset<PII> str;
   T = 1;
   cin >> T;
   while(T --){
   	char op;
   	int l, r;
   	cin >> op >> l >> r;
   	if(op == '+'){
   		stl.insert({l, r});
   		str.insert({r, l});
   	}
   	else{
   		stl.erase(stl.find({l, r}));//避免把重复线段全部删除
   		str.erase(str.find({r, l}));
   	}
   	if(stl.size() <= 1) cout << "NO" << endl;
   	else{
   		auto x = *stl.rbegin(), y = *str.begin();
   		if(x.first > y.first) cout << "YES" << endl;
   		else cout << "NO" << endl;
   	}
   }
   return 0;
}

E. Look Back

题意:给出整数数组a,需要用最少的操作使数组不递减,每次操作可以选择任一元素*2

思路:直接暴力TLE,将每个元素看做a[i] * pow(2, xi),每次操作直到符合条件时记录当前操作次数,若元素递减,则xi++,否则xi–操作

向后求操作的和

AC code:

#include<bits/stdc++.h>
#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<char, int> PCI;
typedef pair<int, int> PII;
const int N = 2e5+10, M = 1001;
const int INF = 0x3f3f3f3f, mod = 998244353;
int T, n, m;
int a[N], b[N], sum[N];
 
int gcd(int a, int b){
	if(b) while((a %= b) && (b %= a));
	return a + b;
}
 
signed main(){
	fast();
	
	T = 1;
	cin >> T;
	while(T --){
		cin >> n;
		for(int i = 0; i < n; i ++)
			cin >> a[i];
		int ans = 0, cnt = 0;
		for(int i = 1; i < n; i ++){
			int tt = 0;
	        int l = a[i - 1], r = a[i];
	        while(l < r) tt --, l *= 2;
	        while(l > r) tt ++, r *= 2;
 
	        cnt = max((LL)0, cnt + tt);
	        ans += cnt;    
		}
		cout << ans << endl;
	}
	return 0;
}

F. You Are So Beautiful

题意:给出整数数组a,找出有多少子数组为为只出现一次

思路:找到每个元素第一次出现和最后一次出现的位置,每出现第一次出现的元素时,记录,每出现最后一次出现的元素时,便可以与前面第一次出现的元素组成唯一的子数组

AC code:

#include<bits/stdc++.h>
#define endl '\n'
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
 
typedef long long LL;
typedef pair<char, int> PCI;
typedef pair<int, int> PII;
const int N = 2e5+10, M = 1001;
const int INF = 0x3f3f3f3f, mod = 998244353;
int T, n, m;
int a[N];
 
int gcd(int a, int b){
	if(b) while((a %= b) && (b %= a));
	return a + b;
}
 
int main(){
	fast();
	
	T = 1;
	cin >> T;
	while(T --){
		map<int, int> mp, st, ed;
		cin >> n;
		for(int i = 0; i < n; i ++){
			cin >> a[i];
			if(mp[a[i]] == 0){
				mp[a[i]] ++;
				st[a[i]] = i;
			}
			ed[a[i]] = i;
		}
		LL cnt = 0, ans = 0;
		for(int i = 0; i < n; i ++){
			if(st[a[i]] == i) cnt ++;
			if(ed[a[i]] == i) ans += cnt;
		}
		cout << ans << endl;
	}
	return 0;
}

G1. Dances (Easy version)

题意:给出a数组的n-1个元素,b数组的n个元素,a[1] = 1,删除ab中的元素使排序后的a[i] < b[i],最少删除几次

思路:排序,双指针遍历

#include<bits/stdc++.h>
#define endl '\n'
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

typedef long long LL;
typedef pair<char, int> PCI;
typedef pair<int, int> PII;
const int N = 2e5+10, M = 1001;
const int INF = 0x3f3f3f3f, mod = 998244353;
int T, n, m;
int a[N], b[N];

int gcd(int a, int b){
	if(b) while((a %= b) && (b %= a));
	return a + b;
}

int main(){
	fast();
	
	T = 1;
	cin >> T;
	while(T --){
		cin >> n >> m;
		a[1] = 1;
		for(int i = 2; i <= n; i ++)
			cin >> a[i];
		for(int i = 1; i <= n; i ++)
			cin >> b[i];
		sort(a + 1, a + n + 1);
		sort(b + 1, b + n + 1);

		int l = 1, r = 1, cnt = 0;
		while(l <= n && r <= n){
			if(a[l] < b[r]) l ++, r ++, cnt ++;
			else r ++;
		}
		cout << n - cnt << endl;
	}
	return 0;
}
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| ^_^ |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         佛祖保佑AC,永无bug缠身       
*/
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值