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 题解 直接 ...
随机推荐
- mysql8.0 忘记root密码
先打开一个cmd:net stop mysql //关闭mysql服务mysqld --shared-memory --skip-grant-tables//跳过登录密码在不关闭第一个CMD的情况下打 ...
- C语言基础篇(三) 指针
导航: 1.指针 2. 数组 3. 结构体,共用体 4. 内存分布图 5. 段错误分析 ----->x<------------->x<---- ...
- 笔记-python-lib-lxml
笔记-python-lib-lxml 1. lxml简介 lxml是一个实现解析网页文件的库,python中自带有解析库,但没有lxml方便好用. The lxml XML toolkit ...
- pocscan扫描框架的搭建
0x00 无意中看到了一篇文章 讲pocscan的搭建..就比较心动 决定自己也搭建一个这样的扫描平台 0x01 安装docker 用的是ubuntu yklin 16.04 x64的系统 在更新源之 ...
- linux环境下kettle部署(JDK安装配置,kettle安装配置,资源库配置,定时执行job)
一.部署准备 1.1 java安装(略) 1.2 JDK配置 1. 命令行键入“cd /etc”进入etc目录 2. 命令行键入“vi profile”打开profile文件 3. ...
- springmvc上传图片并显示--支持多图片上传
实现上传图片功能在Springmvc中很好实现.现在我将会展现完整例子. 开始需要在pom.xml加入几个jar,分别是: <dependency> <groupId>comm ...
- 解决NSTimer循环引用
NSTimer常见用法 @interface XXClass : NSObject - (void)start; - (void)stop; @end @implementation XXClass ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目10
2014-04-25 20:47 题目:分配一个二维数组,尽量减少malloc和free的使用次数,要求能用a[i][j]的方式访问数据. 解法:有篇文章讲了六种new delete二维数组的方式,其 ...
- Python全栈工程师(数值类型、运算符)
ParisGabriel Python 入门基础 python的应用领域: 1.系统运维 2.网络编程(如:网络爬虫,搜索引擎,服务器编程) 3.科学计算 4.航空领域(如:卫星, ...
- LDA和PCA降维的原理和区别
LDA算法的主要优点有: 在降维过程中可以使用类别的先验知识经验,而像PCA这样的无监督学习则无法使用类别先验知识. LDA在样本分类信息依赖均值而不是方差的时候,比PCA之类的算法较优. LDA算 ...