Codeforces Round #624 (Div. 3)

A Add Odd or Subtract Even

分类讨论

import java.util.Scanner;
public class a
{
    public static void main(String[] args)
    {
        int T, a, b, i, ans;
        Scanner input = new Scanner(System.in);
        T = input.nextInt();
        for(i=1;i<=T;i++)
        {
            a=input.nextInt();
            b=input.nextInt();
            if(a==b)ans=0;
            else if(a>b)
            {
                if(a%2==b%2)ans=1;
                else ans=2;
            }
            else
            {
                if(a%2==b%2)ans=2;
                else ans=1;
            }
            System.out.println(ans);
        }
    }
}

B WeirdSort

每个联通块内部排序

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
ll p[maxn], a[maxn], tmp[maxn];
int main()
{
    ll n, i, m, T=read(), last;
    while(T--)
    {
        n=read(), m=read();
        rep(i,1,n)a[i]=read();
        rep(i,1,n)tmp[i]=a[i];
        cl(p);rep(i,1,m)p[read()]++;
        last=-1;
        rep(i,1,n)
        {
            if(p[i])if(last==-1)last=i;
            if(!p[i])
            {
                if(last==-1)continue;
                sort(tmp+last,tmp+i+1);
                last=-1;
            }
        }
        if(last!=-1)sort(tmp+last,tmp+n+1);
        sort(a+1,a+n+1);
        if(vector<ll>(a+1,a+n+1) == vector<ll>(tmp+1,tmp+n+1))printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

C Perform the Combo

后缀和

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
ll ans[300];
char s[maxn];
ll p[maxn], cnt[maxn];
int main()
{
    ll T=read(), n, m, i;
    while(T--)
    {
        n=read(), m=read();
        rep(i,1,n+1)cnt[i]=0;
        scanf("%s",s+1);
        rep(i,1,m)cnt[read()]++;
        drep(i,n,1)cnt[i]+=cnt[i+1];
        cl(ans);
        rep(i,1,n)ans[s[i]]+=cnt[i]+1;
        rep(i,'a','z')printf("%lld ",ans[i]);
        putchar(10);
    }
    return 0;
}

D Three Integers

枚举 b b b,然后枚举 b b b的约数,枚举 b b b的倍数

b , c b,c b,c都枚举道最多 20000 20000 20000即可,因为一种很极端的情况就是 a = 10000 , b = c = 20000 a=10000,b=c=20000 a=10000,b=c=20000,比这个更大的情况肯定都更劣

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
ll T, a, b, c;
int main()
{
    ll i, ans, j, db, dc, tmp, k;
    T=read();
    while(T--)
    {
        a=read(), b=read(), c=read();
        vector<ll> ans{linf,0,0,0};
        for(i=1;i<=20000;i++)
        {
            ll mid = abs(i-b), besta=linf, bestc=linf;
            for(j=1;j*j<=i;j++)
            {
                if(i%j==0)
                {
                    if(abs(besta-a)>abs(j-a))besta=j;
                    if(abs(besta-a)>abs(i/j-a))besta=i/j;
                }
            }
            for(j=i;j<=20000;j+=i)
                if(abs(bestc-c)>abs(j-c))bestc=j;
            ans = min( ans, { abs(i-b) + abs(a-besta) + abs(c-bestc), besta, i, bestc } );
        }
        printf("%lld\n%lld %lld %lld\n",ans[0],ans[1],ans[2],ans[3]);
    }
    return 0;
}

E Construct the Binary Tree

从深度序列的角度来考虑,我一上来先搞一个完全二叉树的深度序列。

然后每次取最后面的一个可以 + 1 +1 +1的数让他 + 1 +1 +1(得保证 + 1 +1 +1完了以后这棵树还是合法的)

最后根据度数序列构造出整棵树

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 5050
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
ll n, d, lis[maxn], cnt[maxn], now;
vector<ll> v[maxn];
int main()
{
    ll T=read(), i, j;
    while(T--)
    {
        n=read(), d=read();
        if(d>n*(n-1)/2)
        {
            printf("NO\n");
            continue;
        }
        cl(cnt), cl(lis);
        lis[1]=0, lis[2]=1;
        cnt[0]=1, cnt[1]=1;
        rep(i,3,n)
        {
            if(cnt[lis[i-1]]==cnt[lis[i-1]-1]*2)lis[i]=lis[i-1]+1;
            else lis[i]=lis[i-1];
            cnt[lis[i]]++;
        }
        now=0;
        rep(i,1,n)now+=lis[i];
        if(now>d){printf("NO\n");continue;}
        while(now<d)
        {
            drep(i,n,1)
            {
                if(cnt[lis[i]]>1)
                {
                    cnt[lis[i]]--;
                    lis[i]++;
                    cnt[lis[i]]++;
                    break;
                }
            }
            now++;
        }
        rep(i,0,n)v[i].clear();
        printf("YES\n");
        v[0].emb(1);
        rep(i,2,n)
        {
            v[lis[i]].emb(i);
            ll num=v[lis[i]].size()-1;
            printf("%lld ",v[lis[i]-1][num>>1]);
        }
        putchar(10);
    }
    return 0;
}

F Moving Points

先按照 x x x排序,然后求如下式子

∑ i = 1 n ∑ j = i + 1 n ( x j − x i ) [ v j > v i ] = ∑ i = 1 n ∑ j = i + 1 n x j [ v j > v i ] − ∑ i = 1 m x i ∑ j = i + 1 n [ v j > v i ] \sum_{i=1}^n \sum_{j=i+1}^n (x_j-x_i) [v_j>v_i] \\ = \sum_{i=1}^n \sum_{j=i+1}^n x_j [v_j>v_i] - \sum_{i=1}^m x_i \sum_{j=i+1}^n [v_j > v_i] i=1nj=i+1n(xjxi)[vj>vi]=i=1nj=i+1nxj[vj>vi]i=1mxij=i+1n[vj>vi]

两个树状数组就可以搞定了

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
ll n;
pll pt[maxn];
struct BIT
{
    ll bit[maxn], n;
    void init(int N){n=N;for(int i=1;i<=n;i++)bit[i]=0;}
    ll lowbit(ll x){return x&(-x);}
    void add(ll pos, ll v)
    {
        for(;pos<=n;pos+=lowbit(pos))bit[pos]+=v;
    }
    ll sum(ll pos)
    {
        ll ans(0);
        for(;pos;pos-=lowbit(pos))ans+=bit[pos];
        return ans;
    }
}bit_sum, bit_cnt;
struct Lisan
{
    int tmp[maxn], tot;
    void clear(){tot=0;}
    void insert(int x){tmp[++tot]=x;}
    void run()
    {
        sort(tmp+1,tmp+tot+1);
        tot=unique(tmp+1,tmp+tot+1)-tmp-1;
    }
    void lisan(int *a, int len)
    {
        for(int i=1;i<=len;i++)a[i]=lower_bound(tmp+1,tmp+tot+1,a[i])-tmp;
    }
    int lisan(int x)
    {
        return lower_bound(tmp+1,tmp+tot+1,x)-tmp;
    }
}ls;
int main()
{
    ll n=read(), i, ans=0;
    rep(i,1,n)pt[i].first=read();
    rep(i,1,n)pt[i].second=read();
    rep(i,1,n)
    {
        ls.insert(pt[i].second);
        ls.insert(pt[i].second-1);
    }
    ls.run();
    bit_sum.init(ls.tot);
    bit_cnt.init(ls.tot);
    rep(i,1,n)pt[i].second=ls.lisan(pt[i].second);
    sort(pt+1,pt+n+1);
    drep(i,n,1)
    {
        ll sum=bit_sum.sum(bit_sum.n)-bit_sum.sum(pt[i].second-1), cnt=bit_cnt.sum(bit_cnt.n)-bit_cnt.sum(pt[i].second-1);
        ans += sum - pt[i].first*cnt;
        bit_sum.add(pt[i].second,pt[i].first);
        bit_cnt.add(pt[i].second,+1);
    }
    printf("%lld",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值