Codeforces Round 925 (Div. 3)

比赛地址

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

A. Recovering a Small String

直接模拟

复制代码
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define lowbit(x) (x&(-x))
#define sz(a) (int)a.size()
#define pb push_back
#define all(a) a.begin(), a.end()
#define int long long
typedef long long LL;
const int mod = 1e9+7;
const int N = 2e5+10;

using namespace std;

inline void solve(){
	int n ; cin >> n ;
	// a a z ; 28
	// a z z : 1 + 26 + 26 = 53
	if(n<=28){
		char c = (char)(n-3+'a') ;
		cout << "aa" <<c << endl; 
	}else if(n<=53){
		char c = (char)(n-28+'a');
		cout << "a" << c << "z" << endl;
	}else{
		char c = (char)(n-53+'a');
		cout << c << "zz" << endl;
	}
	return ;
}
 
signed main()
{
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

B . Make Equal

模拟 , 从后往前遍历,t表示平均值 , 用 x 记录当前每个小于平均值的差 , 如果遇到一个值出现ai>t && ai-t>x,那么表示ai是不可能变成t的,后面需要的少于它多出来的;

复制代码
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define lowbit(x) (x&(-x))
#define sz(a) (int)a.size()
#define pb push_back
#define all(a) a.begin(), a.end()
#define int long long
typedef long long LL;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;

using namespace std;

inline void solve() {
	int n ; cin >> n ;
	LL s = 0 ;
	vector<int> a(n+1) ;
	for(int i=1;i<=n;i++) {
		cin >> a[i] ;
		s += a[i] ;
	}
	LL t = s / n ;
	LL x = 0 ;
	bool tag = true ;
	for(int i=n;i>=1;i--){
		if(a[i] < t){
			x += t - a[i] ;
		}else{
			if(a[i]-t > x){
				tag = false;
				break;
			}
			x -= a[i] - t;
		}
	}
	if(tag) cout << "Yes" << endl;
	else cout << "No" << endl;
	
}

signed main()
{
    IOS
	int _ = 1;
    cin >> _;
    while (_--) solve();
    return 0;
}

C. Make Equal Again

模拟

复制代码
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define lowbit(x) (x&(-x))
#define sz(a) (int)a.size()
#define pb push_back
#define all(a) a.begin(), a.end()
#define int long long
typedef long long LL;
const int mod = 1e9+7;
const int N = 2e5+10;

using namespace std;

inline void solve(){
	int n ; cin >> n ;
	vector<int> a(n+1);
	for(int i=1;i<=n;i++){
		cin >> a[i] ;
	}
	int i=1,j=n;
	int ans = 0 ;
	if(a[i]!=a[j]){
		while(i<=n&&a[i]==a[1]) i++;
		while(j>=1 && a[j]==a[n]) j--;
		ans = n - max(i-1,n-j);
	}
	else{
		while(i<=n && a[i]==a[1]) i++;
		while(j>=1 && a[j]==a[1]) j--;
		if(j<i) ans = 0 ;
		else ans = j - i + 1 ;
	}
	cout << ans << endl;
}
 
signed main()
{
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

D. Divisible Pairs

哈希表解决!

对于( a + b ) % x = 0 , 那么a % x + b % x = 0 ;

对于(a - b)% y = 0 , 那么a % y == b % y ;

如果知道上面的,那么这道题就很简单了 ;

用哈希表来存对应的pair<int,int>,表示 : {ai%x,ai%y} ;

复制代码
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define lowbit(x) (x&(-x))
#define sz(a) (int)a.size()
#define pb push_back
#define all(a) a.begin(), a.end()
#define int long long
#define PII pair<int,int> 
typedef long long LL;
const int mod = 1e9+7;
const int N = 2e5+10;

using namespace std;

int a[N] ;

inline void solve(){
	int n , x , y ;
	cin >> n >> x >> y ;
	for(int i=1;i<=n;i++) cin >> a[i] ;
	int ans = 0 ;
	map<PII,int> mp;
	for(int i=1;i<=n;i++){
		// 求 x - a[i] % x 和 a[i ]% y 组成的pair前面有多少个 
		auto it = mp.find({(x-a[i]%x)%x,a[i]%y});
		if(it!=mp.end()){
			ans += it->second;
		}
		mp[{a[i]%x,a[i]%y}]++;
	} 
	cout << ans << endl;
}
 
signed main()
{
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

E. Anna and the Valentine's Day Gift

贪心模拟题 ;

首先要清楚 : Anan选 后导0 最多的数消除后导0 , 然后Shasha是保护后导0最多的数 ;

遍历每一个元素,将后导0个数不为0的个数存起来,然后排序,然后每次最优都是减小最多的,减去那一部分,最后和m判断即可 ;

复制代码
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define lowbit(x) (x&(-x))
#define sz(a) (int)a.size()
#define pb push_back
#define all(a) a.begin(), a.end()
#define int long long
typedef long long LL;
const int mod = 1e9+7;
const int N = 2e5+10;

using namespace std;

int a[N] ;

bool cmp(int x ,int y){
	return x > y ;
}

inline void solve(){
	// Anan选后导0最多的数消除后导0,然后Shasha是保护后导0最多的数
	int n , m ; cin >> n >> m ;
	LL cnt = 0 ;
	vector<int> b ;
	for(int i=1;i<=n;i++){
		int x ; cin >> x ;
		int zero = 0 ;
		while(x%10==0){
			zero++;
			x /= 10 ;
		}
		while(x){
			cnt ++;
			x /= 10 ;
		}
		if(zero) b.push_back(zero);
	}
	sort(b.begin(),b.end(),cmp);
	for(int i=1;i<b.size();i+=2){
		cnt += b[i];
	}
	if(cnt > m) cout << "Sasha" << endl;
	else cout << "Anna" << endl ;
	return ;
}
 
signed main()
{
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}
相关推荐
大鱼>7 分钟前
AI+货物追踪:全链路货物可视化追踪系统
人工智能·深度学习·算法·机器学习
大江东去浪淘尽千古风流人物11 分钟前
【SLAM】slam高动态位姿估计全链路拆解
算法·面试·职场和发展·视觉里程计·slam·vio·大疆
想你依然心痛14 分钟前
量子计算对嵌入式密码学的挑战与后量子算法准备
算法·密码学·量子计算
Jerry4 小时前
LeetCode 28. 找出字符串中第一个匹配项的下标
算法
Jerry6 小时前
LeetCode 459. 重复的子字符串
算法
海石9 小时前
1500分的题目,确实有实力,不过还是我略胜一筹
算法·leetcode
海石9 小时前
【记忆化搜索】条条大路通AC,走好适合你的那一条,走到后再考虑走得快
算法·leetcode
Jerry10 小时前
LeetCode 151. 反转字符串中的单词
算法
a11177613 小时前
LM 算法迭代过程动画演示(SLAM)
算法
头茬韭菜13 小时前
Context 的生死抉择:四层压缩、截断算法与 Session Memory
算法·ai