Codeforces Round #639 (Div. 2)

A. Puzzle Pieces

题目链接:https://codeforces.ml/contest/1345/problem/A

#include <bits/stdc++.h>
#define rush() int T;cin>>T;while(T--)
#define go(a) while(cin>>a)
#define ms(a,b) memset(a,b,sizeof a)
#define E 1e-8
using namespace std;
typedef __int64 ll;
const int idata=1e3+5;
 
int n,m,t,_;
int i,j,k;
int cnt,num,ans;
int minn,maxx;
stack<int>stk;
queue<int>q;
map<int,int>mp;
 
int main()
{
    rush()
    {
        cin>>n>>m;
        if(n==1 || m==1) cout<<"yes"<<endl;
        else if(n==2 && m==2)cout<<"yes"<<endl;
        else cout<<"no"<<endl;
    }
    return 0;
}

B. Card Constructions

题目链接:https://codeforces.ml/contest/1345/problem/B 

#include <bits/stdc++.h>
#define rush() int T;cin>>T;while(T--)
#define go(a) while(cin>>a)
#define ms(a,b) memset(a,b,sizeof a)
#define E 1e-8
using namespace std;
typedef __int64 ll;
const int idata=1e7+5;
 
ll n,m,t,_;
int i,j,k;
int cnt,num,ans;
int minn,maxx;
stack<int>stk;
queue<int>q;
map<ll,int>mp;
ll a[idata];
 
int main()
{
    a[1]=2;
    num=1;
    for(i=2;i<=idata;i++){
        a[i]=a[i-1]+2+num*3;
        num++;
    }
    //for(i=1;i<=4;i++)cout<<a[i]<<endl;
    rush()
    {
        cin>>n;
        if(n<2){
            cout<<0<<endl;
            continue;
        }
        if(n==2){
            cout<<1<<endl;
            continue;
        }
 
        num=0;
        while(n>=2){
            int pos=upper_bound(a+1,a+idata+1,n)-a;
            pos--;
            n-=a[pos];
            num++;
        }
        cout<<num<<endl;
    }
    return 0;
}

C. Hilbert's Hotel

题目链接:https://codeforces.ml/contest/1345/problem/C 

 题目大意:给出n个数,每个数根据题目给定规则做  a[i]%n,判断有没有数相同,因为每个房间只能住一位客人

 

#include <bits/stdc++.h>
#define rush() int T;cin>>T;while(T--)
#define go(a) while(cin>>a)
#define ms(a,b) memset(a,b,sizeof a)
#define E 1e-8
using namespace std;
typedef __int64 ll;
const int idata=2e5+5;

int n,m,t,_;
int i,j,k;
int a[idata];
vector<int>v;

int main()
{
    cin.tie(0);
    iostream::sync_with_stdio(false);
    rush()
    {
        cin>>n;
        v.clear();
        for(i=0;i<n;i++){
            cin>>_;
            k = ( ( _ % n + n ) % n + i ) % n;
            v.push_back(k);
        }
        int flag=0;
        sort(v.begin(),v.end());
        vector<int>::iterator it;
        for(it=v.begin()+1;it!=v.end();it++){
            if((*it)==(*(it-1))){
                flag=1;
                break;
            }
        }
        if(flag) cout<<"no"<<endl;
        else cout<<"yes"<<endl;
    }
    return 0;
}


 D. Monopole Magnets

题目链接:https://codeforces.ml/contest/1345/problem/D

 题目大意:给你n*m的矩阵,南磁针不能动且可以放在任意位置,北磁针移动的方向为南磁针所在地,要求

1、每行  每列至少一个南磁针

2、每个黑瓷砖都能够被北磁针拜访

3、每个白瓷砖无论如何都不能被北磁针拜访

如果满足以上条件,输出最少几个北磁针可以完成遍历黑瓷砖的任务,如果不能,则-1

 题目解析:有两种情况要输出-1

如果有一行有黑色,那么这些黑色必须要连在一起,因为如果有白瓷砖插入其中,无论南磁针放在   左黑, 右黑还是 白瓷砖那一部分上都不满足条件

如果这一行没有黑色,那么必须有一列也全为白色,因为只有如此将这一行与这一列交叉的那个白瓷砖上放置南,磁针,才能满足每一行放置一个南磁针的条件

                                                                                                                                           ————以下代码转载

#include <bits/stdc++.h>
#define rush() int T;cin>>T;while(T--)
#define go(a) while(cin>>a)
#define ms(a,b) memset(a,b,sizeof a)
#define E 1e-8
using namespace std;
typedef __int64 ll;
const int idata=1e3+5;

int n,m,t,_,ans;
int i,j,k;
char ch[idata][idata];
bool nx[idata],ny[idata];//判断某一行或某一列有没有‘#’
bool vis[idata][idata],ok[idata][idata];

void dfs(int x,int y)
{
    if(vis[x][y] || ch[x][y]=='.') return ;

    //cout<<x<<" "<<y<<endl;
    vis[x][y]=true;
    if(x>0)
        dfs(x-1,y);
    if(x<n-1)
        dfs(x+1,y);
    if(y>0)
        dfs(x,y-1);
    if(y<m-1)
        dfs(x,y+1);
}

int main()
{
    /*cin.tie(0);
    iostream::sync_with_stdio(false);*/
    while(cin>>n>>m)
    {
        for(i=0;i<n;i++){
            cin>>ch[i];
        }
//不满足情况1判断
        for(i=0;i<n;i++){
            for(j=0;j<m;j++){
                if(ch[i][j]=='#'){
                    if(nx[i] && j>0 && ch[i][j-1]=='.'){
                        cout<<"-1"<<endl;
                        return 0;
                    }
                    if(ny[j] && i>0 &&ch[i-1][j]=='.'){
                        cout<<"-1"<<endl;
                        return 0;
                    }
                    nx[i]=ny[j]=1;
                }
            }
        }
//不满足情况2判断
        for(i=0;i<n;i++){
            for(j=0;j<m;j++){
                if(ch[i][j]=='.'){
                    if(nx[i] || ny[j]){
                        ok[i][j]=1;
                    }
                }
            }
        }

        ms(nx,0),ms(ny,0);
        for(i=0;i<n;i++){
            for(j=0;j<m;j++){
                if(ok[i][j]==0){//当前点是‘#’或行列全为白的交叉点
                    nx[i]=ny[j]=1;
                }
            }
        }
        //如果某一行没有黑瓷砖,并且没有一列全白
        for(i=0;i<n;i++)
        if(!nx[i]){
            cout<<"-1"<<endl;
            return 0;
        }
        //如果某一列没有黑瓷砖,并且没有一行全白
        for(j=0;j<m;j++)
        if(!ny[j]){
            cout<<"-1"<<endl;
            return 0;
        }

        int ans=0;
        for(i=0;i<n;i++){

            for(j=0;j<m;j++){

                if(!vis[i][j] && ch[i][j]=='#'){

                    ans++;
                    dfs(i,j);
                }
            }

        }
        cout<<ans<<endl;
    }
    return 0;
}
原创文章 410 获赞 16 访问量 3万+

猜你喜欢

转载自blog.csdn.net/C_Dreamy/article/details/105964661
今日推荐