Codeforces Round #186 (Div. 2) (ABCDE)

Source: Internet
Author: User

Codeforces Round #186 (Div. 2) (ABCDE)

 

 

A. Ilya and Bank Account time limit per test: 2 seconds memory limit per test: 256 megabytes

Ilya is a very clever lion, he lives in an unusual city ZooVille. in this city all the animals have their rights and obligations. moreover, they even have their own bank accounts. the state of a bank account is an integer. the state of a bank account can be a negative number. this means that the owner of the account owes the bank money.

Ilya the Lion has recently had a birthday, so he got a lot of gifts. one of them (the gift of the main ZooVille bank) is the opportunity to delete the last digit or the digit before last from the state of his bank account no more than once. for example, if the state of Ilya's bank account is-123, then Ilya can delete the last digit and get his account balance equal to-12, also he can remove its digit before last and get the account balance equal to-13. of course, Ilya is permitted not to use the opportunity to delete a digit from the balance.

Ilya is not very good at math, and that's why he asks you to help him maximize his bank account. find the maximum state of the bank account that can be obtained using the bank's gift.

Input

The single line contains integerN(10 bytes ≤ bytes |N| Limit ≤ limit 109)-the state of Ilya's bank account.

Output

In a single line print an integer-the maximum state of the bank account that Ilya can get.

Sample test (s) Input
2230
Output
2230
Input
-10
Output
0
Input
-100003
Output
-10000
Note

In the first test sample Ilya doesn't profit from using the present.

In the second test sample you can delete digit 1 and get the state of the account equal to 0.

 

A number can be deleted from the last number or the second to the last number.

 

Question Analysis: positive numbers are output directly, and negative numbers are compared to the two deletion methods.

 

#include 
 
  #include 
  
   #include #include 
   
    using namespace std;int main(){    int n;    scanf(%d, &n);    if(n >= 0)        printf(%d, n);    else    {        n = -n;        if(n < 10)            printf(0);        else        {            int nn = n;            int tmp = n / 100;            int a = n % 10;            int b = (nn / 10) % 10;            printf(%d, max(-(tmp * 10 + a), -(tmp * 10 + b)));        }    }}
   
  
 

 

 

 

B. Ilya and Queries time limit per test: 2 seconds memory limit per test: 256 megabytes

Ilya the Lion wants to help all his friends with passing exams. They need to solve the following problem to pass the IT exam.

You 've got stringSSignature = SignatureS1S2...SN(NIs the length of the string), consisting only of characters. and # andMQueries. Each query is described by a pair of integersLI, Bytes,RI(1 digit ≤ DigitLILatency <latencyRILimit ≤ limitN). The answer to the queryLI, Bytes,RIIs the number of such integersI(LILimit ≤ limitILatency <latencyRI), ThatSISignature = SignatureSIUpload + upload 1.

Ilya the Lion wants to help his friends but is there anyone to help him? Help Ilya, solve the problem.

Input

The first line contains stringSOf lengthN(2 cores ≤ CoresNLimit ≤ limit 105). It is guaranteed that the given string only consists of characters. and #.

The next line contains integerM(1 digit ≤ DigitMLimit ≤ limit 105)-the number of queries. Each of the nextMLines contains the description of the corresponding query.I-Th line contains integersLI, Bytes,RI(1 digit ≤ DigitLILatency <latencyRILimit ≤ limitN).

Output

PrintMIntegers-the answers to the queries in the order in which they are given in the input.

Sample test (s) Input
......43 42 31 62 6
Output
1154
Input
#..###51 35 61 53 63 4
Output
11220

 

 

The number of adjacent positions in [l, r] is equal.

 

Question Analysis: record the number of [1, I] items, which can be directly reduced.

 

#include 
 
  #include 
  
   #include using namespace std;int const MAX = 1e5 + 5;char s[MAX];int cnt[MAX];int main(){    scanf(%s, s + 1);    int len = strlen(s + 1);    for(int i = 1; i <= len; i++)    {        if(s[i] == s[i - 1])            cnt[i] = cnt[i - 1] + 1;        else            cnt[i] = cnt[i - 1];    }    int m;    scanf(%d, &m);    while(m --)    {        int l, r;        scanf(%d %d, &l, &r);        printf(%d, cnt[r] - cnt[l]);    }}
  
 

 

 

C. Ilya and Matrix time limit per test: 1 second memory limit per test: 256 megabytes
Ilya is a very good-natured lion. He likes maths. Of all mathematical objects, his favorite one is matrices. Now he's faced a complicated matrix problem he needs to solve.

He's got a square 2NLimit × limit 2N-Sized matrix and4NIntegers. You need to arrange all these numbers in the matrix (put each number in a single individual cell) so that thebeauty of the resulting matrix with numbers is maximum.

The beauty of a2NLimit × limit 2N-Sized matrix is an integer, obtained by the following algorithm:

Find the maximum element in the matrix. Let's denote it M. If NKeys = defaults 0, then the beauty of the matrix equals M. Otherwise, a matrix can be split into 4 non-intersecting2 NExecutor-cores 1 cores × cores 2NYour-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.

Input

The 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.

Output

On 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) Input
113
Output
13
Input
41 2 3 4
Output
14
Note

Consider 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.

Input

The 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 ).

Output

Print 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) Input
10 4 67 9 116 9 137 7 73 5 6
Output
17
Input
10 7 13 4 158 9 85 6 89 10 61 4 21 4 108 10 13
Output
2
Input
10 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.

Input

The 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.

Output

PrintN 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) Input
4 75 4 3 25 6 5 4
Output
6 4 2 1 
Input
5 52 4 4 1 31 0 1 2 4
Output
4 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]);}
        
       
      
     
    
   


 

 

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.