按照题意模拟 末尾0删除 每个数最多出现1次
进行0次或若干次+1操作 使得得到最多不同数的个数
每个数最多统计1次 n=1e9
A
//n=1e9 模拟 map
map<int,int> mp;//1e9 最多出现1e9次
int main()
{
IO;
int n,cnt=0;//cnt最多1e9个
cin>>n;
//1098
mp[n]++;
cnt++;
while(n)
{
n++;
while(n%10==0)
n=n/10;
if(mp[n])
break;
mp[n]++;
cnt++;
}
cout<<cnt<<endl;
return 0;
}
B最多只能选择1个连续的区间修改 优先改最高位 使值最大
You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in a, and replace each digit x from this segment with f(x).
进行1次区间修改使值最大
不能刚开始比映射值大 不用修改就break 前提是区间修改已经使用
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const ll inf=0x3f3f3f3f3f3f3f3f;
const int INF=0x3f3f3f3f;
const ll mod=1e9+7;
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
ll gcd(ll a,ll b)
{
return b!=0?gcd(b,a%b):a;
}
char a[9];
int main()
{
IO;
int n,f=0;
string s;
cin>>n;
cin>>s;
for(int i=1;i<=9;i++)
{
cin>>a[i];
}
for(int i=0;i<n;i++)
{
if(s[i]<a[s[i]-'0'])//保证改的是最高位
{
s[i]=a[s[i]-'0'];
f=1;//看区间能连续多久
}
else if(f==1 && s[i]>a[s[i]-'0'])//不能刚开始时s[i]大 不用替换这宝贵一次还没用 就break掉
{ //除非已经修改过了 这次不能修改了
break;
}
}
cout<<s<<endl;
return 0;
}
C1
每次操作只能移除左右两端的数
使得移除的数严格递增的最大长度
左右移除 不一定用erase,左右双指针标记2端
queue<char> q;
int a[maxn];
int main()
{
IO;
int n,f=0;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
int l=1,r=n,temp=0,cnt=0;
while(n--)
{
if(a[l]>temp && a[r]>temp)
{
if(a[l]<a[r])
{
temp=a[l];
cnt++;
l++;
q.push('L');
}
else
{
temp=a[r];
cnt++;
r--;
q.push('R');
}
}
else if(a[l]>temp)
{
temp=a[l];
cnt++;
l++;
q.push('L');
}
else if(a[r]>temp)
{
temp=a[r];
cnt++;
r--;
q.push('R');
}
else
break;
}
cout<<cnt<<endl;
while(!q.empty())
{
cout<<q.front();
q.pop();
}
cout<<endl;
return 0;
}