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集合类之ArrayList
学习Java的集合类 (1)成员变量以及初始化 private static final int DEFAULT_CAPACITY = 10; private static final Object[ ...
- 01背包问题:POJ3624
背包问题是动态规划中的经典问题,而01背包问题是最基本的背包问题,也是最需要深刻理解的,否则何谈复杂的背包问题. POJ3624是一道纯粹的01背包问题,在此,加入新的要求:输出放入物品的方案. 我们 ...
- 自动化测试工具QTP和SilkTest横向PK(转)
转自:http://www.uml.org.cn/Test/201405212.asp?artid=1686 众所周知,自动化测试工具曾几何时三足鼎立,Mercury QTP/WinRunner系.I ...
- What he did
//记录组内成员具体完成工作情况,格式为:时间-事件-人物 2015-4-19 用户需求分析---主要由韩林编写,国旗,李春伟辅助编写,国旗发布文档至博客 2015-4-22 快速原型---由郭龙东完 ...
- 【erlang】执行linux命令的两种方法
os.cmd(Cmd) os模块提供了cmd函数可以执行linux系统shell命令(也可以执行windows命令).返回一个Cmd命令的标准输出字符串结果.例如在linux系统中执行os:cmd(& ...
- 1044. Shopping in Mars (25)
分析: 考察二分,简单模拟会超时,优化后时间正好,但二分速度快些,注意以下几点: (1):如果一个序列D1 ... Dn,如果我们计算Di到Dj的和, 那么我们可以计算D1到Dj的和sum1,D1到D ...
- (Python)list的内建函数 filter(), map(), 和 reduce()
这一节,我们将主要学习用于list的三个内建函数: filter(), map(), 和 reduce(). 1.filter(function, sequence) 逐个从sequence中取一个 ...
- BZOJ4293 [PA2015]Siano(线段树)
传送门 这Seg确实不好写,不过因为它与ai的相对顺序无关,所以,我们在对ai排序之后,就可做了.维护一个区间最大值,维护一个和,维护一个区间赋值的懒标记,再维护一个时间变化的标记就可以了. 因为不论 ...
- 4.2.2 网络编程之Socket
1基于TCP协议的Socket 服务器端首先声明一个ServerSocket对象并且指定端口号,然后调用Serversocket的accept()方法接收客户端的数据.Accept()方法在没有数据进 ...
- sqlhelper sqlparameter 实现增删改查
这是sqlHelper.cs类,类内里封装了方法 using System; using System.Collections.Generic; using System.Linq; using Sy ...