1. 程式人生 > 實用技巧 >Codeforces Round #619 (Div. 2) ABC 題解

Codeforces Round #619 (Div. 2) ABC 題解

A. Three Strings

題意:每次可以把c[i]拿去和a[i]或b[i]交換。 問你能否把ab變成相等。

思路:在ab不相等的時候看看c能不能與一方相等來中和。不能的話就不行。

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 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} };

int main()
{
    int kase;
    cin>>kase;
    while(kase--)
    {
        string a, b , c;
        cin>>a>>b>>c;
        int flag = 1;
        for(int i=0; i<a.size(); i++)
        {
            if(c[i]==a[i]||c[i]==b[i]) continue;
            flag = 0; break;
        }
        puts(flag?"YES":"NO");
    }
    return 0;
}


B. Motarack's Birthday

題意:給一個序列,除了若干-1外全是正數,問把所有-1替換成k後,這個序列的max(abs(a[i+1] - a[i]))的最小值為多少。

思路:其實-1改變後能產生影響的也就是它相鄰的非-1的數。我們只需要統計一下和-1相鄰的值的最大值和最小值找出來。k為(最大值+最小值)/2是最優的。

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 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 = 4e5+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();
        rep(i,1,n) a[i] = read();
        ll ma = -1, mi = inf;
        a[0] = a[n+1] = -1;
        int flag = 0;
        rep(i,1,n)
        {
            if(a[i]!=-1) flag=1;
            if(a[i]==-1)
            {
                if(a[i-1]!=-1)
                ma = max(ma, a[i-1]), mi = min(mi, a[i-1]);
                if(a[i+1]!=-1)
                ma = max(ma, a[i+1]), mi = min(mi, a[i+1]);
            }
        }
        if(!flag) cout<<0<<' '<<0<<'\n';
        else
        {
            ll m = max(ma-(ma+mi)/2, (ma+mi)/2 - mi);
            rep(i,1,n) if(a[i]==-1) a[i] = (ma+mi)/2;
            rep(i,1,n-1)
            {
                m = max(m, abs(a[i+1] - a[i]));
            }
            cout<<m<<' '<<(ma+mi)/2<<'\n';
        }
    }
    return 0;
}


#### C. Ayoub's function

題意:定義f(s)為s串在長度1<=l,r<=|s|的滿足有一個1的區間個數總和。

思路:容斥定理。 想到要直接通過1統計f(x)會很麻煩。我們可以考慮從0的角度入手。
f(x)區間裡面有1就行,那就相當於是全排列 - 全為0的方案數。
考慮有一塊長度為l的0串,f(x) = n(n+1)/2 - l(l+1)/2 。我們想要後者越小越好,所以想到把0的塊分散開來。
而我們有m個1,可以分開m+1個0,每塊有(n-m)/(m+1)個0 。 然後用餘下來的0儘量均攤,多出來的貢獻是((n-m)/(m+1)+1)*(n-m)/(m+1)。最後用總數減去即可。

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 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} };

int main()
{
    int kase;
    cin>>kase;
    while(kase--)
    {
        ll n = read(), m = read();
        ll zeros = n - m;
        ll len = zeros/(m+1);
        ll Left = zeros%(m+1);
        ll ans_zeros = ( (len+1)*len/2 ) * (m+1) + Left*(len+1);
        ll ans_all = (n+1)*n/2;
        cout<<ans_all - ans_zeros<<'\n';
    }
    return 0;
}