codeforces835 b贪心 c 二维前缀和

来源:互联网 发布:php中的错误类型有哪些 编辑:程序博客网 时间:2024/05/20 10:10

B. The number on the board
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Some natural number was written on the board. Its sum of digits was not less thank. But you were distracted a bit, and someone changed this number ton, replacing some digits with others. It's known that the length of the number didn't change.

You have to find the minimum number of digits in which these two numbers can differ.

Input

The first line contains integer k (1 ≤ k ≤ 109).

The second line contains integer n (1 ≤ n < 10100000).

There are no leading zeros in n. It's guaranteed that this situation is possible.

Output

Print the minimum number of digits in which the initial number and n can differ.

Examples
Input
311
Output
1
Input
399
Output
0
Note

In the first example, the initial number could be 12.

In the second example the sum of the digits of n is not less thank. The initial number could be equal to n.



#include <iostream>#include <cstdio>#include <string.h>#include <algorithm>#define LL long longusing namespace std;const int AX = 1e6;char a[AX];int  b[AX];int main(){int k ;scanf("%d",&k);scanf("%s",a);int len = strlen(a);int ans = 0;for( int i = 0 ; i < len ; i++ ){ans += (a[i] - '0');if( ans >= k ) {printf("0\n");return 0;}b[i] = a[i] - '0';}sort(b,b+len);int temp = k - ans;for( int  i = 0 ; i < len ; i++ ){temp -= (9-b[i]);if( temp <= 0 ) {printf("%d\n",i+1); return 0;}}return 0;}




C. Star sky
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi,yi), a maximum brightnessc, equal for all stars, and an initial brightnesssi (0 ≤ si ≤ c).

Over time the stars twinkle. At moment 0 the i-th star has brightness si. Let at momentt some star has brightnessx. Then at moment (t + 1) this star will have brightnessx + 1, ifx + 1 ≤ c, and0, otherwise.

You want to look at the sky q times. In thei-th time you will look at the momentti and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x1i,y1i) and the upper right — (x2i,y2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.

A star lies in a rectangle if it lies on its border or lies strictly inside it.

Input

The first line contains three integers n,q,c (1 ≤ n, q ≤ 105,1 ≤ c ≤ 10) — the number of the stars, the number of the views and the maximum brightness of the stars.

The next n lines contain the stars description. Thei-th from these lines contains three integersxi,yi,si (1 ≤ xi, yi ≤ 100,0 ≤ si ≤ c ≤ 10) — the coordinates ofi-th star and its initial brightness.

The next q lines contain the views description. Thei-th from these lines contains five integersti,x1i,y1i,x2i,y2i (0 ≤ ti ≤ 109,1 ≤ x1i < x2i ≤ 100,1 ≤ y1i < y2i ≤ 100) — the moment of thei-th view and the coordinates of the viewed rectangle.

Output

For each view print the total brightness of the viewed stars.

Examples
Input
2 3 31 1 13 2 02 1 1 2 20 2 1 4 55 1 1 5 5
Output
303
Input
3 4 51 1 22 3 03 3 10 1 1 100 1001 2 2 4 42 2 1 4 71 50 50 51 51
Output
3350
Note

Let's consider the first example.

At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.

At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.

At the third view, you can see both stars. At moment 5 brightness of the first is2, and brightness of the second is1, so the answer is 3.

#include <iostream>#include <cstdio>#include <string.h>#include <algorithm>#define LL long longusing namespace std;int a[15][200][200];int main(){int n , q , c;scanf("%d%d%d",&n,&q,&c);int x,y,s;while( n-- ){scanf("%d%d%d",&x,&y,&s);for( int i = 0 ; i <= 10 ; i++ )a[i][x][y] += ( s + i )%( c + 1 ) ;}for( int k = 0 ; k <= 10 ; ++k){for(int i = 1 ; i <= 100 ; i++ ){for( int j = 1 ; j <=100 ; j++ ){a[k][i][j] += a[k][i-1][j] + a[k][i][j-1] -a[k][i-1][j-1];}}}int t, x1, y1, x2, y2; while( q -- ){int ans = 0;scanf("%d%d%d%d%d",&t,&x1,&y1,&x2,&y2);t %= ( c + 1 );ans = a[t][x2][y2] - a[t][x1-1][y2]-a[t][x2][y1-1]+a[t][x1-1][y1-1];printf("%d\n",ans);}return 0;}



               
原创粉丝点击