暑假集训--训练1 二分搜索

来源:互联网 发布:淘宝直销 编辑:程序博客网 时间:2024/06/15 02:21

A - Can you find it?
Time Limit:3000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u

Submit

Status

Description

Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X.

Input

There are many cases. Every data case is described as followed: In the first line there are three integers L, N, M, in the second line there are L integers represent the sequence A, in the third line there are N integers represent the sequences B, in the forth line there are M integers represent the sequence C. In the fifth line there is an integer S represents there are S integers X to be calculated. 1<=L, N, M<=500, 1<=S<=1000. all the integers are 32-integers.

Output

For each case, firstly you have to print the case number as the form “Case d:”, then for the S queries, you calculate if the formula can be satisfied or not. If satisfied, you print “YES”, otherwise print “NO”.

Sample Input

3 3 3
1 2 3
1 2 3
1 2 3
3
1
4
10

Sample Output

Case 1:
NO
YES
NO

#include<iostream>#include<stdio.h>#include<algorithm>#include<vector>using namespace std;bool cmp(int a,int b){    return a<b;}int main(){    int cable=0;    int L,N,M,S,X,T;    vector<int> A;    vector<int> B;    vector<int> C;    vector<int> temp;    while (scanf("%d%d%d",&L,&N,&M)!=EOF)    {        cable++;        A.clear();        B.clear();        C.clear();        temp.clear();        for (int i=0; i<L; i++)        {            scanf("%d",&T);            A.push_back(T);        }        for (int i=0; i<N; i++)        {            scanf("%d",&T);            B.push_back(T);        }        for (int i=0; i<M; i++)        {            scanf("%d",&T);            C.push_back(T);        }        for (int i=0; i<L; i++)        {            for (int j=0; j<M; j++)            {                temp.push_back(A[i]+B[j]);            }        }        sort(temp.begin(),temp.end(),cmp);        scanf("%d",&S);        printf("Case %d:\n",cable);        for (int i=0; i<S; i++)        {            scanf("%d",&X);            bool flag=false;            for (int i=0; i<C.size(); i++)            {                vector<int>::iterator ite=lower_bound(temp.begin(),temp.end(),X-C[i]);                if (ite!=temp.end()&&*ite==X-C[i])                {                    flag=true;                   // cout<<"Test:"<<*ite<<" "<<C[i]<<endl;                    break;                }            }            printf("%s\n",flag?"YES":"NO");        }    }    return 0;}

B - Can you solve this equation?
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u

Submit

Status

Description

现在,给出等式8* X^4+ 7* X^3+ 2* X^2+ 3 * X +6= Y,请找出他在0和100之间的解(包含0和100)。

现在,请你试试运气。。。。

Input

输入的第一行包含一个整数T(1 <= T <=100),表示测试用例的数目。接下来T个数字,每一行都有一个实数Y(abs(Y)<=10^10);

Output

对于每个测试用例,如果有解,你应该输出一个实数(精确到小数点后4位,四舍五入),如果在0到100之间无解,就输出“No solution!”。

Sample Input

2

100

-4

Sample Output

1.6152

No solution!

一定要注意精度 还要主要计算上下界对应的值 还要把太复杂的式子写成一个函数 可读性和复用性都会变好 我这样写太蠢了

#include<iostream>#include<math.h>#include<stdio.h>#include<algorithm>#include<iomanip>#include<vector>using namespace std;long double bs(long double Y){    long double l=0;    long double h=100;    long double m,mk;    long double lk=8*l*l*l*l+7*l*l*l+2*l*l+3*l+6-Y;    long double hk=8*h*h*h*h+7*h*h*h+2*h*h+3*h+6-Y;    if (lk > 0 || hk < 0)        return -1;    else    {        while (fabs(lk - hk) >= 0.0001)        {            m = (l + h) / 2;            mk = 8*m*m*m*m+7*m*m*m+2*m*m+3*m+6-Y;            if (mk >= 0)                h = m;            else                l = m;            lk=8*l*l*l*l+7*l*l*l+2*l*l+3*l+6-Y;            hk=8*h*h*h*h+7*h*h*h+2*h*h+3*h+6-Y;        }        return m;    }    return -1;}int main(){    int T;    long double Y;    cin>>T;    cout<<fixed<<setprecision(4);    while (T--)    {        cin>>Y;        long double temp=bs(Y);        if (temp==-1)            cout<<"No solution!"<<endl;        else        `````   cout<<temp<<endl;    }    return 0;}

C - Sacrament of the sum
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu

Submit

Status

Description

— The Brother of mine, the Head of Monastic Order wants to know tomorrow about the results long-term researches. He wants to see neither more nor less than the Summering Machine! Even moreover, he wants our Machine — only a machine — to demonstrate its comprehension of the Sacrament of the Sum as deeply as it is possible. He wants our Machine to find two numbers that give the sum equal to the Sacred Number 10 000.
— Tsh-sh-sh! This is madness that borders on blasphemy! How can the Machine calculate the Sacred Number? Twenty seven years we work on it, but we’ve could teach it to tell if the sum of two introduced numbers greater or lower than 10 000. Can an ordinary mortal find two numbers that there sum will be equal to 10 000?
— But we’ll have to do it with the help of our Machine, even if it is not capable. Otherwise we’ll have… let’s say, big problems, if it is possible to call boiling oil like this. However, I have an idea. Do you remember, last week we’ve entered two numbers -7 and 13 into the Machine, and it answered that their sum is lower than 10 000. I don’t know how to check this, but nothing’s left for us than to believe to the fruit of our work. Let’s enter now a greater number than -7 and start up the Machine again. We’ll do like this again and again until we find a number that being added to 13 will give us 10 000. The only thing we are to do is to prepare an ascending list of numbers.
— I don’t believe in this… Let’s start with the sum that is obviously greater than the Sacred Number and we’ll decrease one of the summand. So we have more chances to avoid boilin… big problems.

