第一次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) 总结分享的更多相关文章

  1. Codeforces Round #379 (Div. 2) Analyses By Team:Red & Black

    A.Anton and Danik Problems: 给你长度为N的,只含'A','D'的序列,统计并输出何者出现的较多,相同为"Friendship" Analysis: lu ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. Codeforces Round #379 (Div. 2) B. Anton and Digits 水题

    B. Anton and Digits 题目连接: http://codeforces.com/contest/734/problem/B Description Recently Anton fou ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Java swing项目-图书管理系统(swing+mysql+jdbc)

    (一)项目功能分析 该项目是设计一个图书管理系统,主要包含的内容有: (1)管理员登陆界面 ->信息录入 ->登录 ->重置 (2)图书管理系统总界面 ->子界面菜单: 1)图 ...

  2. java io学习之File类

    1.先看下四个静态变量 static String pathSeparator The system-dependent path-separator character, represented a ...

  3. [转] mhvtl虚拟磁带库的安装与应用

    转自:candon123  -- http://candon123.blog.51cto.com/704299/388192/ 1.获取mhvtl: 官方网站:http://mhvtl.nimsa.u ...

  4. each处理json数据

    eg:给传进来的ID中当其对应的值为true时,即给对应的ID标签添加一个class 名为  focus,如: var obj = { id01:'true', id02:'flase', id03: ...

  5. XE随想4:SuperObject增、删、改

    unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

  6. iOS 16进制字符串转换成int十进制

    NSRange rangeErr; rangeErr.location = 6; rangeErr.length = 2; NSString *strings = [value substringWi ...

  7. C++变量的左值和右值

    变量和文字常量都有存储区,并且有相关的类型. 区别在于变量是寻址的,对于每一个变量,都有两个值与其相关联 1  它的数据值,存储在某个内存地址中.有时这个值也被称为对象的右值 文字常量和变量都可被用作 ...

  8. 苹果会在明后年推出13寸屏iPad吗?

    摘要:苹果推大屏iPad的传闻由来已久,近日有国外媒体再次撰文称,这种大屏iPad不仅是苹果Mac继任者,同时也是Surface的有利竞争者……这真的可能吗?这只是分析师的捕风捉影,还是真有这种可能? ...

  9. Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  10. 无需部署的轻量级数据库—SQLLite,使用Demo

    当有程序需要保存轻量数据,而又烦躁序列化到本地的不便,轻量级数据库—SQLLite是一个很好的选择,只需引用System.Data.SQLite.DLL,无需部署数据库,便可像拥有数据库一样保存数据, ...