Codeforces Round #218 (Div. 2)这场相对比较水啊

A. K-Periodic Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

This task will exclusively concentrate only on the arrays where all elements equal 1 and/or 2.

Array a is k-period if its length is divisible by k and there is such array b of length k, that a is represented by array b written exactly times consecutively. In other words, array a is k-periodic, if it has period of length k.

For example, any array is n-periodic, where n is the array length. Array [2, 1, 2, 1, 2, 1] is at the same time 2-periodic and 6-periodic and array [1, 2, 1, 1, 2, 1, 1, 2, 1] is at the same time 3-periodic and 9-periodic.

For the given array a, consisting only of numbers one and two, find the minimum number of elements to change to make the array k-periodic. If the array already is k-periodic, then the required value equals 0.

Input

The first line of the input contains a pair of integers n, k (1 ≤ k ≤ n ≤ 100), where n is the length of the array and the value n is divisible by k. The second line contains the sequence of elements of the given array a1, a2, ..., an (1 ≤ ai ≤ 2), ai is the i-th element of the array.

Output

Print the minimum number of array elements we need to change to make the array k-periodic. If the array already is k-periodic, then print 0.

Examples
Input
6 2
2 1 2 2 2 1
Output
1
Input
8 4
1 1 2 1 1 1 2 1
Output
0
Input
9 3
2 1 1 1 2 1 1 1 2
Output
3
Note

In the first sample it is enough to change the fourth element from 2 to 1, then the array changes to [2, 1, 2, 1, 2, 1].

In the second sample, the given array already is 4-periodic.

In the third sample it is enough to replace each occurrence of number two by number one. In this case the array will look as [1, 1, 1, 1, 1, 1, 1, 1, 1] — this array is simultaneously 1-, 3- and 9-periodic.


题目大意:

有N个数,每K个数作为一组,现在只有1和2两种数字,问改变多少数字可以使得每一组的序列都是一样的。


思路:


考虑每一个位子上最终的答案是1还是2即可。


一共有K个位子,对于每一组的这K个位子都进行统计1和2的数量,每个位子取1和2数量最少的那个数改成另外一个数就行了。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int a[15000];
int main()
{
    int n,k;
    while(~scanf("%d%d",&n,&k))
    {
        int output=0;
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        for(int i=1;i<=k;i++)
        {
            int cont1=0;
            int cont2=0;
            for(int j=i;j<=n;j+=k)
            {
                if(a[j]==1)cont1++;
                else cont2++;
            }
            output+=min(cont1,cont2);
        }
        printf("%d\n",output);
    }
}

B. Fox Dividing Cheese
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Two little greedy bears have found two pieces of cheese in the forest of weight a and b grams, correspondingly. The bears are so greedy that they are ready to fight for the larger piece. That's where the fox comes in and starts the dialog: "Little bears, wait a little, I want to make your pieces equal" "Come off it fox, how are you going to do that?", the curious bears asked. "It's easy", said the fox. "If the mass of a certain piece is divisible by two, then I can eat exactly a half of the piece. If the mass of a certain piece is divisible by three, then I can eat exactly two-thirds, and if the mass is divisible by five, then I can eat four-fifths. I'll eat a little here and there and make the pieces equal".

The little bears realize that the fox's proposal contains a catch. But at the same time they realize that they can not make the two pieces equal themselves. So they agreed to her proposal, but on one condition: the fox should make the pieces equal as quickly as possible. Find the minimum number of operations the fox needs to make pieces equal.

Input

The first line contains two space-separated integers a and b (1 ≤ a, b ≤ 109).

Output

If the fox is lying to the little bears and it is impossible to make the pieces equal, print -1. Otherwise, print the required minimum number of operations. If the pieces of the cheese are initially equal, the required number is 0.

Examples
Input
15 20
Output
3
Input
14 8
Output
-1
Input
6 6
Output
0

题目大意:


现在给你两个数a,b.

我们现在可以对其任意一个数除2,3或者是5(必须是整除才行)

问最少操作次数,使得a==b.


思路:


我们如果数字a能够整除2就一直去除,统计可以进行整除的次数aa

我们如果数字a能够整除3就一直去除,统计可以进行整除的次数bb

我们如果数字a能够整除5就一直去除,统计可以进行整除的次数cc

我们如果数字b能够整除2就一直去除,统计可以进行整除的次数aaa

我们如果数字b能够整除3就一直去除,统计可以进行整除的次数bbb

我们如果数字b能够整除5就一直去除,统计可以进行整除的次数ccc


那么如果最终a==b.那么说明两个数是可以通过操作使得其相等的,那么我们考虑,将两个数扩大,使得操作数减小。

那么显然答案就是abs(aa-aaa)+abs(bb-bbb)+abs(cc-ccc);


Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
int abs(int w)
{
    if(w<0)return -w;
    else return w;
}
int main()
{
    int a,b;
    while(~scanf("%d%d",&a,&b))
    {
        int aa=0,bb=0,cc=0;
        while(a%2==0)a/=2,aa++;
        while(a%3==0)a/=3,bb++;
        while(a%5==0)a/=5,cc++;
        int aaa,bbb,ccc;
        aaa=bbb=ccc=0;
        while(b%2==0)b/=2,aaa++;
        while(b%3==0)b/=3,bbb++;
        while(b%5==0)b/=5,ccc++;
        if(a==b)
        {
            printf("%d\n",abs(aa-aaa)+abs(bb-bbb)+abs(cc-ccc));
        }
        else printf("-1\n");
    }
}


C. Hamburgers
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) и 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "ВSCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again.

Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese.

Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient.

Input

The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C).

The second line contains three integers nb, ns, nc (1 ≤ nb, ns, nc ≤ 100) — the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pb, ps, pc (1 ≤ pb, ps, pc ≤ 100) — the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≤ r ≤ 1012) — the number of rubles Polycarpus has.

Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Output

Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0.

Examples
Input
BBBSSC
6 4 1
1 2 3
4
Output
2
Input
BBC
1 10 1
1 10 1
21
Output
7
Input
BSC
1 1 1
1 1 3
1000000000000
Output
200000000001

题目大意:


给你一个汉堡的形状,然后告诉你初始的每种材料的个数,以及每种材料买一个的价格,问最多可以做几个汉堡。


思路:


煞笔二分+暴力check..


Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
#define ll __int64
ll nb,ns,nc;
ll pb,ps,pc;
ll Have;
char a[1050000];
int Slove(ll mid)
{
    ll needb=0,needs=0,needc=0;
    int n=strlen(a);
    for(int i=0;i<n;i++)
    {
        if(a[i]=='B')
        {
            needb++;
        }
        if(a[i]=='S')
        {
            needs++;
        }
        if(a[i]=='C')
        {
            needc++;
        }
    }
    needb*=mid;
    needc*=mid;
    needs*=mid;
    needb-=nb;
    needc-=nc;
    needs-=ns;
    if(needb<0)needb=0;
    if(needc<0)needc=0;
    if(needs<0)needs=0;
    if(needb*pb+needc*pc+needs*ps<=Have)return 1;
    else return 0;
}
int main()
{
    while(~scanf("%s",a))
    {
        scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&nb,&ns,&nc,&pb,&ps,&pc);
        scanf("%I64d",&Have);
        ll ans=0;
        ll l=0;
        ll r=1e15;
        while(r-l>=0)
        {
            ll mid=(l+r)/2;
            if(Slove(mid))
            {
                l=mid+1;
                ans=mid;
            }
            else r=mid-1;
        }
        printf("%I64d\n",ans);
    }
}

D. 我是萌萌哒D题题解


E.我是萌萌哒E题题解










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值