Codeforces Round #454 Div. 1 [ 906A A. Shockers ] [ 906B B. Seating of Students ] [ 906C C. Party ]
PROBLEM A. Shockers
题
http://codeforces.com/contest/906/problem/A
906A
907C
解
水题,按照题意模拟一下就行了
如果是 ‘ ! ’,那么该字符串中所有没出现的字符标记为否,如果是 ' . '那么该字符串中出现的字符全部标记为否
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio> using namespace std; const int M=40;
const int N=26;
const int MLEN=1e5+44; int flag[M];
int n,len;
char str[MLEN];
bool ed;
int tag[M]; int main()
{
char op[2];
int ans=0;
for(int i=0;i<26;i++)
flag[i]=1;
scanf("%d",&n);
ed=false;
for(int i=1;i<n;i++)
{
scanf("%s%s",op,str);
len=strlen(str);
if(op[0]=='!')
{
if(ed)
{
ans++;
continue;
}
memset(tag,0,sizeof(tag));
for(int j=0;j<len;j++)
tag[str[j]-'a']++;
for(int j=0;j<N;j++)
if(tag[j]==0) flag[j]=0;
}
else if(op[0]=='.')
{
if(ed) continue;
for(int j=0;j<len;j++)
flag[str[j]-'a']=0;
}
else
{
if(ed)
{
ans++;
continue;
}
for(int j=0;j<len;j++)
flag[str[j]-'a']=0;
}
if(ed==0)
{
int tmp=0;
for(int i=0;i<26;i++)
if(flag[i]) tmp++;
if(tmp==1)
ed=1;
}
}
cout<<ans<<endl;
return 0;
}
PROBLEM B. Seating of Students
题
http://codeforces.com/contest/906/problem/B
906B
907D
解
n为行数,m为列数
如果 n,m 的值都小于等于 的话,直接 dfs 暴力跑一下即可
否则,如果 m>n 那么对于第 x 行,记该行元素为 a1,a2,a3,a4,……am,提取下标为奇数的为一组 a1,a3,a5,a7……,提取下表为偶数的为另一组 a2,a4,a6……
如果行号 x 为奇数,则将奇数组放在偶数组前面,组成新的一行,即 a1,a3,a5,a7……,a2,a4,a6……
如果行号 x 为偶数,则将奇数组放再偶数组后面,组成新的一行,即 a2,a4,a6……,a1,a3,a5……
如果 n>m 则类似地对每一列处理
(对于 a1,a2,a3,a4,……ak,如果 k>4,上述重组方法是必然合法的)
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <vector> using namespace std;
namespace solvesmall
{
const int M=24; int n,m,tol;
int mtx[M][M],mdl[M][M];
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
int flag[M];
int ng[M][M]; bool dfs(int x,int y)
{
int xx,yy,ngflag;
if(x>n) return true;
for(int i=1;i<=tol;i++)
if(flag[i]==0)
{
ngflag=0;
for(int p=0;p<4;p++)
{
xx=x+dx[p]; yy=y+dy[p];
if(ng[i][mtx[xx][yy]])
ngflag=1;
}
if(ngflag==0)
{
mtx[x][y]=i;
flag[i]=1;
xx=x; yy=y+1;
if(yy==m+1) xx++,yy=1;
if(dfs(xx,yy)) return true;
mtx[x][y]=0;
flag[i]=0;
}
}
return false;
} void getng()
{
int xx,yy;
memset(ng,0,sizeof(ng));
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
for(int p=0;p<4;p++)
{
xx=i+dx[p]; yy=j+dy[p];
if(mdl[xx][yy]!=0)
ng[mdl[i][j]][mdl[xx][yy]]=1;
}
}
} int solve(int xn,int xm)
{
n=xn,m=xm;
int tmp;
tmp=0; tol=n*m;
memset(mtx,0,sizeof(mtx));
memset(mdl,0,sizeof(mdl));
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
tmp++;
mdl[i][j]=tmp;
}
getng();
memset(flag,0,sizeof(flag));
if(dfs(1,1))
{
puts("YES");
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
printf("%d ",mtx[i][j]);
puts("");
}
}
else
puts("NO");
}
} const int M=1e5+44; int n,m;
int pi[M];
vector<int> ans; void puts1(int li,int ri)
{
int cnt=0;
for(int i=li;i<=ri;i+=2)
{
if(cnt++) printf(" ");
printf("%d",i);
}
} void puts2(int li,int ri)
{
int cnt=0;
for(int i=li+1;i<=ri;i+=2)
{
if(cnt++) printf(" ");
printf("%d",i);
}
} int getpos(int x,int y)
{
return ((x-1)*m+y);
} int main()
{
int li,ri;
scanf("%d%d",&n,&m);
if(n<=4 && m<=4)
{
solvesmall::solve(n,m);
return 0;
}
if(m>n)
{
printf("YES\n");
for(int i=1;i<=n;i++)
{
li=(i-1)*m+1; ri=i*m;
if(i&1)
puts1(li,ri),printf(" "),puts2(li,ri),printf("\n");
else
puts2(li,ri),printf(" "),puts1(li,ri),printf("\n");
}
}
else
{
printf("YES\n");
ans.clear();
for(int i=1;i<=m;i++)
pi[i]=((i-1)&1)+1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
ans.push_back(getpos(pi[j],j));
pi[j]+=2;
if(pi[j]>n)
pi[j]=(j&1)+1;
}
}
for(int i=0;i<ans.size();i++)
{
printf("%d",ans[i]);
if((i+1)%m==0)
printf("\n");
else printf(" ");
}
}
return 0;
}
PROBLEM C. Party
题
http://codeforces.com/contest/906/problem/C
906C
907E
解
其实质就是一个图,然后确定一个团,每次要找到团中的一个点,把这个点所连的所有点都加入到这个团中,求把所有点加入到团中所需的最少操作数
由于点最多只有 个,可以状态压缩,然后对于每个点可以构造一个初始状态,从所有初始状态开始跑一遍带记忆化的 bfs 即可。
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <queue> using namespace std; const int N=(1<<22);
const int M=24; int n,m;
int dp[N+44],op[N+44],lst[N+44];
int val[M];
queue<int> que;
int stk[M],lstk; void bfs()
{
memset(dp,0,sizeof(dp));
memset(op,-1,sizeof(op));
memset(lst,-1,sizeof(lst));
while(!que.empty()) que.pop();
for(int i=0;i<n;i++)
{
dp[val[i]]=1; op[val[i]]=i;
que.push(val[i]);
}
int now,tmp,tmpdp,tmpnow;
while(!que.empty())
{
now=que.front(); que.pop();
for(int i=0;i<n;i++)
{
if(now&(1<<i))
{
tmpnow=now|val[i];
if(dp[tmpnow]==0)
{
dp[tmpnow]=dp[now]+1;
op[tmpnow]=i;
lst[tmpnow]=now;
que.push(tmpnow);
if(tmpnow==((1<<n)-1))
return ;
}
}
}
}
} int main()
{
int a,b,tmp;
scanf("%d%d",&n,&m);
memset(val,0,sizeof(val));
for(int i=0;i<n;i++)
val[i]|=(1<<i);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
a--,b--;
val[a]|=(1<<b);
val[b]|=(1<<a);
}
if(m==n*(n-1)/2)
{
printf("0\n");
return 0;
}
bfs();
lstk=0;
tmp=(1<<n)-1;
while(tmp!=-1)
{
stk[++lstk]=op[tmp];
tmp=lst[tmp];
}
printf("%d\n",lstk);
while(lstk)
{
printf("%d",stk[lstk]+1);
lstk--;
if(lstk!=0) printf(" ");
else printf("\n");
}
return 0;
}
Codeforces Round #454 Div. 1 [ 906A A. Shockers ] [ 906B B. Seating of Students ] [ 906C C. Party ]的更多相关文章
- Codeforces Round #454 Div. 2 A B C (暂时)
A. Masha and bears 题意 人的体积为\(V\),车的大小为\(size\),人能钻进车的条件是\(V\leq size\),人对车满意的条件是\(2V\geq size\). 现知道 ...
- Codeforces Round #454 (Div. 1) CodeForces 906D Power Tower (欧拉降幂)
题目链接:http://codeforces.com/contest/906/problem/D 题目大意:给定n个整数w[1],w[2],……,w[n],和一个数m,然后有q个询问,每个询问给出一个 ...
- Codeforces Round #454 Div. 1
B:考虑2*m怎么构造.因为要求相邻的数不能再相邻,容易想到黑白染色之类的东西,考虑染个色然后大概把黑点扔一边白点扔一边.显然m<=3时无解.对m>4,m为偶数时,如1 2 3 4 5 6 ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
随机推荐
- 删除重复信息且要保留一条的(roacle的rowid另类用法)
由于表的主键失效了(disable),导致导入了一些主键重复的数据,想保留唯一的一条, 最后发现其实可以用rowid来实现,不知道算不算是rowid的另类用法. delete /*+ parallel ...
- HTTP协议的简单了解
1. 用于服务端和客户端通信 客户端发送请求,服务端提供资源: 通过URI定位资源. 2. 通过请求和响应交换进行通信 客户端发送请求,服务端响应请求并返回数据: 请求报文:请求方法.URI.协议版本 ...
- Spring Boot环境的安装
Eclipse 使用springboot框架 环境的安装 1.下载java1.8 ,安装并配置环境变量 环境变量增加java 的安装目录到环境变量中path中增加 %JAVA_HOME%/bin增加变 ...
- ide的debug
webstom 新建立一个配置项 找到webpack.config.js,最后一行加上 devtool: "source-map" 然后点击debug
- 【IntelliJ IDEA】添加一个新的tomcat,tomcat启动无法访问欢迎页面,空白页,404
===================================第一部分,添加一个tomcat================================================== ...
- springboot+eureka+mybatis+mysql环境下报504 Gateway Time-out
1.test环境下的数据库配置的 driver和url有问题, 在工程日志中的表现是不能打印出最新的日志,在部署前的日志能看到报错:
- 查找最大和次大元素(JAVA版)(分治法)
问题描述:对于给定的含有n个元素的无序序列,求这个序列中最大和次大的两个不同元素. 问题求解分析(分治法):先给出无序序列数组a[low...high].第一种情况为当数组中只有一个元素时,此时只存在 ...
- MYSQL WHERE语句
过滤条件(WHERE) 如果你失忆了,希望你能想起曾经为了追求梦想的你. QQ群:651080565(php/web 学习课堂) 例子:淘宝首页上,我们会看到很多个商品,但这些商品,并不是 ...
- python 中startswith()和endswith() 方法
startswith()方法 Python startswith() 方法用于检查字符串是否是以指定子字符串开头如果是则返回 True,否则返回 False.如果参数 beg 和 end 指定值,则在 ...
- 接收端通过Request.InputStream读取流
以下有两种方式可以获取响应的数据流 1. 接收端通过Request.InputStream读取流 public static string StreamRead() { byte[] byts = n ...