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 题目大意 圆环 ...
随机推荐
- 解决Kali Linux没有声音
解决Kali Linux没有声音 Kali Linux系统默认状态下,root用户是无法使用声卡的,也就没有声音.启用的方法如下: (1)在终端执行命令:systemctl --user enab ...
- 在IOS手机safari浏览器的无痕模式下,localStorage不起作用
无痕模式是黑色风格,正常模式是白色风格.在无痕模式中,使用localStorage.setItem()会报错,但在window对象下确实有localStorage.setItem方法. if (typ ...
- js 对象(Object)
一.对象 除了字符串.数字.true.false.null和undefined之外,javascript中的值都是对象. javascript对象属性包括名字和值,属性名可以是包含空字符串在内的任意字 ...
- 寻找房间中心zz
Finding the Centroid of a Room Boundary It's been a while since my last post and I'm sure most of yo ...
- C#数组的声明方式
C#数组的五种声明方式 一.声明一个未经初始化的数组引用,以后可以把这引用初使化为一个数组实例 int[] intArray; intArray = new int[10]; 注:数组的引用必须以相同 ...
- BZOJ3934 : [CQOI2015]标识设计
轮廓线插头DP. 设$f[i][j][a][b][c][d][e]$表示考虑到了$(i,j)$,轮廓线上3个下插头的位置分别为$a,b,c$,是否有右插头,已经放了$e$个$L$的方案数. 然后直接D ...
- HDU 3078 (LCA+树链第K大)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3078 题目大意:定点修改.查询树中任意一条树链上,第K大值. 解题思路: 先用离线Tarjan把每个 ...
- BZOJ2097[Usaco2010 Dec] 奶牛健美操
我猜我这样继续做水题会狗带 和模拟赛的题很像,贪心搞一下. #include<bits/stdc++.h> using namespace std; int read(){ ,f=;cha ...
- BZOJ4553: [Tjoi2016&Heoi2016]序列
Description 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他.玩具上有一个数列,数列中某些项的值 可能会变化,但同一个时刻最多只有一个值发生变化.现在佳媛姐姐已经研究出了所 ...
- HDU 2824 简单欧拉函数
1.HDU 2824 The Euler function 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=2824 3.总结:欧拉函数 题意:求(a ...