Practice_Codeforces Round #404 (Div. 2)

来源:互联网 发布:vb中vbCrLf是什么意思 编辑:程序博客网 时间:2024/05/22 12:22

蒟蒻的水题之路


在A+B的道路上渐行渐远

哇, 今天能勉勉强强做C了耶
一定是我还活在梦里

A. Anton and Polyhedrons
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Anton's favourite geometric figures are regular polyhedrons. Note that there are five kinds of regular polyhedrons:

  • Tetrahedron. Tetrahedron has 4 triangular faces.
  • Cube. Cube has 6 square faces.
  • Octahedron. Octahedron has 8 triangular faces.
  • Dodecahedron. Dodecahedron has 12 pentagonal faces.
  • Icosahedron. Icosahedron has 20 triangular faces.

All five kinds of polyhedrons are shown on the picture below:

Anton has a collection of n polyhedrons. One day he decided to know, how many faces his polyhedrons have in total. Help Anton and find this number!

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of polyhedrons in Anton's collection.

Each of the following n lines of the input contains a string si — the name of the i-th polyhedron in Anton's collection. The string can look like this:

  • "Tetrahedron" (without quotes), if the i-th polyhedron in Anton's collection is a tetrahedron.
  • "Cube" (without quotes), if the i-th polyhedron in Anton's collection is a cube.
  • "Octahedron" (without quotes), if the i-th polyhedron in Anton's collection is an octahedron.
  • "Dodecahedron" (without quotes), if the i-th polyhedron in Anton's collection is a dodecahedron.
  • "Icosahedron" (without quotes), if the i-th polyhedron in Anton's collection is an icosahedron.
Output

Output one number — the total number of faces in all the polyhedrons in Anton's collection.

Examples
input
4IcosahedronCubeTetrahedronDodecahedron
output
42
input
3DodecahedronOctahedronOctahedron
output
28
Note

In the first sample Anton has one icosahedron, one cube, one tetrahedron and one dodecahedron. Icosahedron has 20 faces, cube has 6faces, tetrahedron has 4 faces and dodecahedron has 12 faces. In total, they have 20 + 6 + 4 + 12 = 42 faces.


题目链接:http://codeforces.com/contest/785/problem/A
题意:给定五个空间几何体,每个几何体有不同的面,输入n个几何体(字符串),求总面数。
题解:哇!超级大水题耶!只需用if else判断字符串首字母即可
代码:
#include <iostream>using namespace std;int main(){    ios::sync_with_stdio(false);    cin.tie(0);    string s;    int n, sum = 0;    cin >> n;    while (n--)    {        cin >> s;        if (s[0] == 'T')            sum += 4;        else if (s[0] == 'C')            sum += 6;        else if (s[0] == 'O')            sum += 8;        else if (s[0] == 'D')            sum += 12;        else            sum += 20;    }    cout << sum << endl;    return 0;}

哦对了!这题的Tutorial和C++ code很有意思呀,值得学习学习!
Tutorial:

I think there's nothing to explain in this problem. Just check the polyhedron type, determine its number of faces and sum these numbers.

Time complexity is .

C++ code:
#include <iostream>#include <map>using namespace std;int main() {ios_base::sync_with_stdio(false);map<string, int> vals;vals["Tetrahedron"]  = 4;vals["Cube"]         = 6;vals["Octahedron"]   = 8;vals["Dodecahedron"] = 12;vals["Icosahedron"]  = 20;int n; cin >> n;int res = 0;for (int i = 0; i < n; i++) {string s; cin >> s;res += vals[s];}cout << res << endl;return 0;}
(对!就是想学习map)

B. Anton and Classes
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Anton likes to play chess. Also he likes to do programming. No wonder that he decided to attend chess classes and programming classes.

Anton has n variants when he will attend chess classes, i-th variant is given by a period of time (l1, i, r1, i). Also he has m variants when he will attend programming classes, i-th variant is given by a period of time (l2, i, r2, i).

Anton needs to choose exactly one of n possible periods of time when he will attend chess classes and exactly one of m possible periods of time when he will attend programming classes. He wants to have a rest between classes, so from all the possible pairs of the periods he wants to choose the one where the distance between the periods is maximal.

