Codeforces Round #419

来源:互联网 发布:js判断ie版本是否大于8 编辑:程序博客网 时间:2024/04/30 13:03
A. Karen and Morning
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Karen is getting ready for a new school day!

It is currently hh:mm, given in a 24-hour format. As you know, Karen lovespalindromes, 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, because05:39 backwards is 93:50. On the other hand,05:50 is a palindrome, because05:50 backwards is 05:50.

Input

The first and only line of input contains a single string in the formathh: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 is11. She can wake up at05: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 is1 minute. She can wake up at00:00, when the time is a palindrome.

题目大意:问至少经过长时间,时间可以变成回文的。

直接从当前时间开始一直+1模拟,记下时间,满足就输出。

#include<cstdio>#include<algorithm>#include<cstring>using namespace std;int main(){    int a,b,t;    char c;    while(scanf("%d%c%d",&a,&c,&b) != EOF)    {        t = 0;        while(a / 10 != b % 10 || a % 10 != b / 10)        {            ++b;            if(b == 60) ++a,b = 0;            if(a == 24) a = 0;            ++t;        }        printf("%d\n",t);    }    return 0;}
B. Karen and Coffee
time limit per test
2.5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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. Thei-th recipe suggests that coffee should be brewed betweenli andri 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 betweena andb, 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), andq (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, thei-th line among these contains two integersli andri (1 ≤ li ≤ ri ≤ 200000), describing that thei-th recipe suggests that the coffee be brewed betweenli andri degrees, inclusive.

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

Output

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

Examples
Input
3 2 491 9492 9797 9992 9493 9795 9690 100
Output
3304
Input
2 1 11 1200000 20000090 100
Output
0
Note

In the first test case, Karen knows 3 recipes.

  1. The first one recommends brewing the coffee between91 and94 degrees, inclusive.
  2. The second one recommends brewing the coffee between92 and97 degrees, inclusive.
  3. The third one recommends brewing the coffee between97 and99 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 between92 and94 degrees, inclusive. There are3:92, 93 and 94 degrees are all admissible.

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

In her third question, she wants to know the number of admissible integer temperatures between95 and96 degrees, inclusive. There are none.

In her final question, she wants to know the number of admissible integer temperatures between90 and100 degrees, inclusive. There are4: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 exactly1 degree.
  2. The second one, "What good is coffee that isn't brewed at at least36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly200000 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.

题目大意:给出 n 个可重叠的区间,进行 q 次查询,每次查询一个区间内满足 被覆盖次数大于等于 k 的点数。

用一个数组模拟区间被覆盖的操作,例如每次读入一个 [l,r],cal[l] ++,cal[r + 1] -- 就表示这段区间被覆盖一次。等读完n次,扫描一次 cal 数组,用一个临时变量 num 表示当前点被覆盖的次数,维护一个前缀和 s[i]:区间 1 ~ i 内 满足被覆盖次数大于等于 k 的点数,则可在O(1)内完成查询操作。

#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int maxn = 200000 + 5;int cal[maxn],s[maxn];int main(){     int n,k,q,l,r;     memset(cal,0,sizeof(cal));     memset(s,0,sizeof(s));     scanf("%d %d %d",&n,&k,&q);     for(int i = 0;i < n; ++i)     {         scanf("%d %d",&l,&r);         cal[l]++,cal[r + 1]--;     }     int num = 0;     for(int i = 1;i < maxn; ++i)     {         num += cal[i];         s[i] = s[i - 1];         if(num >= k) s[i] += 1;     }     for(int i = 0;i < q; ++i)     {         scanf("%d %d",&l,&r);         printf("%d\n",s[r] - s[l - 1]);     }     return 0;}
C. Karen and Game
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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 withn rows and m columns. Each cell originally contains the number0.

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

To win the level, after all the moves, the number in the cell at thei-th row and j-th column should be equal togi, 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 andm (1 ≤ n, m ≤ 100), the number of rows and the number of columns in the grid, respectively.

The next n lines each containm integers. In particular, the j-th integer in thei-th of these rows containsgi, 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 integerk, 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 thex-th row".
  • col x, (1 ≤ x ≤ m) describing a move of the form "choose thex-th column".

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

Examples
Input
3 52 2 2 3 20 0 0 1 01 1 1 2 1
Output
4row 1row 1col 4row 3
Input
3 30 0 00 1 00 0 0
Output
-1
Input
3 31 1 11 1 11 1 1
Output
3row 1row 2row 3
Note

In the first test case, Karen has a grid with 3 rows and5 columns. She can perform the following4 moves to beat the level:

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

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

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

题目大意:给定一个 n*m 矩阵,每次可以 给一行加一 或 给一列加一,问至少经过多少次操作可以使矩阵从一个空矩阵变成当前矩阵,并输出操纵。

一开始由于没看到至少就gg了,感觉就是个暴力模拟的题。。。先行后列模拟一次,再先列后行模拟一次,哪个小按哪个来就OK。代码写得丑,勿喷。。。

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 105;const int INF = 1 << 30;int r[2][maxn],c[2][maxn],c1[maxn],c2[maxn],r1[maxn],r2[maxn];int a1[maxn][maxn],a2[maxn][maxn];int main(){    int n,m,t,sum1 = 0,sum2 = 0;    for(int i = 1;i <= maxn; ++i)    {        r1[i] = c1[i] = r[0][i] = c[1][i] = INF;        r2[i] = c2[i] = c[0][i] = r[1][i] = 0;    }    scanf("%d %d",&n,&m);    for(int i = 1;i <= n; ++i)    {        for(int j = 1;j <= m; ++j)        {            scanf("%d",&t);            a1[i][j] = a2[i][j] = t;            if(t < r[0][i]) r[0][i] = t;            c[0][j] += t;            if(t < c[1][j]) c[1][j] = t;            r[1][i] += t;        }    }    for(int i = 1;i <= n; ++i)    {        sum1 += r[0][i];        for(int j = 1;j <= m; ++j)        {            a1[i][j] -= r[0][i];            t = a1[i][j];            if(t < c1[j]) c1[j] = t;            if(t > c2[j]) c2[j] = t;        }    }    for(int j = 1;j <= m; ++j)    {        sum2 += c[1][j];        for(int i = 1;i <= n; ++i)        {            a2[i][j] -= c[1][j];            t = a2[i][j];            if(t < r1[i]) r1[i] = t;            if(t > r2[i]) r2[i] = t;        }    }    for(int i = 1;i <= m; ++i) sum1 = sum1 + c1[i];    for(int i = 1;i <= n; ++i) sum2 = sum2 + r1[i];    bool judge = false;    for(int i = 1;i <= m; ++i)    {        if(c2[i] > c1[i]) judge = true;    }    if(judge) printf("-1\n");    else if(sum1 <= sum2)    {        printf("%d\n",sum1);        for(int i = 1;i <= n; ++i)        {            if(r[0][i] && r[0][i] !=INF)            {                for(int j = 1;j <= m; ++j) c[0][j] -= r[0][i];                while(r[0][i]--) printf("row %d\n",i);            }        }        for(int i = 1;i <= m; ++i)        {            c[0][i] /= n;            if(c[0][i]) while(c[0][i]--) printf("col %d\n",i);        }    }    else if(sum1 > sum2)    {        printf("%d\n",sum2);        for(int i = 1;i <= m; ++i)        {            if(c[1][i] && c[1][i] != INF)            {                for(int j = 1;j <= n; ++j) r[1][j] -= c[1][i];                while(c[1][i]--) printf("col %d\n",i);            }        }        for(int i = 1;i <= n; ++i)        {            r[1][i] /= m;            if(r[1][i]) while(r[1][i]--) printf("row %d\n",i);        }    }    return 0;}