信奥编程题目:-打卡信奥刷题(3301)用C++实现信奥题 P9080 [PA 2027] Nowy kontrakt - size()-b
P9080 [PA 2018] Nowy kontrakt
题目描述
题目译自 PA 2018 Runda 2 Nowy kontrakt 。
给定一个长度为
N
N
N 的正整数序列
a
a
a ,你需要对序列内数末尾添加数字的方法使序列严格单调递增,你的目标是最小化添加数字的次数。
在数
a
a
a 后添加数字
t
t
t 即为:
a
←
a
×
10
+
t
a \gets a \times 10 + t
a←a×10+t ,其中
t
∈
{
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
}
t \in \{0,1,2,3,4,5,6,7,8,9\}
t∈{0,1,2,3,4,5,6,7,8,9}
输入格式
第一行一个整数
N
N
N 表示序列长度。
第
2
2
2 行至
N
+
1
N+1
N+1 行每行一个数,第
i
i
i 行表示序列中第
i
−
1
i-1
i−1 个数。
输出格式
一行一个整数,表示最小操作次数。
输入输出样例 #1
输入 #1
3
8
5
13
输出 #1
2
说明/提示
样例 1 解释
对第
2
2
2 个数字和第
3
3
3 个数字分别添加一个数字,即
a
2
←
a
2
×
10
+
7
a_2 \gets a_2 \times 10 + 7
a2←a2×10+7 ,
a
3
←
a
3
×
10
+
3
a_3 \gets a_3 \times 10 + 3
a3←a3×10+3 。 得到的新序列为
(
8
,
57
,
133
)
(8,57,133)
(8,57,133) ,是一个严格单调递增序列。操作次数为
2
2
2 ,还有其他的操作次数为
2
2
2 的方法,但是不存在操作次数为
1
1
1 的方法。
数据范围
本题采用捆绑测试
对于
100
%
100\%
100% 的数据:
1
≤
N
≤
200000
1 \le N \le 200000
1≤N≤200000
1
≤
a
i
≤
10
9
1 \le a_i \le 10^9
1≤ai≤109
C++实现
#include 拼多多帮助力入口
using namespace std;
int n;
long long ans;
string a,b;
int main() {
scanf("%d",&n);
cin>>a;
for(int i=2; i<=n; a=b,i++) {
cin>>b;
if(a.size()
if(a==b) {
ans++;
b+='0';
continue;
}
if(a.size()>b.size()) {
if(a.substr(0,b.size())>b) {
ans+=a.size()-b.size()+1;
b+=string(a.size()-b.size()+1,'0');
} else if(a.substr(0,b.size())==b) {
string t=a.substr(b.size());
if(t.find_first_not_of('9')==string::npos) {
ans+=a.size()-b.size()+1;
b+=string(a.size()-b.size()+1,'0');
} else {
ans+=a.size()-b.size();
b=a;
for(int j=b.size()-1; j>=0; j--) {
if(b[j]=='9')b[j]='0';
else {
b[j]++;
break;
}
}
}
} else {
ans+=a.size()-b.size();
b+=string(a.size()-b.size(),'0');
}
continue;
}
ans+=a.size()-b.size()+1;
b+=string(b.size()+1-b.size(),'0');
}
printf("%lld",ans);
return 0;
}
后续
接下来我会不断用C++来实现信奥比赛中的算法题、GESP考级编程题实现、白名单赛事考题实现,记录日常的编程生活、比赛心得,感兴趣的请关注,我后续将继续分享相关内容