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 题目大意 圆环 ...
随机推荐
- wpf,图片灰化处理
private BitmapSource ToGray(BitmapSource source) { FormatConvertedBitmap re = new FormatConvertedBit ...
- struts2总结三:struts2配置文件struts.xml的简单总结
一.struts中的常量constant的配置. 在struts2中同一个常量的配置有三种方式,第一种在struts.xml中,第二种在struts.properties中配置,第三种在web.xml ...
- Codeforces 650B Image Preview(尺取法)
题目大概说手机有n张照片.通过左滑或者右滑循环切换照片,滑动需要花费a时间:看一张照片要1时间,而看过的可以马上跳过不用花时间,没看过的不能跳过:有些照片要横着看,要花b时间旋转方向.那么问T时间下最 ...
- webservice 学习笔记
1.webservice的概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的 ...
- MongoDB 入门之安装篇
前言:MongoDB 在各 OS 上的安装比较简单,此文章只用来记录,不考虑技术深度. 一.Ubuntu 导入 MongoDB 公钥,添该软件源文件,更新源列表 sudo apt-key adv -- ...
- iOS之02-第一个OC的类
OC是一门面向对象的语言,因此它也有类.对象.静态\动态方法.成员变量的概念.这讲就来创建第一个OC的类. 第一个类的源码: /* 人 类名:Person 属性(成员变量\实例变量):体重.年龄 行为 ...
- node.js 实现一个简单的登录拦截器
拦截器在web开发中随处可见,比如站点的管理后台,不说所有人都能进入,所以就需要做一个拦截器并友好的跳转到提示页. 下面我们简单实现一种,判断用户是否登录成功,登录不成功的用户自动重定向到登录页面. ...
- 20145308刘昊阳 《Java程序设计》实验一 Java开发环境的熟悉 实验报告
20145308刘昊阳 <Java程序设计>实验一报告 实验名称 Java开发环境的熟悉 实验内容 使用JDK编译.运行简单的Java程序 2.使用Eclipse 编辑.编译.运行.调试J ...
- ubuntu 安装 Terminator
sudo apt-get install terminator Ctrl-Shift-E: 垂直分割Ctrl-Shift-O: 水平分割Ctrl-Shift-P: 激活先前的窗口Ctrl-Shift- ...
- jquery放大镜效果
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8& ...