Codeforces Round #247 (Div. 2) 总结

123 篇文章 0 订阅
A. Black Square
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Quite recently, a very smart student named Jury decided that lectures are boring, so he downloaded a game called "Black Square" on his super cool touchscreen phone.

In this game, the phone's screen is divided into four vertical strips. Each second, a black square appears on some of the strips. According to the rules of the game, Jury must use this second to touch the corresponding strip to make the square go away. As Jury is both smart and lazy, he counted that he wastes exactly ai calories on touching the i-th strip.

You've got a string s, describing the process of the game and numbers a1, a2, a3, a4. Calculate how many calories Jury needs to destroy all the squares?

Input

The first line contains four space-separated integers a1a2a3a4 (0 ≤ a1, a2, a3, a4 ≤ 104).

The second line contains string s (1 ≤ |s| ≤ 105), where the і-th character of the string equals "1", if on the i-th second of the game the square appears on the first strip, "2", if it appears on the second strip, "3", if it appears on the third strip, "4", if it appears on the fourth strip.

Output

Print a single integer — the total number of calories that Jury wastes.

Sample test(s)
input
1 2 3 4
123214
output
13
input
1 5 3 2
11221
output

13

题意:此题就是简单的模拟题,统计出每个区域内出现的正方形个数,然后乘以对应消耗值即可。

<span style="font-size:14px;">#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxm=1e5+10;
char s[maxm];
int main()
{
    int a1,a2,a3,a4;
    while(scanf("%d%d%d%d",&a1,&a2,&a3,&a4)!=EOF)
    {
        scanf("%s",s);
        int sum=0;
        for(int i=0;i<strlen(s);i++)
        {
            if(s[i]=='1')
                sum+=a1;
            else if(s[i]=='2')
                sum+=a2;
            else if(s[i]=='3')
                sum+=a3;
            else
                sum+=a4;
        }
        printf("%d\n",sum);
    }
    return 0;
}</span>

B. Shower Line
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks.

There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower.

Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the(2i - 1)-th man in the line (for the current moment) talks with the (2i)-th one.

Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower.

We know that if students i and j talk, then the i-th student's happiness increases by gij and the j-th student's happiness increases by gji. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower.

Input

The input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows gij(0 ≤ gij ≤ 105). It is guaranteed that gii = 0 for all i.

Assume that the students are numbered from 1 to 5.

Output

Print a single integer — the maximum possible total happiness of the students.

Sample test(s)
input
0 0 0 0 9
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
7 0 0 0 0
output
32
input
0 43 21 18 2
3 0 21 11 65
5 2 0 1 4
54 62 12 0 99
87 64 81 33 0
output
620
Note

In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals:

(g23 + g32 + g15 + g51) + (g13 + g31 + g54 + g45) + (g15 + g51) + (g54 + g45) = 32
<span style="font-size:14px;">#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxm=1e2+10;
int g[maxm][maxm];
int main()
{
    int a[5]={0,1,2,3,4};
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<5;j++)
        {
          scanf("%d",&g[i][j]);
        }
    }
    for(int i=0;i<5;i++)
    {
        for(int j=i+1;j<5;j++)
        {
            g[i][j]=g[j][i]=g[i][j]+g[j][i];
        }
    }
    int Max=0;
    do
    {
        Max=max(Max,g[a[0]][a[1]]+g[a[1]][a[2]]+g[a[2]][a[3]]*2+g[a[3]][a[4]]*2);
    }while(next_permutation(a,a+5));
    printf("%d\n",Max);
    return 0;
}</span>

C. k-Tree
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Quite recently a creative student Lesha had a lecture on trees. After the lecture Lesha was inspired and came up with the tree of his own which he called a k-tree.

k-tree is an infinite rooted tree where:

  • each vertex has exactly k children;
  • each edge has some weight;
  • if we look at the edges that goes from some vertex to its children (exactly k edges), then their weights will equal 1, 2, 3, ..., k.

The picture below shows a part of a 3-tree.

As soon as Dima, a good friend of Lesha, found out about the tree, he immediately wondered: "How many paths of total weight n (the sum of all weights of the edges in the path) are there, starting from the root of a k-tree and also containing at least one edge of weight at least d?".

Help Dima find an answer to his question. As the number of ways can be rather large, print it modulo 1000000007 (109 + 7).

Input

A single line contains three space-separated integers: nk and d (1 ≤ n, k ≤ 100; 1 ≤ d ≤ k).

Output

Print a single integer — the answer to the problem modulo 1000000007 (109 + 7).

Sample test(s)
input
3 3 2
output
3
input
3 3 3
output
1
input
4 3 2
output
6
input
4 5 2
output
7

题意:

题目大意:满k叉树,每个顶点有k条边,边的权重为1~k,现求出从根节点出发,有多少条路径,使得总权值恰好为N,并且每条路径上至少有一条权值不少于d的边。

题目思路:dp[i][h],第一个参数从1-n,遍历权值为i的情况,h有两种情况,0和1,0表示权值为i的所有情况,1表示权值为i的时候路径中存在一个值大于d的情况。

状态转移方程:

//权值为i的处理所有情况

    dp[i][0] = dp[i][0] + dp[i - j][0]

//处理权值为i且存在边大于等于d的情况

        /* if (j >= d) dp[i][1] = dp[i][1] + dp[i - j ][0]   

           else dp[i][1] = dp[i][1] + dp[i - j ][1]        */

        即:dp[i][1] = dp[i][1] + dp[i - j][j<d]

所以结果为dp[n][1]

<span style="font-size:14px;">#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxm=1e2+10;
const int MOD=1e9+7;
int dp[maxm][2];
int main()
{
    int n,k,d;
    while(scanf("%d%d%d",&n,&k,&d)!=EOF)
    {
        dp[0][0]=1;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=min(k,i);j++)
            {
                dp[i][0]=(dp[i][0]+dp[i-j][0])%MOD;
                if(j>=d)
                    dp[i][1]=(dp[i][1]+dp[i-j][0])%MOD;
                else
                    dp[i][1]=(dp[i][1]+dp[i-j][1])%MOD;
            }
        }
        printf("%d\n",dp[n][1]%MOD);
    }
    return 0;
}</span>


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值