Codeforces Round #577 (Div. 2)

A - Important Exam

Meaning of the questions: n personal, m questions, each question has ABCDE five options, each person has an option in each question, each question has a score, the sum of the scores of all ask for the maximum number.

Analysis: a problem has, his score is fixed, a total of n personal answer, choose the option selected five of the most people as the answer, get up to score.

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <iostream>
 5  
 6 #define maxn 1000005
 7 #define mod 1000000007
 8 #define inf 0x3f3f3f3f
 9 #define start ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
10 #define ll long long
11 using namespace std;
12 string a[1005];
13 ll b[1005][10];
14  
15 int main() {
16     start;
17     int n, m;
18     cin >> n >> m;
19     for (int i = 1; i <= n; ++i) {
20         cin >> a[i];
21         for (int j = 0; j < m; ++j)
22             b[j][a[i][j] - 'A']++;
23     }
24     ll ans = 0;
25     for (int i = 0; i < m; ++i) {
26         int x;
27         cin >> x;
28         ll maxt = 0;
29         for (int j = 0; j < 5; ++j)
30             maxt = max(maxt, b[i][j]);
31         ans += maxt * x;
32     }
33     cout << ans << endl;
34     return 0;
35 }
View Code

B - Zero Array

Meaning of the title: to number n, each pick a different number of simultaneous two minus one, ask whether the whole array becomes zero.

Analysis: If the sum is an odd number, clearly not. If the maximum number larger than all others combined number, obviously not. The rest of our analysis: the sum is even, for the array, we pick any two, and to ensure that the selection of uniform (each selected to the maximum and minimum, each minus one), you can set up. (I will not permit, but the feeling is right).

 1 #include <bits/stdc++.h>
 2  
 3 #define maxn 200005
 4 #define mod 1000000007
 5 #define inf 0x3f3f3f3f
 6 #define start ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 7 #define ll long long
 8 using namespace std;
 9 #define int ll
10  
11 signed main() {
12     start;
13     int n;
14     ll ans = 0;
15     ll maxt = 0;
16     cin >> n;
17     for (int i = 1; i <= n; ++i) {
18         int x;
19         cin >> x;
20         ans += x;
21         maxt = max(maxt, x);
22     }
23     if (ans & 1 || maxt * 2 > ans)
24         cout << "NO" << endl;
25     else
26         cout << "YES" << endl;
27     return 0;
28 }
View Code

C - Maximum Median

Meaning of the questions: to n (n is odd) number, and k plus a chance after the operation and asked the array is the number of bits.

Analysis: after sorting, the number for the previous / 2 + 1 of n, is certainly not add an operation, and otherwise the operation to add to the n-a / 2 + 1 number is definitely more effective. Order mid = n / 2 + 1, to ensure that a [mid] increasing (if the n / 2 number of growing, the answer is a [mid], the operation is invalid).

The first operation, a [mid] added to a [mid + 1] (k will add less than if all added a [mid] on).

The second operation, a [mid] and a [mid + 1] is added to a [mid + 2] (if not then add the remaining k uniformly assigned a [mid], a [mid] on).

……

Of n / 2 operations, the a [mid], a [mid + 1], ......, a [n-1] is added to a [n-] (if not then add the remaining k uniformly assigned to a [ mid], a [mid + 1], ......, a [n-1] a)

Of n / 2 + 1 operations, the remaining k uniformly assigned a [mid], a [mid + 1], ......, [n] a on

 1 #include <bits/stdc++.h>
 2 
 3 #define maxn 200005
 4 #define mod 1000000007
 5 #define inf 0x3f3f3f3f
 6 #define start ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 7 #define ll long long
 8 using namespace std;
 9 #define int ll
10 int a[maxn];
11 
12 signed main() {
13     start;
14     int n, k;
15     cin >> n >> k;
16     for (int i = 1; i <= n; ++i)
17         cin >> a[i];
18     sort(a + 1, a + n + 1);
19     int mid = n / 2 + 1;
20     if (a[mid] + k < a[mid + 1]) {
21         cout << a[mid] + k << endl;
22         return 0;
23     }
24     int t = 1;
25     for (int i = 1; mid + i <= n; ++i) {
26         if (k - (a[mid + i] - a[mid]) * i < 0)
27             break;
28         t = i + 1;
29         k -= (a[mid + i] - a[mid]) * i;
30         a[mid] = a[mid + i];
31     }
32     a[mid] += (k / t);
33     cout << a[mid];
34     return 0;
35 }
View Code

D - Treasure Hunting

