0%

1809C. Sum on Subarrays

思路讲解

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