The distance between periods (l1, r1) and (l2, r2) is the minimal possible distance between a point in the first period and a point in the second period, that is the minimal possible |i - j|, where l1 ≤ i ≤ r1 and l2 ≤ j ≤ r2. In particular, when the periods intersect, the distance between them is 0.

Anton wants to know how much time his rest between the classes will last in the best case. Help Anton and find this number!

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of time periods when Anton can attend chess classes.

Each of the following n lines of the input contains two integers l1, i and r1, i (1 ≤ l1, i ≤ r1, i ≤ 109) — the i-th variant of a period of time when Anton can attend chess classes.

The following line of the input contains a single integer m (1 ≤ m ≤ 200 000) — the number of time periods when Anton can attend programming classes.

Each of the following m lines of the input contains two integers l2, i and r2, i (1 ≤ l2, i ≤ r2, i ≤ 109) — the i-th variant of a period of time when Anton can attend programming classes.

Output

Output one integer — the maximal possible distance between time periods.

Examples
input
31 52 62 322 46 8
output
3
input
31 52 63 722 41 4
output
0
Note

In the first sample Anton can attend chess classes in the period (2, 3) and attend programming classes in the period (6, 8). It's not hard to see that in this case the distance between the periods will be equal to 3.

In the second sample if he chooses any pair of periods, they will intersect. So the answer is 0.


题目链接:http://codeforces.com/contest/785/problem/B
题意:要参加象棋课培训和编程课培训,给定象棋课和编程课可选择的时间区间,求两课程之间最大间隔时间(休息时间)。
题解:用结构体储存时间区间,排序,得到区间右值最小值和区间左值最大值,然后左值最大值减去右值最小值即可,特别注意!并没有说明哪个课程在前,所以需要重复两次操作,得到最大值。当左值最大值减去右值最小值为负数时,输出0。
有点像活动安排问题但又不是
代码:
#include <iostream>#include <algorithm>using namespace std;const int maxn = 200010;struct Class{    int l;    int r;};Class ch[maxn], pr[maxn];bool cmp1(const Class &a, const Class &b)//l从大到小{    return a.l > b.l;}bool cmp2(const Class &a, const Class &b)//r从小到大{    return a.r < b.r;}int main(){    ios::sync_with_stdio(false);    cin.tie(0);    int n, m;    int ans1, ans2, ans;    cin >> n;    for (int i = 0; i < n; i++)        cin >> ch[i].l >> ch[i].r;    cin >> m;    for (int i = 0; i < m; i++)        cin >> pr[i].l >> pr[i].r;    sort(ch, ch + n, cmp2);    sort(pr, pr + m, cmp1);    ans1 = pr[0].l - ch[0].r;    sort(ch, ch + n, cmp1);    sort(pr, pr + m, cmp2);    ans2 = ch[0].l - pr[0].r;    ans = max(0, max(ans1, ans2));    cout << ans << endl;    return 0;}
又来学习学习Tutorial呀!其实差不多。都是求出两种情况下区间右值最小值和区间左值最大值,相减得到结果后取最大值!
#include <iostream>#include <algorithm>const int infinity = 1234567890;using namespace std;int main() {ios_base::sync_with_stdio(false);int n; cin >> n;vector<pair<int, int> > a(n);for (int i = 0; i < n; i++) {cin >> a[i].first >> a[i].second;}int m; cin >> m;vector<pair<int, int> > b(m);for (int i = 0; i < m; i++) {cin >> b[i].first >> b[i].second;}int minR1 = infinity, maxL1 = -infinity;int minR2 = infinity, maxL2 = -infinity;for (int i = 0; i < n; i++) {maxL1 = max(maxL1, a[i].first);minR1 = min(minR1, a[i].second);}for (int i = 0; i < m; i++) {maxL2 = max(maxL2, b[i].first);minR2 = min(minR2, b[i].second);}int res = max(maxL2 - minR1, maxL1 - minR2);cout << max(res, 0) << endl;return 0;}
(对!就是想熟悉pair!)
(这个题目还复习了结构体排序,有趣)

C. Anton and Fairy Tale
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:

"Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..."

More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of n grains was full. Then, every day (starting with the first day) the following happens:

  • m grains are brought to the barn. If m grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account).
  • Sparrows come and eat grain. In the i-th day i sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing.

Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day!

Input

The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018) — the capacity of the barn and the number of grains that are brought every day.

