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;
}