【Kickstart】2018 Round (Practice ~ C)
Practice Round
Problem A GBus count (9pt/15pt) (2019年1月14日,kickstart群每日一题)
题意:有一条笔直的大路,上面有城市编号从 1 开始从左到右一次排列。上面有 N 个 GBuses, 每一个 bus[i] 连接 A[i] 到 B[i] 两个地点(包含这两个地方)。我们想要求 P 个城市,每个城市经过的公交车数量。
输入输出 和 数据规模 如下:
There exist some cities that are built along a straight road. The cities are numbered 1, 2, 3... from left to right.
There are N GBuses that operate along this road. For each GBus, we know the range of cities that it serves: the i-th gBus serves the cities with numbers between Ai and Bi, inclusive.
We are interested in a particular subset of P cities. For each of those cities, we need to find out how many GBuses serve that particular city.
Input
The first line of the input gives the number of test cases, T. Then, T test cases follow; each case is separated from the next by one blank line. (Notice that this is unusual for Kickstart data sets.)
In each test case:
The first line contains one integer N: the number of GBuses.
The second line contains 2N integers representing the ranges of cities that the buses serve, in the form A1 B1 A2 B2 A3 B3 ... AN BN. That is, the first GBus serves the cities numbered from A1 to B1 (inclusive), and so on.
The third line contains one integer P: the number of cities we are interested in, as described above. (Note that this is not necessarily the same as the total number of cities in the problem, which is not given.)
Finally, there are P more lines; the i-th of these contains the number Ci of a city we are interested in.
Output
For each test case, output one line containing Case #x: y, where x is the number of the test case (starting from 1), and y is a list of P integers, in which the i-th integer is the number of GBuses that serve city Ci. Limits
1 ≤ T ≤ 10.
Small dataset
1 ≤ N ≤ 50
1 ≤ Ai ≤ 500, for all i.
1 ≤ Bi ≤ 500, for all i.
1 ≤ Ci ≤ 500, for all i.
1 ≤ P ≤ 50.
Large dataset
1 ≤ N ≤ 500.
1 ≤ Ai ≤ 5000, for all i.
1 ≤ Bi ≤ 5000, for all i.
1 ≤ Ci ≤ 5000, for all i.
1 ≤ P ≤ 500.
题解:我们只需要把每个bus的起点和终点读进来,然后对于每一个城市,都去每个bus的区间里面查这个城市是不是在这个bus区间里面,是的话就加一。
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set> using namespace std; void print(vector<pair<int, int>>& bus) {
for (int i = ; i < bus.size(); ++i) {
printf("%d -> %d \n", bus[i].first, bus[i].second);
}
}
void solve(const int id) {
int n, p;
cin >> n;
vector<pair<int, int>> bus(n);
for (int i = ; i < n; ++i) {
int s, e;
cin >> s >> e;
if (s > e) {
swap(s, e);
}
bus[i] = make_pair(s, e);
}
// print(bus);
cin >> p;
vector<int> ans(p, );
for (int i = ; i < p; ++i) {
int city;
cin >> city;
for (auto b : bus) {
if (city >= b.first && city <= b.second) {
ans[i]++;
}
}
}
printf("Case #%d:", id);
for (auto e : ans) {
printf(" %d", e);
}
printf("\n");
return;
}
int main () {
int t;
cin >> t;
for (int idx = ; idx <= t; ++idx) {
solve(idx);
}
return ;
}
总监说线段树,我下周学习一下线段树==
Problem B Googol String (7pt/12pt) (2019年1月15日, kickstart群每日一题)
给了一个非常长的字符串,有一定的生成规则,问字符串的第 K 个字母是啥。
A "0/1 string" is a string in which every character is either 0 or 1. There are two operations that can be performed on a 0/1 string:
- switch: Every 0 becomes 1 and every 1 becomes 0. For example, "100" becomes "011".
- reverse: The string is reversed. For example, "100" becomes "001".
Consider this infinite sequence of 0/1 strings:
S0 = ""
S1 = "0"
S2 = "001"
S3 = "0010011"
S4 = "001001100011011"
...
SN = SN-1 + "0" + switch(reverse(SN-1)).
You need to figure out the Kth character of Sgoogol, where googol = 10^100.
Input
The first line of the input gives the number of test cases, T. Each of the next T lines contains a number K.
Output
For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the Kth character of Sgoogol.
Limits
1 ≤ T ≤ 100.
Small dataset
1 ≤ K ≤ 10^5.
Large dataset
1 ≤ K ≤ 10^18.
题解:小数据暴力可以解出来,大数据不行。大数据看了下答案学习了一下。整个字符串呈中心对称(字符串数组 1-based),2^k 的位置肯定是 0, 左右子串中心对称,并且需要 switch 0,1. 代码怎么写需要复习,一开始还没搞明白怎么写。明天复习。
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set> using namespace std;
typedef long long ll; int bst(ll k, int x);
int solve() {
ll k;
scanf("%lld", &k);
return bst(k, ); //2^60 > 1e18
}
// string is 1-based.
int bst(ll k, int x) {
ll sz = 1LL << x;
if (k == sz) { return ; }
if (k < sz) { //left substr
return bst(k, x-);
}
if (k > sz) { // right substr
return ^ bst(sz - (k - sz), x-); //return 1 - bst(sz - (k - sz), x-1);
}
return -;
}
int main () {
int t;
cin >> t;
for (int idx = ; idx <= t; ++idx) {
int ret = solve();
printf("Case #%d: %d\n", idx, ret);
}
return ;
}
Problem C Sort a scarmbled itinerary (11pt/15pt)
Problem D Sums of Sums (8pt/28pt)
Round A
Problem A Even Digits (8pt/15pt)
Problem B Lucky Dip (10pt/19pt) (2019年1月23日,kickstart群每日一题)
https://code.google.com/codejam/contest/9234486/dashboard#s=p1&a=1
袋子里面有 N 个物品,每个物品都有它的价值,每次从袋子里面取一个物品出来,有 K 次往回丢的机会。我们的目标是最大化物品价值的期望值。求最大期望。
Input
The input starts with one line containing one integer T: the number of test cases. T test cases follow.
Each test case consists of two lines. The first line consists of two integers N and K: the number of items in the bag, and the maximum number of times you may redip. The second line consists of N integers Vi, each representing the value of the i-th item.
Output
For each test case, output one line containing Case #x: y
, where x
is the test case number (starting from 1) and y
is the expected value described above. Your answer will be considered correct if it is within an absolute or relative error of 10-6 of the correct answer. See the FAQ for an explanation of what that means, and what formats of real numbers we accept.
Limits
1 ≤ T ≤ 100.
1 ≤ Vi ≤ 109.
1 ≤ N ≤ 20000.
Small dataset
0 ≤ K ≤ 1.
Large dataset
0 ≤ K ≤ 50000.
Input
5
4 0
1 2 3 4
3 1
1 10 1
3 15
80000 80000 80000
1 1
10
5 3
16 11 7 4 1
Output
Case #1: 2.500000
Case #2: 6.000000
Case #3: 80000.000000
Case #4: 10.000000
Case #5: 12.358400
题解:如果 k == 0, 那么期望值就是所有物品价值的平均值。如果 k > 0, 我们可以想像一下,对于第 i 次我们取出来的物品,如果当前物品的价值大于 i - 1 次的期望值,那么就就留下,如果小于第 i - 1次的期望值,那么就扔回去。
所以,期望的计算公式是 E[0] = sigma(vi), E[i] = sigma(Max(vi, E[i-1]))/N, 返回 E[k]. 暴力算法的时间复杂度是 O(NK)
本题可以用二分优化,先对物品价值进行排序,每次二分出来比E[i-1]大的区间,然后累加。
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map> using namespace std; void solve(int id) {
double res = 0.0;
int n, k;
cin >> n >> k;
int number; long long total = ;
unordered_map<int, int> freq;
for (int i = ; i < n; ++i) {
cin >> number;
total += (long long)number;
freq[number]++;
}
vector<double> e(k+, 0.0);
e[] = (double)total / n;
double t = ;
for (int i = ; i <= k; ++i) {
double t = 0.0;
for (auto p : freq) {
t += max((double)p.first, (double)e[i-]) * (double)p.second;
}
e[i] = t / (double)n;
}
res = e[k];
printf("Case #%d: %lf\n", id, res);
}
int main () {
int t;
cin >> t;
for (int idx = ; idx <= t; ++idx) {
solve(idx);
}
return ;
}
brute force
Problem C Scrambled Words (18pt/30pt)
Round B
Problem A No Line (7pt/13pt)
Problem B Sherlock and Bit Strings (11pt/26pt)
Problem C King's Circle (16pt/27pt)
Round C
链接:https://code.google.com/codejam/contest/4384486/dashboard
Problem A Planet Distance (10pt/15pt) (2019年1月29日,打卡群)
题意是说,给了一个无向图, 里面有个环,要求返回一个距离数组,环上的点在距离数组上都是 0, 不在环上的点,在距离数组上都是距离环的距离。
题解:如何找到一个无向图的环。用kahn算法的思想,bfs解法。先读入边,然后用邻接链表来存储,然后计算每个节点的度(无向图,度就是结点上的边数)。找到所有度为 1 的结点,把这些结点放进队列。然后把结点pop出队列的时候,相当于在图上把这个结点给删除了。所以,和这个结点相邻的结点的度也应该减一。遍历所有跟当前结点相邻的结点。如果相邻结点的边数还剩下一条并且它没有被访问过的话,就把这个相邻的结点也放进队列(变成要删除的的结点,相当于放进一个删除的缓冲区)。bfs的完毕之后,我们找到所有度不为0的结点,那么就是环上的结点了。
如何求剩下的结点距离环的距离。把所有环上的结点放进队列。然后遍历相邻结点计算距离(还是bfs,这个我会==)
时间复杂度是 O(N) 的。此外还有一个实现上的小问题,就是如果变量搞成全局变量的话,每次读新的test case,要注意清空啊==
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <unordered_set>
#include <unordered_map> using namespace std; int v;
unordered_map<int, vector<int>> edges;
vector<int> degree;
void getInput() {
edges.clear();
degree.clear();
cin >> v;
degree = vector<int>(v + , );
for (int i = ; i < v; ++i) {
int s, e;
cin >> s >> e;
edges[s].push_back(e);
degree[s]++;
edges[e].push_back(s);
degree[e]++;
}
} vector<int> planetDistance() {
queue<int> que;
vector<int> visited(v+, );
for (int i = ; i <= v; ++i) {
if (degree[i] == ) {
que.push(i);
degree[i]--;
visited[i] = ; //i has been visited
}
}
while (!que.empty()) {
int curNode = que.front(); que.pop();
for (auto adjNode : edges[curNode]) {
if (visited[adjNode] == && --degree[adjNode] == ) {
que.push(adjNode);
degree[adjNode]--;
visited[adjNode] = ;
}
}
}
vector<int> ret(v+, v+);
que = queue<int>(); //clear queue
for (int i = ; i <= v; ++i) {
if (degree[i] != ) {
que.push(i);
ret[i] = ; //node i is in the circle.
}
}
while (!que.empty()) {
int curNode = que.front(); que.pop();
for (auto adjNode : edges[curNode]) {
if (visited[adjNode] == ) {
ret[adjNode] = ret[curNode] + ;
visited[adjNode] = ;
que.push(adjNode);
}
}
}
vector<int> res(ret.begin() + , ret.end());
return res;
} void solve(const int id) {
getInput();
vector<int> res = planetDistance();
printf("Case #%d:", id);
for (auto r : res) {
cout << " " << r ;
}
cout << endl;
}
int main () {
int t;
cin >> t;
for (int idx = ; idx <= t; ++idx) {
solve(idx);
}
return ;
}
Problem B Fairies and Witches (15pt/21pt)
Problem C Kickstart Alarm (13pt/26pt)
【Kickstart】2018 Round (Practice ~ C)的更多相关文章
- 【Kickstart】2017 Round (Practice ~ G)
Practice Round Problem A Country Leader (4pt/7pt) Problem B Vote (5pt/8pt) Problem C Sherlock and Pa ...
- 【转载】2018 hosts 持续更新访问 gu歌【更新于:2018-05-03】
修改HOSTS实现免费,简单访问谷歌的目的 也是比较稳定的方法.修改hosts.修改hosts的方法,原理在于直接存储谷歌网站的IP地址.这样就不用DNS来解析网址了.也就是说,当我们输入谷歌 ...
- Codeforces 716A Crazy Computer 【模拟】 (Codeforces Round #372 (Div. 2))
A. Crazy Computer time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces 716B Complete the Word【模拟】 (Codeforces Round #372 (Div. 2))
B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 【GDOI】2018题目及题解(未写完)
我的游记:https://www.cnblogs.com/huangzihaoal/p/11154228.html DAY1 题目 T1 农场 [题目描述] [输入] 第一行,一个整数n. 第二行,n ...
- 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】
https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...
- 【枚举】【二分】Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) D. Resource Distribution
题意:有两个服务要求被满足,服务S1要求x1数量的资源,S2要求x2数量的资源.有n个服务器来提供资源,第i台能提供a[i]的资源.当你选择一定数量的服务器来为某个服务提供资源后,资源需求会等量地分担 ...
- 【推导】【贪心】Codeforces Round #472 (rated, Div. 2, based on VK Cup 2018 Round 2) D. Riverside Curio
题意:海平面每天高度会变化,一个人会在每天海平面的位置刻下一道痕迹(如果当前位置没有已经刻划过的痕迹),并且记录下当天比海平面高的痕迹有多少条,记为a[i].让你最小化每天比海平面低的痕迹条数之和. ...
- 【推导】Codeforces Round #472 (rated, Div. 2, based on VK Cup 2018 Round 2) B. Mystical Mosaic
题意:给你一个棋盘的最终局面. 你的一次操作可以选择一些行和列,将它们的交叉点染黑,不能重复选择某行或者某列.问你是否能经过数次操作之后,达到目标局面. 就枚举所有黑点,如果该点行列都没被标记,就给它 ...
随机推荐
- AT2370 Piling Up
https://www.luogu.org/jump/atcoder/2370 题解 答案不是\(2^{2m}\)因为每轮的第一次取球可能会不够. 我们可以设\(dp[i][j]\)表示到了第\(i\ ...
- Codeforces Round #603 (Div. 2) E. Editor
E. Editor 题目链接: https://codeforces.com/contest/1263/problem/E 题目大意: 输入一个字符串S1含有‘(’ , ‘)’ , ‘R’ , ‘L’ ...
- RabbitMQ幂等性概念(七)
幂等性是什么? 我们可以借鉴数据库的乐观锁机制 比如我们执行一条更新库存的sql语句update user set count=count-1,version=version+1 where vers ...
- CG-CTF | Hello,RE!
菜狗开始向着pwn与re进军了(●'◡'●)[说白了,还是在水博客吧] 按r出flag: galf leW{ emoc _oT_ W_ER dlro }! 反一下:fla ...
- Redis之Java客户端Jedis
导读 Redis不仅使用命令客户端来操作,而且可以使用程序客户端操作. 现在基本上主流的语言都有客户端支持,比如Java.C.C#.C++.php.Node.js.Go等. 在官方网站里列一些Java ...
- tp5关联模型进行条件查询
public function wordOne(){ return $this->hasOne('TeachWord','id','w_id')->field('id,pid,title' ...
- java虚拟机规范-运行时栈帧
前言 java虚拟机是java跨平台的基石,本文的描述以jdk7.0为准,其他版本可能会有一些微调. 引用 java虚拟机规范 java虚拟机规范-运行时数据区 java内存运行时的栈帧结构 java ...
- EDM营销必知:电子邮件打开和点击的几组数据
在EDM营销中,了解一下电子邮件何时被打开和点击很重要.这有助于我们在合适的时间发送邮件出去,从而带来最大化的效果. 1.邮件打开的最高峰在早上8点至9点之间,其次是下午三点到四点.因此,在这个时间发 ...
- fpython-笔记(五)装饰器、匿名函数
一.装饰器 装饰器,这个器就是函数的意思,连起来,就是装饰函数,装饰器本身也是一个函数,它的作用是用来给其他函数添加新功能,比如说,我以前写了很多代码,系统已经上线了,但是性能比较不好,现在想把程序里 ...
- 红帽虚拟化RHEV-PXE批量安装RHEV-H
目录 目录 前言 软件环境 前提 部署 PXE 环境 使用 yum 安装所需软件 配置 DHCP 配置 TFTP-Server 配置 vsftpd 服务用于提供安装系统所需软件包 安装 kicksta ...