Output

Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one.

Examples
input
5 2
output
4
input
8 1
output
5
Note

In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens:

  • At the beginning of the first day grain is brought to the barn. It's full, so nothing happens.
  • At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain.
  • At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it.
  • At the end of the second day two sparrows come. 5 - 2 = 3 grains remain.
  • At the beginning of the third day two grains are brought. The barn becomes full again.
  • At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain.
  • At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain.
  • At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty.

So the answer is 4, because by the end of the fourth day the barn becomes empty.


题目链接:http://codeforces.com/contest/785/problem/C
参考来源:Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale
题意:有一个最多装有n个粮食的粮仓,每天晚上会增加m的粮食,但是最多不超过n。然后第i天早上都会被麻雀吃掉i个粮食。问第几天,这个粮仓会被吃空。
题解:题目数据n,m高达1e18,暴力肯定会超时。考虑输入的n和m,如果n小于等于m,那么,n天前,每次谷仓都能补满,第n天,小鸟吃n粒则一定会吃完。然后再考虑n大于m的情况,当时间t=m+1的时候才会出现净消耗。 设出现净消耗后l天谷子里没有余粮,则有l*(l+1)/2>=n-m符合条件的最小的l。最后的答案即为m+l。选用二分天数的方法。

暴力代码:
//暴力TLE#include <iostream>using namespace std;int main(){    ios::sync_with_stdio(false);    cin.tie(0);    long long n, m;    cin >> n >> m;    long long t = n, sum = 0, k = 0;    while (t > 0)    {        t += (t + m < n ? m : n - t);        t -= (++k);        sum++;    }    cout << sum << endl;    return 0;}

二分:
#include <iostream>using namespace std;typedef long long ll;int main(){    ios::sync_with_stdio(false);    cin.tie(0);    ll n, m;    cin >> n >> m;    if (m >= n)    {        cout << n << endl;        return 0;    }    ll l = 0, r = 2e9, mid, ans;    n -= m;    while (l <= r)    {        mid = (l+r)/2;        if (mid*(mid+1)/2 < n)            l = mid + 1;        else        {            r = mid - 1;            ans = mid;        }    }    cout << ans + m << endl;    return 0;}

学一学Tutorial,当然也是二分啦

At first, let's make the following assumption: if a sparrow cannot eat a grain because the barn is empty, the number of grains in the barn becomes negative. It's easy to see that the answer doesn't change because of this.

Now, let's observe the number of grains before sparrows come. At first, the barn remains full for m + 1 days (because sparrows eat less grains than it's added to the barn). Then the number of grains is decreased by one, by two and so on. So, on the m + 1 + i-th day there are  grains in the barn before sparrows come (remember that for any positive integer x the equality  is always true).

How can we determine if the barn is empty? It's reasonable that if there are q grains on the k-th day after grain is brought, then at the end of the k - 1-th day there are q - m grains in the barn. So, if on the k - 1-th day the barn becomes empty (q - m ≤ 0), then there must be q ≤ m grains on the k-th day after grain is brought.

So, we must find such minimal day m + 1 + k, in which there are m or less grains after grain is brought. That is, using the formula above, we must find such minimal k that

It can be easily done using binary search. It's not hard to observe that the answer in this case is m + k (if in the m + 1 + k-th day before sparrows come there are less or equal than m grains, then in the m + 1 + k - 1 = m + k-th day the barn is empty).

The corner case in this problem is m ≥ n. In this case the barn becomes full every day and it becomes empty only in the n-th day when sparrows eat all the grain.

Also notice that k can be found using a formula, but such solutions could fail by accuracy, because the formula is using the square root function.

Time complexity is .


C++ Code:
#include <iostream>#include <cstdint>using namespace std;int main() {ios_base::sync_with_stdio(false);int64_t n, m;cin >> n >> m;if (n <= m) {cout << n << endl;} else {n -= m;int64_t l = 0, r = 2e9;while (l < r) {int64_t m = (l + r) / 2;int64_t val = m * (m+1) / 2;if (val >= n) {r = m;} else {l = m+1;}}cout << l + m << endl;}return 0;}



(我这种弱渣看见题目只会暴力哇!哭根本想不到二分,二分还写错!!!)



The end.
0 0
原创粉丝点击