Codeforces Global Round 13 ABC题解

A. K-th Largest Value

思路:操作就是0变1,1变0。那么只用统计1有多少个就知道第x大是谁了。

view code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll a[maxn];

int main()
{
    ll n = read(), q = read();
    ll zeros = 0, ones = 0;
    rep(i,1,n) a[i] = read();
    rep(i,1,n)
    {
        if(a[i]==1) ones++;
        else zeros++;
    }
    rep(i,1,q)
    {
        ll t = read();
        if(t==1)
        {
            ll x = read();
            if(a[x]==1) ones--, zeros++, a[x] = 0;
            else ones++, zeros--, a[x] = 1;
        }
        else
        {
            ll k = read();
            if(ones>=k) cout<<1<<endl;
            else cout<<0<<endl;
        }
    }
    return 0;
}


B. Minimal Cost

思路:手动模拟几个数据就能找到规律了。要把路封死不得不移动障碍物就两种情况:
1.一列纵向排列,这个时候要把其中一个右移或左移后,再横向或纵向移动一次,选\(min(u+v,v*2)\)
2.分散排列,但每两个之间的间距都不超过1,(如一条斜线),这种就要将其中一个纵向或横向移动, 选\(min(u,v)\)

view code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll a[maxn];

int main()
{
    int kase;
    cin>>kase;
    while(kase--)
    {
        ll n = read(), u = read(), v = read();
        rep(i,1,n) a[i] = read();
        ll ma = 0;
        rep(i,2,n) ma = max(abs(a[i] - a[i-1]) ,ma);
        ll ans = 0;
        if(ma==1) ans = min(u,v);
        else if(ma==0)
        {
            ans = min(u+v,v*2);
        }
        cout<<ans<<endl;
    }
    return 0;
}


C. Pekora and Trampoline

思路:其实贪心的核心很简单:在不得不以第i个蹦床当起跳点的时候,我想它已经尽可能地多次从前面跳过来过了。
那么就从前往后遍历,每次先看看当前已经被跳过来多少次,如果还需要跳(>1),那说明[i+2,i+left]的位置都是以它为起点需要跳的(注意若前面跳过来的次数比a[i]还大的话多出来的还会跳到i+1)。这个时候将前面过来的次数和自己当起点的次数继续传递下去即可。
注意一下细节,这题可能很多人迷之wa2和TL。

view code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(long long i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll a[maxn];
ll cnt[maxn];

int main()
{
    int kase;
    scanf("%d",&kase);
    while(kase--)
    {
        ll n = read();
        rep(i,0,n+1) cnt[i] = 0;
        rep(i,1,n) a[i] = read();
        ll ans = 0;
        rep(i,1,n)
        {
            ll Left = max(1LL, a[i]-cnt[i]);
            ll final = Left + i;
            rep(j,1,cnt[i])
            {
                ll to = max(i + a[i] - cnt[i] + j , i+1);
                if(to>n) break;
                if(to==i+1)
                {
                    ll num = min(cnt[i] - a[i] + 1 - j + 1, cnt[i] - j + 1);
                    cnt[to] += num;
                    j = cnt[i] - a[i] + 1;
                    continue;
                }
                cnt[to] ++;
            }
            ll need = Left - 1;
            if(need)
            {
                rep(j,1,need)
                {
                    if(j+i+1>n) break;
                    cnt[j+i+1]++;
                }
                ans += need;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


posted @ 2021-03-01 00:34  TL自动机  阅读(85)  评论(0编辑  收藏  举报
//鼠标点击特效第二种(小烟花)