CF1990B 题解

· · 题解

题意不再赘述。

Sol

注意到关键条件 x>y。

要想使 x 成为前缀最大值,那么必然有 a_{x+1}=-1,同理有 a_{y-1}=-1。

不妨先把 y 到 x 先填上 1,这样肯定不劣。

现在考虑 1 到 y-1 以及 x+1 到 n 怎么填。一开始我把这些全填了 -1,吃了一发罚时。

为什么呢,考虑 n=8,x=5,y=4,那么根据上面的构造有 a=-1,-1,-1,1,1,-1,-1,-1。其前缀最大在 1 而不是 5。

现在我们有了一个新方向。让 1 到 y-1 以及 x+1 到 n 的和的绝对值尽可能小,那么直接从 x+1 到 n 交替填 -1,1 即可,y 同理。

对于刚才那个例子,我们有 a=-1,1,-1,1,1,-1,1,-1。满足要求。

为什么是对的呢,在后文我们只考虑前缀最大值,后缀最大值同理。

首先 x>y,那么 x-y+1\geq 2,只要我们保证了 \sum_{i=1}^{y-1}a_i\geq -1,那么最大值一定是正数,就不会出现刚才的情况。

考虑 y-1 的奇偶性,如果奇数,和为 -1,否则和为 0。满足要求。

Code

#include <iostream>
using namespace std;
#define int long long
typedef pair<int,int> pii;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
const int N=2e5+10,INF=0x3f3f3f3f3f3f3f3f,mod=1e9+7;
int a[N];
void solve(){
    int n,x,y;
    cin>>n>>x>>y;
    fill(a,a+1+n,0);
    for(int i=x+1;i<=n;i++)a[i]=-1;
    for(int i=1;i<y;i++)a[i]=-1;
    for(int i=1;i<=n;i++){
        if(a[i]==-1)cout<<-1<<' ';
        else cout<<1<<' ';
    }
    cout<<'\n';
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int t;cin>>t;
    while(t--)solve();
    return 0;
}