0
点赞
收藏
分享

微信扫一扫

2022/4/4

千妈小语 2022-04-04 阅读 39
算法c++

J - Anti-merge 黑白染色

从第一个位置开始遍历,他四周如果有和他相同的就把他染成和自己不一样的颜色,之后加进队列知道最后遇不到相同的数;这样从第一个位置开始就形成了一个连通块,看一看用哪一个颜色比较少就用哪一个,因为如果一个位置的颜色与四周的不一样,四周的是不能和这个位置合并的,四周的颜色却可以相等,所以只需要一种标签就可以了;

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+7;
ll n,m,a[510][510],vis[510][510];
ll dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
struct node{
    ll x,y,col;
};
vector<pair<ll,ll>>ans;
void bfs(ll x,ll y){
    queue<node>q;
    q.push(node{x,y,0});
    vector<pair<ll,ll>>w,b;
    //w装的是标签为1的,b装0的
    vis[x][y]=1;
    b.push_back({x,y});
    while(!q.empty()){
        node u=q.front();q.pop();
        for(int i=0;i<4;i++){
            ll tx=u.x+dx[i],ty=u.y+dy[i];
            if(tx<=n&&tx>=1&&ty<=m&&ty>=1&&a[u.x][u.y]==a[tx][ty]&&!vis[tx][ty]){
                vis[tx][ty]=1;
               // cout<<tx<<" "<<ty<<" "<<u.x<<" "<<u.y<<endl;
                if(u.col==1) b.push_back({tx,ty});
                else w.push_back({tx,ty});
                q.push(node{tx,ty,!u.col});
            }
        }
       // cout<<"wb "<<w.size()<<" "<<b.size()<<endl;

    }
     if(b.size()>=w.size()){
            for(auto x:w) ans.push_back(x);
        }
        else {
            for(auto x:b) ans.push_back(x);
        }
}
int main(){
   // freopen("in.txt","r",stdin);
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++) scanf("%lld",&a[i][j]),vis[i][j]=0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++) if(!vis[i][j]) bfs(i,j);
    ll type=(ans.size()==0)?0LL:1LL;
    printf("%lld %d\n",type,ans.size());
    for(auto x:ans){
        printf("%lld %lld %lld\n",x.first,x.second,type);
    }
    return 0;
}
/*
10 10
7 3 6 8 4 5 1 2 8 7
2 5 7 6 1 8 8 6 7 6
7 1 5 6 6 4 5 7 3 3
8 8 8 6 1 7 7 3 3 1
4 7 7 2 8 7 8 4 3 4
3 5 2 6 8 4 1 8 6 5
2 5 7 6 7 8 7 8 4 2
3 2 1 8 7 2 7 3 1 6
8 5 4 2 3 8 7 2 8 1
8 3 2 5 3 5 3 2 7 3
*/

整除光棍 - 题目 - Daimayuan Online Judge

不用找规律,也不需要打表,只需要模拟除法即可,逐渐增加位数,看看是否能够整除x

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+7;
ll x,ans[100005],cnt;
string fac[10005];
ll weis(ll y){
    ll res=0;
    while(y){
        res++;
        y/=10;
    }
    return res;
}
bool check(string s){
    for(int i=0;i<=cnt;i++) ans[i]=0;
    cnt=0;ll shang=0,yu=0,chu=0,pos=0,cut=0;
    while(pos<s.size()){
        ll ct=0;
        while(chu<x&&pos<s.size()){
            chu=chu*10+s[pos]-'0';
            pos++;
            if(ct>=1&&cut>=1) ans[++cnt]=0;
            ct++;
        }
        cut++;
        shang=chu/x; ans[++cnt]=shang;
        yu=chu%x;
        //cout<<shang<<" "<<chu<<" "<<yu<<endl;
        chu=yu;
        if(yu==0) break;
    }
    if(yu==0) return 1;
    else return 0;
}
int main(){
   // freopen("in.txt","r",stdin);
   fac[1]="1";
   for(int i=2;i<=10005;i++) fac[i]=fac[i-1]+"1";
   scanf("%lld",&x);
   ll tmp=x;
   tmp=weis(tmp); cnt=0;
   for(int i=tmp;i<=10000;i++){
    //cout<<i<<" "<<fac[i]<<endl;
    if(check(fac[i])){
        for(int i=1;i<=cnt;i++)
        cout<<ans[i];
        cout<<" ";
        cout<<i<<endl;
        break;
    }
   }
    return 0;
}

D - Pattern Lock 

以此题来记录一下自己贫弱的代码能力,,,

