题解参考网上的答案,以及我自己的想法。

主要参考网站:http://codeforces.com/blog/entry/47181http://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 题解的更多相关文章

  1. 2017 google Round D APAC Test 题解

    首先说明一下:我只是用暴力过了4道题的小数据,就是简单的枚举,大数据都不会做!下面的题解,是我从网上搜到的解答以及查看排行榜上大神的答案得出来的. 首先贴一下主要的题解来源:http://codefo ...

  2. 喵哈哈村的魔法考试 Round #1 (Div.2) 题解

    喵哈哈村的魔法考试 Round #1 (Div.2) 题解 特别感谢出题人,qscqesze. 也特别感谢测题人Xiper和CS_LYJ1997. 没有他们的付出,就不会有这场比赛. A 喵哈哈村的魔 ...

  3. Codeforces Round #182 (Div. 1)题解【ABCD】

    Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...

  4. 喵哈哈村的魔法考试 Round #2 (Div.2) 题解

    喵哈哈村的魔法考试 Round #2 (Div.2) 题解 A.喵哈哈村的战争 题解: 这道题就是for一遍,统计每个村子的战斗力的和,然后统计哪个村子的战斗力和大一点就好了. 唯一的坑点,就是这道题 ...

  5. 中国2017 Google 开发者大会第一天简单回顾

    昨天有幸参加了中国2017 Google 开发者大会,在这第一天就收获满满,昨天太忙了,今天早晨来一起简单回顾一下,可以让没有参加的童鞋们感受一下现场的温度. 早早就来到了会议现场,外面看不出什么特别 ...

  6. Codeforces Round #608 (Div. 2) 题解

    目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...

  7. Codeforces Round #525 (Div. 2)题解

    Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...

  8. Codeforces Round #528 (Div. 2)题解

    Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...

  9. Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F

    Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...

随机推荐

  1. vertical-align:top在单词和中文的表现

    <ul> <li> <img src="../../saasdist_v2/images/staff-img.png" alt="" ...

  2. java 对视频和图片进行加密解密

    import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java. ...

  3. 项目androidAnt编译打包Android项目

    时间紧张,先记一笔,后续优化与完善. Ant编译打包Android项目 在Eclipse中对Android项目停止编译和打包如果项目比较大的话会比较慢,所以改为Ant工具来停止编译和打包 Ant环境配 ...

  4. MmSystem播放Wav格式声音

    //MmSystem播放Wav格式声音 //MmSystem 支持 *.wav声音格式 snd ->SoundRecorderuses MmSystem; //引用MmSystem//播放系统声 ...

  5. python列表删除重复元素的三种方法

    给定一个列表,要求删除列表中重复元素. listA = ['python','语','言','是','一','门','动','态','语','言'] 方法1,对列表调用排序,从末尾依次比较相邻两个元素 ...

  6. JAVA_Reflection

    package com.qf.reflection; import java.lang.reflect.Constructor; import java.lang.reflect.Field; imp ...

  7. 我的开发框架(WinForm)4

    日志模块 对于一个系统来说,日志模块是必不可少的,它能给后面系统的维护和bug的修复,带来极大的方便..net的日志模块有很多,比较流行的有Log4Net,NLog,还有微软企业库的日志模块,我采用的 ...

  8. Linux上安装Redmine

    安装基本的软件环境 # yum install zip unzip libyaml-devel zlib-devel curl-devel openssl-devel httpd-devel apr- ...

  9. [转]oracle odp.net 32位/64位版本的问题

    本文转自:http://www.cnblogs.com/yjmyzz/archive/2011/04/19/2020793.html 如果你的机器上安装了odp.net,且确信machine.conf ...

  10. 虚反矩阵指令pinv之应用

    pinv指令     在多数解的例子中,有时并不是仅要将其中一变数设定为零之解.为使整个系统得到最佳化,亦可利用pinv指令求得最小模组之合理解.pinv(A)又称为虚反矩阵(pseudoinvers ...