Codeforces Round #427 (Div. 2)

B. The number on the board

题意:

有一个数字,它的每个数位上的数字的和不小于等于k。现在他改变了若干位,变成了一个新的数n,问现在的数和原来的数最多有多少位不同。

思路:

如果现在的数字各位数字之和大于等于k,那么它就可能没有被改变。

反之,那么每个数的最大改变量就是9减去这个数,于是把9减去每个数得到的结果从大到小排序,用k减去现在的数位和得到的结果记为kk,遍历一遍差量数组,用kk去减,知道kk小于0跳出。

代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 using namespace std;
 5
 6 bool cmp(int a,int b)
 7 {
 8     return a > b;
 9 }
10
11 int b[100005];
12
13 int main()
14 {
15     long long k;
16
17     scanf("%I64d",&k);
18
19     char s[100005];
20
21     long long sum = 0;
22
23     scanf("%s",s);
24
25     int len =strlen(s);
26
27     for (int i = 0;i < len;i++)
28     {
29         int t = s[i] - ‘0‘;
30         b[i] = 9 - t;
31         sum += t;
32     }
33
34
35     if (sum >= k) printf("0\n");
36     else
37     {
38         sort(b,b+len,cmp);
39
40         long long ans = k - sum;
41
42         int num = 0;
43
44         for (int i = 0;i < len;i++)
45         {
46             ans -= b[i];
47
48             num++;
49
50             if (ans <= 0)
51             {
52                 break;
53             }
54         }
55
56         printf("%d\n",num);
57     }
58
59     return 0;
60 }

C. Star sky

题意:

在一个平面直角坐标系上,有闪亮的星星。每个星星的最大亮度为相同的c,每颗星星有不同的起始亮度si,从开始时刻起每一秒亮度加1,如果当前亮度为c,那么下一秒就是0.

一开始给出每个星星的坐标和初始亮度,之后给出若干个询问,给出此时的时刻,矩形的左下角的坐标和右上角的坐标,统计在这个矩形内的所有的星星的亮度的总和(包括边界)。

思路:

一开始用暴力直接tle,遂在赛后看题解补题。前缀法,总的来说就是sum (a,b,c,d) = pre(c-1,b) - pre(a,y-1)-pre(c - 1,d - 1);

什么意思呢,pre(a,b)代表的是在(0,0)到(a,b)这个区间内的所有星星的总数(当然是分亮度的),看图

在每个询问的时候,统计0到c的亮度的星星在t时刻的亮度 (j + t) % (c + 1)

然后对亮度进行累加。(中间把j写成了tt,wa了无数次,还是要深入思考,彻底搞懂)

代码:

#include <stdio.h>
#include <string.h>

int pre[105][105][15];

int main()
{
    int n,q,c;

    scanf("%d%d%d",&n,&q,&c);

    for (int i = 0;i < n;i++)
    {
        int x,y,s;

        scanf("%d%d%d",&x,&y,&s);

        pre[x][y][s]++;
    }

    for (int i = 1;i <= 100;i++)
        for (int j = 1;j <=100;j++)
        for (int k = 0;k <= c;k++)
        {
            pre[i][j][k] += (pre[i-1][j][k] + pre[i][j-1][k] - pre[i-1][j-1][k]);
        }

    for (int i = 0;i < q;i++)
    {
        int t,x,y,a,b;

        scanf("%d%d%d%d%d",&t,&x,&y,&a,&b);

        int ans = 0;

        for (int j = 0;j <= c;j++)
        {
            int tt = (j+t) % (c + 1);

            int tmp = pre[a][b][j] - pre[a][y-1][j] - pre[x-1][b][j] + pre[x-1][y-1][j];

            ans += tmp * tt;
        }

        printf("%d\n",ans);
    }

    return 0;
}
时间: 2024-07-30 02:58:05

Codeforces Round #427 (Div. 2)的相关文章

Codeforces Round #427 (Div. 2) C. Star sky(二维前缀和)

题目链接:Codeforces Round #427 (Div. 2) C. Star sky 题意: 在一个二维平面上有n个星星,每个星星有一个初始的亮度,每过去一秒,星星的亮度变化为(s+1)%(c+1). 现在有q个询问,问t秒后一个矩形区域的星星的总亮度为多少. 题解: 由于c不大,将每颗星星按照初始亮点分类,每一类在任意时间的亮度都是相同的,然后对应加一加就行了. 1 #include<bits/stdc++.h> 2 #define RT(l,r) (l+r|l!=r) 3 #de

Codeforces Round #427 (Div. 2) D. Palindromic characteristics(Manacher求回文串)

题目链接:Codeforces Round #427 (Div. 2) D. Palindromic characteristics 题意: 给你一个串,定义k-th回文串,让你求每个k-th的数量. 题解: manacher处理好后做一下dp就行了. 当然也可以直接dp不用manacher. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 5 cons

Codeforces Round #427 (Div. 2) Problem C Star sky (Codeforces 835C) - 前缀和

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

Codeforces Round #427 (Div. 2) D. Palindromic characteristics

D. Palindromic characteristics Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes. A string is 1-palindrome if and only if

Codeforces Round #427 (Div. 2) D dp

D. Palindromic characteristics 题意:求给定字符串每阶回文子串有多少个. tags:根本没想到 dp..直接看官方题解吧 dp[i][j] 代表第 i 个字符到第 j 个字符的子串是几阶回文. Solution. Let's calculate the following dp. dp[l][r] is the maximum k such that the substring built from characters from l to r is k-palin

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我