Codeforces Round #555 A~E(D没写)

A.直接模拟即可

//Codeforces Round #555 A
//模拟 
#include <bits/stdc++.h>
using namespace std;
set<int> a;
int main()
{
	int n;
	int ans = 0;
	scanf("%d",&n);
	while(a.find(n)==a.end()){
		a.insert(n);
		ans++;
		n++;
		while(n%10==0)n/=10;
	}
	printf("%d\n",ans); 
	return 0;
}

B.只能换连续个,注意小于时进入,等于继续循环

//B
//贪心 连续大就换 
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 200005;
int n;
char s[maxn];
int vis[10],f[10];
int main()
{
	int fg=0;
	scanf("%d",&n);
	scanf("%s",s);
	for(int i=1;i<10;i++){
		scanf("%d",&f[i]);
	}
	for(int i=0;i<n;i++){
		{
			if(s[i]-'0'<f[s[i]-'0']){
				while(i<n&&s[i]-'0'<=f[s[i]-'0']){
					s[i] = f[s[i]-'0'] + '0';	
					i++;
					fg = 1;
				}
			}
			if(fg)break;
		}	
	}
	printf("%s",s);

	printf("\n");
	return 0;
}

C1.简单版本,左右哪个小就挑哪个

//C1
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+5;
int n,num,l,r,lst;
int a[maxn]; 
deque<int> q;
queue<char> ans;
int main()
{
	int fg = 0;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&a[i]);
		q.push_back(a[i]);
	}
	l = q.front(); r = q.back();
	if(l<r)lst = l,q.pop_front(),ans.push('L'),num++;
	else lst = r,q.pop_back(),ans.push('R'),num++;
	
	while(q.size()>=2){
		l = q.front(); r = q.back();
		if(l>lst&&r>lst){
			if(l<r)lst = l,q.pop_front(),ans.push('L');
			else lst = r,q.pop_back(),ans.push('R');
			num++;
		}else if(l>lst){
			lst = l; q.pop_front(); ans.push('L');	num++;
		}else if(r>lst){
			lst = r; q.pop_back(); ans.push('R');	num++;
		}else {
			break;
		}
	}
	
	if(q.front()>lst)ans.push('L'),num++;
	printf("%d\n",num);
	while(!ans.empty()){
		cout<<ans.front(); ans.pop();
	}
	
	return 0;
}

C2.若有相等,则只能选择一边走到底,哪边多选哪边。其他和简单C1一样

//C2
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+5;
int n,num,l,r,lst;
int a[maxn]; 
deque<int> q,ch;
queue<char> ans;
int main()
{
	int fg = 0;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&a[i]);
		q.push_back(a[i]);
	}
	if(n==1){
		printf("1\nL\n");
		return 0;
	}
	
	while(q.size()>=2){
		l = q.front(); r = q.back();
		if(l>lst&&r>lst){
			if(l<r){ lst = l,q.pop_front(),ans.push('L'),num++;; 
			}
			else if(l>r){  lst = r,q.pop_back(),ans.push('R'),num++;
			}
			else if(l==r){
				lst = l;  fg = 1;
				q.pop_front();	q.pop_back(); num++;
				int numl=0,numr=0,tp;
			while(!q.empty()){
				tp = q.front(); if(tp<=lst)break;
				lst = tp;
				q.pop_front(); numl++;//计算选择左边最大值 
				ch.push_back(lst);
				}
			while(!ch.empty()){//还原之前操作 
				tp = ch.back(); ch.pop_back();
				q.push_front(tp);
			}
				lst = l;
			while(!q.empty()){
				tp = q.back(); if(tp<=lst)break;
				lst = tp;
				q.pop_back(); numr++;
			}	
				if(numl>=numr){
					num+=numl; numl++; while(numl--)ans.push('L');
				}else if(numl<numr){
					num+=numr; numr++; while(numr--)ans.push('R');
				}
				break;
			}
			
		}else if(l>lst){
			lst = l; q.pop_front(); ans.push('L');	num++;
		}else if(r>lst){
			lst = r; q.pop_back(); ans.push('R');	num++;
		}else if(l<=lst&&r<=lst){
			break;
		}
	}
	
	if(!fg&&q.size()>0&&q.front()>lst)ans.push('L'),num++;
	
	printf("%d\n",num);
	while(!ans.empty()){
		cout<<ans.front(); ans.pop();
	}
	
	
	return 0;
}
/*
9
2 5 6 5 6 9 7 5 2

*/

E.  发现n-a[i]就是最优解,直接multiset查找即可

//E
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 2e5+100;
int a[maxn];
multiset<int> b;
int n;
int main() {
	int tp,ling=0;
	scanf("%d",&n);
	for(int i=0; i<n; i++) {
		scanf("%d",&a[i]);
	}
	for(int i=0; i<n; i++) {
		scanf("%d",&tp);
		b.insert(tp);
	}


	for(int i=0; i<n; i++) {
		{
			int x = (n - a[i])%n;
			multiset<int>::iterator id = b.lower_bound(x);
			if(id==b.end()) 
				id = b.begin();//找不到则取第一个 
			a[i]=((*id)+a[i])%n;
			b.erase(id);// 注意end()不可删除 
		}
	}

	for(int i=0; i<n; i++)
		printf("%d%c",a[i]," \n"[i==n-1]);

	return 0;
}

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wym_king

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值