Haven’t come to an agreement, the Brothers went away to their cells. By next day everyone of them has prepared a list of numbers that, to his opinion, could save them… Can both of the lists save them together?
Your program should decide, if it is possible to choose from two lists of integers such two numbers that their sum would be equal to 10 000.

Input

You are given both of these lists one by one. Format of each of these lists is as follows: in the first line of the list the quantity of numbers Ni of the i-th list is written. Further there is an i-th list of numbers each number in its line (Ni lines).The following conditions are satisfied: 1 <= Ni <= 50 000, each element of the lists lays in the range from -32768 to 32767. The first list is ascending and the second one is descending.

Output

You should write “YES” to the standard output if it is possible to choose from the two lists of integers such two numbers that their sum would be equal to 10 000. Otherwise you should write “NO”.

Sample Input

4
-175
19
19
10424
3
8951
-424
-788

Sample Output

YES

#include<iostream>#include<math.h>#include<stdio.h>#include<algorithm>#include<iomanip>#include<vector>using namespace std;int main(){    int n,m,x;    int a[1000005]={0};    int b[1000005]={0};    scanf("%d", &n);    for(int i=0; i<n; i++)    {        scanf("%d", &x);        if(x>=0) a[x]=1;        else b[-x]=1;    }    scanf("%d", &m);    bool flag=false;    while(m--)    {        scanf("%d", &x);        x=10000-x;        if(x>=0 && a[x]) flag=true;        if(x<0 && b[-x]) flag=true;        if(flag) break;    }    if(flag)        printf("YES\n");    else         printf("NO\n");    return 0;}

D - Pie
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u

Submit

Status

Description

My birthday is coming up and traditionally I’m serving pie. Not just one pie, no, I have a number N of them, of various tastes and of various sizes. F of my friends are coming to my party and each of them gets a piece of pie. This should be one piece of one pie, not several small pieces since that looks messy. This piece can be one whole pie though.

My friends are very annoying and if one of them gets a bigger piece than the others, they start complaining. Therefore all of them should get equally sized (but not necessarily equally shaped) pieces, even if this leads to some pie getting spoiled (which is better than spoiling the party). Of course, I want a piece of pie for myself too, and that piece should also be of the same size.

What is the largest possible piece size all of us can get? All the pies are cylindrical in shape and they all have the same height 1, but the radii of the pies can be different.

Input

One line with a positive integer: the number of test cases. Then for each test case:
—One line with two integers N and F with 1 <= N, F <= 10 000: the number of pies and the number of friends.
—One line with N integers ri with 1 <= ri <= 10 000: the radii of the pies.

Output

For each test case, output one line with the largest possible volume V such that me and my friends can all get a pie piece of size V. The answer should be given as a floating point number with an absolute error of at most 10^(-3).

Sample Input

3
3 3
4 3 3
1 24
5
10 5
1 4 2 3 4 5 6 5 4 2

Sample Output

25.1327
3.1416
50.2655

开始π值是从百度复制了一段下来 总是WA 后来改成了acos(-1.0)就过了 迷

#include<iostream>#include<math.h>#include<stdio.h>#include<algorithm>#include<iomanip>#include<vector>using namespace std;const double pi = acos(-1.0);int T,N,F,r;double V[10005]= {0};bool test(double x){    int num=0;    for(int i=0; i<N; i++)    {        num += (int)(V[i]/x);    }    if(num>=F)        return true;    else return false;}int main(){    scanf("%d",&T);    while(T--)    {        double maxV=0;        scanf("%d%d",&N,&F);        F+=1;        for (int i=0; i<N; i++)        {            scanf("%d",&r);            V[i] = pi*r*r;            maxV += V[i];        }        double max=maxV/F;        double left = 0.0;        double right = max;        double mid;        while((right-left)>1e-6)        {            mid = (left+right)/2;            if(test(mid))                left = mid;            else right = mid;        }        printf("%.4f\n",mid);    }    return 0;}

E - 4 Values whose Sum is 0
Time Limit:15000MS Memory Limit:228000KB 64bit IO Format:%lld & %llu

Submit

Status

Description

The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .

Input

The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 2 28 ) that belong respectively to A, B, C and D .

Output

For each input file, your program has to write the number quadruplets whose sum is zero.

Sample Input

6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45

Sample Output

5

Hint

Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).

注意lower_bound后面的重复值 也要算进去

#include<iostream>#include<math.h>#include<stdio.h>#include<algorithm>#include<iomanip>#include<vector>using namespace std;bool cmp(int a,int b){    return a<b;}int A[4010]= {0};int B[4010]= {0};int C[4010]= {0};int D[4010]= {0};int sumAB[16000010]= {0};int sumCD[16000010]= {0};int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        for (int i=0; i<n; i++)            scanf("%d%d%d%d",&A[i],&B[i],&C[i],&D[i]);        int temp=0;        for (int i=0; i<n; i++)            for (int j=0; j<n; j++)            {                sumAB[temp]=A[i]+B[j];                sumCD[temp]=C[i]+D[j];                temp++;            }        sort(sumCD,sumCD+temp,cmp);        int ans=0;        for (int i=0; i<temp; i++)        {            int *ptr=lower_bound(sumCD,sumCD+temp,-sumAB[i]);            if (*ptr!=-sumAB[i]||ptr==sumCD+temp)                continue;            else if (*ptr==-sumAB[i]&&ptr!=sumCD+temp)            {                for (;*ptr==-sumAB[i]&&ptr!=sumCD+temp;ptr++)                {                    ans++;                }            }        }        cout<<ans<<endl;    }    return 0;}
0 0
原创粉丝点击