zoukankan      html  css  js  c++  java
  • Codeforces Round #699 (Div. 2) ABC题解

    A. Space Navigation

    思路:分别统计往px和py方向的步数,看看能不能分别大于等于px和py。

    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 '
    '
    #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 x = read(), y = read();
            string s;
            cin>>s;
            ll xx1 = 0, yy1 = 0, xx2 = 0, yy2 = 0;
            for(int i=0; i<s.size(); i++)
            {
                if(s[i]=='L') xx2--;
                else if(s[i]=='R') xx1++;
                else if(s[i]=='U') yy1 ++;
                else yy2++;
            }
            int flag = 0;
            int flag1 = 0;
            if(y>=0&&yy1>=y) flag = 1;
            else if(y<=0&&abs(yy2)>=abs(y)) flag = 1;
            if(x>=0&&xx1>=x) flag1 = 1;
            else if(x<=0&&abs(xx2)>=abs(x)) flag1 = 1;
            puts(flag&&flag1?"YES":"NO");
    
        }
        return 0;
    }
    
    

    B. New Colony

    思路:数据量小,直接模拟就行。最坏的情况全部走完也不需要很多k。

    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 '
    '
    #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(), k = read();
            rep(i,1,n) a[i] = read();
            int pos = 0;
            int flag = 0;
            while(k>0)
            {
                k--;
                rep(i,1,n)
                {
                    if(i==n)
                    {
                        flag = 1;
                        break;
                    }
                    if(a[i]>=a[i+1]) continue;
                    pos = i;
                    a[i]++;
                    break;
                }
                if(flag) break;
            }
            if(k||flag) cout<<-1<<endl;
            else cout<<pos<<endl;
        }
        return 0;
    }
    
    

    C. Fence Painting

    思路:策略如下:
    定义要改的数为(a[i]!=b[i])的。
    1.我遍历到当前的c[i],如果这个c[i]就是我要改的数,那优先改。
    2.否则,如果当前这个数不是我要改的数,但是它是某个a[i]==b[i]产生的“不用改”,那就可以在对应位置修改。(这个是在不满足条件1的情况下才采取的下策)。
    3.若还不满足上述条件,那就说明当前还找不到具体的位置放,那就先存起来,一旦后面遍历到的c[i]满足上述((1) (2))之一,就可以让这些存起来的数放到他们的位置,反正会被覆盖掉。
    4.详见代码注释吧。

    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 '
    '
    #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];
    ll c[maxn];
    ll Ans[maxn];
    
    int main()
    {
        int kase;
        cin>>kase;
        while(kase--)
        {
            ll n = read(), m = read();
            map<ll,stack<ll> > Map; // Map存放需要改变的位置
            map<ll,ll> Pos; //Pos记录一下每个b[i]对应的i,多个b[i]取最后那个
            rep(i,1,n) a[i] = read();
            rep(i,1,n)
            {
                b[i] = read();
                if(a[i]!=b[i])     //只存放需要改变的位置
                Map[b[i]].push(i);
                Pos[b[i]] = i;  //记录b[i]位置
            }
            rep(i,1,m) c[i] = read();
            stack<ll > pre;     //pre记录一下前面遇到没地方放的数
            int flag = 1;
            rep(i,1,m)
            {
                if(Map[c[i]].size())    //如果当前c[i]有对应需要改变的 ,就优先改
                {
                    ll cur = Map[c[i]].top();
                    Map[c[i]].pop();
                    Ans[i] = cur;
                    a[cur] = b[cur];
                    while(pre.size())       // 同时把前面没地方放的那些数放到当前这个位置,意思就是前面随便改我这个位置,利用当前c[i]我又改回来
                    {
                        ll id = pre.top();
                        pre.pop();
                        Ans[id] = cur;
                    }
                }
                else        //若没有一定要改的位置
                {
                    if(Pos[c[i]]&&pre.size())   //先看看这个数存不存在(Pos[c[i]]),若同时前面有数放,则同上
                    {
                        while(pre.size())
                        {
                             ll id = pre.top();
                            pre.pop();
                            Ans[id] = Pos[c[i]];
                            Ans[i] = Pos[c[i]];
                            a[Pos[c[i]]] = b[Pos[c[i]]];
                        }
                    }
                    else if(Pos[c[i]]) Ans[i] = Pos[c[i]];      //前面没有数的但可以放,就放当前
                    else pre.push(i);   //其他就没办法了,只能放进pre,让后面的c[j]来完成
                }
            }
            rep(i,1,n) if(a[i]!=b[i]) flag = 0;
            if(pre.size()) flag = 0;
            if(flag)
            {
                cout<<"YES"<<endl;
                rep(i,1,m) cout<<Ans[i]<<' '; cout<<endl;
            }
            else cout<<"NO"<<endl;
        }
        return 0;
    }
    
    

  • 相关阅读:
    微信二维码 场景二维码 用于推送事件,关注等 注册用户 ,经过测试
    简单的 Helper 封装 -- CookieHelper
    简单的 Helper 封装 -- SecurityHelper 安全助手:封装加密算法(MD5、SHA、HMAC、DES、RSA)
    Java反射机制
    Windows Azure Web Site (13) Azure Web Site备份
    Windows Azure Virtual Machine (1) IaaS用户手册
    Windows Azure Web Site (1) 用户手册
    Windows Azure Web Site (12) Azure Web Site配置文件
    Windows Azure Web Site (11) 使用源代码管理器管理Azure Web Site
    Windows Azure Web Site (10) Web Site测试环境
  • 原文地址:https://www.cnblogs.com/Bgwithcode/p/14380391.html
Copyright © 2011-2022 走看看