先考虑n是偶数的情况,不是偶数就留下后面三行,然后在考虑m是偶数的情况,不是就留下后三列;

 

 

(7条消息) 2021 Jiangsu Collegiate Programming Contest 总结_Evil_boy__的博客-CSDN博客

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+7;
ll n,m;
int main(){
   // freopen("in.txt","r",stdin);
   scanf("%lld%lld",&n,&m);
    if(n%2==0){
        for(ll i=1;i<=n;i+=2){
            printf("%d 1\n",i);
            for(ll j=1;j<m;j++){
                printf("%lld %lld\n",i,j+1);
                printf("%lld %lld\n",i+1,j);
            }
            printf("%lld %lld\n",i+1,m);
        }
    }
    else{
        for(ll i=1;i<=n-3;i+=2){
            printf("%lld 1\n",i);
            for(ll j=1;j<m;j++){
                printf("%lld %lld\n",i,j+1);
                printf("%lld %lld\n",i+1,j);
            }
            printf("%lld %lld\n",i+1,m);
        }
        if(m%2==0){
        for(ll i=m-1;i>=1;i-=2){
           printf("%lld %lld\n",n-2,i);
           printf("%lld %lld\n",n-2,i+1);
           printf("%lld %lld\n",n-1,i);
           printf("%lld %lld\n",n-1,i+1);
           printf("%lld %lld\n",n,i);
           printf("%lld %lld\n",n,i+1);
        }
    }
    else{
        for(ll i=m-1;i>=3;i-=2){
           printf("%lld %lld\n",n-2,i);
           printf("%lld %lld\n",n-2,i+1);
           printf("%lld %lld\n",n-1,i);
           printf("%lld %lld\n",n-1,i+1);
           printf("%lld %lld\n",n,i);
           printf("%lld %lld\n",n,i+1);
    }
        cout << n - 1 << " " << 2 <<endl;
        cout << n - 2 << " " << 3 << endl;
        cout << n << " " << 2 << endl;
        cout << n - 2 << " " << 1 << endl;
        cout << n - 1 << " " << 3 << endl;
        cout << n << " " << 1 << endl;
        cout << n - 2 << " " << 2 << endl;
        cout << n << " " << 3 << endl;
        cout << n - 1 << " " << 1 << endl;
    }
    }
    return 0;
}

Binary String To Subsequences 

vector存储第cnt个字串最后一个元素的值,a[i]记录第i个元素在第几个vector中。拿一个one记录最后一个1的vector的位置,zero记录第一个0的位置,如果是0且one>0,说明还存在至少one个vector末尾的元素是1,则a[i]=one,one--,若one<=0,说明现在是没有了,要新开一个vector了,a[i]=++cnt;若是1且zero<=cnt,则说明还有vector末尾是0,则让a[i]=zero,zero++,one--,因为存0的vector变少了,存1的变多了嘛;最后输出cnt和a[i]即可

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+7;
ll n,t,a[200005];
string s;
vector<ll>v[200005];
int main(){
   // freopen("in.txt","r",stdin);
    scanf("%lld",&t);
    while(t--){
        scanf("%lld",&n);
        for(int i=1;i<=n;i++) a[i]=0,v[i].clear();
        ll cnt=0,one=0,zero=2;
        cin>>s;s=" "+s;
        v[++cnt].push_back(s[1]-'0'); a[1]=cnt;
        if(s[1]=='1') one++;else zero=1;
        for(int i=2;i<=n;i++){
            if(s[i]-'0'==0){
                if(one>0){
                    v[cnt].push_back(s[i]-'0');
                    a[i]=one;
                    one=max(0LL,one-1);
                    zero=max(1LL,zero-1);
                }
                else {
                    v[++cnt].push_back(s[i]-'0');
                    a[i]=cnt;
                }
            }
            else{
                if(zero>=1&&zero<=cnt){
                    v[zero].push_back(s[i]-'0');
                    a[i]=zero;
                    zero++;one++;
                }
                else{
                    v[++cnt].push_back(s[i]-'0');
                    a[i]=cnt;
                    zero++;one++;
                }
            }
        }
        printf("%lld\n",cnt);
        for(int i=1;i<=n;i++) printf("%lld ",a[i]);
        printf("\n");
    }
    return 0;
}
举报

相关推荐

2022/4/4偷懒

2022/4/9-2022/4/10

2022-4-4至2022-4-10周报

2022/4/13

2022/4/17

2022/4/6

2022/4/30

2022/4/2

2022/4/1

0 条评论