Codeforces954D.E

Codeforces 954 D.E

D

原题链接:Codeforces 954 D

本题算是一道分类讨论题,主要分的情况为以下几种:

  1. n为3且第二位为0的情况
  2. n大于2且存在0的情况
  3. 其他情况

当n为3且第二位为0时,只需要输出即可.

当n大于2且存在0时,只需输出0即可,因为一定做得到*0。

当为其他情况时,对于“1”而言我们优先去乘,对于其他数字,我们优先去加,再除最后一位以外的每一位乘10并加上其后一位即可。

代码:(代码是cp别人的,自己写的惨不忍睹)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define lowbit(x) ((x)&(-x))
#define endl '\n'

typedef long long LL;
typedef pair<int,int> PII;
const int N=2e5+10,M=2e5+10,mod=998244353,INF=0x3f3f3f3f;
int n, m, k;
char s[N];

void solve() {
cin >> n >> s + 1;
vector<int> cnt(10);
for (int i = 1; i <= n; i++) {
if (s[i] == '0' && n > 2) {
if (n == 3 && i == 2) {
int a = s[i-1] - '0';
int b = s[i+1] - '0';
cout << min(a * b, a + b) << endl;
} else cout << "0\n";
return;
}
cnt[s[i] - '0'] ++;
}
int res = INF;
for (int i = 1; i <= n - 1; i ++) {
int ans = 10 * (s[i] - '0') + s[i + 1] - '0';
cnt[s[i] - '0'] -- , cnt[s[i + 1] - '0'] --;
for (int i = 2; i <= 9; i ++) {
ans += cnt[i] * i;
}
cnt[s[i] - '0'] ++ , cnt[s[i + 1] - '0'] ++;
res = min(res, ans);
}
cout << res << endl;
}

int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int _;
_ = 1;
cin >> _;
while(_ --) {
solve();
}
return 0;
}
CPP

E

原题链接:Codeforces 954 E

本题也算是一道分类讨论题目。

显然,对于两个数,只有模k的余数相同时才有可能经过变换使相同,所以我们可以先根据不同的模,进行分类。

然后,对于模相同的数组,显然最小的操作方案数为先sort,再求sum(arr[i+1]-arr[i]),这时我们需要讨论的是arr数组size的奇偶性问题。

当n为偶数时,arr数组的size必须均为偶数。

当n为奇数时,必须有且只有一个size为奇数,再对为奇数的这个数组进行分类讨论那个数不选即可。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
using namespace std;
int t;
int n,k;
void solve(){
cin>>n>>k;
set<int>s;
map<int,vector<int> >arr;
int index;
for(int i=1;i<=n;i++){
cin>>index;
if(s.count(index))s.erase(index);
else s.insert(index);
}
for(auto i:s){
arr[i%k].push_back(i);
}
for(auto&[x,y]:arr){
sort(y.begin(),y.end());
}
ll res=0;
if(n%2==0){
for(auto&[x,y]:arr){
if(y.size()%2==1){
cout<<-1<<endl;
return;
}
for(int i=0;i+1<y.size();i+=2){
res+=(y[i+1]-y[i])/k;
}
}
cout<<res<<endl;
}
else {
int pos=-1;
for(auto&[x,y]:arr){
if(y.size()%2==1){
if(pos==-1)pos=x;
else {
cout<<-1<<endl;
return;
}
}
}
if(pos==-1){
cout<<-1<<endl;
return;
}
for(auto&[x,y]:arr){
if(x!=pos){
for(int i=0;i+1<y.size();i+=2){
res+=(y[i+1]-y[i])/k;
}
}
else {
ll f[y.size()+2];
ll g[y.size()+2];
fill(f,f+y.size()+1,0);
fill(g,g+y.size()+1,0);
for(int i=0;i+1<y.size();i+=2){
f[i]+=((i-2>=0)?f[i-2]:0)+(y[i+1]-y[i])/k;
}
for(int i=y.size()-2;i>=0;i-=2){
g[i]+=((i+2<y.size())?g[i+2]:0)+(y[i+1]-y[i])/k;
}//这是一种巧妙的在保证时间复杂度的情况下,枚举每一位是否选择的方法
ll ans=1e18;
for(int i=0;i<y.size();i+=2){
ans=min(ans,(i-2>=0?f[i-2]:0)+(i+1<y.size()?g[i+1]:0));
}
res+=ans;
}
}
cout<<res<<endl;
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>t;
while(t--){
solve();
}
}
CPP

Codeforces954D.E
https://shepherdzzx.github.io/Codeforces954D-E/
Author
Shepherdz
Posted on
June 26, 2024
Licensed under