#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#define ll long long
using namespace std;
inline int read(){
int x=0,o=1;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')o=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*o;
}
const int N=55;
int n,m,now,ans1,ans2,ans3;
int visit[N][N],size[N*N];
int a[N][N],b[N][N][4];
inline void dfs(int x,int y){
if(x<1||x>n||y<1||y>m)return;
visit[x][y]=ans1;++now;++size[ans1];
for(int i=0;i<=3;++i){
if(b[x][y][i]==1)continue;
if(i==0&&!visit[x][y-1])dfs(x,y-1);
if(i==1&&!visit[x-1][y])dfs(x-1,y);
if(i==2&&!visit[x][y+1])dfs(x,y+1);
if(i==3&&!visit[x+1][y])dfs(x+1,y);
}
return;
}
int main(){
m=read(),n=read();
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j){
a[i][j]=read();
for(int k=0;k<=3;++k){
if(a[i][j]&(1<<k))b[i][j][k]=1;
}
}
memset(size,0,sizeof(size));
while(now<n*m){
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
if(visit[i][j]==0){
++ans1;dfs(i,j);
ans2=max(ans2,size[ans1]);
}
}
printf("%d\n%d\n",ans1,ans2);
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
for(int k=0;k<=3;++k){
if(b[i][j][k]){
if(k==0){
if(j-1<1)continue;
if(visit[i][j]==visit[i][j-1])continue;
ans3=max(ans3,size[visit[i][j]]+size[visit[i][j-1]]);
}
if(k==1){
if(i-1<1)continue;
if(visit[i][j]==visit[i-1][j])continue;
ans3=max(ans3,size[visit[i][j]]+size[visit[i-1][j]]);
}
if(k==2){
if(j+1>m)continue;
if(visit[i][j]==visit[i][j+1])continue;
ans3=max(ans3,size[visit[i][j]]+size[visit[i][j+1]]);
}
if(k==3){
if(i+1>n)continue;
if(visit[i][j]==visit[i+1][j])continue;
ans3=max(ans3,size[visit[i][j]]+size[visit[i+1][j]]);
}
}
}
}
}
printf("%d\n",ans3);
for(int j=1;j<=m;++j)
for(int i=n;i>=1;--i){
if(b[i][j][1]&&i-1>=1){
if(visit[i][j]!=visit[i-1][j])
if(size[visit[i][j]]+size[visit[i-1][j]]==ans3){
printf("%d %d N\n",i,j);
return 0;
}
}
if(b[i][j][3]&&i+1<=n){
if(visit[i][j]!=visit[i+1][j])
if(size[visit[i][j]]+size[visit[i+1][j]]==ans3){
printf("%d %d N\n",i+1,j);
return 0;
}
}
if(b[i][j][0]&&j-1>=1){
if(visit[i][j]!=visit[i][j-1])
if(size[visit[i][j]]+size[visit[i][j-1]]==ans3){
printf("%d %d E\n",i,j-1);
return 0;
}
}
if(b[i][j][2]&&j+1<=m){
if(visit[i][j]!=visit[i][j+1])
if(size[visit[i][j]]+size[visit[i][j+1]]==ans3){
printf("%d %d E\n",i,j);
return 0;
}
}
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#define ll long long
using namespace std;
inline int read(){
int x=0,o=1;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')o=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*o;
}
struct ppx{
double val;int i,j;
}a[40005];
inline bool cmp(const ppx &x,const ppx &y){return x.val<y.val;}
inline int gcd(int a,int b){
if(b==0)return a;
return gcd(b,a%b);
}
int main(){
int n=read(),tot=0;
for(int i=0;i<=n-1;++i){
for(int j=i+1;j<=n;++j){
if(i==0&&j==1){
a[++tot].val=(double)i*1.0/j;
a[tot].i=i;a[tot].j=j;
continue;
}
if(i==1){
a[++tot].val=(double)i*1.0/j;
a[tot].i=i;a[tot].j=j;
continue;
}
if(i!=0){
if(gcd(i,j)==1){
a[++tot].val=(double)i*1.0/j;
a[tot].i=i;a[tot].j=j;
continue;
}
}
}
}
sort(a+1,a+tot+1,cmp);
for(int i=1;i<=tot;++i)
printf("%d/%d\n",a[i].i,a[i].j);
printf("1/1\n");
return 0;
}
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#define ll long long
using namespace std;
inline int read(){
int x=0,o=1;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')o=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*o;
}
const int N=1005;
int a[N],b[N];
int main(){
int n=read();
for(int i=1;i<=n;++i)a[i]=read(),b[i]=a[i];
sort(b+1,b+n+1);
int ans=0;
for(int i=1;i<=n;++i){
if(a[i]!=b[i]){
++ans;int bj=0;
for(int j=n;j>=i+1;--j){
if(a[j]==b[i]&&a[i]==b[j]){bj=1;swap(a[i],a[j]);break;}
}
if(bj)continue;
for(int j=n;j>=i+1;--j){
if(a[j]==b[i]){bj=1;swap(a[i],a[j]);break;}
}
}
}
printf("%d\n",ans);
return 0;
}
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#define ll long long
using namespace std;
inline int read(){
int x=0,o=1;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')o=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*o;
}
const int N=50;
int n,m,a[N][N];
int v[N],b[N],visit[N];
inline bool check(){
for(int i=1;i<=n;++i)
if(b[i]<v[i])return false;
return true;
}
inline void dfs(int now,int nowm,int goal){
if(now>goal){
if(check()){
printf("%d ",goal);
for(int i=1;i<=m;++i)
if(visit[i])printf("%d ",i);
printf("\n");
exit(0);
}
return;
}
if(m-nowm+1<goal-now)return;//剪枝1
for(int i=nowm;i<=m;++i){//剪枝2:从上一次选的位置+1开始选
if(!visit[i]){
visit[i]=1;
for(int j=1;j<=n;++j)b[j]+=a[i][j];
dfs(now+1,i+1,goal);
visit[i]=0;
for(int j=1;j<=n;++j)b[j]-=a[i][j];
}
}
}
int main(){
n=read();for(int i=1;i<=n;++i)v[i]=read();
m=read();
for(int i=1;i<=m;++i)
for(int j=1;j<=n;++j)
a[i][j]=read();
for(int i=1;i<=m;++i){
for(int j=1;j<=m-i+1;++j){
memset(visit,0,sizeof(visit));
memset(b,0,sizeof(b));
dfs(1,j,i);
}
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#define ll long long
using namespace std;
inline int read(){
int x=0,o=1;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')o=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*o;
}
int n,m,B,D,ans[105];
inline bool pd(int x,int y){
int s=x^y,sum=0;
for(int i=0;(1<<i)<=s;i++)
if((1<<i)&s)sum++;
return sum>=D;
}
inline bool check(int x){
for(int i=1;i<=m;i++)
if(!pd(x,ans[i]))return false;
return true;
}
int main(){
n=read(),B=read(),D=read();
for(int i=0;i<(1<<(B+1))&&m<n;++i)
if(check(i))ans[++m]=i;
for(int i=1;i<=n;++i){
printf("%d ",ans[i]);
if(i%10==0)puts("");
}puts("");
return 0;
}
原文:https://www.cnblogs.com/PPXppx/p/11298852.html