Codeforces Round #690 (Div. 3)

Codeforces Round #690 (Div. 3)

  • A
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize(2)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
void io(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}

int t,n;
const int maxn=400;
int a[maxn],b[maxn];

int main()
{
    io();
    cin>>t;
    while(t--){
        cin>>n;
        for(int i=1;i<=n;++i) cin>>a[i];
        int tmp=(n+1)/2;
        for(int i=1,j=1;i<=tmp;++i,j+=2){
            b[j]=a[i];
        }
        for(int i=n,j=2;i>tmp;i--,j+=2){
            b[j]=a[i];
        }
        for(int i=1;i<=n;++i) cout<<b[i]<<" ";
        cout<<"\n";
    }
    return 0;
}
  • B
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize(2)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
void io(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}

int t,n;
string s;
string p="2020";


int main()
{
    io();
    cin>>t;
    while(t--){
        cin>>n>>s;
        int len=s.size();
        int rec=n-4;
        int f=0;
        for(int i=0;i<len;++i){
            string stmp=s;
            stmp.erase(i,rec);
            // cout<<stmp<<"\n";
            if(stmp==p){
                f=1;
                // cout<<f<<"\n";
                break;
            }
        }
        if(f) cout<<"YES\n";
        else cout<<"NO\n";
    }
    return 0;
}
  • C
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize(2)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
void io(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}

int t,n;
int num[15];
int ans[15];
int f;

void dfs(int x,int pos,int rec)
{
    if(x==0){
        f=1;
        return;
    }
    if(rec==0) return;
    for(int i=1;i<=9;++i){
        if(num[i]) continue;
        // x-=i;
        ans[pos]=i;
        num[i]=1;
        dfs(x-i,pos+1,rec-1);
        if(f) break;
        ans[pos]=0;
        num[i]=0;
    }
}

int main()
{
    io();
    cin>>t;
    while(t--){
        cin>>n;
        if(n<10){
            cout<<n<<"\n";
            continue;
        }
        if(n>45){
            cout<<-1<<"\n";
            continue;
        }
        memset(num,0,sizeof(num));
        memset(ans,0,sizeof(ans));
        f=0;
        for(int i=2;i<=9;++i){
            dfs(n,0,i);
            if(f) break;
        }
        if(f){
            for(int i=0;ans[i]!=0;++i) cout<<ans[i];
            cout<<"\n";
        }else cout<<"-1\n";
    }
    return 0;
}
  • D
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize(2)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
void io() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); }

const int maxn = 3010;
int t, n;
int a[maxn];

int main()
{
    io();
    cin >> t;
    while (t--) {
        cin >> n;
        int maxx = 0, sum = 0;
        for (int i = 1; i <= n; ++i) {
            cin >> a[i];
            sum += a[i];
            maxx = max(maxx, a[i]);
        }
        int ans = 1e9;
        for (int i = 1; i <= n; ++i) {
            if (sum % i) continue;
            if (sum / i < maxx) continue;
            int tmp = sum / i;
            int j = 1;
            int tans = 0;
            int f = 0;
            for (int k = 0; k < i; ++k) {
                int st = 0;
                int recj = j;
                for (; j <= n; ++j) {
                    st += a[j];
                    if (st == tmp) {
                        tans += (j - recj);
                        j++;
                        break;
                    }
                    if (st >= tmp) {
                        f = 1;
                        break;
                    }
                }
                if (k != i - 1 && j > n) f = 1;
                if (f) break;
            }
            if(!f) ans = min(tans, ans);
        }
        cout << ans << "\n";
    }
    return 0;
}
  • E1 &E2

  • 二分+组合数+逆元

    • E1
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize(2)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
void io()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}

int t, n;
const int maxn = 2e5 + 10;
int a[maxn];

