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入门基本知识
1. 什么是语言 语言是一个事物与另外一个事物沟通的介质. python则是人(程序员)与计算机沟通的介质. 2. 什么是编程 编程就是程序员将自己想要让计算机做的事情用编程语言翻译出来写到一系列的文 ...
- python scrapy实战糗事百科保存到json文件里
编写qsbk_spider.py爬虫文件 # -*- coding: utf-8 -*- import scrapy from qsbk.items import QsbkItem from scra ...
- 《Cracking the Coding Interview》——第18章:难题——题目13
2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...
- 一个初学者的辛酸路程-jQuery
前言: 主要概要: 1.HTML+CSS补充 2.DOM事件 3.jQuery示例 内容概要: 1.布局 代码如下 <!DOCTYPE html> <html lang=" ...
- 洛谷P1331海战
题目描述 在峰会期间,武装部队得处于高度戒备.警察将监视每一条大街,军队将保卫建筑物,领空将布满了F-2003飞机.此外,巡洋船只和舰队将被派去保护海岸线. 不幸的是因为种种原因,国防海军部仅有很少的 ...
- 线段树 (区间更新,区间查询) poj http://poj.org/problem?id=3468
题目链接 #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...
- C++ Primer 第3章 字符串、向量和数组
C++ Primer 第3章 字符串.向量和数组 C Primer 第3章 字符串向量和数组 1 命名空间的using声明 2 标准库类型string 3 标准库类型vector 4 迭代器介绍 5 ...
- Opencv3.0.0安装包
这个资源是Opencv3.0.0安装包,包括Windows软件包,Android软件包,IOS软件包,还有opencv的源代码:需要的下载吧. 点击下载
- Linux静态ip设置及一些网络设置
网络服务配置文件 /etc/sysconfig/network 网络接口配置文件 /etc/sysconfig/network-scripts/ifcfg-INTERFACE_NAME 修改IP永久生 ...
- mininet、floodlight在第一次SDN上机作业中出现的一些问题
mininet.floodlight在第一次SND上机作业中出现的一些问题 首先给出链接 VMware安装 mininet安装 floodlight安装及问题,各个版本Ubuntu SDN第一次上机作 ...