NYour-keys 1-sized submatrices, then the beauty of the matrix equals the sum of numberMAnd other four beauties of the described submatrices.As you can see, the algorithm is recursive.
Help Ilya, solve the problem and print the resulting maximum beauty of the matrix.
InputThe first line contains integer 4N(1 limit ≤ limit 4NLimit ≤ limit 2 · 106). The next line contains4NIntegersAI(1 digit ≤ DigitAILimit ≤ limit 109)-the numbers you need to arrange in the2NLimit × limit 2N-Sized matrix.
OutputOn a single line print the maximum value of the beauty of the described matrix.
Please, do not use the % lld specifier to read or write 64-bit integers in bytes ++. It is preferred to use thecin, cout streams or the % I64d specifier.
Sample test (s) Input113
Output13
Input41 2 3 4
Output14
NoteConsider the second sample. You need to arrange the numbers in the matrix as follows:
1 23 4
Then the beauty of the matrix will equal: 4 + 1 + 2 + 3 + 4 = 14.
Add 4 ^ n to a 2 ^ n * 2 ^ n matrix. The maximum value of beauty is required, the beautiful value of a matrix is equal to the maximum value in every 2 ^ I * 2 ^ I sub-matrix.
Question Analysis: sort from big to small, calculate the sum of prefixes, divide each time by 4, indicating the sum of the first 4 ^ n, and the sum of the first 4 ^ n-1... The first four major sums can be added to the class. This question seems like cin, cout, and nausea...
#include
#include
#include #include
#define ll long longusing namespace std;ll a[1 << 21]; bool cmp(ll p, ll q){ return p > q;}int main(){ ll n, ans = 0; scanf(%I64d, &n); for(int i = 1; i <= n; i++) scanf(%I64d, &a[i]); sort(a + 1, a + n + 1, cmp); for(int i = 1; i <= n; i++) a[i] += a[i - 1]; while(n) { ans += a[n], n /= 4; } printf(%I64d, ans);}
D. Ilya and Roads time limit per test: 3 seconds memory limit per test: 256 megabytes
Everything is great about Ilya's city, doesn't the roads. The thing is, the only ZooVille road is representedNHoles in a row. We will consider the holes numbered from 1N, From left to right.
Ilya is really keep on helping his city. So, he wants to fix at leastKHoles (perharps he can fix more) on a single ZooVille road.
The city hasMBuilding companies,I-Th company needsCIMoney units to fix a road segment containing holes with numbers of at leastLIAnd at mostRI. The companies in ZooVille are very greedy, so, if they fix a segment containing some already fixed holes, they do not decrease the price for fixing the segment.
Determine the minimum money Ilya will need to fix at leastKHoles.
InputThe first line contains three integersN, Bytes,M, Bytes,K(1 digit ≤ DigitNLimit ≤ limit 300, limit 1 limit ≤ limitMLimit ≤ limit 105, limit 1 ≤ limitKLimit ≤ limitN). The nextMLines contain the companies 'description.I-Th line contains three integersLI, Bytes,RI, Bytes,CI(1 digit ≤ DigitLILimit ≤ limitRILimit ≤ limitN, Memory 1 ≤ memoryCILimit ≤ limit 109 ).
OutputPrint a single integer-the minimum money Ilya needs to fix at leastKHoles.
If it is impossible to fix at leastKHoles, print-1.
Please, do not use the % lld specifier to read or write 64-bit integers in bytes ++. It is preferred to use thecin, cout streams or the % I64d specifier.
Sample test (s) Input10 4 67 9 116 9 137 7 73 5 6
Output17
Input10 7 13 4 158 9 85 6 89 10 61 4 21 4 108 10 13
Output2
Input10 1 95 10 14
Output-1
N holes and m intervals, each company can only repair holes in the interval [li, ri] and requires ci. Now I want to repair the minimum cost of at least k holes, note that if the same hole is repaired multiple times, the cost will be accumulated.
Question Analysis: n is so small, the interval dp of the red fruit, dp [I] [j] indicates the minimum cost of j repairs in the first I holes, p [I] [j] indicates the minimum cost from the I-th hole to the j-th hole. p [I] [j], calculate the input directly. The transfer equation is dp [I] [j] = min (dp [I-1] [j], dp [I-k] [j-k] + p [I-k + 1] [I] (1 <= k <= j ))
#include
#include
#include #define ll long longusing namespace std;ll const INF = 1ll << 40;int const MAX = 305;ll dp[MAX][MAX], p[MAX][MAX];int main(){ int n, m, k; scanf(%d %d %d, &n, &m, &k); for(int i = 0; i <= n; i++) for(int j = i; j <= n; j++) dp[i][j] = p[i][j] = INF; while(m --) { int l, r, c; scanf(%d %d %d, &l, &r, &c); for(int i = l; i <= r; i++) p[l][i] = min(p[l][i], (ll)c); } dp[0][0] = 0; for(int i = 1; i <= n; i++) { for(int j = 0; j <= i; j++) { dp[i][j] = dp[i - 1][j]; for(int k = 1; k <= j; k++) dp[i][j] = min(dp[i][j], dp[i - k][j - k] + p[i - k + 1][i]); } } if(dp[n][k] == INF) printf(-1); else printf(%I64d, dp[n][k]);}
E. Ilya and Two Numbers time limit per test: 2 seconds memory limit per test: 256 megabytes
Ilya has recently taken up archaeology. He's recently found two numbers, written inM-Based notation. Each of the found numbers consisted of exactlyNDigits. Ilya immediately started looking for information about those numbers. He learned that the numbers are part of a cyphered code and the one who can decypher it can get the greatest treasure.
After considerable research Ilya understood that to decypher the code, he shoshould do the following:
Rearrange digits in the first number in some manner. similarly, rearrange digits in the second number in some manner. as a result of this operation, the numbers can get leading zeroes. add numbers, digit by digit, moduloM. In other words, we need to get the third number of lengthN, Each digit of the number is the sum of the respective numbers of the found numbers. for example, suppose there are two numbers recorded in the ternary notation, 001210 and 012111, then if you add them to each other digit by digit modulo 3, you will get number 010021. the key to the code is the maximum possible number that can be obtained in the previous step.Help Ilya, find the key to the code.
InputThe first line contains two integersN, Bytes,M(1 digit ≤ DigitN, Bytes,MLimit ≤ limit 105, middle,MRows> limit 1). The second line contains the first found number, the third line contains the second found number.
The numbers are recorded as a sequence of digits inM-Based notation. Each digit is an integer from 0MLimits-limits 1. The digits in the line are written in the order from the most significant digits to the least significant ones.
The given numbers can contain in leading zeroes.
OutputPrintN M-Base digits. The resulting third number written inM-Based notation. Print the digits in the order from the most significant digits to the least significant ones.
Sample test (s) Input4 75 4 3 25 6 5 4
Output6 4 2 1
Input5 52 4 4 1 31 0 1 2 4
Output4 4 4 3 2
Give n numbers to two rows, each of which can be sorted at will. Now, calculate the maximum value of the non-carry addition of the numbers to m in the two rows.
Question Analysis: How many times each number in each row appears during input, and then I starts from M-1 enumeration, obviously for an I, I am going to find m-I-1 and add it together. If not, I will look for a smaller sum than m-I-1 but as big as possible. Here we can use the stack to maintain the first row, the second row of queue maintenance is equivalent to maintaining a monotonically incrementing stack and a monotonically incrementing queue. If the I in the first row finds the number that can be added in the second row, the corresponding number in the second row is still available, then, subtract m from the queue and add the elements in the queue and Stack directly. Because they are monotonous and the sum is smaller than m, you can add them directly. It is best to draw on paper, difficult to describe clearly
#include
#include
#include #include
#include
using namespace std;int const MAX = 100005;int a[MAX], b[MAX], ans[MAX];stack
s;queue
q;int main(){ int n, m, t, cnt = 0; scanf(%d %d, &n, &m); for(int i = 0; i < n; i++) { scanf(%d, &t); a[t] ++; } for(int i = 0; i < n; i++) { scanf(%d, &t); b[t] ++; } for(int i = 0; i < m; i++) { while(a[i]) { s.push(i); a[i] --; } int j = m - i - 1; while(b[j]) { if(!s.empty()) { ans[cnt ++] = j + s.top(); s.pop(); } else q.push(j - m); b[j] --; } } while(!s.empty()) { ans[cnt ++] = s.top() + q.front(); s.pop(); q.pop(); } sort(ans, ans + cnt); for(int i = n - 1; i > 0; i--) printf(%d , ans[i]); printf(%d, ans[0]);}