0%

思路讲解

感觉这题真出的没什么意思,纯粹就是看你有没有把题目读懂,还有K==1的特殊情况有没有想到。

AC代码

https://codeforces.com/contest/2075/submission/311347701

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
88
89
90
// Problem: B. Array Recoloring
// Contest: Codeforces - Educational Codeforces Round 176 (Rated for Div. 2)
// URL: https://codeforces.com/contest/2075/problem/B
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int) a.size())

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
typedef double DB;
typedef pair<DB,DB> pdd;
constexpr ll MAXN=static_cast<ll>(5e3)+10,INF=static_cast<ll>(5e18)+3;

ll N,K,T,A[MAXN],sortA[MAXN];
ll pos[MAXN];
bool vis[MAXN];

inline bool cmp(const ll &a,const ll &b){
return A[a]>A[b];
}

inline void solve(){
cin>>N>>K;
FOR(i,1,N){
cin>>A[i];
sortA[i]=A[i];
}
sort(sortA+1,sortA+1+N);
FOR(i,1,N){
pos[i]=i;
}
sort(pos+1,pos+1+N,cmp);
ll ansT=0;
FOR(i,1,K){
vis[pos[i]]=true;
ansT+=A[pos[i]];
}
ll ans=ansT;
if(K!=1){
ans+=A[pos[K+1]];
cout<<ans<<"\n";
}else{
ans=0;
FOR(i,1,N){
ll lans=0;
if(i==1){
lans+=A[1];
lans+=A[N];
}else if(i==N){
lans+=A[N];
lans+=A[1];
}else{
lans+=A[i];
lans+=max(A[1],A[N]);
}
ans=max(ans,lans);
}
cout<<ans<<"\n";
}
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>T;
while(T--){
solve();
}
return 0;
}
/*

*/

心路历程(WA,TLE,MLE……)

思路讲解

难点在于怎么把这个双循环搞成单循环

image

然后记得读题,那个栅栏仅能由两种颜色构成,多了,少了都不行。

AC代码

这个+2也是比较迷,但加了就AC了,我也不知道为什么。

可能是原来比16还要多减一次1,但其实和16一样。

https://codeforces.com/contest/2075/submission/311344453

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
88
89
90
91
92
93
94
95
96
97
98
99
100
// Problem: C. Two Colors
// Contest: Codeforces - Educational Codeforces Round 176 (Rated for Div. 2)
// URL: https://codeforces.com/contest/2075/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int) a.size())

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
typedef double DB;
typedef pair<DB,DB> pdd;
constexpr ll MAXN=static_cast<ll>(2e5)+10,INF=static_cast<ll>(5e18)+3;

ll N,M,T,A[MAXN],sumA[MAXN];

inline void solve(){
cin>>N>>M;
// ll maxA=0;
FOR(i,1,M){
cin>>A[i];
}
sort(A+1,A+1+M);

sumA[0]=0;
FOR(i,1,A[M]){
ll idx=M-(lower_bound(A+1,A+1+M,N-i)-A)+1;
if(i==N){
idx=0;
}
sumA[i]=sumA[i-1]+idx;
}
ll ans=0;
// FOR(i,1,M){ // 暴力法存档
// FOR(j,1,A[i]){
// ll idx=M-(lower_bound(A+1,A+1+M,N-j)-A)+1;
// if(N-j<=A[i]){
// idx-=1;
// }
// if(j==N){
// idx=0;
// }
// ans+=idx;
// }
// }
FOR(i,1,M){
ans+=sumA[A[i]];
// if(A[i]==N){
// if((A[i]-(N-A[i])+1)>0){
// ans-=(A[i]-(N-A[i])+1);
// }
// }else{
if((A[i]-(N-A[i])+1)>0){
ans-=(A[i]-(N-A[i])+1);
}

if(A[i]>=N){
ans+=2;
}
}
// #ifdef LOCAL
// FOR(i,1,A[M]){
// cerr<<sumA[i]<<" ";
// }
// cerr<<"\n";
// #endif
cout<<ans<<"\n";
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>T;
while(T--){
solve();
#ifdef LOCAL
cerr << '\n';
#endif
}
return 0;
}
/*

*/

心路历程(WA,TLE,MLE……)

思路讲解

题解还用了二进制分析?我看不懂,但我大受震撼。(做的时候没看)

我的理解是这样的,首先无关痛痒的操作,一定是交给floor做会往大里走,交给ceil做会往小里走。