Personally feel that the difficulty of more than 2000, the final race only 369 AC (I'm obviously not one of them), while the first three of the most difficult questions B there are 2520 people AC, difficulty gradient design is not reasonable. But this is not the reason I made the first three questions wa8, swap points get away, admit dish. Rank 1 after the game looked great God code, read one hour finally understood.

The meaning of problems: n row m column grid, with k treasures, and q columns of security. Starting from (1,1), but to walk, left and right three directions. When he took, up from only security column. Q will pick up all the treasures finish requires a minimum number of steps.

analysis:

First, for the i-th row, we rise up from the j-th column, we consider only the leftmost and rightmost treasures, treasures located in the leftmost column of the l, rightmost treasure in the first r columns.

Second, assuming that the rightmost acquire the treasure (the left side of the case to get the treasure found in supplements), then left to get the most treasures need abs (jl) + abs (lr) step, then consider the two most recent security from l column (if l not only consider the left to the right a), is set to q and w.

Third, we (i + 1-row treasures not necessarily) the first line has a treasure for the i-th row (referred to as s), we are the first column q and w columns rise up.

Fourth, look at the first line i + l2 1 and r2, assume also acquire the left-most treasure, if it rose up from the column q, we have just ascended from the i-th row to the left of the treasures were to go abs (jl) + abs (lr) + s + abs (q-l2) + abs (l2-r2); if the column rose up from w, then go abs (jl) + abs (lr) + s + abs ( w-l2) + abs (l2-r2), we naturally get a minimum.

Fifth, take complete after Lee l2 consider the two most recent two security column (left if l do not consider only the one on the right), to the third step.

Added: The first line of the first column from default rose up, and apparently the right to take the treasure.

For the j-th column is the first step up from the first row of recurrence, in fact, this situation j up to four points.

For the second and third step, we should also be considered acquire the leftmost and rightmost situation, so we need to consider line up to four, after four leftmost or rightmost row come from each of the minimum, and then extended out of four.

Great God of code (with a personal style rewrite a bit):

 1 #include <bits/stdc++.h>
 2 
 3 #define maxn 200005
 4 #define mod 1000000007
 5 #define inf 0x3f3f3f3f3f3f3f3f
 6 #define start ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 7 #define ll long long
 8 using namespace std;
 9 
10 struct node {
11     int x, y;
12 
13     bool operator<(const node &b) const {
14         if (x != b.x)
15             return x < b.x;
16         return y < b.y;
17     }
18 
19     node(int x = 0, int y = 0) : x(x), y(y) {}
20 };
21 
22 vector<node> treasures;
23 vector<int> safe;
24 
25 signed main() {
26     start;
27     int n, m, k, q;
28     cin >> n >> m >> k >> q;
29     for (int i = 0; i < k; ++i) {
30         int x, y;
31         cin >> x >> y;
32         treasures.emplace_back(x, y);
33     }
34     treasures.emplace_back(1, 1);
35     ++k;
36     for (int i = 0; i < q; ++i) {
37         int x;
38         cin >> x;
39         safe.push_back(x);
40     }
41     Sort (treasures.begin (), treasures.end ()); // press rows, the columns are arranged 
42 is      Sort (safe.begin (), safe.end ()); // security arranged in columns 
43 is      Map <LL, LL> selected_col; // first find the treasure that the previous column where the column select safety (up to 4), second case the number of step 
44 is      selected_col [ . 1 ] = 0 ; // default is the first row from 1 liter up 
45      LL ANS;
 46 is      int CURRENT_ROW of = . 1 ; // row of discussion, it is not present for the first row 
47      for ( int I = 0 , J = 0 ; I <K; I J =) { // J is a treasure of the next column pointer 
48         the while (j <K && Treasures [j] == Treasures .x [I] .x) // j is moved to the first line at the treasure 
49              ++ j;
 50          int Row = Treasures [I] .x; // the line are talking 
51 is          LL = L treasures [I] .y, R & lt treasures = [J - . 1 ] .y; // the row of the leftmost and rightmost two columns treasure 
52 is          for (IT & Auto: selected_col) // & important 
53 is              it.second = Row + - current_row; // each case together with the necessary increase in the number of steps required, so current_row initialized. 1 
54 is          current_row = Row; // transfer 
55          Map <LL, LL> Mint; //for the first and l r, second upper treasure a case where the minimum number of steps to a column or r, l. 
56 is          Mint [L] = Mint [R & lt] = INF;
 57 is          for (Auto & IT: selected_col) {
 58              Mint [L] = min (Mint [L], it.second + ABS (it.first - R & lt) + ABS (L - R & lt));
 59              Mint [R & lt] = min (Mint [R & lt], it.second + ABS (it.first - L) + ABS (R & lt - L));
 60          }
 61 is          ANS = min (Mint [ l], Mint [R & lt]);
 62 is          selected_col.clear ();
 63 is          for (Auto & IT: Mint) {
 64              / * found from it.first (only l or r) treasure latest two * / 
65              // under the right Tucao here if there is no security r columns, and no sentry, you will have trouble
66             int safe_index = lower_bound(safe.begin(), safe.end(), it.first) - safe.begin();
67             int safe_col = safe[safe_index];
68             //cout << safe_col << endl;
69             ll cost = it.second + abs(it.first - safe_col);
70             if (selected_col.find(safe_col) == selected_col.end() || cost < selected_col[safe_col])
71                 selected_col[safe_col] = cost;
72             if (safe_index > 0) {
73                 --safe_index;
74                 safe_col = safe[safe_index];
75                 cost = it.second + abs(it.first - safe_col);
76                 if (selected_col.find(safe_col) == selected_col.end() || cost < selected_col[safe_col])
77                     selected_col[safe_col] = cost;
78             }
79         }
80     }
81     cout << ans << endl;
82     return 0;
83 }
View Code

The code is actually a bit of a problem, if you take the middle instrumentation method, multiple runs, safe [safe_index] randomness may cause the answer wrong. (This is what I changed data in the sample 2 obtained a certain operating results)

Great God code itself, too, do not believe can go and see. (This does not change the fact that people are the god)

 

 

 

 

 

 

Personal understanding: each line only need to maintain the leftmost and rightmost two treasures. While small bug fixes, get a similar code.

 1 #include <bits/stdc++.h>
 2 
 3 #define maxn 200005
 4 #define mod 1000000007
 5 #define inf 0x3f3f3f3f3f3f3f3f
 6 #define start ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 7 #define ll long long
 8 using namespace std;
 9 int maxx[maxn];
10 int minn[maxn];
11 vector<int> safe;
12 
13 signed main() {
14     start;
15     memset(minn, 0x3f, sizeof(minn));
16     minn[1] = maxx[1] = 1;
17     int n, m, k, q;
18     cin >> n >> m >> k >> q;
19     for (int i = 0; i < k; ++i) {
20         int x, y;
21         cin >> x >> y;
22         maxx[x] = max(maxx[x], y);
23         minn[x] = min(minn[x], y);
24     }
25     ++k;
26     for (int i = 0; i < q; ++i) {
27         int x;
28         cin >> x;
29         safe.push_back(x);
30     }
31     sort(safe.begin(), safe.end());
32     map<ll, ll> selected_col;
33     selected_col[1] = 0;
34     ll ans;
35     int current_row = 1;
36     for (int row = 1; row <= n; ++row) {
37         if (maxx[row] == 0)
38             continue;
39         ll l = minn[row], r = maxx[row];
40         for (auto &it:selected_col)
41             it.second += row - current_row;
42         current_row = row;
43         map<ll, ll> mint;
44         mint[l] = mint[r] = inf;
45         for (auto &it:selected_col) {
46             mint[l] = min(mint[l], it.second + abs(it.first - r) + abs(l - r));
47             mint[r] = min(mint[r], it.second + abs(it.first - l) + abs(r - l));
48         }
49         ans = min(mint[l], mint[r]);
50         selected_col.clear();
51         for (auto &it:mint) {
52             int safe_index = lower_bound(safe.begin(), safe.end(), it.first) - safe.begin();
53             if(safe_index!=safe.size()) {
54                 int safe_col = safe[safe_index];
55                 ll cost = it.second + abs(it.first - safe_col);
56                 if (selected_col.find(safe_col) == selected_col.end() || cost < selected_col[safe_col])
57                     selected_col[safe_col] = cost;
58             }
59             if (safe_index > 0) {
60                 --safe_index;
61                 int safe_col = safe[safe_index];
62                 ll cost = it.second + abs(it.first - safe_col);
63                 if (selected_col.find(safe_col) == selected_col.end() || cost < selected_col[safe_col])
64                     selected_col[safe_col] = cost;
65             }
66         }
67     }
68     cout << ans << endl;
69     return 0;
70 }
View Code

(E and F of this chicken dish is certainly not write)

(If you have questions, please in the comments section and I said)

Guess you like

Origin www.cnblogs.com/F-Mu/p/11306615.html