Codeforces Round #308 (Div. 2 ),

Source: Internet
Author: User

Codeforces Round #308 (Div. 2 ),
Once again shamelessly publicize a hair: new blog http://shijieyywd.com /? P = 99

This time, cf was a little drunk, and I did not dare to do anything next time I had div1.

A. Vanya and Table

Give you n Rectangles and ask you the sum of area.

The sum of area is not the sum of area. However, I had a 233333 million error in WA.

#include <bits/stdc++.h>using namespace std;int n;int main() {    int a, b, c, d;    cin >> n;    int ans = 0;    for (int i = 0; i < n; i++) {        cin >> a >> b >> c >> d;        ans += abs(c - a + 1) * abs(d - b + 1);    }    cout << ans << endl;    return 0;}
B. Vanya and Books

Give you an integer n and ask you the total number of digits from 1 to n. For example, n = 13, 1, listen 2, listen 3, listen 4, Listen 5, listen 6, listen 7, listen 8, then 9, then 10, then 11, then 12, listen 13 has 17 digits in total.

F [I] indicates from 1 10i The total number of digits. For example, from 753 to 100 is three digits, so the answer is f [2] + 753*3.

#include <bits/stdc++.h>using namespace std;int n;long long f[12];long long t[12];int main() {    cin >> n;    t[1] = 10;    for (int i = 2; i <= 10; i++)        t[i] = t[i - 1] * 10;    f[1] = 11;    for (int i = 2; i <= 10; i++)        f[i] = f[i - 1] + (t[i] - t[i - 1]) * i + 1;    long long ans;    long long base;    for (int i = 0; i <= n; i++) {        if (t[i] > n) {            base = i - 1;            break;        }    }    ans = f[base] + (n - t[base]) * (base + 1);    cout << ans << endl;    return 0;}
C. Vanya and Scales

Use quality W0, w1,..., w100 Each weight has one weight m, and the weight can be placed on the left of the balance or on the right. Ask if the output is "YES" or "NO.

For example, 3, 7: 3 on the left and 9 on the right.

Assume that it can be called out, then m is represented in the w hexadecimal notation. The value of each digit must be 0, 1 or w-1. Otherwise, it will not work.

If one of them is w-1, the current weight is put together with the item, which is equivalent to adding the weight to the item.

We only need to simulate this process, extract every bit of m, and then calculate it.

#include <bits/stdc++.h>using namespace std;int n, m;int main() {    cin >> n >> m;    if (n == 3) {        cout << "YES" << endl;        return 0;    }    while (m) {        int z = m % n;        if (z <= 1) {            m /= n;        } else if (z == n - 1) {            m = m / n + 1;        } else {            cout << "NO" << endl;            return 0;        }    }    cout << "YES" << endl;    return 0;}
D. Vanya and Triangles

Let's give you n points under two-dimensional coordinates and ask how many triangles with an area not 0 can constitute.

If area is not considered, the total number of solutions is C3n .

If three points are collocated, these three points cannot form a triangle.

We enumerate each vertex I and calculate the slope of all other vertices connected to I. If there is a repeated slope, it indicates that three points are in the same line, that is, if m points have the same slope as I, the number of invalid solutions is C2m .

#include <bits/stdc++.h>using namespace std;const double INF = 1e10;const double eps = 1e-8;struct Point{    int x, y;    Point() {}    Point(int a, int b) : x(a), y(b) {}};int n;Point p[2005];int main() {    cin >> n;    for (int i = 0; i < n; i++)        cin >> p[i].x >> p[i].y;    if (n < 3) {        cout << 0 << endl;        return 0;    }    long long tot = (long long)n * (n - 1) * (n - 2) / 6;;    long long sum = 0;    vector<double> ans;    for (int i = 0; i < n; i++) {        ans.clear();        for (int j = i + 1; j < n; j++) {            int dx = p[j].x - p[i].x;            int dy = p[j].y - p[i].y;            double k;            if (dx == 0) k = INF;            else if (dy == 0) k = 0.0;            else k = (double)dy / (double)dx;            ans.push_back(k);        }        sort(ans.begin(), ans.end());        double num = ans[0];        int cnt = 0;        for (int j = 0; j < ans.size(); j++) {            if (fabs(ans[j] - num) < eps) {                cnt++;            } else {                sum += (long long)cnt * ((long long)cnt - 1ll) / 2;;                cnt = 1;                num = ans[j];            }        }        sum += (long long)cnt * ((long long)cnt - 1ll) / 2;    }    cout << tot - sum << endl;    return 0;}
E. Vanya and Brackets

Here is an expression with only a multiplication sign and a plus sign. The numbers are from 1 to 9. You must add a bracket to maximize the value of the expression and ask the maximum value. Multiplication number <= 15.

The left bracket must be on the right side of the multiplication sign, and the right brace must be on the left side of the multiplication sign. If not, you can adjust the position of the bracket to increase the value of the expression. This should not be difficult to think about.

Therefore, you only need to enumerate the positions of parentheses and then calculate the expression.

#include <bits/stdc++.h>using namespace std;int n;string str;vector<int> pos;stack<char> sc;stack<long long> sn;long long twoResult(long long a, long long b, char c) {    return (c == '*') ? a * b : a + b;}void cal() {    char t = sc.top();    sc.pop();    long long a = sn.top();    sn.pop();    long long b = sn.top();    sn.pop();    sn.push(twoResult(a, b, t));}long long expression(string s) {    for (int i = 0; i < s.length(); i++) {        char c = s[i];        if (isdigit(c)) {            sn.push(c - '0');        } else if (c == '(') {            sc.push(c);        } else if (c == ')') {            while (sc.top() != '(')                cal();            sc.pop();        } else {            if (c == '+') {                while (!sc.empty() && sc.top() == '*')                    cal();                sc.push(c);            } else sc.push(c);        }    }    while (!sc.empty()) cal();    return sn.top();}int main() {    cin >> str;    n = str.size();    pos.push_back(-1);    for (int i = 1; i < n; i += 2)        if (str[i] == '*') pos.push_back(i);    pos.push_back(n);    int len = pos.size();    long long ans = 0;    for (int i = 0; i < len - 1; i++) { // left        for (int j = i + 1; j < len; j++) { // right            string s = str;            s.insert(pos[i] + 1, 1, '(');            s.insert(pos[j] + 1, 1, ')');            ans = max(ans, expression(s));        }    }    cout << ans << endl;    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.