然后那如果事关痛痒的操作,那么还是先floor做,再ceil做大,反之就小。

如果要系统的解释还是要依赖二进制。其实答案要么是 x/2n+mx/2^{n+m},要么是其+1。我们来解释一下。

image

我们发现,如果要将一个进位传递过去,会消耗所有路径上的1,因此并不会出现累加的情况。

其他就看官解吧,讲的很清楚。

https://codeforces.com/blog/entry/140702

主要就是要注意,ceil的话opX=1的时候会卡住,需要对这一情况进行预判。

1
2
3
if(opX==1){
break;
}

AC代码

https://codeforces.com/contest/2082/submission/311004371

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Problem: B. Floor or Ceil
// Contest: Codeforces - Codeforces Round 1010 (Div. 2, Unrated)
// URL: https://codeforces.com/contest/2082/problem/B
// Memory Limit: 512 MB
// Time Limit: 1000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int) a.size())

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
typedef double DB;
typedef pair<DB,DB> pdd;
constexpr ll MAXN=static_cast<ll>(1e6)+10,INF=static_cast<ll>(5e18)+3;

ll X,N,M,T,A[MAXN];

inline void solve(){
cin>>X>>N>>M;
ll opM=M,opN=N,opX=X;
while(opX>0){
if(opX%2==0){
if(opM>0){
opX/=2;
--opM;
}else if(opN>0){
opX/=2;
--opN;
}else{
break;
}
}else{
if(opM>0){
// #ifdef LOCAL
// cerr << "op前 "<<opX << '\n';
// #endif
if(opX==1 && opN!=0){
opX=0;
break;
}else if(opX==1 && opN==0){
break;
}
opX=ceil(opX*1.0/2);
// #ifdef LOCAL
// cerr << opX << '\n';
// #endif
--opM;
}else if(opN>0){
opX/=2;
--opN;
}else{
break;
}
}
}
cout<<opX<<" ";
opM=M,opN=N,opX=X;
while(opX>0){
if(opX%2==0){
if(opN>0){
opX/=2;
--opN;
}else if(opM>0){
opX/=2;
--opM;
}else{
break;
}
}else{
if(opN>0){
opX/=2;
--opN;
}else if(opM>0){
if(opX==1){
break;
}
opX=ceil(opX*1.0/2);
--opM;
}else{
break;
}
}
}
cout<<opX<<"\n";
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>T;
while(T--){
solve();
}
return 0;
}
/*
AC
https://codeforces.com/contest/2082/submission/311004371
*/

心路历程(WA,TLE,MLE……)

思路讲解

https://codeforces.com/contest/2052/submission/297107696

jianly的思路我觉得比官解更好(官解都看不懂是什么意思)

jianly的思路就是逆向思维,先将345612(这是输入进来的)转化为654321,再将654321转化为123456

整个过程我们都记录下来,reverse一下输出即可。

AC代码

https://codeforces.com/contest/2052/submission/311474567

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
// Problem: CF2052A Adrenaline Rush
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF2052A
// Memory Limit: 1000 MB
// Time Limit: 3000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int) a.size())

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
typedef double DB;
typedef pair<DB,DB> pdd;
constexpr ll MAXN=static_cast<ll>(1e3)+10,INF=static_cast<ll>(5e18)+3;

ll N,M,C[MAXN];

inline void solve(){
cin>>N;
FOR(i,1,N){
cin>>C[i];
}
vector<pll> ans;
// 先将345612(这是输入进来的)转化为654321
ROF(i,N,1){
ll pi=0;
FOR(j,1,N){
if(C[j]==i){
pi=j;
break;
}
}
ROF(j,pi-1,1){
if(C[j]<C[j+1]){
// 不仅顺序最后要倒过来,超车顺序也要倒过来
ans.pb({C[j],C[j+1]});
swap(C[j],C[j+1]);
}
}
}
FOR(i,1,N){
FOR(j,i+1,N){
ans.pb({j,i});
}
}
reverse(ans.begin(),ans.end());
cout<<SZ(ans)<<"\n";
FOR(i,0,SZ(ans)-1){
cout<<ans[i].fi<<" "<<ans[i].se<<"\n";
}
// #ifdef LOCAL
// FOR(i,1,N){
// cerr<<C[i]<<" ";
// }
// cerr<<"\n";
// #endif
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
solve();
return 0;
}
/*
AC
https://codeforces.com/contest/2052/submission/311474567
*/

心路历程(WA,TLE,MLE……)