Codeforces Round #419 (Div. 2) A B C D

A. Karen and Morning
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard 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 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.

。。。。。。

#include <cstdio>
#include <iostream>
#include <string.h>
using namespace std;
int n,m;
char s[5];

bool check(int n,int m) {
    if (n%10==m/10&&n/10==m%10) return true; else return false;
}

int main() {
    scanf("%d:%d",&n,&m);
    int ans=0;
    while (1) {
        if (check(n,m)) {
            cout << ans;
            return 0;
        }
        ans++;
        m++;
        if (m==60) {
            m=0;
            n++;
            if (n==24) n=0;
        }
    }
}

B. Karen and Coffee
time limit per test2.5 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard 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. 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.

The first one recommends brewing the coffee between 91 and 94 degrees, inclusive.
The second one recommends brewing the coffee between 92 and 97 degrees, inclusive.
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.

The first one, “wikiHow to make Cold Brew Coffee”, recommends brewing the coffee at exactly 1 degree.
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.

题意:长度200000的数组,要求实现将区间所有数加1,查询区间内多少个元素大于某个数两种操作。

利用前缀和。对区间(l,r)加1则令a[l]+1,a[r+1]-1,前缀和即为每一个数的和。加完之后利用树状数组查询。

#include <cstdio>
#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;
const int maxn=200005;
int a[maxn],sum[maxn],f[maxn];

int lowbit(int x) {
    return (x&(-x));
}

void update (int k,int c) {
    int i;
    for (i=k;i<=maxn-1;i+=lowbit(i)) {
        f[i]+=c;
    }
}

int find (int k) {
    int i,ans=0;
    for (i=k;i>=1;i-=lowbit(i)) {
        ans+=f[i];
    //  cout << ans << ' ' << i << endl;
    } 
    return ans;
}

int main() {
    int n,k,q,l,r,i,j;
    scanf("%d%d%d",&n,&k,&q);
    memset(a,0,sizeof(a));
    for (i=1;i<=n;i++) {
        scanf("%d%d",&l,&r);
        a[l]++;a[r+1]--;
    }
    sum[0]=0;
    for (i=1;i<=maxn-1;i++) {
        sum[i]=sum[i-1]+a[i];
    }
    memset(f,0,sizeof(f));
    for (i=1;i<=maxn-1;i++) {
        if (sum[i]>=k) update(i,1);
    }
    for (i=1;i<=q;i++) {
        scanf("%d%d",&l,&r);
        int ans=find(r);
        ans-=find(l-1);
        printf("%d\n",ans);
    }
    return 0;
}

C. Karen and Game
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard 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 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 gi, 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.

题意:n行m列的矩阵,每一次操作将整行+1或整列+1,求最少步数从0矩阵开始构造给定矩阵,并给出构造方法。无解输出-1。

题解:设第i行增加xi次,第i列增加yi次,则可将矩阵A=a i,j 表示如下:
a11=x1+y1 a12=x1+y2 …. a1m=x1+ym
a21=x2+y1 a22=x2+y2 …. a2m=x2+ym
………………………………………………………….
an1=xn+y1 an2=xn+y2 …. anm=xn+ym

化简得:
xi-xk=ai1-ak1=ai2-ak2=…=aim-akm (1<=i<=n,k为1到n间任意数字)
yi-yl=a1i-a1l=a2i-a2l=…=ami-aml (1<=i<=m,l为1到n间任意数字)

显然,线性方程组有解的充分条件为:
ai1-ak1=ai2-ak2=…=aim-akm且a1i-a1l=a2i-a2l=…=ami-aml

要使所有解和最小,只要使xk和yl最小。而xk+yl=akl,只要使akl最小,即A中最小元素。这时,分别令xk=0、yl=0,解出所有元素,两种解中的一个便是最优解。

#include <cstdio>
#include <iostream>
using namespace std;
int n,m;
int a[105][105],x[105],y[105];

int main() {
    int i,j,n,m;
    scanf("%d%d",&n,&m);
    for (i=1;i<=n;i++) 
        for (j=1;j<=m;j++) 
            scanf("%d",&a[i][j]);
    for (i=2;i<=n;i++) 
        for (j=2;j<=m;j++) 
            if ((a[i][j]-a[i-1][j])!=(a[i][j-1]-a[i-1][j-1])) {
                cout << -1;
                return 0;
            }
    for (j=2;j<=m;j++) 
        for (i=2;i<=n;i++) 
            if ((a[i][j]-a[i][j-1])!=(a[i-1][j]-a[i-1][j-1])) {
                cout << -1;
                return 0;
            }
    int ans=0,mi=0x3f3f3f3f,mini=0,minj=0;
    for (i=1;i<=n;i++) 
        for (j=1;j<=m;j++) 
            if (a[i][j]<mi) {
                mi=a[i][j];
                mini=i;
                minj=j;
            }
    for (i=1;i<=n;i++) {
        x[i]=a[i][1]-a[mini][1];
        ans+=x[i];
    }
    for (j=1;j<=m;j++) {
        y[j]=a[1][j]-a[1][minj];
        ans+=y[j];
    }
    if (n>m) {
        for (j=1;j<=m;j++) 
            y[j]+=mi;
        ans+=mi*m;
    } else{
        for (i=1;i<=n;i++) 
            x[i]+=mi;
        ans+=mi*n;
    }
    printf("%d\n",ans);
    for (i=1;i<=n;i++) 
        for (j=1;j<=x[i];j++) 
            printf("row %d\n",i);
    for (i=1;i<=m;i++) 
        for (j=1;j<=y[i];j++) 
            printf("col %d\n",i);
    return 0;
}

D题请戳:

http://blog.csdn.net/sinat_35406909/article/details/77717768

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值