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.
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.
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.
6 2 2 1 2 2 2 1
1
8 4 1 1 2 1 1 1 2 1
0
9 3 2 1 1 1 2 1 1 1 2
3
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);
}
}
题目大意:
现在给你两个数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");
}
}
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.
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.
Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0.
BBBSSC 6 4 1 1 2 3 4
2
BBC 1 10 1 1 10 1 21
7
BSC 1 1 1 1 1 3 1000000000000
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题题解