int main()
{
    io();
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 0; i < n; ++i)
            cin >> a[i];
        sort(a, a + n);
        long long ans = 0;
        for (int i = 0; i < n - 2; ++i)
        {
            int tmp = a[i] + 2;
            int pos = upper_bound(a, a + n, tmp) - a;
            pos--;
            if (pos >= i + 2)
            {
                ans += (1ll * (pos - i) * (1ll * (pos - i - 1)) / 2ll) * 1ll;
            }
        }
        cout << ans << "\n";
    }
    return 0;
}
  • E2
#include<bits/stdc++.h>
#define MOD 1000000007
using namespace std;
#pragma GCC optimize(2)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
void io(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}

const int maxn=2e5+10;
int t,n,m,k;
int a[maxn];

ll ksm(ll x,ll y){ // 快速幂
    ll ret=1;
    x%=MOD;
    while(y){
        if(y&1)ret=ret*x%MOD;
        x=(x%MOD)*(x%MOD);
        x%=MOD;
        y>>=1;
    }
    return ret;
}

ll jc(ll x){ // 算阶乘
    ll rec=1;
    while(x){
        rec*=x;
        rec%=MOD;
        x-=1;
    }
    return rec;
}

int main()
{
    io();
    cin>>t;
    while(t--){
        cin>>n>>m>>k;
        for(int i=0;i<n;++i) cin>>a[i];
        sort(a,a+n);
        long long ans=0;
        for(int i=0;i<n-m+1;++i){
            int tmp=a[i]+k;
            int pos=upper_bound(a,a+n,tmp)-a; // 二分找范围
            pos--;
            ll w=jc(m-1);
            ll inv=ksm(w,MOD-2); // 费马小定理
            if(pos>=i+m-1){ // if里面都是计算组合数
                ll rr=1;
                for(int j=0;j<m-1;++j){
                    rr=(rr%MOD*(pos-i-j)%MOD+MOD)%MOD;
                }
                rr=(rr%MOD*inv%MOD+MOD)%MOD;
                ans=(ans%MOD+rr%MOD)%MOD;
            }
        }
        cout<<ans<<"\n";
    }
    return 0;
}
  • F
  • 二分+树状数组(差分建树),树状数组很久没写过了,顺带复习一下(反正都是贴板子来着,主要是复习下差分建树(本来想手撸线段树,但是太懒了,线段树太长了
#include<bits/stdc++.h>
    using namespace std;
    #define mk(a, b) make_pair(a, b)
    typedef pair<int, int> pii;
    void io()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
    }
 
    const int maxn = 2e5 + 10;
    int t, n;
    pii pa[maxn];
    int a[maxn];
    int c[maxn];
 
    int lowbit(int x) { return x & (-x); }
 
    void updata(int pos, int k)
    {
        while (pos <= n) {
            c[pos] += k;
            pos += lowbit(pos);
        }
    }
 
    int getsum(int pos)
    {
        int res = 0;
        while (pos > 0) {
            res += c[pos];
            pos -= lowbit(pos);
        }
        return res;
    }
 
    int main()
    {
        io();
        cin >> t;
        while (t--) {
            cin >> n;
            for (int i = 1; i <= n; ++i) {
                int a, b;
                cin >> a >> b;
                pa[i] = mk(a, b);
            }
            sort(pa + 1, pa + n + 1);
            for (int i = 1; i <= n; ++i) a[i] = pa[i].first; // 懒人操作,懒得写cmp,所以直接把first单独存一下方便后面用upperbound,手写二分我容易出锅
            memset(c, 0, (n + 5)*sizeof(int) );
            int ans = 0;
            for (int i = 1; i <= n; ++i) {
                pii now = pa[i];
                int y = now.second;
                int pos = upper_bound(a, a + n + 1, y) - a - 1;
                int tmp = getsum(i);
                tmp += pos - i;
                tmp++;
                ans = max(ans, tmp);
                updata(i, 1);
                updata(pos + 1, -1);
            }
            cout << n - ans << "\n";
        }
        return 0;
    }
  • 最后纪念一下CF第一次看见比赛变绿
  • image-20201219002858675
posted @ 2020-12-19 00:30  0xDkXy_DWM  阅读(74)  评论(0编辑  收藏  举报