HDU 6020---MG loves apple(枚举)

题目链接

Problem Description

MG is a rich boy. He has n apples, each has a value of V(0<=V<=9). 
A valid number does not contain a leading zero, and these apples have just made a valid N digit number. 
MG has the right to take away K apples in the sequence, he wonders if there exists a solution: After exactly taking away K apples, the valid N−K digit number of remaining apples mod 3 is zero. 
MG thought it very easy and he had himself disdained to take the job. As a bystander, could you please help settle the problem and calculate the answer?

Input

The first line is an integer T which indicates the case number.(1<=T<=60)
And as for each case, there are 2 integer N(1<=N<=100000),K(0<=K<N) in the first line which indicate apple-number, and the number of apple you should take away.
MG also promises the sum of N will not exceed 1000000。
Then there are N integers X in the next line, the i-th integer means the i-th gold’s value(0<=X<=9).

Output

As for each case, you need to output a single line.
If the solution exists, print”yes”,else print “no”.(Excluding quotation marks)

Sample Input

2

5 2

11230

4 2

1000

Sample Output

yes

no

题意:

思路:

代码如下:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
int  a[100005];
char s[100005];

void cal(int &a3, int &E1,int &E2,int N)
{
    a3=0; E1=0; E2=0;
    for(int i=1;i<=N;i++)
    {
        if(a[i]==3) break;
        if(a[i]==0) a3++;
    }
    for(int i=1;i<=N;i++)
    {
        if(a[i]==0) break;
        if(a[i]==1) E1=1;
        if(a[i]==2) E2=1;
    }
    return ;
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int N,K;
        int s1=0,s2=0,s3=0;
        scanf("%d%d",&N,&K);
        scanf("%s",s+1);
        for(int i=1;i<=N;i++)
        {
            a[i]=s[i]-‘0‘;
            if(a[i]%3==1) a[i]=1,s1++;
            else if(a[i]%3==2) a[i]=2,s2++;
            else s3++,a[i]=(a[i])?3:0;
        }
        int ans=(s1+s2*2)%3;
        int a3,E1,E2,f=0;
        cal(a3,E1,E2,N);
        for(int C=0;C<=s2&&C<=K;C++)  ///C->2; B->1; A->0;
        {
            int B=((ans-C*2)%3+3)%3;
            for(;B<=s1&&C+B<=K;B=B+3)
            {
                int A=K-C-B;
                if(A<=s3)
                {
                    if(A>a3) f=1;
                    else if(B<s1&&E1) f=1;
                    else if(C<s2&&E2) f=1;
                    if(f) break;
                }
            }
            if(f) break;
        }
        if((N==K+1)&&s3) f=1;
        if(f) puts("yes");
        else puts("no");
    }
    return 0;
}
时间: 2024-11-06 22:05:28

HDU 6020---MG loves apple(枚举)的相关文章

【HDU 6020】 MG loves apple (乱搞?)

MG loves apple Accepts: 20 Submissions: 693 Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) 问题描述 MGMG是一个财富爆表的男孩子.他拥有N(1<=N<=100000)N(1<=N<=100000)个苹果,每个苹果上标有一个数字00~99,代表它的价值. 一个合法的数字是不含有前导零的,这nn个苹果恰好排成了一个合法的N

hdu 6021 MG loves string

MG loves string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 131    Accepted Submission(s): 50 Problem Description MG is a busy boy. And today he's burying himself in such a problem: For a

【HDU 6021】 MG loves string (枚举+容斥原理)

MG loves string Accepts: 30 Submissions: 67 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) 问题描述 MGMG是一个很忙碌的男孩子.今天他沉迷于这样一个问题: 对于一个长度为NN的由小写英文字母构成的随机字符串,当它进行一次变换,所有字符ii都会变成a[i]a[i]. MGMG规定所有a[i]a[i]构成了2626个字母组成的排列. M

HDU 4876 ZCC loves cards

我决定记录下这么恶心的代码.比赛的时候头晕脑胀,写得好搓,错的地方好多好多,回来调了好久.... 做法大概就是C(20,6)选出卡牌后,再k!枚举排列,再k*k得出该排列能得出什么数字. 当然,光这样做绝对会T,里面加了各种剪枝后就1650ms险过了.. 最主要的剪枝是选出k张牌后,看牌能不能组成L~ans里面各个数字,能才进行下一步.然后k!可以拆成(k-x)!*(x!)..不过这里其实大概没什么大优化吧 #include<cstdio> #include<iostream> #

HDU 4873 ZCC Loves Intersection(JAVA、大数、推公式)

在一个D维空间,只有整点,点的每个维度的值是0~n-1 .现每秒生成D条线段,第i条线段与第i维度的轴平行.问D条线段的相交期望. 生成线段[a1,a2]的方法(假设该线段为第i条,即与第i维度的轴平行)为,i!=j时,a1[j]=a2[j],且随机取区间[0,n-1]内的整数.然后a1[i],a2[i]在保证a1[i]<a2[i]的前提下同样随机. 由于D条线段各自跟自己维度的轴平行,我们可以转换成只求第i个维度与第j个维度的相交期望,然后乘以C(2,n)就好了 显然线段[a1,a2]和线段[

hdu 5677 ztr loves substring 二维费用背包+回文

题目链接: hdu 5677 ztr loves substring 官方题解: //这部分是错的(首先,对于每一个串i跑一次manancher,令g[i][j]=p[j]-1g[i][j]=p[j]−1 这样,g就存储了所有的回文子串的长度 为了方便,把g降到一维表示) 首先,因为字符串长度较小,所以直接二重for循环找就好了,用一个数组 g记录各个回文串的长度 不妨把每一个子串抽象成一个物品 费用为二维的,即{长度,1} 价值是Bool型的 这样就成了一个二维判断可行性费用背包问题 设f(i

hdu 4430 Yukari&#39;s Birthday 枚举+二分

注意会超long long 开i次根号方法,te=(ll)pow(n,1.0/i); Yukari's Birthday Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3262    Accepted Submission(s): 695 Problem Description Today is Yukari's n-th birt

HDU 4876 ZCC loves cards _(:зゝ∠)_ 随机输出保平安

GG,,,g艹 #include <cstdio> #include <iostream> #include <algorithm> #include <string.h> #include <vector> #include <queue> #include <math.h> using namespace std; vector<int>G[21][7];//G[i][j] 表示n=i k=j的情况下 二进

hdu 4882 ZCC Loves Codefires(数学题+贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4882 ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943

hdu 4882 ZCC Loves Codefires(贪心)

# include<stdio.h> # include <algorithm> # include <string.h> using namespace std; struct node { int v; int t; }; struct node a[100010]; bool cmp(node a,node b) { return a.v *a.t+(a.v+b.v)*b.t<b.v*b.t+(a.v+b.v)*a.t; } int main() { int