Codeforces Round #340 (Div. 2)题解
来源:互联网 发布:淘宝运营书籍推荐2016 编辑:程序博客网 时间:2024/05/12 23:15
A. Elephant
An elephant decided to visit his friend. It turned out that the elephant’s house is located at point 0 and his friend’s house is located at point x(x > 0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine, what is the minimum number of steps he need to make in order to get to his friend’s house.
Input
The first line of the input contains an integer x (1 ≤ x ≤ 1 000 000) — The coordinate of the friend’s house.
Output
Print the minimum number of steps that elephant needs to make to get from point 0 to point x.
Sample test(s)
Input
5
Output
1
Input
12
Output
3
Note
In the first sample the elephant needs to make one step of length 5 to reach the point x.
In the second sample the elephant can get to point x if he moves by 3, 5 and 4. There are other ways to get the optimal answer but the elephant cannot reach x in less than three moves.
题意:大象去看它的朋友,大象的家在数轴的原点,朋友的家在x(x>0)的地方,大象一次可以前进1, 2, 3, 4, 5,问大象到达朋友家需要的最少移动次数
简单水题。由于是让求最少的移动次数,所以每次应尽可能走的多,但还不能超过x
#include <iostream>#include <iomanip>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <vector>#include <algorithm>#define N 55const int mm = 1000000007;using namespace std;int main(){#ifndef ONLINE_JUDGE// freopen("1.txt", "r", stdin);#endif ios::sync_with_stdio(false); cin.tie(0); int x, ans; cin >> x; ans = x/5; x %= 5; ans += x/4; x %= 4; ans += x/3; x %= 3; ans += x/2; x %= 2; ans += x; cout << ans; return 0;}
B. Chocolate
Bob loves everything sweet. His favorite chocolate bar consists of pieces, each piece may contain a nut. Bob wants to break the bar of chocolate into multiple pieces so that each part would contain exactly one nut and any break line goes between two adjacent pieces.
You are asked to calculate the number of ways he can do it. Two ways to break chocolate are considered distinct if one of them contains a break between some two adjacent pieces and the other one doesn’t.
Please note, that if Bob doesn’t make any breaks, all the bar will form one piece and it still has to have exactly one nut.
Input
The first line of the input contains integer n (1 ≤ n ≤ 100) — the number of pieces in the chocolate bar.
The second line contains n integers ai (0 ≤ ai ≤ 1), where 0 represents a piece without the nut and 1 stands for a piece with the nut.
Output
Print the number of ways to break the chocolate into multiple parts so that each part would contain exactly one nut.
Sample test(s)
Input
3
0 1 0
Output
1
Input
5
1 0 1 0 1
Output
4
Note
In the first sample there is exactly one nut, so the number of ways equals 1 — Bob shouldn’t make any breaks.
In the second sample you can break the bar in four ways:
10|10|1
1|010|1
10|1|01
1|01|01
题意:Bob 有一块巧克力条,他希望把这块巧克力条分成若干块,而且每块必须有一个坚果,问有多少种分发?
简单题,但有一个坑。这道题我WA了好几次,每次都是在第七组数据错。到最后才想到,如果全是0,就应该输出0。
思路:先把特殊情况处理了,然后计算相邻两个坚果直接有多少可以划分的地方,然后乘到一起就是最后的结果了
#include <iostream>#include <iomanip>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <vector>#include <algorithm>#define N 55const int mm = 1000000007;using namespace std;int a[110];int main(){#ifndef ONLINE_JUDGE// freopen("1.txt", "r", stdin);#endif ios::sync_with_stdio(false); cin.tie(0); int i, j, n, sum = 0; long long ans = 1; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } if (!sum) { cout << 0; return 0; } int s1, s2; for (i = 0; i < n; i++) if (a[i]) break; s1 = s2 = i; for (i++; i <= n; i++) { if (a[i]) { s1 = s2; s2 = i; ans *= s2-s1; } } cout << ans; return 0;}
C. Watering Flowers
A flowerbed has many flowers and two fountains.
You can adjust the water pressure and set any values r1(r1 ≥ 0) and r2(r2 ≥ 0), giving the distances at which the water is spread from the first and second fountain respectively. You have to set such r1 and r2 that all the flowers are watered, that is, for each flower, the distance between the flower and the first fountain doesn’t exceed r1, or the distance to the second fountain doesn’t exceed r2. It’s OK if some flowers are watered by both fountains.
You need to decrease the amount of water you need, that is set such r1 and r2 that all the flowers are watered and the r12 + r22 is minimum possible. Find this minimum value.
Input
The first line of the input contains integers n, x1, y1, x2, y2 (1 ≤ n ≤ 2000, - 107 ≤ x1, y1, x2, y2 ≤ 107) — the number of flowers, the coordinates of the first and the second fountain.
Next follow n lines. The i-th of these lines contains integers xi and yi ( - 107 ≤ xi, yi ≤ 107) — the coordinates of the i-th flower.
It is guaranteed that all n + 2 points in the input are distinct.
Output
Print the minimum possible value r12 + r22. Note, that in this problem optimal answer is always integer.
Sample test(s)
Input
2 -1 0 5 3
0 2
5 2
Output
6
Input
4 0 0 5 0
9 4
8 3
-1 0
1 4
Output
33
Note
The first sample is (r12 = 5, r22 = 1):
The second sample is (r12 = 1, r22 = 32):
题意:花园里有两个喷泉和一些花,两个喷泉的范围是r1, r2。要使所有 的花都可以被浇到,问最小的r12+r22是多少
思路:先计算出每个花都两个喷泉的距离的平方,再遍历其中的一个喷泉的范围,然后二分查找当第一个喷泉的范围是s1[i]的时候第二个喷泉的最小距离。最后取最小值就行了
#include <iostream>#include <iomanip>#include <cstdio>#include <cstdlib>#include <string>#include <cstring>#include <cmath>#include <algorithm>#define N 2005using namespace std;int n, x[N], y[N];long long s1[N], s2[N], b[N];bool judge(long long aa, long long bb){ for (int i = 2; i < n+2; i++) if (s1[i] > aa && s2[i] > bb) return false; return true;}int main(){#ifndef ONLINE_JUDGE// freopen("1.txt", "r", stdin);#endif ios::sync_with_stdio(false); cin.tie(0); long long ans; int i, j, k, l, r, mid, t; cin >> n ; for (i = 0; i < n+2; i++) cin >> x[i] >> y[i]; for (i = 0; i < n+2; i++) { s1[i] = (long long)(x[i]-x[0])*(x[i]-x[0]) + (long long)(y[i]-y[0])*(y[i]-y[0]); s2[i] = (long long)(x[i]-x[1])*(x[i]-x[1]) + (long long)(y[i]-y[1])*(y[i]-y[1]); b[i] = s2[i]; } sort(b, b+n+2); ans = 9223372036854775807ll; for (i = 0; i < n+2; i++) { l = 0; r = n+1; while(l <= r) { mid = (l+r)/2; if (judge(s1[i], b[mid])) { t = mid; r = mid-1; } else l = mid+1; } ans = min(ans, s1[i]+b[t]); } cout << ans; return 0;}
D. Polyline
There are three points marked on the coordinate plane. The goal is to make a simple polyline, without self-intersections and self-touches, such that it passes through all these points. Also, the polyline must consist of only segments parallel to the coordinate axes. You are to find the minimum number of segments this polyline may consist of.
Input
Each of the three lines of the input contains two integers. The i-th line contains integers xi and yi ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th point. It is guaranteed that all points are distinct.
Output
Print a single number — the minimum possible number of segments of the polyline.
Sample test(s)
Input
1 -1
1 1
1 2
Output
1
Input
-1 -1
-1 3
4 3
Output
2
Input
1 1
2 3
3 2
Output
3
Note
The variant of the polyline in the first sample:
The variant of the polyline in the second sample:
The variant of the polyline in the third sample:
题意:给出三个点,然后计算连接着三个点最少的线段数,要求所有的线段必须平行于坐标轴。
思路:结果一共有三种1, 2, 3。
分情况讨论
1.先按x坐标排序
1.如果三个点的x坐标都一样,只需要一共线段。
2.如果某两个x坐标一样,然后看另一个点的y值,如果这个点的y值在另外两个点的y值中间,则需要三段;否则,需要两段。
3.如果三个点的x值都不一样,再按y排序。如果三个点的y坐标都一样,只需要一共线段。
4.如果某两个y坐标一样,然后看另一个点的x值,如果这个点的x值在另外两个点的y值中间,则需要三段;否则,需要两段。
5.都不满足的时候就需要3段
#include <iostream>#include <iomanip>#include <cstdio>#include <cstdlib>#include <string>#include <cstring>#include <cmath>#include <algorithm>#define N 1005using namespace std;struct point{ long long x, y; friend bool operator < (point a, point b) { if (a.x == b.x) return a.y < b.y; return a.x < b.x; }};bool cmp(point a, point b){ if (a.y == b.y) return a.x < b.x; return a.y < b.y;}int main(){#ifndef ONLINE_JUDGE// freopen("1.txt", "r", stdin);#endif ios::sync_with_stdio(false); cin.tie(0); point a[3]; cin >> a[0].x>> a[0].y; cin >> a[1].x >> a[1].y; cin >> a[2].x >> a[2].y; sort(a, a+3); if (a[0].x == a[1].x) { if (a[1].x == a[2].x) cout << 1; else { if (a[0].y < a[2].y && a[2].y < a[1].y ) cout << 3; else cout << 2; } } else if (a[1].x == a[2].x) { if (a[0].y > a[1].y && a[0].y < a[2].y) cout << 3; else cout << 2; } else { sort(a, a+3, cmp); if (a[0].y == a[1].y && a[1].y == a[2].y) cout << 1; else if (a[0].y == a[1].y) { if (a[0].x < a[2].x && a[2].x < a[1].x) cout << 3; else cout << 2; } else if (a[1].y == a[2].y) { if (a[0].x > a[1].x && a[0].x < a[2].x) cout << 3; else cout << 2; } else cout << 3; } return 0;}
E. XOR and Favorite Number
Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is given by a pair li and ri and asks you to count the number of pairs of integers i and j, such that l ≤ i ≤ j ≤ r and the xor of the numbers ai, ai + 1, …, aj is equal to k.
Input
The first line of the input contains integers n, m and k (1 ≤ n, m ≤ 100 000, 0 ≤ k ≤ 1 000 000) — the length of the array, the number of queries and Bob’s favorite number respectively.
The second line contains n integers ai (0 ≤ ai ≤ 1 000 000) — Bob’s array.
Then m lines follow. The i-th line contains integers li and ri (1 ≤ li ≤ ri ≤ n) — the parameters of the i-th query.
Output
Print m lines, answer the queries in the order they appear in the input.
Sample test(s)
Input
6 2 3
1 2 1 1 0 3
1 6
3 5
Output
7
0
Input
5 3 1
1 1 1 1 1
1 5
2 4
1 3
Output
9
4
4
Note
In the first sample the suitable pairs of i and j for the first query are: (1, 2), (1, 4), (1, 5), (2, 3), (3, 6), (5, 6), (6, 6). Not a single of these pairs is suitable for the second query.
In the second sample xor equals 1 for all subarrays of an odd length.
题意:给n个整数,求在区间[L, R]上有多少子区间满足子区间内所有的数异或的结果是k
思路:新学的姿势:莫队(mo’s algorithm)(建议先写这道题小Z的袜子(莫队算法) )。会了莫队之后就会这道题了
#include <iostream>#include <iomanip>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <cmath>#include <vector>#include <algorithm>#define N 100009#define ll long longconst int mm = 0x3f3f3f3f;using namespace std;int a[N], sum[N], s1[N*20], s2[N*20];long long ans[N];struct query{ int l, r, num, index; friend bool operator < (query a, query b) { if (a.num == b.num) return a.r < b.r; return a.num < b.num; }}qq[N];int main(){#ifndef ONLINE_JUDGE freopen("1.txt", "r", stdin);#endif ios::sync_with_stdio(false); cin.tie(0); int i, j, n, m, k, t, L, R; long long cur; cin >> n >> m >> k; sum[0] = 0; for(i = 1; i <= n; i++) cin >> a[i]; for (i = 1; i <= n; i++) sum[i] = sum[i-1]^a[i]; t = (int)sqrt(n); for (i = 0; i < m; i++) { cin >> qq[i].l >> qq[i].r; qq[i].num = qq[i].l/t; qq[i].index = i; } sort(qq, qq+m); L = 1; R = 0; cur = 0; memset(ans,0,sizeof(ans)); for (i = 0; i < m; i++) { while(R < qq[i].r) { R++; s1[sum[R-1]]++; s2[sum[R]]++; cur += s1[sum[R]^k]; } while(R > qq[i].r) { cur -= s1[sum[R]^k]; s1[sum[R-1]]--; s2[sum[R]]--; R--; } while(L < qq[i].l) { cur -= s2[sum[L-1]^k]; s1[sum[L-1]]--; s2[sum[L]]--; L++; } while(L > qq[i].l) { L--; s1[sum[L-1]]++; s2[sum[L]]++; cur += s2[sum[L-1]^k]; } ans[qq[i].index] = cur; } for(i = 0; i < m; i++) cout << ans[i] << endl; return 0;}
- Codeforces Round #340 (Div. 2)题解
- Codeforces Round #340 (Div. 2)题解
- 【codeforces】Codeforces Round #276 (Div. 2) 题解
- 【codeforces】Codeforces Round #277 (Div. 2) 题解
- 【codeforces】Codeforces Round #279 (Div. 2) 题解
- 【codeforces】Codeforces Round #283 (Div. 2) 【题解】
- 【codeforces】Codeforces Round #291 (Div. 2) 题解
- Codeforces Round #131 (Div. 2) 完整题解
- Codeforces Round #135 (Div. 2)题解
- Codeforces Round #192 (Div. 2) 题解报告
- Codeforces Round #194 (Div. 2) 题解
- Codeforces Round #216 (Div. 2)部分题解
- Codeforces Round #226 (Div. 2) 题解
- Codeforces Round #177 (Div. 2) 题解
- Codeforces Round #FF (Div. 2) 题解
- Codeforces Round #256 (Div. 2) 题解
- Codeforces Round #257 (Div. 2) 题解
- Codeforces Round #259 (Div. 2) 题解
- K-mean聚类算法
- Leetcode 162. Find Peak Element
- NIO学习笔记(1)之组成、NIO与IO区别、NIO和IO如何影响应用程序的设计和通道
- sqlhelper封装
- FREERTOS 入门
- Codeforces Round #340 (Div. 2)题解
- 一些常用的API接口分享给大家 很赞哦
- linux命令介绍——Jps
- /var/log目录下的20个Linux日志文件功能详解
- css和javascript的一些笔记(四) xml语言基础知识
- Jenkins构建常见问题
- Study Note: Logistic Regression(2)
- Android HTML5 Video视频标签自动播放与自动全屏问题解决
- 欢迎使用CSDN-markdown编辑器