Codeforces Round #441

A. Trip For Meal
time limit per test
1 second
memory limit per test
512 megabytes

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.

Examples
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 个点的最小距离,可以重复经过这些点。

可以直接推的,可能细节有点多。好在数据不大,直接报搜就行了,每次优先走短的。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef struct node
{
    int v,c;
    node(int vv = 0,int cc = 0):v(vv),c(cc){};
}node;
const int maxn = 5;
int n,sum = 0;
vector<node> v[maxn];
bool cal(int now)
{
    if(n == 0) return true;
    int x = v[now][0].v,y = v[now][1].v;
    int xc = v[now][0].c,yc = v[now][1].c;
    int nex;
    if(xc > yc) nex = y,sum += yc;
    else nex = x,sum += xc;
    --n;
    if(cal(nex)) return true;
}
int main()
{
    int x;
    scanf("%d",&n);
    scanf("%d",&x);
    v[1].push_back(node(2,x));
    v[2].push_back(node(1,x));
    scanf("%d",&x);
    v[1].push_back(node(3,x));
    v[3].push_back(node(1,x));
    scanf("%d",&x);
    v[2].push_back(node(3,x));
    v[3].push_back(node(2,x));
    --n;
    cal(1);
    printf("%d\n",sum);
    return 0;
}
B. Divisiblity of Differences
time limit per test
1 second
memory limit per test
512 megabytes

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 n, k and m (2 ≤ k ≤ n ≤ 100 000, 1 ≤ 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.

Examples
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 个,他们之间两两的差是 m 的倍数。

 

把 a[i] 改成 a[i] % m,然后排下序。模 m 同余的数就满足题中的条件。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef struct node
{
    int a,b;
}node;
bool cmp(node x,node y)
{
    return x.b < y.b;
}
const int maxn = 1e5 + 5;
int n,m,k;
node s[maxn];
void print(int x)
{
    printf("Yes\n");
    for(int j = 1,i = x - k + 1;j <= k; ++j,++i) printf("%d ",s[i].a);
    printf("\n");
}
int main()
{
    scanf("%d %d %d",&n,&k,&m);
    for(int i = 0;i < n; ++i)
    {
        scanf("%d",&s[i].a);
        s[i].b = s[i].a % m;
    }
    sort(s,s + n,cmp);
    s[n].a = s[n].b = -1;
    int cou,p = 0;
    while(p < n)
    {
        cou = 1;
        while(p < n - 1 && s[p].b == s[p + 1].b) ++cou,++p;
        if(cou >= k) break;
        ++p;
    }
    if(cou >= k) print(p);
    else printf("No\n");
    return 0;
}
 
C. Classroom Watch
time limit per test
1 second
memory limit per test
512 megabytes

Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer x was given. The task was to add x to the sum of the digits of the number x written in decimal numeral system.

Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova.

Input

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

Output

In the first line print one integer k — number of different values of x satisfying the condition.

In next k lines print these values in ascending order.

Examples
Input
21
Output
1
15
Input
20
Output
0
Note

In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21.

In the second test case there are no such x.

题目大意:给定一个数 n ,问有多少个 x 满足 x 加上它的个位数字的和等于 n 。
由于 n < 1e9,所以 x 的各位数字的和不会超过100。从 n 往下枚举100个数即可。
代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + 5;
int a[maxn];
int n;
int cal(int x)
{
    int sum = 0;
    while(x > 0)
    {
        sum += x % 10;
        x /= 10;
    }
    return sum;
}
int main()
{
    int num = 0;
    scanf("%d",&n);
    for(int i = 0;i <= 100; ++i)
    {
        if(n == n - i + cal(n - i)) a[num++] = n - i;
    }
    printf("%d\n",num);
    if(num > 0)
    {
        sort(a,a + num);
        for(int i = 0;i < num; ++i) printf("%d\n",a[i]);
    }
    return 0;
}

 

D. Sorting the Coins
time limit per test
1 second
memory limit per test
512 megabytes

Recently, Dima met with Sasha in a philatelic store, and since then they are collecting coins together. Their favorite occupation is to sort collections of coins. Sasha likes having things in order, that is why he wants his coins to be arranged in a row in such a way that firstly come coins out of circulation, and then come coins still in circulation.

For arranging coins Dima uses the following algorithm. One step of his algorithm looks like the following:

  1. He looks through all the coins from left to right;
  2. If he sees that the i-th coin is still in circulation, and (i + 1)-th coin is already out of circulation, he exchanges these two coins and continues watching coins from (i + 1)-th.

Dima repeats the procedure above until it happens that no two coins were exchanged during this procedure. Dima calls hardness of ordering the number of steps required for him according to the algorithm above to sort the sequence, e.g. the number of times he looks through the coins from the very beginning. For example, for the ordered sequence hardness of ordering equals one.

Today Sasha invited Dima and proposed him a game. First he puts n coins in a row, all of them are out of circulation. Then Sasha chooses one of the coins out of circulation and replaces it with a coin in circulation for n times. During this process Sasha constantly asks Dima what is the hardness of ordering of the sequence.

The task is more complicated because Dima should not touch the coins and he should determine hardness of ordering in his mind. Help Dima with this task.

Input

The first line contains single integer n (1 ≤ n ≤ 300 000) — number of coins that Sasha puts behind Dima.

Second line contains n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) — positions that Sasha puts coins in circulation to. At first Sasha replaces coin located at position p1, then coin located at position p2 and so on. Coins are numbered from left to right.

