0%

思路讲解

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……)

思路讲解

最长上升子序列询问版,还有最大值限制。

不过确实LIS的dp做法我确实没学过,那个贪心做法倒是学过的。

P1020 [NOIP1999 提高组] 导弹拦截

贪心做法总结来说就是利用一个数组,既保留长度信息,也保留位置最低数信息,进而避免使用多个数组导致时间复杂度上升。具体可以参见下面的leetcode题解。

https://leetcode.com/problems/longest-increasing-subsequence/solutions/1326308/c-python-dp-binary-search-bit-segment-tree-solutions-picture-explain-o-nlogn

然后有了这个贪心方法,再加上离线trick即可,就能求出答案。处理到哪里,就把所有和i相关的询问(R==i)给处理掉。

当然,需要小心,这道题目还要求严格递增,不过也简单,把upper_bound换成lower_bound即可

AC代码

https://atcoder.jp/contests/abc393/submissions/63583196

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
// Problem: F - Prefix LIS Query
// Contest: AtCoder - AtCoder Beginner Contest 393
// URL: https://atcoder.jp/contests/abc393/tasks/abc393_f
// Memory Limit: 1024 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,4> 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,Q,T,A[MAXN];
arr query[MAXN];

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

inline void solve(){
cin>>N>>Q;
for(int i=1;i<=N;++i){
cin>>A[i];
}
for(int i=1;i<=Q;++i){
cin>>query[i][0]>>query[i][1];
query[i][2]=i;
}
sort(query+1,query+1+Q);
vector<ll> aux; // 辅助数组的英文
ll tot=1;
for(int i=1;i<=N;++i){ // aux里的所有数都比i的编号小
ll idx=lower_bound(all(aux),A[i])-aux.begin();
if(idx==aux.size()){
aux.pb(A[i]);
}else{
aux[idx]=A[i];
}
// 我们采用离线思想,处理到哪里,就把所有和i相关的询问给处理掉。
while(query[tot][0]<=i && tot<=Q){
if(query[tot][0]==i){
ll x=query[tot][1];
ll lans=upper_bound(all(aux),x)-aux.begin();
if(lans<0){
query[tot][3]=0;
}else{
query[tot][3]=lans;
}
}
++tot;
}
// #ifdef LOCAL
// for(int i=0;i<aux.size();++i){
// cerr<<aux[i]<<" ";
// }
// cerr<<"\n";
// #endif
}
sort(query+1,query+1+Q,cmpi);
for(int i=1;i<=Q;++i){
cout<<query[i][3]<<"\n";
}
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
solve();
return 0;
}
/*
AC
https://atcoder.jp/contests/abc393/submissions/63583196
*/

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

https://atcoder.jp/contests/abc393/submissions/63582964

有点招笑了,样例竟然会RE?这在我电脑上跑的好好的,只能说C++经典咏流传了。

搞半天原来是while的问题,要加一个tot≤Q

1
while(query[tot][0]<=i && tot<=Q){

思路讲解

2025牛客WC1-J-硝基甲苯之袭

和这道题比较类似吧,都是GCD,而且都要枚举。

其实思路都是差不多的,就是类似于埃氏筛的枚举方法,可以将时间复杂度降至O(nlogn)左右。

具体而言,我们讲解一下下面这段程序。i其实是在枚举divisor(也就是除数,答案),j就是在枚举能被这个divisor整除的数。如果我们发现所有能够被整除的数在A中出现了K次及以上,就对这其中所有的数都更新答案为K。

1
2
3
4
5
6
7
8
9
10
11
for(int i=1;i<=M;++i){
ll cnt=0;
for(int j=i;j<=M;j+=i){
cnt+=occA[j];
}
if(cnt>=K){
for(int j=i;j<=M;j+=i){
ans[j]=i;
}
}
}

AC代码

https://atcoder.jp/contests/abc393/submissions/63486992

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
// Problem: E - GCD of Subset
// Contest: AtCoder - AtCoder Beginner Contest 393
// URL: https://atcoder.jp/contests/abc393/tasks/abc393_e
// Memory Limit: 1024 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>(1.2e6)+10,INF=static_cast<ll>(5e18)+3;

ll N,K,T,A[MAXN],sortA[MAXN],ans[MAXN],occA[MAXN];

inline void solve(){
cin>>N>>K;
ll M=0;

for(int i=1;i<=N;++i){
cin>>A[i];
M=max(M,A[i]);
// sortA[i]=A[i];
}
// sort(sortA+1,sortA+1+N);
for(int i=1;i<=M;++i){
ans[i]=1;
occA[i]=0;
}
for(int i=1;i<=N;++i){
occA[A[i]]+=1;
}
// 上面全部是在初始化
for(int i=1;i<=M;++i){
ll cnt=0;
for(int j=i;j<=M;j+=i){
cnt+=occA[j];
}
if(cnt>=K){
for(int j=i;j<=M;j+=i){
ans[j]=i;
}
}
}
for(int i=1;i<=N;++i){
cout<<ans[A[i]]<<"\n";
}
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
solve();
return 0;
}
/*
AC
https://atcoder.jp/contests/abc393/submissions/63486992
*/

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