2017 google Round C APAC Test 题解

时间:2023-09-02 17:26:55

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

主要参考网站: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 2017 google Round C APAC Test 题解 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的,最后一题,就需要一些做题的经验,加上自己的分析,分析简单的样例,得出结果。