Codeforces round #261 (Div. 2) [ABCDE]

Source: Internet
Author: User
Codeforces round #261 (Div. 2) [ABCDE]

ACM

Address: codeforces round #261 (Div. 2)

A-pashmak and garden

Question:
The side of a square is parallel to the coordinate axis. Two points of the Square are given and the other two points are obtained.

Analysis:
Determines whether the X axis is parallel or the Y axis, and if.

Code:

/**  Author:      illuz <iilluzen[at]gmail.com>*  File:        A.cpp*  Create Date: 2014-08-15 23:35:17*  Descripton:   */#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int N = 0;int main() {int x1, y1, x2, y2, x3, y3, x4, y4;int a;while (cin >> x1 >> y1 >> x2 >> y2) {if (x1 == x2) {a = y1 - y2;cout << x1 + a << ' ' << y1 << ' ' << x2 + a << ' ' << y2 << endl;} else if (y1 == y2) {a = x1 - x2;cout << x1 << ' ' << y1 + a << ' ' << x2 << ' ' << y2 + a << endl;} else {if (abs(x1 - x2) != abs(y1 - y2)) {cout << -1 << endl;continue;}cout << x1 << ' ' << y2 << ' ' << x2 << ' ' << y1 << endl;}}return 0;}


B-pashmak and flowers

Question:
Take two numbers out of N to maximize the difference value. There are several methods to obtain the difference value.
The two methods are different if and only if: the two methods have at least one number at a different position.

Analysis:

Obviously, the difference isMax-min.

If the two numbers are not the same, the method ismax_cnt * min_cnt.
If they are the same, note thatmax_cnt * min_cntThere are some numbers with the same method.
For example, 5 1 1 1 1.

  1. We can consider this as follows: for the first time, we can get 5 types, for the second time, we can get (5-1) minutes, but here (I, j) and (J, I) both get, so it's halved, so the result isn*(n-1)/2.
  2. You can also consider it as follows. In order not to get duplicates, it is required that the first fetch must be in front of the second fetch. If the first fetch is pos1, the next fetch can only be (n-1) minutes; if you get pos2 for the first time, the second time (n-2 ).... accumulative is(n-1)*n/2.

Code:

/**  Author:      illuz <iilluzen[at]gmail.com>*  File:        B.cpp*  Create Date: 2014-08-15 23:51:15*  Descripton:   */#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define repf(i,a,b) for(int i=(a);i<=(b);i++)typedef long long ll;const int N = 2e5 + 10;ll t, mmax, mmin;ll a[N];int main() {while (cin >> t) {repf (i, 0, t - 1) {cin >> a[i];}sort (a, a + t);if (a[0] == a[t - 1]) {cout << 0 << ' ' << t * (t - 1) / 2 << endl;continue;}mmax = 0;mmin = 0;int i = 0;while (i < t && a[i] == a[0])mmin++, i++;i = t - 1;while (i >= 0 && a[i] == a[t - 1])mmax++, i--;cout << a[t - 1] - a[0] << ' ' << mmin * mmax << endl;}return 0;}


C-pashmak and buses

Question:
N people take a bus and K take them to d places to play. Ask how to arrange for them to stay together for D days (the victory of the fff team ).

Analysis:
Equivalent to n columns in Row D, with 1 ~ for each position ~ The integer of K, which must not have the same two columns.
You just need a solution.

Code:

/**  Author:      illuz <iilluzen[at]gmail.com>*  File:        C.cpp*  Create Date: 2014-08-16 00:47:18*  Descripton:   */#include<cmath>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int N = 1110;int a[N], sum;int n, d, k, m[N][N];void dfs(int x) {    if(sum >= n)return;    if(x >= d) {        for (int i = 0; i < d; i++)m[i][sum] = a[i];        sum++;        return;    }    for(int i = 1; i <= min(k, 1001); i++) {        a[x] = i;        dfs(x + 1);    }}int main() {    while (~scanf("%d%d%d", &n, &k, &d)) {        memset(m, 0, sizeof(m));        sum = 0;        dfs(0);        if(sum < n)puts("-1");        else {            for(int i = 0; i < d; i++) {                for(int j = 0; j < n; j++)printf("%d ", m[i][j]);                puts("");            }        }    }    return 0;}


D-pashmak and parmida's Problem

Question:
Give some numbers a [n], calculate (I, j ),i<jSo that:f(1, i, a[i]) > f(j, n, a[j]).
f(lhs, rhs, x)In{Value of a [k] in the range of [LHS, RHS] = x}.

Analysis:
Obviously:
1.f(1, i, a[i])It refers to the number that includes a [I] before a [I]. There are several values = A [I].
2.f(j, n, a[j])It refers to the number in which a [J] is followed by a [J]. There are several values = A [J].

