Codeforces Round #665 (Div. 2) ABC题解

A. Distance and Axis

题意:给出OX坐标上OA的长度,每次可以将A移动一个单位,问最少多少次可以找出坐标B的整数解使得 | OB - AB | = k。

思路:分类讨论。
1.若B在A的左边,有两种情况,一个是在O的左边,那么k = OA,只需要看两者是否相等,相等时不需要移动,不相等时A移动到k长度的位置,其实就是和k重合。另一种情况是在OA之间,此时k一定小于等于OA,且OB = (k + OA)/2 或 (OA - k)/2 ,显然k和OA奇偶性一致时答案为0,不一样时挪动一位。
2.B在A的右边,这个时候k也等于OA,讨论同上。

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

ll n , k;

void solve()
{
    ll ans = inf;
    if(k == n) ans = 0;
    else if(k<=n) ans = ((k&1)!=(n&1));
    ans = min(ans, abs(n - k));
    cout<<ans<<'
';
}

int main()
{
    int kase;
    cin>>kase;
    while(kase--)
    {
        cin>>n>>k;
        solve();
    }
    return 0;
}

B. Ternary Sequence

题意:A、B数组都有若干的2,1,0 ,可随意将a[i]和b[j]匹配,a[i] > b[j]时贡献为两者乘积,等于时为0,小于时为负的乘积。问怎么匹配可以使得最后的和最大。

贪心一下,感觉我的方法比较麻烦。
因为我们有正贡献的其实就只有A(2) * B(1)的时候,我们就想让这个搭配尽量的多,同时让A(1) * B(2)尽可能少。
1.A中0先把B的2尽量抵消掉。然后有剩的0再把B的0抵消掉。
2.A中的2把B的1全部吃掉,正贡献。
3.A中的1吃B的1,又剩下的把B残留的0抵消掉。
4.为了让对面的2少一点,若A还有剩下来的2就拿去抵消,还有剩的去抵消B的0 。
5.然后就是A的给B吃了,0->0, 0->1, 1->2的顺序,达到最后A(1) * B(2)最少。

#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 a0 = read(), a1 = read(), a2 = read(), b0 = read(), b1 = read(), b2 = read();
        ll ans = 0;
        ll d0 = min(a0,b2);
        a0 -= d0, b2 -= d0;
        ll d1 = min(a2, b1);        // 2 - > 1
        b1 -= d1, a2 -= d1, ans += d1 * 2;
        ll d2 = min(a1, b1);// 1-> 1
        a1 -= d2, b1 -= d2;
        ll d3 = min(a1, b0); // 1->0
        a1 -= d3, b0 -= d3;
        ll d4 = min(a2,b2); // 2- >2;
        a2 -= d4, b2 -= d4;
        ll d5 = min(a2, b0);    // 2->0
        a2 -= d5, b0 -= d5;
        ll d6 = min(a0, b2); // 0 - > 2
        a0 -= d6, b2 -= d6;
        ll d7 = min(a0, b1); // 0 -> 1
        a0 -= d7, b1 -= d7;
        ll d8 = min(a1,b2); // 1->2
        a1 -= d8, b2 -= d8, ans -= d8*2;
        cout<<ans<<'
';
    }
    return 0;
}

C. Mere Array

题意:若数组中两个元素的gcd等于数组中的最小值,那么就可以将两者调换。问这个数组能否达到单调非减的序列。

思路:其实gcd(a,b)等于最小值,说明a b都是最小值mi的倍数。而且通过观察发现,所有mi的倍数之间,我们都可以通过和mi进行交换,达到这些数任意调换的效果。
如2 12 6, 12和6虽然不能直接换,但是可以先拿2和6换-> 6 12 2。
再拿2和12换->6 2 12.
最后6 2再换回来-> 2 6 12 。
所以我们拿到一个a数组,用b数组表示a排序完后的结果。若a[i]和b[i]不相等,且都不为mi的倍数的话,说明这个位置的调换是不可能实现的。其他情况均可以。

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <unordered_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} };

ll a[maxn];
ll b[maxn];

int main()
{
    int kase;
    cin>>kase;
    while(kase--)
    {
        ll n = read();
        rep(i,1,n) a[i] = read(), b[i] = a[i];
        sort(b+1,b+1+n);
        ll mi = b[1];
        int flag = 1;
        rep(i,1,n)
        {
            if(a[i]!=b[i]&&(b[i]%mi||a[i]%mi))
            {
                flag = 0;
                break;
            }
        }
        if(flag) cout<<"YES"<<'
';
        else cout<<"NO"<<'
';
    }
    return 0;
}

``` </font>
原文地址:https://www.cnblogs.com/Bgwithcode/p/13545033.html