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 ...
随机推荐
- STREAMS流机制
STREAMS流机制 基本概念 STREAMS(流)是系统V提供的构造内核设备驱动程序和网络协议包的一种通用方法,对STREAMS进行讨论的目的是为了理解系统V的终端接口,I/O多路转接中poll(轮 ...
- 设置mysql 数据集为utf-8
To set the default to UTF-8, you want to add the following to my.cnf [client] default-character-set= ...
- PHP查看当前端口号
<?php echo "当前页面服务器IP地址为:"; echo $_SERVER["SERVER_ADDR"]; echo "<br / ...
- 20145225《Java程序设计》 第7周学习总结
20145225<Java程序设计> 第7周学习总结 教材学习内容总结 第十三章 时间与日期 13.1认识时间与日期 时间的度量:GMT.UT.TAI.UTC.Unix.epoch. 年历 ...
- 分布式日志2 用redis的队列写日志
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 0909 45作业one
1.编译原理学什么? 答: 初遇编译原理,我知道编译原理是计算机专业设置的一门重要的专业课程,主要是介绍编译程序构造的一般原理和基本方法.其内容大概包括语言和文法.词法分析.语法分析.语法制导翻译.中 ...
- BFS 或 同余模定理(poj 1426)
题目:Find The Multiple 题意:求给出的数的倍数,该倍数是只由 1与 0构成的10进制数. 思路:nonzero multiple 非零倍数 啊. 英语弱到爆炸,理解不了题意... ...
- Linux 修改IP地址
vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE="eth0" BOOTPROTO="static" #静 ...
- grunt-replace和grunt-include-replace问题
关于最近在做的项目要用到的grunt-replace和grunt-include-replace,百度上很多将grunt的压缩合并的教程,可是很少讲关于这两个插件的教程,不过官网上有教程,我就是按照官 ...
- 新机自动创建yum库
#!/bin/bash#for a newsystem to setup auto reposity YUM_DIR=/etc/yum.repos.dMOUNT_DIR=/mntDEV_DIR=&qu ...