周中记录--2017.10.26

昨天晚上做了一场练习赛,出了两道水题……都是逻辑题。然后别的题就没看。

感觉自己读题的速度变慢了。就比以前差不少了。

先写一下这两个题吧。

Winnie-the-Pooh likes honey very much! That is why he decided to visit his friends. Winnie has got three best friends: Rabbit, Owl and Eeyore, each of them lives in his own house. There are winding paths between each pair of houses. The length of a path between Rabbit's and Owl's houses is a meters, between Rabbit's and Eeyore's house is b meters, between Owl's and Eeyore's house is c meters.

For enjoying his life and singing merry songs Winnie-the-Pooh should have a meal n times a day. Now he is in the Rabbit's house and has a meal for the first time. Each time when in the friend's house where Winnie is now the supply of honey is about to end, Winnie leaves that house. If Winnie has not had a meal the required amount of times, he comes out from the house and goes to someone else of his two friends. For this he chooses one of two adjacent paths, arrives to the house on the other end and visits his friend. You may assume that when Winnie is eating in one of his friend's house, the supply of honey in other friend's houses recover (most probably, they go to the supply store).

Winnie-the-Pooh does not like physical activity. He wants to have a meal n times, traveling minimum possible distance. Help him to find this distance.

Input

First line contains an integer n (1 ≤ n ≤ 100) — number of visits.

Second line contains an integer a (1 ≤ a ≤ 100) — distance between Rabbit's and Owl's houses.

Third line contains an integer b (1 ≤ b ≤ 100) — distance between Rabbit's and Eeyore's houses.

Fourth line contains an integer c (1 ≤ c ≤ 100) — distance between Owl's and Eeyore's houses.

Output

Output one number — minimum distance in meters Winnie must go through to have a meal n times.

Example
Input
3
2
3
1
Output
3
Input
1
2
3
5
Output
0
Note

In the first test case the optimal path for Winnie is the following: first have a meal in Rabbit's house, then in Owl's house, then in Eeyore's house. Thus he will pass the distance 2 + 1 = 3.

In the second test case Winnie has a meal in Rabbit's house and that is for him. So he doesn't have to walk anywhere at all.


然后这个题就比较简单了。有三个房子,给出移动的次数和三个房子两两之间的距离,求走n次最短的距离。房子可以来回走,就找最短距离就可以了。刚开始在R的房子里。

源代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <cmath>
using namespace std;
int main(){
    int n,a,b,c;
    while (scanf("%d%d%d%d",&n,&a,&b,&c)!=EOF){
        if (n == 1)
            printf("0\n");
        else if (n == 2){
            printf("%d\n",min(a,b));
        }
        else if (n > 2){
            int sum = 0;
            sum += min(a,b);
            for (int j = 2; j < n; j++)
                sum += min(min(a,b),c);
            printf("%d\n",sum);
        }
    }
}

另一个题如下:

You are given a multiset of n integers. You should select exactly k of them in a such way that the difference between any two of them is divisible by m, or tell that it is impossible.

Numbers can be repeated in the original multiset and in the multiset of selected numbers, but number of occurrences of any number in multiset of selected numbers should not exceed the number of its occurrences in the original multiset. 

Input

First line contains three integers nk and m (2 ≤ k ≤ n ≤ 100 0001 ≤ m ≤ 100 000) — number of integers in the multiset, number of integers you should select and the required divisor of any pair of selected integers.

Second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the numbers in the multiset.

Output

If it is not possible to select k numbers in the desired way, output «No» (without the quotes).

Otherwise, in the first line of output print «Yes» (without the quotes). In the second line print k integers b1, b2, ..., bk — the selected numbers. If there are multiple possible solutions, print any of them. 

Example
Input
3 2 3
1 8 4
Output
Yes
1 4 
Input
3 3 3
1 8 4
Output
No
Input
4 3 5
2 7 7 7
Output
Yes
2 7 7 

这个题是说,给n个数从中挑出k个数,使得这k个数的差%m == 0.

思路:如果k个数的差%m = 0,那么如果n个数中和m取余相同的数,差值和m取余就为0

源代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <stdio.h>
#include <cmath>
using namespace std;
int main(){
    int n,k,m;
    int a[100005];
    int flag[100005];//用于记录第i个数和m取余的值
    int flag1[100005];//用于记录第i个数和m取余的值的个数
    while (scanf("%d%d%d",&n,&k,&m)!=EOF){
        memset(flag1,0,sizeof(flag1));
        int flag2 = 0;
        int flag3 = 0;
        for (int i = 1; i <= n; i++){
            cin >> a[i];
            flag[i] = a[i] % m;
            flag1[flag[i]]++;
            if (flag1[flag[i]] == k){
                flag2 = 1;
                flag3 = flag[i];
            }
        }
        if (flag2 == 0)
            printf("No\n");
        else{
            printf("Yes\n");
            for (int i = 1; i <= n; i++){
                if (flag[i] == flag3){
                    printf("%d ",a[i]);
                    k--;
                }
                if (k == 0){
                    printf("\n");
                    break;
                }
            }
        }
    }
    return 0;
}

今天开了数位dp的题。明天上完课回来就开始做吧。争取完成任务。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值