Codeforces Round #648 (Div. 2) A~F题解

开始补cf了,还是记录一下,加深思路,打的应该都是div2。题面不截图了,直接说题意,思路,代码。

A

题意:给一个01矩阵,两个人轮流填格子,仅当第i行,第j列全为0时才能填,不能填的人输,问谁赢?Ashish先手,Vivek后手。

思路:记录全为零的行列即可,看能填的位置有几个,奇数个先手赢。

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define lowbit(x) ((-x)&x)
#define met(a, b) memset(a, b, sizeof(a))
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define bep(i, a, b) for(int i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define debug cout << "KKK" << endl
#define ls num*2
#define rs num*2+1
#define re return
using namespace std;
const ll mod = 258280327;
const double PI = acos(-1);
const ll INF = 2e18+1;
const int inf = 1e9+5;
const double eps = 1e-10;
const int maxn = 1e6 + 5;
int a[55], b[55];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int T; cin >> T;
    while(T--){
        met(a, 0); met(b, 0);
        int n, m; cin >> n >> m;
        rep(i, 1, n){
            rep(j, 1, m){
                int x; cin >> x;
                if(x) a[i] = b[j] = 1;
            }
        }
        int ans = 0;
        rep(i, 1, n){
            rep(j, 1, m){
                if(a[i] + b[j] == 0){
                    ans++;
                    a[i] = b[j] = 1;
                    break;
                }
            }
        }
        if(ans % 2 == 1) cout << "Ashish" << endl;
        else cout << "Vivek" << endl;
    }
    return 0;
}
View Code

B

题意:给你长度为n的数组a和b(b数组是01数组),问你能否交换一些数的位置使得a数组最后从小到大排列。当b[i]!=b[j]时可以交换a[i]和a[j]。

