Codeforces Round #395 Div.2 题解
感受
第一次参加CF的rating比赛,感觉还是非常exciting,前18分钟把AB切掉之后一直在考虑C题,结果最后还是没有想出来Orz
传送门
A
比较水的模拟,就是计算:\(\frac{z}{lcm(a,b)}\)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n, m, z;
int gcd(int n, int m) {
return m == 0 ? n : gcd(m, n%m);
}
int main() {
cin >> n >> m >> z;
cout << (z/((ll)n*m/gcd(n, m)));
return 0;
}
B
水题。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000;
int main() {
int n, a[maxn];
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
if(n&1){
for(int i = 1; i <= n; i++) {
if(i & 1) {
cout << a[n-i+1];
}
else cout << a[i];
cout << ' ';
}
}
else {
for(int i = 1; i <= n/2; i++) {
if(i & 1) cout << a[n-i+1];
else cout << a[i];
cout << ' ';
}
for(int i = n/2+1; i <= n; i++) {
if(i & 1) cout << a[i];
else cout << a[n-i+1];
cout << ' ';
}
}
return 0;
}
C
考试的时候并没有切出来。。。
后来发现很简单。。。
题目大意
给出一颗树及各个点的颜色,求一个点,使得以该点为根时,其所有子树(不包括整棵树)颜色相同。
题解
解法1:\(O(n+m)\):如果有一条边,其两端颜色不同,那么不难得到必然有一个点是根,分别使用dfs检查即可。
解法2:\(O(n)\):统计所有颜色不同的边,如果有解,那么这些边一定有一个共同的端点,统计一下即可。
代码
考场tle代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
vector<int> G[maxn];
int n, c[maxn];
int color;
inline int read() {
int x = 0, f = 1; char ch = getchar();
while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
while(ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar();}
return x * f;
}
int vis[maxn];
bool dfs(int x) {
vector<int>::iterator it;
for(it = G[x].begin(); it != G[x].end(); it++) {
int &v = *it;
if(!vis[v]) {
vis[v] = 1;
if(c[v] != color) return false;
if(!dfs(v)) return false;
}
}
return true;
}
bool check(int x) {
vector<int>::iterator it;
for(it = G[x].begin(); it != G[x].end(); it++) {
color = c[*it];
vis[x] = 1;
if(!dfs(*it)) return false;
}
return true;
}
int main() {
scanf("%d", &n);
for(int i = 1; i < n; i++) {
int u = read(), v = read();
G[u].push_back(v);
G[v].push_back(u);
}
set<int> col;
for(int i = 1; i <= n; i++) {c[i] = read(); col.insert(c[i]);}
for(int i = 1; i <= n; i++) {
if(G[i].size() < col.size() - 1) continue;
memset(vis, 0, sizeof(vis));
vis[i] = 1;
if(check(i)) {
cout << "YES" << endl << i;
return 0;
}
}
cout << "NO" << endl;
return 0;
}
正解(解法2)
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORD(i,a,b) for (int i = (a); i >= (b); i--)
#define REP(i,a) FOR(i,0,(int)(a)-1)
#define reset(a,b) memset(a,b,sizeof(a))
#define BUG(x) cout << #x << " = " << x << endl
#define PR(x,a,b) {cout << #x << " = "; FOR (_,a,b) cout << x[_] << ' '; cout << endl;}
#define CON(x) {cout << #x << " = "; for(auto i:x) cout << i << ' '; cout << endl;}
#define mod 1000000007
#define pi acos(-1)
#define eps 0.00000001
#define pb push_back
#define sqr(x) (x) * (x)
#define _1 first
#define _2 second
int n, u, v, lis[100005], cnt[100005], tot;
vector<int> adj[100005];
int main() {
ios::sync_with_stdio(false);
cin >> n;
REP (i, n - 1) {
cin >> u >> v;
adj[v].pb(u);
adj[u].pb(v);
}
FOR (i, 1, n) cin >> lis[i];
FOR (i, 1, n) {
for (int nex: adj[i]) if (lis[i] != lis[nex]) {
cnt[i]++;
cnt[nex]++;
tot++;
}
}
FOR (i, 1, n) if (cnt[i] == tot) {
cout << "YES" << endl << i;
return 0;
}
cout << "NO";
}
D
题意:
给出一些边长为odd的矩形,用最多四种颜色*给矩形染色,使得相邻颜色不同。
题解:
首先边长为odd,那么如果两个矩形相邻,那么他们左上顶点的奇偶性一定不同。
所以我们根据左上顶点的奇偶染色即可。
具体见代码。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 500011;
int n;
struct edge{
int sx,sy;
int xx,xy;
}a[MAXN];
inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
}
inline void work(){
n=getint();
for(int i=1;i<=n;i++) {
a[i].xx=getint(); a[i].xy=getint();
a[i].sx=getint(); a[i].sy=getint();
a[i].xx=abs(a[i].xx); a[i].xy=abs(a[i].xy);
}
printf("YES\n");
for(int i=1;i<=n;i++) {
if(a[i].xx%2==1 && a[i].xy%2==1) printf("1");
else if(a[i].xx%2==1 && a[i].xy%2==0) printf("2");
else if(a[i].xx%2==0 && a[i].xy%2==1) printf("3");
else printf("4");
printf("\n");
}
}
int main()
{
work();
return 0;
}
E
不会做。
Codeforces Round #395 Div.2 题解的更多相关文章
- Codeforces Round #182 (Div. 1)题解【ABCD】
Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...
- Codeforces Round #608 (Div. 2) 题解
目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...
- Codeforces Round #525 (Div. 2)题解
Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...
- Codeforces Round #528 (Div. 2)题解
Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...
- Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F
Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...
- Codeforces Round #677 (Div. 3) 题解
Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...
- Codeforces Round #665 (Div. 2) 题解
Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...
- Codeforces Round #160 (Div. 1) 题解【ABCD】
Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...
- Codeforces Round #383 (Div. 2) 题解【ABCDE】
Codeforces Round #383 (Div. 2) A. Arpa's hard exam and Mehrdad's naive cheat 题意 求1378^n mod 10 题解 直接 ...
随机推荐
- python爬虫:爬取猫眼TOP100榜的100部高分经典电影
1.问题描述: 爬取猫眼TOP100榜的100部高分经典电影,并将数据存储到CSV文件中 2.思路分析: (1)目标网址:http://maoyan.com/board/4 (2)代码结构: (3) ...
- Python入门必知的几个点
Python是Guido van Rossum在1989年圣诞节期间,为了打发无聊的圣诞节而编写的一个编程语言.全世界差不多有600多种编程语言,但流行的编程语言也就那么20来种.如果你听说过TIOB ...
- C语言函数篇(五)静态库和动态库的创建和使用
使用库函数是源码的一种保护??? <我猜的.> 库函数其实不是新鲜的东西,我们一直都在用,比如C库. 我们执行pringf() 这个函数的时候,就是调用C库的函数. 下面记录静态库和动态库 ...
- 笔记-scrapy-scarpyd
笔记-scrapy-scarpyd 1. scrapy部署 会写爬虫之后就是部署.管理爬虫了,下面讲一下如何部署scrapy爬虫. 现在使用较多的管理工具是Scrapyd. scrapyd是 ...
- com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 's.areaname' in 'field list'错误
在使用mybatis框架做查询的时候,出现了如下错误: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown colum ...
- 【Max Points on a Line 】cpp
题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...
- python学习笔记-参数带*
#!/usr/bin/python # -*- coding: utf-8 -*- def powersum (power,*args): #所有多余的参数都会作为一个元组存储在args中 s ...
- Linux 文本对比 diff 命令详解(整理)
diff 命令详解 1.概述 windows系统下面就有不错的文本对比工具可以使用,例如常用的Beyond Compare,WinMerge都是图形界面的比较工具而且使用非常方便,如果你仅仅是在win ...
- URAL 1684. Jack's Last Word ( KMP next函数应用 )
题意:问第二行的串能不能恰好分割成几个串,使得这几个串都是第一行串的前缀.如果是,输出No, 并输出这几个串,否则输出Yes. 这题是Special Judge,把两个串连接起来,中间用一个未出现过的 ...
- python 常见的错误类型 和 继承关系
BaseException +-- SystemExit #系统结束 +-- KeyboardInterrupt #键盘中断 ctrl+D +-- GeneratorExit #主动结束 +-- Ex ...