Google Code Jam Round 1A 2015 解题报告
题目链接:https://code.google.com/codejam/contest/4224486/
Problem A. Mushroom Monster
这题题意就是,有N个时间点,每个时间点,Kaylin可以吃掉一定数量的mushroom,Bartholomew可以放入任意数量的mushroom。
现在给出N个时间点分别有多少mushroom。
问:若Kaylin每个时间点可以吃任意数量的mushroom,那为了配合每个时间点的mushroom,Kaylin最少要吃掉多少蘑菇。
问:若Kaylin已恒定速度吃mushroom(即在每个时间点间吃的数量相同,若盘子空了则暂停进食),那为了配合每个时间点的mushroom,Kaylin最少要吃掉多少蘑菇。
看懂题目就是水题,第一问,只要吃掉下一个时间点相对于当前时间点减少的mushroom数量。
第二问,只要保证吃的速度要大于等于所有时间点mushroom减少的数量,即取需求速度最大值。
代码:
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <numeric>
#include <queue>
using namespace std; const int MAXN = ;
const int INF = 0x3f3f3f3f; int a[MAXN], maxr[MAXN];
int T, n; int solve1() {
int res = ;
for(int i = ; i < n - ; ++i)
res += max(, a[i] - a[i + ]);
return res;
} int solve2() {
int spd = ;
for(int i = ; i < n - ; ++i)
spd = max(spd, a[i] - a[i + ]);
int res = ;
for(int i = ; i < n - ; ++i)
res += min(a[i], spd);
return res;
} int main() {
freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
scanf("%d", &T);
for(int t = ; t <= T; ++t) {
scanf("%d", &n);
for(int i = ; i < n; ++i)
scanf("%d", &a[i]);
maxr[n] = ;
for(int i = n - ; i >= ; --i)
maxr[i] = max(maxr[i + ], a[i]);
printf("Case #%d: %d %d\n", t, solve1(), solve2());
}
}
Problem B. Haircut
题目大意:已知有B个理发师,和B个理发师给任意一个人理发所需要的时间。现在有N个人在排队,若处于队首,会找当前不是正在理发的编号最小的理发师理发。
问:处于队列第N个的人会找第几个理发师。
思路:二分时间 time,计算在time个单位时间里,有sum人已经或正在理发,又有cnt个理发师恰好没有正在理发的人。
那么,若sum + cnt ≥ N,那么第N个人在前 time 个单位时间里,一定有机会开始理发。
如此二分可以得到第N个人开始理发的时间,再由上述的sum、cnt可以得到第N个人让第几个理发师理发。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL; const int MAXN = ; int a[MAXN];
int T, n, b; bool check(LL k) {
LL sum = , cnt = ;
for(int i = ; i < b; ++i) {
cnt += (k % a[i] == );
sum += (k - ) / a[i] + ;
}
return sum + cnt >= n;
} int solve() {
LL l = , r = LL(n) * *max_element(a, a + b);//1000000;//
while(l < r) {
LL mid = (l + r) >> ;
if(!check(mid)) l = mid + ;
else r = mid;
}
LL sum = , p = ;
for(int i = ; i < b; ++i)
sum += (l - ) / a[i] + ;
for(int i = ; i < b; ++i)
if(l % a[i] == && sum + ++p == n) return i + ;
return -;
} int main() {
freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
scanf("%d", &T);
for(int t = ; t <= T; ++t) {
scanf("%d%d", &b, &n);
for(int i = ; i < b; ++i)
scanf("%d", &a[i]);
printf("Case #%d: %d\n", t, solve());
}
}
Problem C. Logging
给N个点,分别问对于每个点来说,至少删掉多少个点,能使该点在剩下的点的凸包的边上。
思路1:因为凸包的边的性质是:对于一条边,每个点都在其同侧。那么,O(n^2)枚举所有边,O(n)枚举所有点,看要删掉那些点。复杂度O(n^3),可以跑过小数据,牛叉的电脑可以过大数据(RMB玩家与贫民玩家的区别就体现在这里了!)
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long LL; const int MAXN = ; struct Point {
int x, y, id;
Point() {}
Point(int x, int y): x(x), y(y) {}
void read(int i) {
id = i;
scanf("%d%d", &x, &y);
}
Point operator - (const Point &rhs) const {
return Point(x - rhs.x, y - rhs.y);
}
bool operator < (const Point &rhs) const {
if(y != rhs.y) return y < rhs.y;
return x < rhs.x;
}
}; LL cross(const Point &a, const Point &b) {
return (LL)a.x * b.y - (LL)a.y * b.x;
}
//ret >= 0 means turn right
LL cross(const Point &op, const Point &sp, const Point &ed) {
return cross(sp - op, ed - op);
} Point p[MAXN];
int ans[MAXN];
int n, top, T; void update_min(int &a, int b) {
if(a > b) a = b;
} void solve() {
for(int i = ; i < n; ++i) for(int j = i + ; j < n; ++j) {
int lsum = , rsum = ;
for(int k = ; k < n; ++k) {
LL t = cross(p[i], p[j], p[k]);
if(t > ) lsum++;
if(t < ) rsum++;
}
update_min(ans[i], min(lsum, rsum));
update_min(ans[j], min(lsum, rsum));
}
} int main() {
freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
scanf("%d", &T);
for(int t = ; t <= T; ++t) {
scanf("%d", &n);
for(int i = ; i < n; ++i)
p[i].read(i), ans[i] = n - ;
solve();
printf("Case #%d:\n", t);
for(int i = ; i < n; ++i)
printf("%d\n", ans[i]);
}
}
思路2:枚举每一个点,其他点绕枚举点排序,用一条过枚举点的直线旋转,计算要删掉的最少点。复杂度O(n^2logn)。
代码:
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long LL; struct Point {
int x, y;
Point() {}
Point(int x, int y): x(x), y(y) {}
Point operator - (const Point &rhs) const {
return Point(x - rhs.x, y - rhs.y);
}
double ang() const {
return atan2(y, x);
}
bool type() const {
if(y != ) return y > ;
return x < ;
}
}; LL cross(const Point &a, const Point &b) {
return (LL)a.x * b.y - (LL)a.y * b.x;
} const int MAXN = ; Point p[MAXN], v[MAXN << ];
int n, T; bool cmp(const Point &a, const Point &b) {
if(a.type() != b.type()) return a.type() < b.type();
return cross(a, b) > ;
} void solve(int n) {
for(int i = ; i < n; ++i)
v[i - ] = p[i] - p[];
n--;
sort(v, v + n, cmp);
copy(v, v + n, v + n);
int res = ;
for(int i = , j = ; i < n; ++i) {
if(i == j) j++;
while(j < i + n && cross(v[i], v[j]) >= ) ++j;
res = max(res, j - i);
}
printf("%d\n", n - res);
} int main() {
freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
scanf("%d", &T);
for(int t = ; t <= T; ++t) {
scanf("%d", &n);
for(int i = ; i < n; ++i)
scanf("%d%d", &p[i].x, &p[i].y);
printf("Case #%d:\n", t);
for(int i = ; i < n; ++i) {
swap(p[], p[i]);
solve(n);
swap(p[], p[i]);
}
}
}
Google Code Jam Round 1A 2015 解题报告的更多相关文章
- Google Code Jam Round 1A 2015 Problem B. Haircut 二分
Problem You are waiting in a long line to get a haircut at a trendy barber shop. The shop has B barb ...
- Google Code Jam Round 1C 2015 Problem A. Brattleship
Problem You're about to play a simplified "battleship" game with your little brother. The ...
- [Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product
Problem A. Minimum Scalar Product This contest is open for practice. You can try every problem as ...
- 【二分答案】Google Code Jam Round 1A 2018
题意:有R个机器人,去买B件商品,有C个收银员,每个收银员有能处理的商品数量上限mi,处理单件商品所需的时间si,以及最后的装袋时间pi. 每个收银员最多只能对应一个机器人,每个机器人也最多只能对应一 ...
- 【贪心】Google Code Jam Round 1A 2018 Waffle Choppers
题意:给你一个矩阵,有些点是黑的,让你横切h刀,纵切v刀,问你是否能让切出的所有子矩阵的黑点数量相等. 设黑点总数为sum,sum必须能整除(h+1),进而sum/(h+1)必须能整除(v+1). 先 ...
- [C++]Store Credit——Google Code Jam Qualification Round Africa 2010
Google Code Jam Qualification Round Africa 2010 的第一题,很简单. Problem You receive a credit C at a local ...
- Google Code Jam Africa 2010 Qualification Round Problem B. Reverse Words
Google Code Jam Africa 2010 Qualification Round Problem B. Reverse Words https://code.google.com/cod ...
- Google Code Jam Africa 2010 Qualification Round Problem A. Store Credit
Google Code Jam Qualification Round Africa 2010 Problem A. Store Credit https://code.google.com/code ...
- Google Code Jam 2010 Round 1C Problem A. Rope Intranet
Google Code Jam 2010 Round 1C Problem A. Rope Intranet https://code.google.com/codejam/contest/61910 ...
随机推荐
- 2016中国大学生程序设计竞赛 - 网络选拔赛 J. Alice and Bob
Alice and Bob Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- 《Invert》开发日志04:工具、资源和服务
这篇记录一下<Invert>用到的工具.资源和服务.秉承两个原则:一,绝不侵犯版权:二,尽量节省开支. 首先是工具.游戏引擎使用免费的Unity个人版: 编码IDE使用免费的VisualS ...
- 2014亚马逊在线笔试题目及解决方案(MMChess问题)
整体思路:关键是需要知道当前Steps数组中的全排列即可,而且需要不重复的全排列.即关键在于非递归的全排列实现即可~ 其实直接利用STL中的next_permutation算法的,这里我又自己实现了一 ...
- grunt 检测js配置
module.exports = function(grunt) { // 项目配置 grunt.initConfig({ pkg: grunt.file.readJSON('package.json ...
- C#自定义控件属性显示在属性面板中操作
private Color controleColor; [Browsable(true)] [Description("控件颜色"), Category("自定义&qu ...
- mongodb用子文档做为查询条件的两种方法
{ "_id": ObjectId("52fc6617e97feebe05000000"), "age": 28, "level& ...
- 阿里云SLB双机IIS多站点负载均衡部署笔记
首先SLB是通过局域网与ECS链接 ECS1服务器 test文件夹增加index.html test1文件夹增加index.html 设置ECS1服务器(130)IIS test站点 设置test主机 ...
- cookie小析
cookie用于存储数据,当用户访问了某个网站(网页)时,我们就可以通过cookie来向访问者电脑上存储数据1.不同的浏览器存放的cookie位置不同,也不能通用2.cookie的存储是以域名的形式进 ...
- mysql锁 实战测试代码
存储引擎 支持的锁定 MyISAM 表级锁 MEMORY 表级锁 InnoDB 行级锁 BDB 页面锁 表级锁:开销小,加锁快:不会出现死锁:锁定粒度大,发生锁冲突的概率最高,并发度最低.行级锁:开销 ...
- windows php 5.5 执行exe 不是有效的win32程序
双击运行php-cgi.exe弹出对话框提示不是有效的win32应用程序.此为版本问题,PHP5.5版本 最低要运行于操作系统版本号最低要6.0 ,而WINDOWS 2003 系统为5.2 因此无法运 ...