0%

思路讲解

题目说了一堆花里胡哨的,其实意思很简单,就是把你这个点,和所有点(包括你这个点)组成的线段的长度总和就是答案。

那么想要知道这个,我就想到了前缀和。但前缀和的话短的-长的是负值怎么办?可以对前缀和进行分段,具体的,其实就是这段代码。

1
2
3
4
5
for(int i=1;i<=N;++i){
ll idx=upper_bound(X+1,X+1+N,(arr){X[i][0],INF,INF})-X;
ll lsum=sum[idx-1],rsum=sum[N]-sum[idx-1];
X[i][2]=(X[i][0]+1)*(idx-1)-lsum+rsum+N-idx+1-X[i][0]*(N-idx+1);
}

AC代码

https://codeforces.com/problemset/submission/1857/309735803

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
// Problem: E. Power of Points
// Contest: Codeforces - Codeforces Round 891 (Div. 3)
// URL: https://codeforces.com/problemset/problem/1857/E
// 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 (long long i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (long long 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;
arr X[MAXN];
ll sum[MAXN];

inline bool cmpi(const arr &a,const arr &b){
return a[1]<b[1];
}

inline void solve(){
cin>>N;
for(int i=1;i<=N;++i){
cin>>X[i][0];
X[i][1]=i;
}
sort(X+1,X+1+N);
sum[0]=0;
for(int i=1;i<=N;++i){
sum[i]=X[i][0]+sum[i-1];
}
for(int i=1;i<=N;++i){
ll idx=upper_bound(X+1,X+1+N,(arr){X[i][0],INF,INF})-X;
ll lsum=sum[idx-1],rsum=sum[N]-sum[idx-1];
X[i][2]=(X[i][0]+1)*(idx-1)-lsum+rsum+N-idx+1-X[i][0]*(N-idx+1);
}
// 输出答案阶段
sort(X+1,X+1+N,cmpi);
for(int i=1;i<=N;++i){
cout<<X[i][2]<<" ";
}
cout<<"\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/problemset/submission/1857/309735803
*/

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

思路讲解

总体思路就是一个贪心,想办法省最多的钱。首先,这一天他没有去,那这钱他是省不了了,因为后面的天他去,肯定省后面的多的钱,前面的天也没法买他,因此,我们把这个人加到que0里(实际是一个栈,这种我应该用deque,代码里是vector)。

然后可以省钱的,必须要给他找个搭档,先在que0里找,再在rem(A[i]==’1’的i的set)里找,找小的(通过删除rem.begin()实现)。

AC代码

https://codeforces.com/contest/2026/submission/309701489

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
// Problem: C. Action Figures
// Contest: Codeforces - Educational Codeforces Round 171 (Rated for Div. 2)
// URL: https://codeforces.com/problemset/problem/2026/C
// Memory Limit: 512 MB
// Time Limit: 2500 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define FOR(i, a, b) for (long long i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (long long 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>(4e5)+10,INF=static_cast<ll>(5e18)+3;

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

inline void solve(){
cin>>N;
ll ans=(1+N)*N/2;
vector<ll> seq0;
set<ll> rem;
for(int i=1;i<=N;++i){
cin>>A[i];
if(A[i]=='0') seq0.push_back(i);
if(A[i]=='1') rem.insert(i);
}
ll idx=0;
// for(int i=N;i>=1;--i){
// if(i<=idx) break;
// if(A[i]=='1' && vis[i]){
//
// ans-=i;
// ++idx;
// }
// }
// #ifdef LOCAL
// cerr << "OK "<<T << '\n';
// #endif
while(!rem.empty()){
set<ll>::iterator it=prev(rem.end());
while(!seq0.empty() && seq0.back()>*it ){
seq0.pop_back();
}
// #ifdef LOCAL
// cerr << seq0.back() << '\n';
// #endif
if(!seq0.empty()){
seq0.pop_back();
ans-=*it;
}else{
if(rem.size()>=2){
rem.erase(rem.begin());
ans-=*it;
// #ifdef LOCAL
// cerr << ans <<" "<< *it << '\n';
// #endif
}
}
rem.erase(it);
}
cout<<ans<<"\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/2026/submission/309701489
*/

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

思路讲解

ABC - 370 - E - Avoid K Partition

这种子序列的问题,无论是dp还是构造,抓手就是以下标i为结尾的子序列。

以这个结构单元来分析,不重不漏,一点一点的来。

AC代码

https://codeforces.com/problemset/submission/1809/309628591

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
// Problem: C. Sum on Subarrays
// Contest: Codeforces - Educational Codeforces Round 145 (Rated for Div. 2)
// URL: https://codeforces.com/problemset/problem/1809/C
// Memory Limit: 512 MB
// Time Limit: 2000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define FOR(i, a, b) for (long long i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (long long 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 N,K,T,A[MAXN];

inline void solve(){
cin>>N>>K;
ll res=0;
for(int i=1;i<=N;++i){
if(res==K){
A[i]=-999;
}else if(res+i>K){
ll rem=K-res;
A[i]=-((i-1-rem)*2+1);
res=K;
}else{
res+=i;
A[i]=2;
}
}
for(int i=1;i<=N;++i){
cout<<A[i]<<" ";
}
cout<<"\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/problemset/submission/1809/309628591
*/

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

思路讲解

哈哈,其实说起来也简单,在xor中,加上这个数和减去这个数都是xor,因此对区间进行反转操作,就是对这个区间进行异或就可以了,不用区分是加上还是减去。

当然,怎么对区间进行异或那?其实也很简单,异或前缀和。

AC代码

https://codeforces.com/problemset/submission/1872/309601127

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
// Problem: E. Data Structures Fan
// Contest: Codeforces - Codeforces Round 895 (Div. 3)
// URL: https://codeforces.com/problemset/problem/1872/E
// 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 (long long i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (long long 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>(1e5)+10,INF=static_cast<ll>(5e18)+3;

ll N,T,A[MAXN];
char S[MAXN];
ll sum[MAXN];
inline void solve(){
cin>>N;
sum[0]=0;
for(int i=1;i<=N;++i){
cin>>A[i];
sum[i]=sum[i-1]^A[i];
}
ll res1=0,res0=0;
for(int i=1;i<=N;++i){
cin>>S[i];
if(S[i]=='1'){
res1^=A[i];
}else{
res0^=A[i];
}
}
ll Q;
cin>>Q;
for(int i=1;i<=Q;++i){
ll op;
cin>>op;
if(op==1){
ll l,r;
cin>>l>>r;
res1^=(sum[r]^sum[l-1]);
res0^=(sum[r]^sum[l-1]);
}else{
ll ind;
cin>>ind;
if(ind){
cout<<res1<<" ";
}else{
cout<<res0<<" ";
}
}
}
cout<<"\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/problemset/submission/1872/309601127
*/

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

思路讲解

把图论和回文放在一起考,也是比较新颖的。

6了,好像是bfs,离谱。对100这个数字还是要敏感一点,其实是能够接受O(n**4)的算法的。

AC代码

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