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 题目大意 圆环 ...
随机推荐
- [BZOJ2599][Race][IOI2011]点分治
这是为了真正去学一下点分治..然后看了迪克李的ppt 又是一道写(改)了很久的题..终于ac了 1354799 orzliyicheng 2599 Accepted 31936 kb 23584 ms ...
- BFS HDOJ 2102 A计划
题目传送门 题意:中文题面 分析:双层BFS,之前写过类似的题.总结坑点: 1.步数小于等于T都是YES 2. 传送门的另一侧还是传送门或者墙都会死 3. 走到传送门也需要一步 #include &l ...
- (转)hadoop 集群间数据迁移
hadoop集群之间有时候需要将数据进行迁移,如将一些保存的过期文档放置在一个小集群中进行保存. 使用的是社区提供的功能,distcp.用法非常简单: hadoop distcp hdfs://nn1 ...
- Android 情景模式设置
情景模式的设置大家应当相当熟悉了,但是在Android中如何通过自己的程序进行情景模式的设置呢,情景模式分为多种多种,即可以使用系统自带的,也可 以使用自定义的,但是在开发某些程序时,可能需要在程序中 ...
- into outfile 生成sql脚本
select concat('insert into t_dm_stage(STAGE_ID,STAGE_NAME) values(',STAGE_ID,',','\'',STAGE_NAME,'\' ...
- CentOS下强行umount卸载设备
fuser -cu /usr/local/tomcat7/webapps/dsideal_yy/html/down/ fuser -ck /usr/local/tomcat7/webapps/dsid ...
- 【HDU】1846 Brave Game
http://acm.hdu.edu.cn/showproblem.php?pid=1846 题意:二人博弈,1堆石子每次取1~m个,没有石子可取的输,输出先手胜利还是后手胜利. #include & ...
- minicom使用
http://blog.chinaunix.net/uid-9525959-id-2001815.html 创建log文件 : minicom -C my_capturefile
- html5 可以讓使用者輸入url網址 ,去play影片
<html> <head> <title>Simple Video Example</title> <script> function pl ...
- jquery插件之文字无缝向上滚动
该插件乃本博客作者所写,目的在于提升作者的js能力,也给一些js菜鸟在使用插件时提供一些便利,老鸟就悠然地飞过吧. 此插件旨在实现目前较为流行的无缝向上滚动特效,当鼠标移动到文字上时,向上滚动会停止, ...