hdu 5143 暴力枚举

来源:互联网 发布:网络丢包测试工具 编辑:程序博客网 时间:2024/06/06 02:10

NPY and arithmetic progression

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1100    Accepted Submission(s): 353


Problem Description
NPY is learning arithmetic progression in his math class. In mathematics, an arithmetic progression (AP) is a sequence of numbers such that the difference between the consecutive terms is constant.(from wikipedia)
He thinks it's easy to understand,and he found a challenging problem from his talented math teacher:
You're given four integers, a1,a2,a3,a4, which are the numbers of 1,2,3,4 you have.Can you divide these numbers into some Arithmetic Progressions,whose lengths are equal to or greater than 3?(i.e.The number of AP can be one)
Attention: You must use every number exactly once.
Can you solve this problem?
 

Input
The first line contains a integer T — the number of test cases (1T100000).
The next T lines,each contains 4 integers a1,a2,a3,a4(0a1,a2,a3,a4109).
 

Output
For each test case,print "Yes"(without quotes) if the numbers can be divided properly,otherwise print "No"(without quotes).
 

Sample Input
31 2 2 11 0 0 03 0 0 0
 

Sample Output
YesNoYes
Hint
In the first case,the numbers can be divided into {1,2,3} and {2,3,4}.In the second case,the numbers can't be divided properly.In the third case,the numbers can be divided into {1,1,1}.


题意理解不清楚:

第一:输入数组 1  2  2  1表示1个1 , 2个2 ,  2个3 , 1个4

第二:常数组可以很长很长,非常数列只能是三个或者四个元素

非常数列有三种情况123  234  1234,枚举这三种情况后再用常数列进行覆盖即可


#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int deal(int arr[]){    int flag=0;    for(int i=1;i<=4;i++)        if(arr[i]==0||arr[i]>=3)            flag++;    if(flag==4)        return 1;    else        return 0;}int main(){    int T;    int a[10],b[10];    //freopen("in.txt","r",stdin);    scanf("%d",&T);    while(T--)    {        for(int i=1;i<=4;i++){            scanf("%d",&a[i]);            b[i]=a[i];        }        int flag=0;        for(int i=0;i<=2;i++){            for(int j=0;j<=2;j++){                for(int k=0;k<=2;k++){                    a[1]-=(i+k);                    a[2]-=(i+j+k);                    a[3]-=(i+j+k);                    a[4]-=(j+k);                    if(deal(a)){                        flag=1;                        goto temp;                    }                    for(int p=1;p<=4;p++)                        a[p]=b[p];                }            }        }        temp:;        if(flag)            puts("Yes");        else            puts("No");    }    return 0;}


0 0
原创粉丝点击