Codeforces Round #346 (Div. 2) E F
因为很久没有个人认真做题了 昨天晚上开了场虚拟cf来锻炼个人手速
选的是第一次做cf的场 那时候7出3还被hack...之后也没补题 这次做的时候顺便回忆了一下以前比赛的时候是怎么想的 发现经验还是很重要的..
E 给出n个城市和m条双向边 把所有的双向边都变成单向的 如果一个城市的入度为0 那它就是孤独的 问有多少个孤独的
想到一个联通分量中 如果没有环(树) 那么肯定有一个点是孤独的 有环就没有了
那就对于每一个未访问的点bfs一下 然后dfs判断有没有环 O(n)的复杂度
F 一个n*m的谷仓与每个格子的高度 给出一个数p 要求对谷仓中的格子进行操作 只能减少它的高度 最后所有的格子要么是高度0 要么是高度x 高度为x的谷仓应该属于同一个联通分量且只有一个 并且至少有一个谷仓的高度没变 最后所有谷仓的高度加起来等于p
nm1000 首先想到了枚举地图 假设这个点是不变的 即这个点高度是x 判断p%x减少耗时 对这个点进行bfs 看满足高度大于等于x且属于一个连通分量的点是否到达p/x个 如果到达 再次bfs选择p/x个 输出
但是这个复杂度最差是n*m*n*m 即有n*m个数字 p为这n*m个数字的lcm cf跑的多快要挂
然而本着交一个证明来过的想法 我加了一个判断p/x与n*m关系的优化 并且修改了vis数组的memset次数 交了一次
发现居然一气跑到107组才t
之后又想到一个优化: 先判断一下当前 如果这个数为x的话 大于等于x的数能否足够p/x
这样就需要一个类似于前缀和的东西 对所有种数字进行记录并排序 记录下来他们的个数 利用代码中xd的累加 这样 cz[x] 表示的就是大于等于x的有多少了
如果不够p/x 就直接continue
不过最后ac了发现一共只有107组数据这种感觉....
E
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<math.h>
#include<iostream>
#include<queue>
using namespace std;
int n , m ;
int a[100050];
int b[100050];
vector<int >q[100050];
int vis[100050];
int bfs(int u){
int cnt = 1;
queue<int >que;
que.push(u);
vis[u] = 1;
while(!que.empty()){
int uu = que.front();que.pop();
for(int i=0;i<q[u].size();i++){
int v = q[u][i];
if(vis[v] == 0){
vis[v] = 1;
cnt ++ ;
que.push(v);
}
}
}
return cnt ;
}
bool dfs(int u, int fa){
vis[u] = 2;
for(int i = 0;i<q[u].size();i++){
int v= q[u][i];
if(v == fa){
continue;
}
if(vis[v] == 2){
return true;
}
vis[v] = 2;
bool ok = dfs(v,u);
if(ok ){
return true;
}
}
return false;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
q[i].clear();
}
for(int i=1;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
q[u].push_back(v);
q[v].push_back(u);
}
memset(vis,0,sizeof(vis));
int ans = 0;
for(int i=1;i<=n;i++){
if(vis[i] == 0){
int cnt = bfs(i);
bool ok = dfs(i,-1);
if(ok){ }
else {
ans ++ ;
}
}
}
printf("%d\n",ans);
}
F
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<math.h>
#include<iostream>
#include<queue>
using namespace std;
#define L long long
L n , m , p;
L a[1050][1050];
L vis[1050][1050];
L b[1050*1050];
int dx[4]= {0,0,1,-1};
int dy[4]= {1,-1,0,0};
bool check(int x,int y)
{
if(x>=1&&x<=n&&y>=1&&y<=m)
{
return true;
}
return false;
}
L bfs(int x,int y,int h,int res)
{
queue<int >q;
q.push(x);
q.push(y);
L cnt = 1;
vis[x][y] = res;
while(!q.empty())
{
int xx = q.front();
q.pop();
int yy = q.front();
q.pop();
for(int i=0; i<4; i++)
{
int xz = xx+ dx[i];
int yz = yy+ dy[i];
if(check(xz,yz))
{
if(vis[xz][yz] != res && a[xz][yz] >= h)
{
vis[xz][yz] = res ;
cnt ++;
q.push(xz);
q.push(yz);
}
}
}
}
return cnt ;
}
void cl(int x,int y,int h,int res,int many)
{
queue<int >q;
q.push(x);
q.push(y);
L cnt = 1;
vis[x][y] = -1;
if(cnt == many)
{
return ;
}
while(!q.empty())
{
int xx = q.front();
q.pop();
int yy = q.front();
q.pop();
for(int i=0; i<4; i++)
{
int xz = xx+ dx[i];
int yz = yy+ dy[i];
if(check(xz,yz))
{
if(vis[xz][yz] == res && a[xz][yz] >= h)
{
vis[xz][yz] = -1 ;
cnt ++;
q.push(xz);
q.push(yz);
if(cnt == many)
{
return ;
}
}
}
}
}
}
bool cmp(L a,L b)
{
return a<b;
}
int main()
{
scanf("%lld%lld%lld",&n,&m,&p);
L ma = 0;
map<L , int >cz;
int cnt = 0;
for(int i= 1; i<=n; i++)
{
for(int k=1; k<=m; k++)
{
scanf("%lld",&a[i][k]);
if(ma < a[i][k])
{
ma = a[i][k];
}
if(cz[a[i][k]] == 0)
{
cnt ++;
b[cnt ] = a[i][k];
}
cz[a[i][k]] ++ ;
}
}
L res = 0;
bool ok = false;
memset(vis,0,sizeof(vis));
sort(b+1,b+1+cnt,cmp);
L xd = 0;
for(int i= cnt ; i>=1; i--)
{
cz[b[i]] = cz[b[i]] + xd;
xd = cz[b[i]] ;
}
for(int i=1; i<=cnt ; i++)
{
L h = b[i];
if(p%h != 0)
{
continue;
}
L many = p/h;
if(many > n*m)
{
continue;
}
if(cz[b[i]] < many)
{
continue;
}
res ++ ;
ok = false;
for(int k = 1; k<=n; k++)
{
if(ok)
{
break;
}
for(int j=1; j<=m; j++)
{
if(vis[k][j] != res && a[k][j] == h)
{ vis[k][j] = res ;
L many2 = bfs(k,j,h,res);
if(many2 >= many)
{
printf("YES\n");
cl(k,j,h,res,many);
for(int q = 1; q<=n; q++)
{
for(int w = 1; w<=m; w++)
{
if(vis[q][w] == -1)
{
printf("%d",h);
}
else printf("0"); if(w == m)
{
printf("\n");
}
else printf(" ");
}
}
ok = true;
break;
}
}
}
}
if(ok )
{
break;
}
}
if(ok == false)
{
printf("NO\n");
}
}
Codeforces Round #346 (Div. 2) E F的更多相关文章
- Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)
Codeforces Round #346 (Div. 2)---E. New Reform E. New Reform time limit per test 1 second memory lim ...
- Codeforces Round #573 (Div. 1) 差F
Codeforces Round #573 (Div. 1) E 题意:二维平面上有 n 个点,你可以放至多 m 条直线使得 (0,0) 与每个点的连线至少与一条直线相交.求原点与所有直线的距离最小值 ...
- Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集 bfs
F. Polycarp and Hay 题目连接: http://www.codeforces.com/contest/659/problem/F Description The farmer Pol ...
- Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集
题目链接: 题目 F. Polycarp and Hay time limit per test: 4 seconds memory limit per test: 512 megabytes inp ...
- Codeforces Round #346 (Div. 2) A Round-House
A. Round House 题目链接http://codeforces.com/contest/659/problem/A Description Vasya lives in a round bu ...
- Codeforces Round #541 (Div. 2) (A~F)
目录 Codeforces 1131 A.Sea Battle B.Draw! C.Birthday D.Gourmet choice(拓扑排序) E.String Multiplication(思路 ...
- Codeforces Round #346 (Div. 2) A. Round House 水题
A. Round House 题目连接: http://www.codeforces.com/contest/659/problem/A Description Vasya lives in a ro ...
- Codeforces Round #532 (Div. 2):F. Ivan and Burgers(贪心+异或基)
F. Ivan and Burgers 题目链接:https://codeforces.com/contest/1100/problem/F 题意: 给出n个数,然后有多个询问,每次回答询问所给出的区 ...
- 补题—Codeforces Round #346 (Div. 2) _智商欠费系列
这次的题目相对容易 但是智商依旧不够用 原因有三点 1.英文水平堪忧 2 逻辑不严密 3 细节掌握不够好 传送门 http://codeforces.com/contest/659 A 题目大意 圆环 ...
随机推荐
- CSS3属性
1.边框阴影(box-shadow ): 投影方式,X轴偏移,Y轴偏移,阴影模糊半径,阴影扩展半径,颜色 2.边框图像(border-image) 3.边框圆角:border-radius:5px 4 ...
- 【noip2014T3】
上文有提到noip2014还有没A的嘛..就先把这个坑给填了 flappy bird好sad啊 还是先做解方程 八中的数据好强了,然而我最后凑了四个质数就A了,感谢shy! 作为联赛最后一题,学习它的 ...
- mybatis做like模糊查询
http://www.cnblogs.com/cyttina/p/3894428.html
- MongoDB 入门之基础 DCL
此文章主要记录部分主要的 MongoDB 的 DCL 操作. MongoDB 默认不需要用户名和密码就可以用 mongodb.exe 登录 一.开启 MonogoDB 的权限模式 修改 MongoDB ...
- pointers on c (day 1,chapter1)
c语言的优先级 优先级 运算符 名称或含义 使用形式 结合方向 说明 1 [] 数组下标 数组名[常量表达式] 左到右 () 圆括号 (表达式)/函数名(形参表) . 成员选择(对象) 对象.成员名 ...
- yarn map failed
Container launch failed for container_1385017085286_4943_01_000053 : org.apache.hadoop.yarn.exceptio ...
- [转]复制虚拟机后linux中的eth0变成eth1问题
为什么原来的eth0会变成eth1? 很多Linux distribution使用udev动态管理设备文件,并根据设备的信息对其进行持久化命名.udev会在系统引导的过程中识别网卡,将mac地址和网卡 ...
- AngularJS的Filter用法详解
上一篇讲了自定义Directive,本篇是要讲到AngularJS的Filter. Filter简介 Filter是用来格式化数据用的. Filter的基本原型( '|' 类似于Linux中的管道模式 ...
- 【BZOJ】1975: [Sdoi2010]魔法猪学院
题意 \(n(2 \le n \le 5000)\)个点,找尽量多的不同\(1\)到\(n\)的路径,每一次的花费就是路径的全值和,要求在费用不超过\(E\)的情况下路径最多. 分析 裸的最段路. 题 ...
- Sping Environment为Null的原因和解决方法
参考:https://github.com/spring-projects/spring-boot/issues/4711 这个issue提出不到20天给我搜出来了,还是相信google的强大 问题: ...