Codeforces Round #419 (Div. 2)

Codeforces Round #419 (Div. 2)

Table of Contents Codeforces Round #419 (Div. 2)A. Karen and MorningB. Karen and CoffeeC. Karen and GameE. Karen and Supermarket

A. Karen and Morning

A. Karen and Morning

Karen is getting ready for a new school day!

It is currently hh:mm, given in a 24-hour format. As you know, Karen loves palindromes, and she believes that it is good luck to wake up when the time is a palindrome.

What is the minimum number of minutes she should sleep, such that, when she wakes up, the time is a palindrome?

Remember that a palindrome is a string that reads the same forwards and backwards. For instance, 05:39 is not a palindrome, because 05:39 backwards is 93:50. On the other hand, 05:50 is a palindrome, because 05:50 backwards is 05:50.

Input

The first and only line of input contains a single string in the format hh:mm (00 ≤  hh  ≤ 23, 00 ≤  mm  ≤ 59).

Output

Output a single integer on a line by itself, the minimum number of minutes she should sleep, such that, when she wakes up, the time is a palindrome.

Examples

input

05:39

output

11

input

13:31

output

0

input

23:59

output

1

Note

In the first test case, the minimum number of minutes Karen should sleep for is 11. She can wake up at 05:50, when the time is a palindrome.

In the second test case, Karen can wake up immediately, as the current time, 13:31, is already a palindrome.

In the third test case, the minimum number of minutes Karen should sleep for is 1 minute. She can wake up at 00:00, when the time is a palindrome.

题意:给一个时间,求过多久后变成回文

分析:注意没有60'的时间。

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int h,m;
    scanf("%d:%d",&h,&m);

    int tmp = 0;
    int htmp = h;
    tmp = h % 10;
    tmp = tmp * 10;
    tmp = tmp + h / 10;
    //printf("%d