Output

Print n + 1 numbers a0, a1, ..., an, where a0 is a hardness of ordering at the beginning, a1 is a hardness of ordering after the first replacement and so on.

Examples
Input
4
1 3 4 2
Output
1 2 3 2 1
Input
8
6 8 3 4 7 2 1 5
Output
1 2 2 3 4 3 4 5 1
Note

Let's denote as O coin out of circulation, and as X — coin is circulation.

At the first sample, initially in row there are coins that are not in circulation, so Dima will look through them from left to right and won't make any exchanges.

After replacement of the first coin with a coin in circulation, Dima will exchange this coin with next three times and after that he will finally look through the coins and finish the process.

XOOO  →  OOOX

After replacement of the third coin, Dima's actions look this way:

XOXO  →  OXOX  →  OOXX

After replacement of the fourth coin, Dima's actions look this way:

XOXX  →  OXXX

Finally, after replacement of the second coin, row becomes consisting of coins that are in circulation and Dima will look through coins from left to right without any exchanges.

题目大意:有一排硬币,每次按照给定顺序换掉给定位置上的一些,每次操作移动的规则跟冒泡排序类似,问经过多少次操作可以把所有换过的硬币移动到硬币队列的最后。
这道属于读题一小时,十分钟 KO 类型。。。
每个时刻的操作次数就是从右边第一个没被换过的硬币数起,它左边的被换过的硬币的个数加再加上1。
比如:( XOXOO ) = 2,( XOXXOOXX ) = 3。
这里笔者用二分的方法找“右边第一个没被换过的硬币”的位置,用树状数组统计它左边的被换过的硬币个数。复杂度 O(n logn logn)。不过看到网上大神有复杂度更低的做法,有时间去Orz一下。
#include}
<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 3 * 1e5 + 5;
int t[maxn],a[maxn];
int n;
int lowbit(int x)
{
    return x & (-x);
}
int add(int x,int v)
{
    while(x <= n + 1)
    {
        t[x] += v;
        x += lowbit(x);
    }
}
int query(int x)
{
    int sum = 0;
    while(x > 0)
    {
        sum += t[x];
        x -= lowbit(x);
    }
    return sum;
}
int bs()
{
    int l = 1,r = n + 1;
    int mid,ans = n + 1;
    while(l < r)
    {
        mid = l + (r - l) / 2;
        if(query(n + 1) - query(mid - 1) == n - mid + 2) ans = mid,r = mid;
        else l = mid + 1;
    }
    return ans;
}
int main()
{
    memset(t,0,sizeof(t));
    int p = 0,temp,sum;
    scanf("%d",&n);
    add(n + 1,1);
    a[p++] = 1;
    for(int i = 0;i < n; ++i)
    {
        sum = 0;
        scanf("%d",&temp);
        add(temp,1);
        int loc = bs() - 1;
        a[p++] = query(loc) + 1;

    }
    for(int i = 0;i < p; ++i) printf("%d ",a[i]);
    printf("\n");
    return 0;
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值