Although the range of a [x] is not small, but the range of N is 1000, not very large, so we can use map to pre-processf(1, i, a[i])Andf(j, n, a[j]), Marked as S1 [N], S2 [N].

In this way, the request is satisfied.s1[i] > s[j], i < jThe number of conditions is the same as that of reverse order. In this case, you can use the line segment tree or tree array to find the reverse order number pair to solve this problem. If you do not know how to solve the line segment tree, you can see: HDU 1394 minimum inversion number (the line segment tree calculates the smallest reverse order number pair ).

Code:

/**  Author:      illuz <iilluzen[at]gmail.com>*  File:        D.cpp*  Create Date: 2014-08-16 00:18:08*  Descripton:   */#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <map>using namespace std;#define rep(i,n) for(int i=0;i<(n);i++)#define repu(i,a,b) for(int i=(a);i<(b);i++)#define repd(i,a,b) for(int i=(a);i>=(b);i--)typedef long long ll;#define lson(x) ((x) << 1)#define rson(x) ((x) << 1 | 1)const int N = 1e6 + 10;const int ROOT = 1;// below is sement point updated versionstruct seg {ll w;};struct segment_tree { seg node[N << 2];void update(int pos) {node[pos].w = node[lson(pos)].w + node[rson(pos)].w;}void build(int l, int r, int pos) {if (l == r) {node[pos].w = 0;return;}int m = (l + r) >> 1;build(l, m, lson(pos));build(m + 1, r, rson(pos));update(pos);}// add the point x with yvoid modify(int l, int r, int pos, int x, ll y) {if (l == r) {node[pos].w += y;return;}int m = (l + r) >> 1;if (x <= m)modify(l, m, lson(pos), x, y);elsemodify(m + 1, r, rson(pos), x, y);update(pos);}// query the segment [x, y]ll query(int l, int r, int pos, int x, int y) {if (x <= l && r <= y)return node[pos].w;int m = (l + r) >> 1;ll res = 0;if (x <= m)res += query(l, m, lson(pos), x, y);if (y > m)res += query(m + 1, r, rson(pos), x, y);return res;}} sgm;ll t, a[N];int s1[N], s2[N];map<ll, int> mp;int main() {while (cin >> t) {mp.clear();rep (i, t) {cin >> a[i];mp[a[i]]++;s1[i] = mp[a[i]];}mp.clear();for (int i = t - 1; i >= 0; i--) {mp[a[i]]++;s2[i] = mp[a[i]];}sgm.build(1, t, ROOT);ll ans = 0;rep (i, t) {ans += sgm.query(1, t, ROOT, s2[i] + 1, t); sgm.modify(1, t, ROOT, s1[i], 1);//cout << s1[i] << ' ' << s2[i] << ' ' << ans << endl;}cout << ans << endl;}return 0;}


E-pashmak and Graph

Question:
A directed weighted graph is provided, and the length of the longest incremental link is required. That is, the weight of the current edge must be greater than that of the previous edge.

Analysis:
At the beginning, I wrote a search + map for memory, and then I got TLE qvq...
In fact, we can use the array's DP to first sort the edges from small to large and process them from small to small. For the same type of edges, we can perform edge DP and then update the point DP.

@ Barty:

Sort all edges by edge weight from small to large and scan them sequentially. If there is no duplicate edge weight, for the directed edge (u, v, d, you can directly update the longest path to V using the longest path + 1 obtained to the U point.
However, the question does not ensure that all edge weights are different. In order to ensure strict increments, a buffer must be provided for the same edge weights.

Code:

/**  Author:      illuz <iilluzen[at]gmail.com>*  Blog:        http://blog.csdn.net/hcbbt*  File:        E.cpp*  Create Date: 2014-08-16 09:43:59*  Descripton:   */#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define repf(i,a,b) for(int i=(a);i<=(b);i++)const int N = 3e5 + 10;struct Edge {int x;int y;int w;bool operator <(const Edge& e) const {return w < e.w;}} e[N];int n, m;int edge[N], node[N];// edges and nodes' dpint main() {while (~scanf("%d%d", &n, &m)) {memset(edge, 0, sizeof(edge));memset(node, 0, sizeof(node));repf (i, 1, m) {scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].w);}sort(e + 1, e + m + 1);repf (i, 1, m) {int j = i;while (j <= m && e[i].w == e[j].w) {// update edges' dpint x = e[j].x;edge[j] = max(edge[j], node[x] + 1);j++;}j = i;while (j <= m && e[i].w == e[j].w) {// update nodes' dpint y = e[j].y;node[y] = max(edge[j], node[y]);j++;}i = j - 1;}int ans = 0;repf (i, 1, m)ans = max(ans, edge[i]);printf("%d\n", ans);}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.