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)的更多相关文章

  1. 【Kickstart】2017 Round (Practice ~ G)

    Practice Round Problem A Country Leader (4pt/7pt) Problem B Vote (5pt/8pt) Problem C Sherlock and Pa ...

  2. 【转载】2018 hosts 持续更新访问 gu歌【更新于:2018-05-03】

      修改HOSTS实现免费,简单访问谷歌的目的   也是比较稳定的方法.修改hosts.修改hosts的方法,原理在于直接存储谷歌网站的IP地址.这样就不用DNS来解析网址了.也就是说,当我们输入谷歌 ...

  3. 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 ...

  4. 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 ...

  5. 【GDOI】2018题目及题解(未写完)

    我的游记:https://www.cnblogs.com/huangzihaoal/p/11154228.html DAY1 题目 T1 农场 [题目描述] [输入] 第一行,一个整数n. 第二行,n ...

  6. 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】

    https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...

  7. 【枚举】【二分】Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) D. Resource Distribution

    题意:有两个服务要求被满足,服务S1要求x1数量的资源,S2要求x2数量的资源.有n个服务器来提供资源,第i台能提供a[i]的资源.当你选择一定数量的服务器来为某个服务提供资源后,资源需求会等量地分担 ...

  8. 【推导】【贪心】Codeforces Round #472 (rated, Div. 2, based on VK Cup 2018 Round 2) D. Riverside Curio

    题意:海平面每天高度会变化,一个人会在每天海平面的位置刻下一道痕迹(如果当前位置没有已经刻划过的痕迹),并且记录下当天比海平面高的痕迹有多少条,记为a[i].让你最小化每天比海平面低的痕迹条数之和. ...

  9. 【推导】Codeforces Round #472 (rated, Div. 2, based on VK Cup 2018 Round 2) B. Mystical Mosaic

    题意:给你一个棋盘的最终局面. 你的一次操作可以选择一些行和列,将它们的交叉点染黑,不能重复选择某行或者某列.问你是否能经过数次操作之后,达到目标局面. 就枚举所有黑点,如果该点行列都没被标记,就给它 ...

随机推荐

  1. Spring Cloud Commons教程(三)忽略网络接口

    有时,忽略某些命名网络接口是有用的,因此可以将其从服务发现注册中排除(例如,在Docker容器中运行).可以设置正则表达式的列表,这将导致所需的网络接口被忽略.以下配置将忽略“docker0”接口和以 ...

  2. C/C++头文件的编写

    在C语言的学习过程中,我们一般把所有的代码写在一个文件中.随着自身水平的提高,我们发现代码越写越长,代码行数越来越多,把一个工程的所有代码写在一个文件中让人看起开非常吃力.于是我们开始想把代码中的函数 ...

  3. getAttribute和getParameter

    getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为Object对象类型 getParame ...

  4. 源码编译安装Apache/2.4.37-------踩了无数坑,重装了十几次服务器才会的,不容易啊!

    1.先进入/usr/local/中创建三个文件夹 apr apr-util apache cd /usr/local目录 mkdir apr mkdir apr-util mkdir apache 2 ...

  5. 词频分析 评论标签 nltp APP-分析买家评论的评分-高频词:二维关系

    0-定评论结果:好评.差评,1星.4星,二元化为“积极.消极”,取一元的数据为样本 1-得到词频结果:如手机类的“积极样本”得到前10的高频词:运行(run running ran).内存(memor ...

  6. 006-Spring Boot自动配置-Condition、Conditional、Spring提供的Conditional自动配置

    一.接口Condition.Conditional(原理) 主要提供一下方法 boolean matches(ConditionContext context, AnnotatedTypeMetada ...

  7. Delphi XE2 之 FireMonkey 入门(24) - 数据绑定: TBindingsList: TBindExpression.Direction

    在学习 BindingSource 属性时, 可以让两个控件互为绑定源; TBindExpression 对应的功能是 Direction 属性. 先在窗体上添加 Edit1.Edit2.Bindin ...

  8. 《图解设计模式》读书笔记8-2 MEMENTO模式

    目录 Memento模式 示例代码 程序类图 代码 角色和类图 模式类图 角色 思路拓展 接口可见性 保存多少个Memento 划分Caretaker和Originator的意义 Memento模式 ...

  9. EditPlus配色方案

    找到配置文件:editplus_u.ini配置文件 [Options] Placement=2C00000002000000030000000083FFFF0083FFFFFFFFFFFFFFFFFF ...

  10. gitlab上传代码及报错总结

    将目录变成git可管理的仓库 git init 将文件添加到暂存区中 git add README.md 将文件提交到仓库 git commit -m "fisrt commit" ...