",tmp);

    if(tmp>=m&&tmp<60)
        printf("%d
",tmp-m);
    else {
        h++;

        while(true) {
            tmp = 0;
            tmp = h % 10;
            tmp = tmp * 10;
            tmp = tmp + h / 10;
            if(tmp<60)
                break;
            h++;
        }

        tmp = (h - htmp-1)*60 + tmp + (60-m);

        if(h==24) {
            tmp = 0;
            printf("%d
",60-m);
        }
        else printf("%d
",tmp);
    }

    return 0;
}

B. Karen and Coffee

B. Karen and Coffee

To stay woke and attentive during classes, Karen needs some coffee!

Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe".

She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste.

Karen thinks that a temperature is admissible if at least k recipes recommend it.

Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range?

Input

The first line of input contains three integers, n, k (1 ≤ k ≤ n ≤ 200000), and q (1 ≤ q ≤ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively.

The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≤ li ≤ ri ≤ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive.

The next q lines describe the questions. Each of these lines contains a and b, (1 ≤ a ≤ b ≤ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive.

Output

For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive.

Examples

input

3 2 4
91 94
92 97
97 99
92 94
93 97
95 96
90 100

output

3
3
0
4

input

2 1 1
1 1
200000 200000
90 100

output

0

Note

In the first test case, Karen knows 3 recipes.

  1. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive.
  2. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive.
  3. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive.

A temperature is admissible if at least 2 recipes recommend it.

She asks 4 questions.

In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible.

In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible.

In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none.

In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible.

In the second test case, Karen knows 2 recipes.

  1. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree.
  2. The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees.

A temperature is admissible if at least 1 recipe recommends it.

In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none.

题意:要3本书中,要有至少2本书符合条件,三本书的描叙是,[l,r] 说,咖啡的最适合的温度,然后4个问题,[l,r] 说,这些温度里面,有几个温度是符合条件的额。

分析:直接用前缀和会超时,差分递推;

#include <bits/stdc++.h>

using namespace std;

const int maxn = 200000+5;
int d[maxn];
int ans[maxn];

int main()
{
    //freopen("out.txt","w",stdout);
    int n,k,q;
    scanf("%d%d%d",&n,&k,&q);

    for(int i=0;i<n;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        d[l]++;
        r++;
        d[r]--;
    }

    for(int i=1;i<maxn;i++)
        d[i] = d[i-1] + d[i];

    //for(int i=0;i<maxn;i++)
        //printf("%d
",d[i]);

    for(int i=1;i<maxn;i++)
        if(d[i]>=k)
            ans[i] = ans[i-1] + 1;
        else ans[i] = ans[i-1];

    while(q--) {
        int l,r;
        scanf("%d%d",&l,&r);
        l--;
        printf("%d
",ans[r]-ans[l]);
    }

    return 0;
}

C. Karen and Game

C. Karen and Game

On the way to school, Karen became fixated on the puzzle game on her phone!

The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0.

One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column.

To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g**i, j.

Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task!

Input

The first line of input contains two integers, n and m (1 ≤ n, m ≤ 100), the number of rows and the number of columns in the grid, respectively.

The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains gi, j (0 ≤ gi, j ≤ 500).

Output

If there is an error and it is actually not possible to beat the level, output a single integer -1.

Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level.

The next k lines should each contain one of the following, describing the moves in the order they must be done:

  • row x, (1 ≤ x ≤ n) describing a move of the form "choose the x-th row".
  • col x, (1 ≤ x ≤ m) describing a move of the form "choose the x-th column".

If there are multiple optimal solutions, output any one of them.

Examples

input

3 5
2 2 2 3 2
0 0 0 1 0
1 1 1 2 1

output

4
row 1
row 1
col 4
row 3

input

3 3
0 0 0
0 1 0
0 0 0

output

-1

input

3 3
1 1 1
1 1 1
1 1 1

output

3
row 1
row 2
row 3

Note

In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level:

In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center.

In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level:

Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.

题意:最少多少步骤变成给出的图。

分析:贪心,row>col时,贪心每次开始直接加行数,那么之前就先填好列(最少的数)。

#include <bits/stdc++.h>

using namespace std;

int n,m;
const int maxn = 100 + 5;
int a[maxn][maxn];
int suma,sumb;
vector<int> r,c;

void col() {
    for(int i=0;i<m;i++) {
        int minv = a[0][i];
        for(int j=1;j<n;j++)
            minv = min(minv,a[j][i]);

        for(int j=0;j<n;j++)
            a[j][i]-=minv;

        sumb +=minv*n;
        for(int k=0;k<minv;k++)
            c.push_back(i+1);
    }
}

void row() {
    for(int i=0;i<n;i++) {
        int minv = a[i][0];
        for(int j=1;j<m;j++)
            minv = min(minv,a[i][j]);

        for(int j=0;j<m;j++)
            a[i][j]-=minv;

        sumb +=minv*m;
        for(int k=0;k<minv;k++)
            r.push_back(i+1);
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++) {
        for(int j=0;j<m;j++)
        {
            scanf("%d",&a[i][j]);
            suma += a[i][j];
        }
    }

    if(n>m) {
        col();
        row();
    }
    else {
        row();
        col();
    }

    if(suma!=sumb)
        puts("-1");
    else {
        printf("%d
",r.size()+c.size());
        for(int i=0;i<r.size();i++)
            printf("row %d
",r[i]);
        for(int i=0;i<c.size();i++)
            printf("col %d
",c[i]);
    }

    return 0;
}

E. Karen and Supermarket

E. Karen and Supermarket

On the way home, Karen decided to stop by the supermarket to buy some groceries.

She needs to buy a lot of goods, but since she is a student her budget is still quite limited. In fact, she can only spend up to b dollars.

The supermarket sells n goods. The i-th good can be bought for c**i dollars. Of course, each good can only be bought once.

Lately, the supermarket has been trying to increase its business. Karen, being a loyal customer, was given n coupons. If Karen purchases the i-th good, she can use the i-th coupon to decrease its price by d**i. Of course, a coupon cannot be used without buying the corresponding good.

There is, however, a constraint with the coupons. For all i ≥ 2, in order to use the i-th coupon, Karen must also use the x**i-th coupon (which may mean using even more coupons to satisfy the requirement for that coupon).

Karen wants to know the following. What is the maximum number of goods she can buy, without exceeding her budget b?

Input

The first line of input contains two integers n and b (1 ≤ n ≤ 5000, 1 ≤ b ≤ 109), the number of goods in the store and the amount of money Karen has, respectively.

The next n lines describe the items. Specifically:

  • The i-th line among these starts with two integers, ci and di (1 ≤ di < ci ≤ 109), the price of the i-th good and the discount when using the coupon for the i-th good, respectively.
  • If i ≥ 2, this is followed by another integer, xi (1 ≤ xi < i), denoting that the x**i-th coupon must also be used before this coupon can be used.

Output

Output a single integer on a line by itself, the number of different goods Karen can buy, without exceeding her budget.

Examples

input

6 16
10 9
10 5 1
12 2 1
20 18 3
10 2 3
2 1 5

output

4

input

5 10
3 1
3 1 1
3 1 2
3 1 3
3 1 4

output

5

Note

In the first test case, Karen can purchase the following 4 items:

  • Use the first coupon to buy the first item for 10 - 9 = 1 dollar.
  • Use the third coupon to buy the third item for 12 - 2 = 10 dollars.
  • Use the fourth coupon to buy the fourth item for 20 - 18 = 2 dollars.
  • Buy the sixth item for 2 dollars.

The total cost of these goods is 15, which falls within her budget. Note, for example, that she cannot use the coupon on the sixth item, because then she should have also used the fifth coupon to buy the fifth item, which she did not do here.

In the second test case, Karen has enough money to use all the coupons and purchase everything.

题意:结点优惠时,必须优惠,求最多能选多少个物品。

分析:转换题意,dp[u][j][0/1] :根节点u,选j个物品,用不用优惠券的最少费用。

然后枚举j即可。

树形背包:

dp[u][j+k][0] = min(dp[u][j+k][0],dp[u][j][0]+dp[v][k][0])

dp[u][j+k][1] = min(dp[u][j+k][1],dp[u][j][1]+dp[v][k][1])

dp[u][j+k][1] = min(dp[u][j+k][1],dp[u][j][1]+dp[v][k][0])

#include <bits/stdc++.h>

using namespace std;

const int maxn = 5000+5;
const int inf = 0x3f3f3f3f;
int dp[maxn][maxn][2];
int c[maxn],d[maxn];
vector<int> e[maxn];

int sz[maxn];

void dfs(int u) {
    sz[u] = 1;
    dp[u][0][0] = 0;
    dp[u][1][0] = c[u];
    dp[u][1][1] = c[u] - d[u];
    for(int i=0;i<e[u].size();i++) {
        int v = e[u][i];
        dfs(v);

        for(int j=sz[u];j>=0;j--) {
            for(int k=0;k<=sz[v];k++) {
                dp[u][j+k][0] = min(dp[u][j+k][0],dp[u][j][0]+dp[v][k][0]);
                dp[u][j+k][1] = min(dp[u][j+k][1],dp[u][j][1]+dp[v][k][1]);
                dp[u][j+k][1] = min(dp[u][j+k][1],dp[u][j][1]+dp[v][k][0]);
            }
        }
        sz[u] +=sz[v];
    }

}

int n,b;
int main()
{
    memset(dp,inf,sizeof(dp));
    scanf("%d%d",&n,&b);

    scanf("%d%d",&c[1],&d[1]);
    for(int i=2;i<=n;i++) {
        int u;
        scanf("%d%d%d",&c[i],&d[i],&u);
        e[u].push_back(i);
    }

    dfs(1);
    int ans=0;
    for(int i=n;i>=0;i--)
    {
        if(dp[1][i][0]<=b||dp[1][i][1]<=b)
        {
            ans=i;
            break;
        }
    }
    cout<<ans<<endl;

    return 0;
}
原文地址:https://www.cnblogs.com/TreeDream/p/7163449.html