Dichotomy applications

Today learn new knowledge set

1.bool function can directly manipulate variables and returns a value of 0.1;

As will be seen this code is not input a [i] of the parameter but may still be operated

Behind the return statement is true returns 1

Otherwise it returns 0

Therefore, for if (judge (k)) is determined to be

BOOL Judge ( const  int & len)   // for determining subfunctions 
{
     Long  Long CNT = 0 ;           // some cases may explode int 
    for ( int I = 0 ; I <n-; I ++ ) 
        CNT + = ( Long  Long ) ( A [I] / len);
     return CNT> = K; 
}

2. About dichotomy

The simplest form is the dichotomy of the interval to take change

At present, I think the difficulty is to determine the conditions

Today made a few half title

I feel like a chicken dish

The first question is to practice hand template title

The concept of a clear dichotomy

#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int n,x,i,mid,t,w;
const int N=2000005;
int a[N];
int main()
{
    while(~scanf("%d %d",&n,&x))
    {
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        sort(a,a+n);
        t=0;w=n-1;
        while(t<=w)
        {
            mid=(t+w)/2;
            if(a[mid]==x)
                break;
            else
                if(a[mid]>x)
                w=mid-1;
                else
                    if(a[mid]<x)
                    t=mid+1;
        }
        if(a[mid]==x)
        printf("%d\n",mid+1);
        else
            printf("%d\n",mid);
    }
    //cout << "Hello world!" << endl;
    return 0;
}

After writing this problem you can use the built-c Lv remove the standard is a direct function of the

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
const int N=2000006;
int a[N];
int n,x;
int t,w,mid;
int main()
{
    while(~scanf("%d%d",&n,&x))
    {
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        sort(a,a+n);
     printf("%d\n",upper_bound(a,a+n,x)-a);

    }
    //cout << "Hello world!" << endl;
    return 0;
}

Then is the magic bucket sort, actually appeared in half of the problem = =

(Overtime issue has been the heart ineffable pain

Small fresh but also play a dirty trick, he twice found the problem (Problem 8) and can not be stumped everyone, so he decided to change the quiet Mimi wave data. 
So: 
given 2 to 10,000 distinct positive integers, your task is to calculate how many of these number inside the number to meet: a pair number is twice the number of another number. Given such 1432971822, the answer is 3, because 1 to 2 times, 2 times 4, 9 18 twice.

Input

Input comprises n sets of test data. The first line of a positive integer n. 
The next row n represents n sets of data, each row of data is given 2 to 10,000 and less than 100,000 different twenty-two positive integer. Each line of the last number is 0, which indicates the end of the line, this number does not belong to that given positive integer from 2 to 10,000.

Output

Each set of input data, output line, given the number of the number which satisfies a number is twice the number of the other.

Sample Input

3
1 4 3 2 9 7 18 22 0
2 4 8 10 0
7 5 11 13 1 3 0

Sample Output

3 
2 
0 
For this question I feel is deleted delete multiple sets of multi-group = =;
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int n;
const int N=10002;
const int K=2000005;
int a[N],pai[K];
int i,j,temp,ans,zu,k;
int main()
{      scanf("%d",&zu);
        for(i=0;i<zu;i++)
        {
            k=0;ans=0;
            memset(pai,0,sizeof(pai));
            while(scanf("%d",&temp)&&temp)
            {
                a[++k]=temp;
                pai[temp]++;;
            }
            for(j=1;j<=k;j++)
            {
                if(pai[2*a[j]]!=0)
                    ans++;
            }
            printf("%d\n",ans);
        }


    //cout << "Hello world!" << endl;
    return 0;
}

Behind the questions I find it difficult = =

So hard

The idea is to have all RE

Biequ

Annoying to not be written

To just fill the code behind sticky

#include <bits/stdc++.h>
using namespace std;
const double pi=acos(-1.0);
int h;
int check(double r)
{
    if (pow(r,pi)>=h*(pi*r*r-r))
        return 1;
    else
        return 0;

}

int main()
{
    //ios::sync_with_stdio(false);
    int t;
    double l,r,mid;
    scanf("%d",&t);
    while(t--)
    {
     scanf("%d",&h);

     l=0;r=100000;

     while(l<r)
     {
       mid=(l+r)/2.0;
      if (r-l<=1e-8) break;
       if (check(mid)==1) //mid大了
        r=mid;
       else
        l=mid;
     }
      printf("%.4lf\n",mid);

     }


    return 0;
}
#include <iostream>
#include<bits/stdc++.h>
#include<math.h>
const double pai=acos(-1.0);
int zu,h;
double v1,v2;
double r;
double temp,i,j,mid,t;
using namespace std;
int judge(double r)
{
    if (pow(r,pai)>=h*(pai*r*r-r))
        return 1;
    else
        return 0;

}

int main()
{
    scanf("%d",&zu);
    while(zu--)
    {

        scanf("%d",&h);
        t=0;r=100000;
        while(t<r)
        {
             mid=(t+r)/2;
            if(r-t<=1e-8)
            break;
            if(judge(mid))
                r=mid;
            else
                t=mid;

        }
        printf("%.4f\n",r);
    }

    //cout << "Hello world!" << endl;
    return 0;
}
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define double long long int
using namespace std;
const int maxn=1e5+10;
int n,k;
double a[maxn];
bool check(double x)
{
    int cnt=0;
    for(int i=0;i<n;i++)
        cnt+=(int)(a[i]/x);
    return cnt>=k;
}
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++)
        scanf("%lf",&a[i]);
    double eps=1e-6;
    double l=0.0,r=0x3f3f3f3f;
    while(r-l>eps)
    {
        double mid=l+(r-l)/2.0;
        if(!check(mid))
            r=mid;
        else
            l=mid+eps;
    }

    printf("%lld\n",(floor(r*100)/100.0));
    return 0;
}

 

 

Guess you like

Origin www.cnblogs.com/ballcoming/p/12153398.html