Codeforces Round 1006 (Div. 3) D/E/F

D. For Wizards, the Exam Is Easy, but I Couldn't Handle It

原题链接:https://mirror.codeforces.com/contest/2072/problem/D

思路:

每次对 \(l~r\) 的序列进行操作后,\(a[l]\) 处的数会移到 \(r\) 的右侧,对 \(inv\) 的个数的影响是 \(cnt0 - cnt1\) 其中 \(cnt0\)\(l~r\) 中小于 \(a[l]\) 的个数而 \(cnt1\)\(l~r\)中大于 \(a[l]\) 的个数。而答案就是去找这个最大值所对应的区间。

这题数据范围并不大,且保证了 \({n^2}\) 的总和不超过 \(4⋅10^6\),所以可以考虑暴力的解法

时间复杂度 \(O(n^2)\)

代码

#include<bits/stdc++.h>
using namespace std;    

typedef long long ll;
typedef pair<int,int> PII;

const int N=2e3+10,mod=998244353;

int n;
int suf[N][N],pre[N][N];

void solve(){
    int n;
    cin>>n;

    vector<int> a(n);
    for(int i=0;i<n;i++)cin>>a[i];

    vector<int> all=a;
    
    sort(all.begin(),all.end());
    all.erase(unique(all.begin(),all.end()),all.end());

    for(auto x:all){
        suf[x][n]=0;
        for(int i=n-1;i>=0;i--){
            suf[x][i]=suf[x][i+1]+(a[i]<x);
        }
        pre[x][0]=a[0]>x;
        for(int i=1;i<n;i++){
            pre[x][i]=pre[x][i-1]+(a[i]>x);
        }
    }


    int ml=1,mr=1,mx=0;
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(suf[a[i]][i+1]-suf[a[i]][j+1]-(pre[a[i]][j]-pre[a[i]][i])>mx){
                ml=i+1,mr=j+1;
                mx=suf[a[i]][i+1]-suf[a[i]][j+1]-(pre[a[i]][j]-pre[a[i]][i]);
            }
        }
    }

    cout<<ml<<' '<<mr<<endl;
}

int main() {
    cin.tie(0)->sync_with_stdio(false);
    cout.tie(0);

    int t=1;
    cin>>t;

    while(t--)solve();
}

E. Do You Love Your Hero and His Two-Hit Multi-Target Attacks?

原题链接:https://mirror.codeforces.com/contest/2072/problem/E

思路:

\(n\) 最大可为500,而 \(k\) 的值最大为 \(10^5\),所以完全可以保证答案有解。具体的构造就是,在不超过 \(k\) 的前提下在每一行尽可能多地放置,然后如果在当前行无法再放之后,就再另起一行继续放,直到为k。

时间复杂度 不知道 应该为根号k

代码

#include<bits/stdc++.h>
using namespace std;    

typedef long long ll;
typedef pair<int,int> PII;

const int N=2e5+10,mod=998244353;

int n;

void solve(){
    int k;
    cin>>k;

    if(k==0){
        cout<<0<<endl;
        return;
    }

    int i=0,j=0;
    vector<vector<PII>> points;
    
    while(k){   
        vector<PII> temp;
        while(k>=temp.size()){
            k-=temp.size();
            temp.push_back({i,j++});
        }
        i++;
        points.push_back(temp);
    }

    cout<<j<<endl;
    for(auto temp:points){
       for(auto [x,y]:temp){
            cout<<x<<' '<<y<<endl;
        }
    }
}   


int main() {
    cin.tie(0)->sync_with_stdio(false);
    cout.tie(0);

    int t=1;
    cin>>t;

    while(t--)solve();
}

F. Goodbye, Banker Life

原题链接:https://mirror.codeforces.com/contest/2072/problem/F

思路:

直接打表看了看,发现能用递归写,就直接写了。 但这题正解应该是杨辉三角,然后判断奇偶性。

时间复杂度 O(n)

代码

#include<bits/stdc++.h>
using namespace std;    

typedef long long ll;
typedef pair<int,int> PII;

const int N=1e6+10,mod=998244353;

int n,k;
int a[N];

void dfs(int st,int len){
    int t=__lg(len);
    if((1<<t)==len){
        for(int i=st;i<st+len;i++)a[i]=1;
        return;
    }
    dfs(st,len-(1<<t));
    dfs(st+(1<<t),len-(1<<t));
}

void solve(){
    cin>>n>>k;

    memset(a,0,sizeof a);

    dfs(1,n);

    for(int i=1;i<=n;i++){
        if(a[i]) cout<<k<<' ';
        else cout<<0<<' ';
    }
    cout<<endl;
}

int main() {
    cin.tie(0)->sync_with_stdio(false);
    cout.tie(0);

    int t=1;
    cin>>t;

    while(t--)solve();
}
posted @ 2025-02-26 20:03  宋佳奇  阅读(40)  评论(0)    收藏  举报