Codeforces Round #379 (Div. 2) 总结分享
第一次Virtual participation,contest时只ac了A、B两道,感觉这两道题没什么好说的,直接从C题开始。
C. Anton and Making Potions
题意
制作n瓶药水,初始时每制作一瓶花费x秒,有两类法术,第一类(包含m个法术)使制作每瓶药的时间由x变为a[i] (a[i] < x) 并消耗b[i]点法力值,第二种(k个法术)能瞬间制造c[i]瓶并消耗d[i]点法力值,初始法力值为s,最多在两种类型中分别选一个法术来加快进度。求制作这n瓶药水最少花费的时间。
分析
暴力法肯定爆时间O(m*k),所以没敢用。之后看Tutorial,第一类就暴力,第二类在第一类的基础上二分查找,这样O(m*log2k)就不会爆了。
代码
#include <bits/stdc++.h> using namespace std; const int max_n = ; int n, m, k;
int x, s;
int a[max_n], b[max_n], c[max_n], d[max_n]; inline int max_complete(int mana_left)
{
int l = , r = k;
while (l < r)
{
int m = (l + r + ) / ;
if (d[m] <= mana_left) l = m; else r = m-;
}
return c[l];
} int main()
{
cin >> n >> m >> k;
cin >> x >> s;
a[] = x;
b[] = ;
c[] = ;
d[] = ;
for (int i = ; i <= m; i++) cin >> a[i];
for (int i = ; i <= m; i++) cin >> b[i];
for (int i = ; i <= k; i++) cin >> c[i];
for (int i = ; i <= k; i++) cin >> d[i];
long long ans = 1LL * n * x;
for (int i = ; i <= m; i++)
{
int mana_left= s - b[i];
if (mana_left< ) continue;
ans = min(ans, 1LL * (n - max_complete(mana_left)) * a[i]);
}
cout << ans << endl;
return ;
}
Unfold Code
总结
虽然最大只有2e5,内存足够的情况下,max_n稍大一点更好;
abcd[0]存放特殊元素,好处是使后续操作统一化,不必写额外语句去判断特殊情况;
数字的LL后缀代表这是一个long long型常量,1LL*int 将int变成long long;
二分法查找近似数(如:找到比给定值小的最大元素),这里的第二类本身是有序的(非递减的),适用二分查找法;
D. Anton and Chess
题意
一个无限大的棋盘,一个白王和n个黑色棋子(车、象、皇三种),判断白王是否被将军了。车走十字,象走对角线,皇既可走十字,又可走对角线。
分析
在王的8个方向中,判断每个方向离王最近的黑棋能否将军即可。知道王的坐标,8个方向可以用函数表示出来,分别为y=y0,x=x0,y=x+y0-x0,y=-x+y0+x0。黑棋坐标代进去就知道是不是在这8个方向上,然后保存各个方向离白王最近的黑棋,再判断这8个黑棋能否将军。
代码
#include <bits/stdc++.h>
using namespace std;
const int max_n = ;
typedef struct {
char type;
int x;
int y;
}piece;
int n, X, Y;
piece a[max_n], near[];
int main()
{
cin >> n;
cin >> X >> Y; // white king
for(int i=; i<n; i++) {
cin >> a[i].type >> a[i].x >> a[i].y;
}
for(int i=; i<n; i++) {
if(a[i].x == X) {
if(a[i].y > Y) {
if(near[].type== || a[i].y-Y < near[].y-Y) {
near[] = a[i];
}
} else {
if(near[].type== || a[i].y-Y > near[].y-Y) {
near[] = a[i];
}
}
} else if(a[i].y == Y) {
if(a[i].x > X) {
if(near[].type== || a[i].x-X < near[].x-X) {
near[] = a[i];
}
} else {
if(near[].type== || a[i].x-X > near[].x-X) {
near[] = a[i];
}
}
} else if(a[i].y == a[i].x + Y - X) {
if(a[i].x > X) {
if(near[].type== || a[i].x-X < near[].x-X) {
near[] = a[i];
}
} else {
if(near[].type== || a[i].x-X > near[].x-X) {
near[] = a[i];
}
}
} else if(a[i].y == - * a[i].x + Y + X){
if(a[i].x > X) {
if(near[].type== || a[i].x-X < near[].x-X) {
near[] = a[i];
}
} else {
if(near[].type== || a[i].x-X > near[].x-X) {
near[] = a[i];
}
}
}
}
for(int i=; i<=; i+=) {
if(near[i].type==) continue;
if(near[i].type == 'Q' || near[i].type == 'R') {
cout << "YES";
return ;
}
}
for(int i=; i<=; i+=) {
if(near[i].type==) continue;
if(near[i].type == 'Q' || near[i].type == 'B') {
cout << "YES";
return ;
}
}
cout << "NO";
return ;
}
Unfold Code
E. Anton and Tree
题意
一个无环无向图(acyclic undirected gragh),有白和黑两种颜色的顶点,一次选择一个顶点,使与其连通的相同颜色的顶点一起变为另一种颜色。欲使所有顶点变成同一种颜色,求最少执行几次操作?
分析
既然同一块连通的颜色一起改变,那就可以把这样的一块压缩成一个点;可以用一个线性表来储存原图顶点与压缩图顶点的对应编号,①.每次选一个点dfs,确定出一个压缩点,也就是说:所有点访问一遍就能确定每个原图顶点与压缩图顶点的对应编号(此时还没形成压缩图的边);②.然后再遍历一次原图,通过比较邻接点的颜色与自身是否相同,来确定哪些压缩点之间是存在边的。 通过这两个操作就能构建一个压缩顶点图,然后再找到这个无环无向图的最长路径/2就是结果(压缩后的图是黑白相间的,随便画一条出来看看就知道了);关键是如何找到这条最长路径,其实很简单,先随便找一个点v,找到离点v距离最远的点s,则s必定是最长路径两端点的其中之一,这里可用反证法证明(设最长路另一端点为t,假设存在一个不是最长路端点s'的点到v的距离大于s,则最长路将会是s'---t,而不是s---t,矛盾);再由s找到距离最远的点t,这个距离就是最长路径长度,使用这种方法只需要遍历图两次。
代码
#include <bits/stdc++.h> using namespace std; int n, cn, diameter=, s;
vector < vector<int> > g;
vector < vector<int> > cg;
vector <int> color;
vector <bool> viewed;
vector <int> comp; void comp_dfs(int v, int col) {
comp[v] = cn;
viewed[v] = true;
int len = g[v].size();
for(int i=; i<len; i++) {
if(viewed[g[v][i]] || color[g[v][i]]!=col) continue;
comp_dfs(g[v][i], col);
}
} void dfs(int v, int d) {
viewed[v] = true;
int len = cg[v].size();
for(int i=; i<len; i++) {
if(viewed[cg[v][i]]) continue;
dfs(cg[v][i],d+);
}
if(d>diameter) {
diameter = d;
s = v;
}
} int main()
{
std::ios::sync_with_stdio(false);
cin >> n;
g.resize(n);
color.resize(n);
for(int i=; i<n; i++) {
cin >> color[i];
}
viewed.resize(n);
viewed.assign(n,false);
for(int i=; i<n-; i++) {
int v1, v2;
cin >> v1 >> v2;
v1--;
v2--;
g[v1].push_back(v2);
g[v2].push_back(v1);
}
comp.resize(n);
cg.resize(n);
// compress
for(int i=; i<n; i++) {
if(!viewed[i]) {
comp_dfs(i,color[i]);
cn++;
}
}
// link
for(int i=; i<n; i++) {
int len = g[i].size();
for(int j=; j<len; j++) {
if(color[i] != color[g[i][j]]) {
cg[comp[i]].push_back(comp[g[i][j]]);
}
}
}
viewed.resize(cn);
viewed.assign(cn,false);
dfs(,);
viewed.assign(cn,false);
dfs(s,);
cout << (diameter)/;
return ;
}
Unfold Code
总结
用vector代替链表来构建邻接表更简单(C++ STL);
comp_dsf只是将最初传入的顶点与其连通的同色点压缩成一个点(赋予相同的压缩后编号),并没有实际建立压缩后的边;
建立边:压缩完所有块后,再遍历原图,颜色不同的相邻点之间必定存在边;
Codeforces Round #379 (Div. 2) 总结分享的更多相关文章
- Codeforces Round #379 (Div. 2) Analyses By Team:Red & Black
A.Anton and Danik Problems: 给你长度为N的,只含'A','D'的序列,统计并输出何者出现的较多,相同为"Friendship" Analysis: lu ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 直径
E. Anton and Tree 题目连接: http://codeforces.com/contest/734/problem/E Description Anton is growing a t ...
- Codeforces Round #379 (Div. 2) D. Anton and Chess 水题
D. Anton and Chess 题目连接: http://codeforces.com/contest/734/problem/D Description Anton likes to play ...
- Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分
C. Anton and Making Potions 题目连接: http://codeforces.com/contest/734/problem/C Description Anton is p ...
- Codeforces Round #379 (Div. 2) B. Anton and Digits 水题
B. Anton and Digits 题目连接: http://codeforces.com/contest/734/problem/B Description Recently Anton fou ...
- Codeforces Round #379 (Div. 2) A. Anton and Danik 水题
A. Anton and Danik 题目连接: http://codeforces.com/contest/734/problem/A Description Anton likes to play ...
- Codeforces Round #379 (Div. 2) D. Anton and Chess 模拟
题目链接: http://codeforces.com/contest/734/problem/D D. Anton and Chess time limit per test4 secondsmem ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路
题目链接:http://codeforces.com/contest/734/problem/E E. Anton and Tree time limit per test 3 seconds mem ...
- Codeforces Round #379 (Div. 2) D. Anton and Chess —— 基础题
题目链接:http://codeforces.com/contest/734/problem/D D. Anton and Chess time limit per test 4 seconds me ...
随机推荐
- java核心知识点学习----并发和并行的区别,进程和线程的区别,如何创建线程和线程的四种状态,什么是线程计时器
多线程并发就像是内功,框架都像是外功,内功不足,外功也难得精要. 1.进程和线程的区别 一个程序至少有一个进程,一个进程至少有一个线程. 用工厂来比喻就是,一个工厂可以生产不同种类的产品,操作系统就是 ...
- java核心知识点学习----重点学习线程池ThreadPool
线程池是多线程学习中需要重点掌握的. 系统启动一个新线程的成本是比较高的,因为它涉及与操作系统交互.在这种情形下,使用线程池可以很好的提高性能,尤其是当程序中需要创建大量生存期很短暂的线程时,更应该考 ...
- js取最小最大值--Math.min()、math.max()
一.Math.min() 返回一组表达式中最小者 eg: var n = Math.min( 2 , 30 ,1 , 200-10 , 300*22 , 20-30 ); alert(n); //打印 ...
- Python学习笔记第七天(列表解析)
列表解析: 根据已有列表,高效生成列表的方式,它的执行效率要快很多,比for循环的速度要快很多 示例如下: 生成器表达式 生成器表达式并不真正创建数字列表,而是返回一个生成器对象,此对象在每次计算出一 ...
- u盘安装ubuntu
安装ubuntu 准备工作:首先需要安装好UltraISO(版本要比较新,有些老版本有照做后不能安装的错误) 准备好一个U盘. 下载好ubuntu光盘.(我用的12.04版) 步骤: 1.制作安装U盘 ...
- Eclipse的安装和java环境变量的设置
首先准备工作是要下载好Eclipse和java JDK. 必须要注意的是,Eclipse和java JDK必须下载同一位数的版本,即64位同为64位,32位同为32位.否则在安装完成运行Eclipse ...
- lab 7 函数超级多的类
#include<iostream>#include<string>#include<cmath>using namespace std; class Ration ...
- 存储构造题(Print Check)
连接:Print Check 题意:n行m列的矩阵,有k次涂色,每一涂一行或者一列,求最后的涂色结果. 从数据的大小看,暴力肯定要TLE: 问题是如何存储数据. 首先:我们只要最后的涂色结果. 其次: ...
- Failed to execute goal on project MakeFriends: Could not resolve dependencie The POM for .chengpai.jtd:jtd-service-api:jar:1.0-SNAPSHOT is missing, no dependency information available
本笔者在学习maven的基础,然后建立了一个maven的项目,然后想对其进行依赖操作,pom.xml进行依赖操作时候出现了这样的错误,说是找不到这个依赖的包,但是事实上已经导入了这个包. 同时,也在m ...
- TCP/IP协议基础(转)
转自 http://www.chinaunix.net 作者:Bernardus160 发表于:2003-12-03 17:33:15 TCP/IP协议基础 -------------------- ...