思路:发现只要有一堆1里面有个0或者一堆0里面有一个1就能随便交换。如果全是0或者全是1,但是已经是非降序的了也可以。

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define lowbit(x) ((-x)&x)
#define met(a, b) memset(a, b, sizeof(a))
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define bep(i, a, b) for(int i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define debug cout << "KKK" << endl
#define ls num*2
#define rs num*2+1
#define re return
using namespace std;
const ll mod = 258280327;
const double PI = acos(-1);
const ll INF = 2e18+1;
const int inf = 1e9+5;
const double eps = 1e-10;
const int maxn = 1e6 + 5;
int a[550];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int T; cin >> T;
    while(T--){
        int n; cin >> n;
        rep(i, 1, n) cin >> a[i];
        int num1 = 0, num0 = 0;
        rep(i, 1, n){
            int x; cin >> x;
            if(x) num1 = 1;
            else num0 = 1;
        }
        int flag = 1;
        rep(i, 2, n){
            if(a[i] < a[i-1]) {
                flag = 0;
                break;
            }
        }
        if(flag || (!flag && num0 && num1)) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    return 0;
}
View Code

C

题意:给你一个1~n的一个排列数组a,一个1~n的一个排列数组b,可以整体随意左右移动(相当于循环的),问最多有几个对应位置相等。1 2 3 和3 2 1,就第二个位置相等,都是2,移动后也是只能有一个。

思路:一看n是2e5,还是C题,肯定是线性复杂度了,然后自己写几个例子,1 3 4 2和2 1 4 3。

最佳匹配意识到一个数字在ab数组中的位置差就是当前要移动的步数,(b数组往右移动1个和往左移动3效果一样,因为是循环的),我们同一让b数组往右移动,用一个数组记录所有每对数字匹配上时b数组需要往右移动多少。这样相当于遍历了所有情况。

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define lowbit(x) ((-x)&x)
#define met(a, b) memset(a, b, sizeof(a))
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define bep(i, a, b) for(int i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define debug cout << "KKK" << endl
#define ls num*2
#define rs num*2+1
#define re return
using namespace std;
const ll mod = 258280327;
const double PI = acos(-1);
const ll INF = 2e18+1;
const int inf = 1e9+5;
const double eps = 1e-10;
const int maxn = 2e5 + 5;
int a[maxn], b[maxn], cnt[maxn];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n; cin >> n;
    rep(i, 1, n){
        int x; cin >> x;
        a[x] = i;
    }
    rep(i, 1, n){
        int x; cin >> x;
        b[x] = i;
    }
    int ans = 1;
    rep(i, 1, n){
        ans = max(ans, ++cnt[(n+a[i]-b[i])%n]);
    }
    cout << ans << endl;
    return 0;
}
View Code

D

题意:给一个n*m图,B是坏人,G是好人, ‘.’表示空,‘#’表示墙,右下角是出口,每个人只能在图中上下左右移动,问能否使一些'.’变成'#'使得所有好人都能出去,所有坏人都出不去。

思路:首先想到如果好人坏人挨着,那要么都能出去,都不能出去,肯定不行。再想其实就是好人和坏人不能互相访问到,怎么样才行?把坏人用墙围一圈不就行了么?在这个基础上再判断一下好人是否都能出去。

为什么可以?思考一下如果存在一中方法是所有坏人都不能访问到好人,并且好人都能出去,那么在这个基础上再把坏人围一圈仍然成立,如果因为围的墙把好人出去的路挡住了,说明没有这个墙坏人就能访问到好人了,因此成立。

围好之后从n,m点出发bfs看是不是和所有好人都连通了就行。

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define lowbit(x) ((-x)&x)
#define met(a, b) memset(a, b, sizeof(a))
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define bep(i, a, b) for(int i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define debug cout << "KKK" << endl
#define ls num*2
#define rs num*2+1
#define re return
using namespace std;
const ll mod = 258280327;
const double PI = acos(-1);
const ll INF = 2e18+1;
const int inf = 1e9+5;
const double eps = 1e-10;
const int maxn = 1e6 + 5;
char ma[55][55];
bool vis[55][55];
int flag;
struct node{
    int x, y;
};
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int bfs(int x, int y){
    if(ma[x][y] == '#') return 0;
    met(vis,0); int num = 0;
    vis[x][y] = 1;
    queue<node> q;
    q.push((node){x, y});
    while(!q.empty()){
        node now = q.front();
        q.pop();
        rep(i, 0, 3){
            int nx = now.x + dx[i];
            int ny = now.y + dy[i];
            if(vis[nx][ny]) continue;
            if(ma[nx][ny] == 'G' || ma[nx][ny] == '.'){
                if(ma[nx][ny] == 'G') num++;
                vis[nx][ny] = 1;
                q.push((node){nx, ny});
            }
        }
    }
    return num;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int T; cin >> T;
    while(T--){
        met(ma, 0);
        int n, m; cin >> n >> m;
        rep(i, 1, n) cin >> (ma[i]+1);
        flag = 1;
        int ans = 0;
        rep(i, 1, n){
            rep(j, 1, m){
                if(ma[i][j] == 'B'){
                    if(ma[i-1][j] == 'G' || ma[i+1][j] == 'G' || ma[i][j+1] == 'G' || ma[i][j-1] == 'G'){
                        flag = 0;
                    }
                    if(ma[i-1][j] == '.') ma[i-1][j] = '#';
                    if(ma[i+1][j] == '.') ma[i+1][j] = '#';
                    if(ma[i][j+1] == '.') ma[i][j+1] = '#';
                    if(ma[i][j-1] == '.') ma[i][j-1] = '#';
                }
                if(ma[i][j] == 'G') ans++;
            }
        }
        if(!flag){
            cout << "No" << endl;
            continue;
        }
        if(bfs(n, m) == ans) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    return 0;
}
View Code

E

题意:给你n个数,任意选几个数组成一个新数组,该元素为k的数组值是:如果第x个二进制有 >=max(1, k-2)个元素该二进制位是1,那个数组的值加上2^x。

思路:说的是用到鹊巢原理,不知道也不影响。一看这个max(1, k-2),发现如果选三个数,那数组的值不就是 (a | b | c)吗?再考虑大于3个的,担心如果abc在某个二进制位下都为0,有没有可能多加入该位都是1的个元素使数组的值变大?不可能,因为至少要有n-2个该位上都为1,abc都3个了,故不可能。如果存在一种方案最优解元素大于3个,其实也能由3个元素表示。因为某个二进制位要贡献答案,最多有2个不为1,所以我们选的3个元素一定可以。这也就是鹊巢原理。

枚举一下abc取个max(a|b|c)就行了。

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define lowbit(x) ((-x)&x)
#define met(a, b) memset(a, b, sizeof(a))
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define bep(i, a, b) for(int i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define debug cout << "KKK" << endl
#define ls num*2
#define rs num*2+1
#define re return
using namespace std;
const ll mod = 258280327;
const double PI = acos(-1);
const ll INF = 2e18+1;
const int inf = 1e9+5;
const double eps = 1e-10;
const int maxn = 2e5 + 5;
ll a[maxn];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    ll ans = 0, n; cin >> n;
    rep(i, 1, n) cin >> a[i];
    rep(i, 1, n){
        rep(j, i+1, n){
            rep(k, j+1, n){
                ans = max(ans, a[i] | a[j] | a[k]);
            }
        }
    }
    cout << ans << endl;
    return 0;
}
View Code

F

题意:给你一个长度为n的a数组,每次可以选择一个k<=n/2,把前k个和后k个交换,问能否变成b数组。比如 1 2 3 4,k = 2,让前两个和后两个换一下:3 4 1 2。

思路:还是画一画,因为这个题是对称交换,很难不去找对称的有什么性质,发现a[i]和a[n-i+1]这对就像cp,是成对的。并且可以以任意前后顺序出现在数组中。就是比如a[1]=1, a[4]=1,也可以交换成a[1]=4,a[4]=1。这个14就是对cp,也能变到中间,a[2]=1,a[3]=4或者a[2]=4,a[3]=1。剩下的就好办了,记录两个数组所有的cp随便找个顺序排一下,比较一下是否cp相等。

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define lowbit(x) ((-x)&x)
#define met(a, b) memset(a, b, sizeof(a))
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define bep(i, a, b) for(int i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define debug cout << "KKK" << endl
#define ls num*2
#define rs num*2+1
#define re return
using namespace std;
const ll mod = 258280327;
const double PI = acos(-1);
const ll INF = 2e18+1;
const int inf = 1e9+5;
const double eps = 1e-10;
const int maxn = 500 + 5;
inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline int rd(){
    int x = 0; char ch = gc(); bool positive = 1;
    for (; !isdigit(ch); ch = gc())    if (ch == '-')    positive = 0;
    for (; isdigit(ch); ch = gc())    x = x * 10 + ch - '0';
    return positive ? x : -x;
}

struct node{
    int x, y;
    bool operator <(const node & a)const{
        if(x != a.x) return x < a.x;
        return y < a.y;
    }
}p1[maxn], p2[maxn];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int T = rd();
    while(T--){
        int n = rd();
        vector<int> v1, v2;
        v1.pb(-1), v2.pb(-1);
        rep(i, 1, n) v1.pb(rd());
        rep(i, 1, n) v2.pb(rd());
        rep(i, 1, n/2){
            if(v1[i] > v1[n-i+1]) swap(v1[i], v1[n-i+1]);
            p1[i] = (node){v1[i], v1[n-i+1]};
            if(v2[i] > v2[n-i+1]) swap(v2[i], v2[n-i+1]);
            p2[i] = (node){v2[i], v2[n-i+1]};
        }
        sort(p1+1, p1+1+n/2);
        sort(p2+1, p2+1+n/2);
        int flag = 1;
        rep(i, 1, n/2){
            if(p1[i].x != p2[i].x || p1[i].y != p2[i].y) flag = 0;
        }
        if(n % 2 == 1 && v1[n/2+1] != v2[n/2+1]) flag = 0; 
        if(flag) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/philo-zhou/p/13431053.html