Codeforces_723
A.取中间那个点即可。
#include<bits/stdc++.h>
using namespace std; int a[]; int main()
{
ios::sync_with_stdio(false);
cin >> a[] >> a[] >> a[];
sort(a,a+);
cout << a[]-a[] << endl;
return ;
}
B.模拟,注意判断是否在括号内。
#include<bits/stdc++.h>
using namespace std; int n;
string s; int main()
{
cin >> n;
getchar();
getline(cin,s);
int maxx = ,cnt = ,now = ,flag = ;
for(int i = ;i < n;i++)
{
if(s[i] == '(')
{
flag = ;
now = ;
}
else if(s[i] == ')')
{
flag = ;
if(now) cnt++;
now = ;
}
else if(s[i] == '_')
{
if(flag == && now) cnt++;
now = ;
}
else
{
now++;
if(flag == ) maxx = max(maxx,now);
}
}
cout << maxx << " " << cnt << endl;
return ;
}
C.先算出ans,再把个数不到ans的数填满。
#include<bits/stdc++.h>
using namespace std; int n,m,a[],b[] = {}; int main()
{
ios::sync_with_stdio(false);
cin >> n >> m;
for(int i = ;i <= n;i++)
{
cin >> a[i];
if(a[i] <= m) b[a[i]]++;
}
int ans = n/m,cnt = ,now = ;
for(int i = ;i <= n;i++)
{
if(a[i] <= m && b[a[i]] <= ans) continue;
while(b[now] >= ans) now++;
if(now == m+) break;
if(a[i] <= m) b[a[i]]--;
a[i] = now;
b[now]++;
cnt++;
}
cout << ans << " " << cnt << endl;
for(int i = ;i <= n;i++) cout << a[i] << " ";
cout << endl;
return ;
}
D.dfs找出每一个湖泊,按面积排序,把cnt-k个小的填满。
#include<bits/stdc++.h>
using namespace std; int n,m,k,vis[][] = {},ok,sum;
int dx[] = {,,-,};
int dy[] = {-,,,};
string s[];
struct xxx
{
int x,y,sum;
friend bool operator <(xxx a,xxx b)
{
return a.sum < b.sum;
}
}a[]; void dfs1(int x,int y)
{
if(vis[x][y]) return;
vis[x][y] = ;
sum++;
for(int i = ;i < ;i++)
{
int xx = x+dx[i],yy = y+dy[i];
if(xx < || xx > n || yy < || yy > m)
{
ok = ;
continue;
}
if(s[xx][yy] == '*') continue;
dfs1(xx,yy);
}
} void dfs2(int x,int y)
{
s[x][y] = '*';
for(int i = ;i < ;i++)
{
int xx = x+dx[i],yy = y+dy[i];
if(xx < || xx > n || yy < || yy > m || s[xx][yy] == '*') continue;
dfs2(xx,yy);
}
}
int main()
{
ios::sync_with_stdio(false);
cin >> n >> m >> k;
int cnt = ;
for(int i = ;i <= n;i++)
{
cin >> s[i];
s[i] = " "+s[i];
}
for(int i = ;i <= n;i++)
{
for(int j = ;j <= m;j++)
{
if(s[i][j] == '*' || vis[i][j]) continue;
ok = ;
sum = ;
dfs1(i,j);
if(ok)
{
a[++cnt].x = i;
a[cnt].y = j;
a[cnt].sum = sum;
}
}
}
sort(a+,a++cnt);
cnt -= k;
int ans = ;
for(int i = ;i <= cnt;i++)
{
ans += a[i].sum;
dfs2(a[i].x,a[i].y);
}
cout << ans << endl;
for(int i = ;i <= n;i++)
{
for(int j = ;j <= m;j++) cout << s[i][j];
cout << endl;
}
return ;
}
E.因为是无向图,所以有偶数个度为奇数的点,我们把他们与第n+1个点相连,则所有点的度都是偶数个,存在欧拉回路,输出这个回路的路径(不包括第n+1个点),符合要求。
#include<bits/stdc++.h>
using namespace std; int n,m,d[];
set<int> s[]; void dfs(int now)
{
while(s[now].size())
{
int t = *s[now].begin();
s[now].erase(t);
s[t].erase(now);
if(now != n+ && t != n+) cout << now << " " << t << endl;
dfs(t);
}
} int main()
{
int T;
cin >> T;
while(T--)
{
cin >> n >> m;
memset(d,,sizeof(d));
while(m--)
{
int x,y;
cin >> x >> y;
s[x].insert(y);
s[y].insert(x);
d[x]++;
d[y]++;
}
int ans = ;
for(int i = ;i <= n;i++)
{
if(d[i]%)
{
s[i].insert(n+);
s[n+].insert(i);
}
else ans++;
}
cout << ans << endl;
for(int i = ;i <= n;i++) dfs(i);
}
return ;
}
F.原图无向连通,可以把原图的除s,t外的点分为三类团。
①与s相连。②与t相连。③与s,t相连。
若不存在第③类团,则把相应团与s或t相连,最后把s与t相连。
若存在第③类团,则s与t不必相连,可以通过第③类团来把他们相连,这样其中某个点的度就少了1,为最优情况。
#include<bits/stdc++.h>
using namespace std; struct xx
{
int x,y;
xx(int a,int b):x(a),y(b){};
};
int n,m,x,y,dx,dy,cnt = ,a[] = {},b[] = {},vis[] = {};
vector<int> v[];
vector<xx> ans; void dfs(int now)
{
vis[now] = ;
for(int i = ;i < v[now].size();i++)
{
int t = v[now][i];
if(t == x) a[cnt] = now;
else if(t == y) b[cnt] = now;
else if(!vis[t])
{
ans.push_back(xx(now,t));
dfs(t);
}
}
} int main()
{
ios::sync_with_stdio(false);
cin >> n >> m;
for(int i = ;i <= m;i++)
{
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
cin >> x >> y >> dx >> dy;
for(int i = ;i <= n;i++)
{
if(vis[i] || i == x || i == y) continue;
++cnt;
dfs(i);
}
int z = ;
for(int i = ;i <= cnt;i++)
{
if(a[i] == )
{
ans.push_back(xx(b[i],y));
dy--;
}
else if(b[i] == )
{
ans.push_back(xx(a[i],x));
dx--;
}
else z++;
}
if(dx < || dy < || dx+dy- < z)
{
cout << "No" << endl;
return ;
}
if(z == ) ans.push_back(xx(x,y));
else
{
int flag = ;
for(int i = ;i <= cnt;i++)
{
if(a[i] == || b[i] == ) continue;
if(flag)
{
if(dx)
{
ans.push_back(xx(a[i],x));
dx--;
}
else
{
ans.push_back(xx(b[i],y));
dy--;
}
}
else
{
flag = ;
ans.push_back(xx(a[i],x));
dx--;
ans.push_back(xx(b[i],y));
dy--;
}
}
}
cout << "Yes" << endl;
for(int i = ;i < ans.size();i++) cout << ans[i].x << " " << ans[i].y << endl;
return ;
}
Codeforces_723的更多相关文章
随机推荐
- 查找2-n之间素数的个数
题目描述 查找2-n之间素数的个数.n为用户输入值.素数:一个大于1的正整数,如果除了1和它本身以外,不能被其他正整数整除,就叫素数.如2,3,5,7,11,13,17…. 输入 整数n 输出 2-n ...
- UGUI源码之EventSystem
今天研究下UGUI的源码,先从EventSystem入手.EventSystem是用来处理点击.键盘输入以及触摸等事件的. 1.BaseInputModule EventSystem开头声明了两个变量 ...
- Longhorn入门级教程!轻松实现持久化存储!
介 绍 在本文中你将学会如何使用k3s在Civo上运行Longhorn.如果你还没使用过Civo,可以到官网注册(https://www.civo.com/ )还可以申请免费的使用额度.首先,需要一个 ...
- springboot整合apache ftpserver详细教程(看这一篇就够了)
原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/12192425.html,否则将追究法律责任!!! 一.Apache ftpserver相关 ...
- Scala与Mongodb实践3-----运算环境的搭建
目的:使的在IDEA中编辑代码,令代码实现mongodb运算,且转换较为便捷 由实验2可知,运算环境的搭建亦需要对数据进行存储和计算,故需要实现类型转换,所以在实验2的基础上搭建环境. 由菜鸟教程可得 ...
- 关于java php go 中AES加解密秘钥长度问题
今天心血来朝,想用go把php中的一个小功能重写一下,但在解密aes加密的数据时碰到了个坑! php的mcrypt拓展(貌似php7.1版本以上不支持了)提供了aes的加解密: 而且php aes 的 ...
- Linux session(会话)
笔者在前文<Linux job control>中介绍了进程组(job)的概念以及常见的 job control 操作,本文接着介绍 session 的概念.本文中演示部分使用的环境为 u ...
- DP-Fibonacci
善于发现 DP 中的 Fibonacci 我们在做 DP 题时 , 会发现有一些题 类似于找规律的题 ,观察测试样例 , 要对数据敏感 , 比如输入 2 输出 1 , 输入 3 就输出 2 …… ...
- dp-最长公共子序列(LCS)
字符序列 与 字符字串的区别 序列是可以不连续的字符串 , 字串必须要是连续的 . 问题描述 : 给定两串字符串 abcde 和 acdf , 找出 2 串中相同的字符序列,观察知 相同的字符序列为 ...
- dp——洛谷 P1541 乌龟棋 —— by hyl天梦
题目:(转自 https://www.luogu.com.cn/problem/P1541) 题目描述 乌龟棋的棋盘是一行NN个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第NN格是 ...