2017 google Round C APAC Test 题解
题解参考网上的答案,以及我自己的想法。
主要参考网站:http://codeforces.com/blog/entry/47181,http://codeforces.com/blog/entry/47185。讲的都非常仔细,我建议看这个上面的题解,开拓思路,然后就是看排行榜上大神们的答案,当然可以直接看下面我的题解。
第一题
1.看懂题意很重要,如果理解了怎么计算,代码应该很快就写出来。maximum possible expected number,求最大期望个数,就是一个位置可以访问多次,根据单次捕获的概率p,则期望有:e = p + (1 - p) * p + (1 - p)^2 * p + ....分别对应访问第一次,第二次,第三次,第四次,把这些加起来就是期望。然后写一个简单的dfs就可以,因为step比较小,大数据也很快可以跑完。
/*
ID: y1197771
PROG: test
LANG: C++
*/
#include<bits/stdc++.h>
#define pb push_back
#define FOR(i, n) for (int i = 0; i < (int)n; ++i)
#define dbg(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl
typedef long long ll;
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1e3 + ;
bool e[][];
int vis[][];
int r, c, s, rs, cs;
double p, q;
double res;
int dx[] = {-, , , };
int dy[] = {, , , -};
void dfs(int x, int y, int step, double v) {
if(step == ) {
res = max(res, v);
return;
}
for (int i = ; i < ; i++) {
int cx = x + dx[i];
int cy = y + dy[i];
if(cx < || cx >= r || cy < || cy >= c) continue;
if(e[cx][cy]) {
vis[cx][cy]++;
double td = pow( - p, vis[cx][cy] - ) * p;
dfs(cx, cy, step - , v + td);
vis[cx][cy]--;
} else {
vis[cx][cy]++;
double td = pow( - q, vis[cx][cy] - ) * q;
dfs(cx, cy, step - , v + td);
vis[cx][cy]--;
} } }
void solve() {
cin >> r >> c >> rs >> cs >> s;
cin >> p >> q;
memset(e, , sizeof e);
memset(vis, , sizeof vis);
for (int i = ; i < r; i++) {
for (int j = ; j < c; j++) {
char ch; cin >> ch;
if(ch == 'A') e[i][j] = ;
}
}
res = ;
dfs(rs, cs, s, );
printf("%.7f\n", res);
}
int main() {
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
int _;
cin >> _;
for (int i = ; i <= _; i++) {
cout << "Case #" << i << ": ";
solve();
} return ;
}
2.读题,就是求全部为0的正方形的的个数,1代表有monster,然后这个问题跟maximize squarehttp://www.geeksforgeeks.org/maximum-size-sub-matrix-with-all-1s-in-a-binary-matrix/一样,这道题,我们用dp[i][j]来存储以i,j位置的小方块作为最右下角的正方形的个数,dp(i,j) represents the side length of the maximum square whose bottom right corner is the cell with index (i,j) in the original matrix. 递推公式跟前面这题是一样的,最后把所有的位置加起来即可。
递推公式:dp[i][j] = min(dp[i - 1][j], dp[i][j - 1], dp[i][j]) + 1, if monster[i][j] = 0; dp[i][j] = 0, if monster[i][j] = 1. res = sum dp[i][j], 0 <= i < r, 0 <= j < c.
复杂度:r*c = 3000 * 3000 = 9000000, 很快就跑完了。
3. 理解什么是assignment variable和 variable arguments, 就是左边和右边的变量, 然后解题办法很多,比较简单,就是看所有的顶点是否可以到达,首先解析字符串表达式,构建图,然后计算所有的点是否都可以到达。这题注意入度为0的顶点,也就是a = f();这类的,可以构造一个伪节点,给右边没有变量的表达式添加一条从伪节点到左边变量的边,最后就是bfs或者dfs遍历,看所有的点是否都可以从伪节点到达。
/*
ID: y1197771
PROG: test
LANG: C++
*/
#include<bits/stdc++.h>
#define pb push_back
#define FOR(i, n) for (int i = 0; i < (int)n; ++i)
#define dbg(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl
typedef long long ll;
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1e3 + ;
int n;
map<string, int> m;
int num;
int degree[maxn * ];
vector<int> e[maxn * ];
//set<int> e[maxn * 11];
int get(string s) {
if(m.count(s)) return m[s];
m[s] = num++;
e[num - ].clear();
return m[s];
}
set<int> se;
void work(string s) {
int t = s.find('=');
string s1 = s.substr(, t);
//cout << s1 << endl;
int head = get(s1);
se.insert(head);
int t1 = s.find('(');
s = s.substr(t1 + );
//cout << s << endl;
auto it = s.find(',');
while(it != string::npos) {
s1 = s.substr(, it);
//cout << s1 << endl;
int b = get(s1);
e[b].pb(head);
degree[head]++;
s = s.substr(it + );
it = s.find(',');
}
//cout << s << endl;
it = s.find(')');
s1 = s.substr(, it);
//cout << s1 << endl;
if(s1.size() == ) return;
int b = get(s1);
e[b].pb(head);
degree[head]++; }
void solve() {
cin >> n;
m.clear(); se.clear();
num = ;
memset(degree, , sizeof degree);
string str;
for (int i = ; i < n; i++) {
cin >> str;
work(str);
}
{
//for (auto i = m.begin(); i != m.end(); i++) {
// cout << i->first << " " << degree[i->second] << endl;
//}
}
queue<int> q;
for (int i = ; i < num; i++) {
if(degree[i] == ) {
q.push(i);
if(!se.count(i)) {
puts("BAD"); return;
}
}
}
while(!q.empty()) {
int u = q.front(); q.pop();
for (auto x : e[u]) {
degree[x]--;
if(degree[x] == ) q.push(x);
}
}
for (int i = ; i < num; i++) {
if(degree[i]) {
puts("BAD"); return;
}
}
puts("GOOD");
}
int main() {
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
int _;
cin >> _;
for (int i = ; i <= _; i++) {
cout << "Case #" << i << ": ";
solve();
}
return ;
}
我当时做的有点麻烦,搞成拓扑排序了,其实也是正确的。
4. 读完题意,想到排序,但是不知道怎么排序,这个题目应该属于博弈游戏,nim之类的先手必胜,先手必败之类的,但是需要仔细分析情况,我当时没有搞明白。
后来看题解,原来是这样。
下面是cf上面大神的讲解,讲的非常清楚,仔细,我认为分析的特别透彻。
The solution to problem D is actually deceptively simple. Let's say whoever picks last wins.
If there are no soldiers, then Alice loses, because she has no moves. Otherwise, let the highest attack of the soldiers be maxA and the highest defense be maxD. We have two cases:
- If there is a soldier with (Ai, Di) = (maxA, maxD), then Alice picks this soldier and wins immediately.
- Otherwise, the players will never pick any soldier with attack maxA or defense maxD. The reason for this is that, if one player picks a soldier with attack maxA, the other immediately picks any soldier with defense maxD and wins. Therefore, as no soldiers with attack maxA or defense maxD will ever be picked, we can simply delete these soldiers and start again.
The straightforward O(n2) implementation is good enough, but it should be possible to implement this in by sorting the soldiers first.
我看排行榜前几名的代码,感觉比较麻烦,也没去弄懂。这个比较简单。如果存在(maxA, maxD)的士兵,就是先手必胜。如果没有,然后就是单独最大的士兵,如果只有这些士兵,就是先手必败,后手必胜,可以直接返回0,如果还有其他的士兵,这些士兵决定了剩下的过程,删除掉前面的单独最大的士兵,然后重新开始,这时候就相当于重新开始一盘游戏,直接递归调用。
/*
ID: y1197771
PROG: test
LANG: C++
*/
#include<bits/stdc++.h>
#define pb push_back
#define FOR(i, n) for (int i = 0; i < (int)n; ++i)
#define dbg(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl
typedef long long ll;
using namespace std;
typedef pair<int, int> pii;
const int maxn = 4e3 + ;
bool in[maxn];
int p[maxn][];
int n;
int cnt = ;
bool can(int x, int y) {
//cout << x << " " << y << endl;
//cnt++;
//if(cnt > 5) return 0;
int mx = x, my = y;
bool f = ;
for (int i = ; i < n; i++) {
if(!in[i]) {
f = ;
mx = max(mx, p[i][]);
my = max(my, p[i][]);
}
}
if(!f) return false;
for (int i = ; i < n; i++) {
if(!in[i]) {
if(p[i][] == mx && p[i][] == my) {
return ;
}
}
}
f = ;
int id = ;
for (int i = ; i < n; i++) {
if(!in[i]) {
if(p[i][] == mx || p[i][] == my) continue;
id = i; f = ; break;
}
}
if(!f) return ;
for (int i = ; i < n; i++) {
if(!in[i]) {
if(p[i][] == mx || p[i][] == my)
in[i] = ;
}
}
return can(, );
}
void solve() {
cin >> n;
int x, y;
for (int i = ; i < n; i++) {
cin >> x >> y;
p[i][] = x, p[i][] = y;
in[i] = ;
} if(can(, )) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
//for (int i = 0; i < n; i++) {
// cout << i << " " << in[i] << endl;
//}
}
int main() {
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
int _; cin >> _;
for (int i = ; i <= _; i++) {
cout << "Case #" << i << ": ";
solve();
} return ;
}
上面是按照上面的思路写的代码,可以通过现在的大小数据,都是在1s内跑完的。代码写的比较丑,但是还是比较直接的。
总结:
这次比赛还是比较简单的,至少2,3题很容易,这2题的大小数据分数很容易拿到。第一题分析清楚期望,也是可以很快ac的,最后一题,就需要一些做题的经验,加上自己的分析,分析简单的样例,得出结果。
2017 google Round C APAC Test 题解的更多相关文章
- 2017 google Round D APAC Test 题解
首先说明一下:我只是用暴力过了4道题的小数据,就是简单的枚举,大数据都不会做!下面的题解,是我从网上搜到的解答以及查看排行榜上大神的答案得出来的. 首先贴一下主要的题解来源:http://codefo ...
- 喵哈哈村的魔法考试 Round #1 (Div.2) 题解
喵哈哈村的魔法考试 Round #1 (Div.2) 题解 特别感谢出题人,qscqesze. 也特别感谢测题人Xiper和CS_LYJ1997. 没有他们的付出,就不会有这场比赛. A 喵哈哈村的魔 ...
- Codeforces Round #182 (Div. 1)题解【ABCD】
Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...
- 喵哈哈村的魔法考试 Round #2 (Div.2) 题解
喵哈哈村的魔法考试 Round #2 (Div.2) 题解 A.喵哈哈村的战争 题解: 这道题就是for一遍,统计每个村子的战斗力的和,然后统计哪个村子的战斗力和大一点就好了. 唯一的坑点,就是这道题 ...
- 中国2017 Google 开发者大会第一天简单回顾
昨天有幸参加了中国2017 Google 开发者大会,在这第一天就收获满满,昨天太忙了,今天早晨来一起简单回顾一下,可以让没有参加的童鞋们感受一下现场的温度. 早早就来到了会议现场,外面看不出什么特别 ...
- 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 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...
随机推荐
- 搭建maven+spring+mybatis工程
一.maven 命令搭建web项目 可以参考我之前的一篇博文maven快速入门 1.搭建web工程 mvn archetype:generate -DgroupId=com.yuanmeng.spri ...
- btrace拓展工具-java应用性能诊断优化利器
Btrace是一个实时监控工具,可以无需修改应用代码(事实上它修改了字节码),来达到不可告人的秘密!这是性能调优和诊断的利器! 它可以获取应用程序代码的执行时间,他可以让你无需修改代码,帮你做时间的打 ...
- 【转】linux下cppunit的安装
以下内容来自:http://www.51testing.com/html/51/279751-170160.html 1. 安装 cppunit的下载地址为:http://sourceforge.ne ...
- 模板 树链剖分BFS版本
//点和线段树都从1开始 //边使用vector vector<int> G[maxn]; ],num[maxn],iii[maxn],b[maxn],a[maxn],top[maxn], ...
- Andropid自己定义组件-坐标具体解释
在做一个view背景特效的时候被坐标的各个获取方法搞晕了,几篇抄来抄去的博客也没弄非常清楚. 如今把整个总结一下. 事实上仅仅要把以下这张图看明确就没问题了. watermark/2/text/aHR ...
- android131 360 01 闪屏页和主页面
主界面: 软件升级流程: 清单文件: <?xml version="1.0" encoding="utf-8"?> <manifest xml ...
- mysql之sql语句细节问题汇总
一.mysql count distinct null 使用注意事项 1 用一个例子来讲解一个问题,现在又一个库表hello,表内容如下: id name 1 Null 2 ...
- 【转】使用Beaglebone Black的I2C (二)——使用C语言和i2c-dev驱动
在本博客的<使用Beaglebone Black的I2C(一)>中,介绍了BBB上无需编程对i2c总线进行读写操作的方法,本文将介绍如何在c语言程序中使用i2c-dev驱动来操作i2c设备 ...
- CMS漏洞
例1, discuz!后台弱口令/暴力破解 1.http://club.lenovo.com.cn/admin.php
- 学习jQuery后的部分总结
1.remove和empty <div id="div1"> <ul id="ul1"> <li>嘿嘿</li> ...