zoukankan      html  css  js  c++  java
  • 浙南联合训练赛20180804

    A - Beavergnaw

     POJ - 2405 

    When chomping a tree the beaver cuts a very specific shape out of the tree trunk. What is left in the tree trunk looks like two frustums of a cone joined by a cylinder with the diameter the same as its height. A very curious beaver tries not to demolish a tree but rather sort out what should be the diameter of the cylinder joining the frustums such that he chomped out certain amount of wood. You are to help him to do the calculations. 

    We will consider an idealized beaver chomping an idealized tree. Let us assume that the tree trunk is a cylinder of diameter D and that the beaver chomps on a segment of the trunk also of height D. What should be the diameter d of the inner cylinder such that the beaver chmped out V cubic units of wood?

    Input

    Input contains multiple cases each presented on a separate line. Each line contains two integer numbers D and V separated by whitespace. D is the linear units and V is in cubic units. V will not exceed the maximum volume of wood that the beaver can chomp. A line with D=0 and V=0 follows the last case.

    Output

    For each case, one line of output should be produced containing one number rounded to three fractional digits giving the value of d measured in linear units.

    Sample Input

    10 250
    20 2500
    25 7000
    50 50000
    0 0
    

    Sample Output

    8.054
    14.775
    13.115
    30.901

    一个圆柱截去两个圆台和一个圆柱为V

    PI/4*D*D*D-PI/3*(D-d)*(Dd+DD+dd)/4-PI/4*d*d*d=V

    就可以直接解出来了,弄错了系数

    #include<stdio.h>
    #include<iostream>
    #include<math.h>
    using namespace std;
    const double PI=acos(-1.);
    int main()
    {
        double D,V;
        while(cin>>D>>V,D+V)
        {
            printf("%.3f
    ",pow(D*D*D-V*6./PI,1/3.0));
        }
        return 0;
    }

    B - Trip For Meal

     CodeForces - 876A

    Winnie-the-Pooh likes honey very much! That is why he decided to visit his friends. Winnie has got three best friends: Rabbit, Owl and Eeyore, each of them lives in his own house. There are winding paths between each pair of houses. The length of a path between Rabbit's and Owl's houses is a meters, between Rabbit's and Eeyore's house is b meters, between Owl's and Eeyore's house is c meters.

    For enjoying his life and singing merry songs Winnie-the-Pooh should have a meal ntimes a day. Now he is in the Rabbit's house and has a meal for the first time. Each time when in the friend's house where Winnie is now the supply of honey is about to end, Winnie leaves that house. If Winnie has not had a meal the required amount of times, he comes out from the house and goes to someone else of his two friends. For this he chooses one of two adjacent paths, arrives to the house on the other end and visits his friend. You may assume that when Winnie is eating in one of his friend's house, the supply of honey in other friend's houses recover (most probably, they go to the supply store).

    Winnie-the-Pooh does not like physical activity. He wants to have a meal n times, traveling minimum possible distance. Help him to find this distance.

    Input

    First line contains an integer n (1 ≤ n ≤ 100) — number of visits.

    Second line contains an integer a (1 ≤ a ≤ 100) — distance between Rabbit's and Owl's houses.

    Third line contains an integer b (1 ≤ b ≤ 100) — distance between Rabbit's and Eeyore's houses.

    Fourth line contains an integer c (1 ≤ c ≤ 100) — distance between Owl's and Eeyore's houses.

    Output

    Output one number — minimum distance in meters Winnie must go through to have a meal n times.

    Examples

    Input
    3
    2
    3
    1
    Output
    3
    Input
    1
    2
    3
    5
    Output
    0

    Note

    In the first test case the optimal path for Winnie is the following: first have a meal in Rabbit's house, then in Owl's house, then in Eeyore's house. Thus he will pass the distance 2 + 1 = 3.

    In the second test case Winnie has a meal in Rabbit's house and that is for him. So he doesn't have to walk anywhere at all.

    这个题可以转换为给你一个三角形三边,要经过n次点的最短路径,你在a和b这条边上的点

    n为1,无路可走

    n为2,你可以走a和b的任意一边,当然选择最小的啦

    n>2,你可以走到a和b,然后可以循环,也可以走c,这样就是最短了

    #include<stdio.h>
    #include<bits/stdc++.h>
    using namespace std;
    #define lson l,(l+r)/2,rt<<1
    #define rson (l+r)/2+1,r,rt<<1|1
    #define dbg(x) cout<<#x<<" = "<< (x)<< endl
    #define pb push_back
    #define fi first
    #define se second
    #define ll long long
    #define sz(x) (int)(x).size()
    #define pll pair<long long,long long>
    #define pii pair<int,int>
    #define pq priority_queue
    const int N=1e5+5,MD=1e9+7,INF=0x3f3f3f3f;
    const ll LL_INF=0x3f3f3f3f3f3f3f3f;
    const double eps=1e-9,e=exp(1),PI=acos(-1.);
    int main()
    {
        int n,a,b,c;
        cin>>n>>a>>b>>c;
        if(n==1)
            printf("0");
        else if(n==2)
            printf("%d",min(a,b));
        else printf("%d",min(a,b)+(n-2)*min(a,min(b,c)));
        return 0;
    }

    C - Hanoi Tower Troubles Again!

     ZOJ - 1239 

    People stopped moving discs from peg to peg after they know the number of steps needed to complete the entire task. But on the other hand, they didn't not stopped thinking about similar puzzles with the Hanoi Tower. Mr.S invented a little game on it. The game consists of N pegs and a LOT of balls. The balls are numbered 1,2,3... The balls look ordinary, but they are actually magic. If the sum of the numbers on two balls is NOT a square number, they will push each other with a great force when they're too closed, so they can NEVER be put together touching each other.

    The player should place one ball on the top of a peg at a time. He should first try ball 1, then ball 2, then ball 3... If he fails to do so, the game ends. Help the player to place as many balls as possible. You may take a look at the picture above, since it shows us a best result for 4 pegs.


    Input

    The first line of the input contains a single integer T, indicating the number of test cases. (1<=T<=50) Each test case contains a single integer N(1<=N<=50), indicating the number of pegs available.


    Output

    For each test case in the input print a line containing an integer indicating the maximal number of balls that can be placed. Print -1 if an infinite number of balls can be placed.


    Sample Input

    2
    4
    25


    Sample Output

    11
    337

    尝试猜了一发规律

    前面作差是1 2 4 4 6

    就猜一下下,正好25是337,然后就过了

    #include <iostream>
    using namespace std;
    typedef long long ll;
    int main()
    {
        int T,a[55];
        a[1]=1,a[2]=3;
        for(int i=3,t;i<55;i++)a[i]=a[i-1]+(i+1)/2*2;
        cin>>T;
        while(T--)
        {
            int n;
            cin>>n;cout<<a[n]<<"
    ";
        }
        return 0;
    }

    D - Knapsack problem

     FZU - 2214 

    Given a set of n items, each with a weight w[i] and a value v[i], determine a way to choose the items into a knapsack so that the total weight is less than or equal to a given limit B and the total value is as large as possible. Find the maximum total value. (Note that each item can be only chosen once).

    Input

    The first line contains the integer T indicating to the number of test cases.

    For each test case, the first line contains the integers n and B.

    Following n lines provide the information of each item.

    The i-th line contains the weight w[i] and the value v[i] of the i-th item respectively.

    1 <= number of test cases <= 100

    1 <= n <= 500

    1 <= B, w[i] <= 1000000000

    1 <= v[1]+v[2]+...+v[n] <= 5000

    All the inputs are integers.

    Output

    For each test case, output the maximum value.

    Sample Input

    1
    5 15
    12 4
    2 2
    1 1
    4 10
    1 2

    Sample Output

    15

    背包

    平时都是去背这个容量。这个题容量高达1e9,但是价值不大

    所以dp[i]=j,表示价值为i的最小容量

    依次更新即可

    价值当然也可以是0,sb了wa一发

    #include <iostream>
    using namespace std;
    typedef long long ll;
    const ll INF=1LL<<60;
    ll dp[5005];
    int w[505],v[505];
    int main()
    {
        int T;
        cin>>T;
        while(T--)
        {
            int n,m;
            cin>>n>>m;
            ll sumv=0,sumw=0;
            for(int i=1;i<=n;i++)
            {
                cin>>w[i]>>v[i];
                sumw+=w[i],sumv+=v[i];
            }
            for(int i=0;i<5005;i++)dp[i]=INF;
            dp[sumv]=sumw;
            for(int i=1;i<=n;i++)
                for(int j=v[i];j<=sumv;j++)
                    if(dp[j]>=w[i])dp[j-v[i]]=min(dp[j-v[i]],dp[j]-w[i]);
            for(int i=sumv;i>=0;i--)
            {
                if(dp[i]<=m)
                {
                    cout<<i<<"
    ";
                    break;
                }
            }
        }
        return 0;
    }

    E - Classroom Watch

     CodeForces - 875A 

    Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer xwas given. The task was to add x to the sum of the digits of the number x written in decimal numeral system.

    Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova.

    Input

    The first line contains integer n (1 ≤ n ≤ 109).

    Output

    In the first line print one integer k — number of different values of x satisfying the condition.

    In next k lines print these values in ascending order.

    Examples

    Input
    21
    Output
    1
    15
    Input
    20
    Output
    0

    Note

    In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21.

    In the second test case there are no such x.

    这个题当时就秒的比较快,因为你很快就会发现这个数位和好小啊。所以你需要枚举的数就很少了

    #include<stdio.h>
    #include<bits/stdc++.h>
    using namespace std;
    #define lson l,(l+r)/2,rt<<1
    #define rson (l+r)/2+1,r,rt<<1|1
    #define dbg(x) cout<<#x<<" = "<< (x)<< endl
    #define pb push_back
    #define fi first
    #define se second
    #define ll long long
    #define sz(x) (int)(x).size()
    #define pll pair<long long,long long>
    #define pii pair<int,int>
    #define pq priority_queue
    const int N=1e5+5,MD=1e9+7,INF=0x3f3f3f3f;
    const ll LL_INF=0x3f3f3f3f3f3f3f3f;
    const double eps=1e-9,e=exp(1),PI=acos(-1.);
    vector<int>V;
    int la(int x)
    {
        int ans=0;
        while(x)
        {
            ans+=x%10;
            x/=10;
        }
        return ans;
    }
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=max(n-1000,0);i<=n;i++)
            if(i+la(i)==n)V.push_back(i);
        cout<<(int)V.size()<<"
    ";
        for(auto X:V)cout<<X<<"
    ";
        return 0;
    }

    F - Litmus Test

     ZOJ - 2351 

    The pH scale measures the concentration of protons (H+) in a solution and, therefore, its acidity or alkalinity. The pH value of a solution is a number between 0 and 14; it is less than 7 if the solution is acidic, greater than 7 if the solution is basic, and 7 if it is neutral.

    The formula for calculating pH is

    pH = -log 10 [H +]

    where [H+] is the concentration of protons measured in moles per litre.

    To calculate the pH value of an acid, one has to determine the concentration of protons in the solution. When an acid is dissolved in water, an equilibrium is reached and is governed by the equation

    a = [H +] [acid ions] / [acid]

    where Ka is the acidity constant (known for each acid), [acid ions] is the concentration of the acid ions that have dissolved, and [acid] is the concentration of the undissolved acid. Before the acid is added, both [H+] and [acid ions] are assumed to be 0.

    For example, the acidity constant of methanoic acid is 1.6 x 10-4. Dissolving one mole of acid molecules results in one mole of H+ and one mole of acid ions. If the initial concentration of the methanoic acid is 0.1 moles/L and x moles of acid are dissolved (per liter), then the final concentration at equilibrium would be 0.1 - xmoles/L for the acid and x moles/L for H+ and the acid ions.

    Input

    The input consists of a number of test cases. Each test case contains 4 numbers on a line: two positive floating-point numbers specifying the acidity constant Ka and the original concentration of the acid (in moles/liter) added to the water, as well as two positive integers m and n indicating that each mole of acid molecules is dissolved into m moles of H+ ions and n moles of acid ions. The floating-point numbers are specified in scientific notation as shown below. The input is terminated with a line containing four zeros.

    Output

    For each test case, print on a line the pH value of the solution, rounded to 3 decimal places.

    This problem contains multiple test cases!

    The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

    The output format consists of N output blocks. There is a blank line between output blocks.

    Sample Input

    1

    1.6e-04 1.0e-01 1 1
    1.6e-04 1.0e-01 4 1
    1.5e-05 5.0e-02 1 2
    0 0 0 0

    Sample Output

    2.407
    2.101
    3.216

    化学题,也没那么难想吧。

    溶液中的电离出来的各离子浓度乘积(c(A+)*c(B-))与溶液中未电离的电解质分子浓度(c(AB))的比值是一个常数,叫做该弱电解质的电离平衡常数

    一摩尔酸会电离出 m 摩尔 H+ 和 n 摩尔酸根离子(例如盐酸是Cl-,硫酸是SO4 2-

    有c摩尔酸分子,电离了x摩尔,那么剩下c-x

    有如下方程
    Ka=m*x(H+) * n*x(酸根离子) / (c-x )

    m*n*x*x+Ka*x-Ka*c=0

    所以可以用求根公式求得x

    PH值为-log(x*m)

    #include<stdio.h>
    #include<iostream>
    #include<math.h>
    using namespace std;
    int main()
    {
        double ka,c,m,n;
        int t=0,T;
        cin>>T;
        while(T--)
        {
            if(t)printf("
    ");
            while(cin>>ka>>c>>m>>n,m+n)
            {
                double s=(sqrt(ka*ka+4*ka*c*m*n)-ka)/2/n;
                printf("%.3f
    ",-log10(s));
            }
            t=1;
        }
        return 0;
    }

    G - Distance in Tree

     CodeForces - 161D 

    tree is a connected graph that doesn't contain any cycles.

    The distance between two vertices of a tree is the length (in edges) of the shortest path between these vertices.

    You are given a tree with n vertices and a positive number k. Find the number of distinct pairs of the vertices which have a distance of exactly k between them. Note that pairs (vu) and (uv) are considered to be the same pair.

    Input

    The first line contains two integers n and k (1 ≤ n ≤ 50000, 1 ≤ k ≤ 500) — the number of vertices and the required distance between the vertices.

    Next n - 1 lines describe the edges as "ai bi" (without the quotes) (1 ≤ ai, bi ≤ nai ≠ bi), where ai and bi are the vertices connected by the i-th edge. All given edges are different.

    Output

    Print a single integer — the number of distinct pairs of the tree's vertices which have a distance of exactly k between them.

    Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

    Examples

    Input
    5 2
    1 2
    2 3
    3 4
    2 5
    Output
    4
    Input
    5 3
    1 2
    2 3
    3 4
    4 5
    Output
    2

    Note

    In the first sample the pairs of vertexes at distance 2 from each other are (1, 3), (1, 5), (3, 5) and (2, 4).

    这个题目不算难吧,很典型的树形dp,本来自己怎么也统计不出来自己写出来层数多的样例,发现总是统计不够

    因为还有一个乘法原理,就是一个节点会有左子树和右子树,这样节点之间也有距离为k的一个乘法原理

    所以就是dfs这个树 dp[i][j][k]表示第i个节点,距离为j的子树0(全部1)的数目

    #include<stdio.h>
    #include<bits/stdc++.h>
    using namespace std;
    #define lson l,(l+r)/2,rt<<1
    #define rson (l+r)/2+1,r,rt<<1|1
    #define dbg(x) cout<<#x<<" = "<< (x)<< endl
    #define pb push_back
    #define fi first
    #define se second
    #define ll long long
    #define sz(x) (int)(x).size()
    #define pll pair<long long,long long>
    #define pii pair<int,int>
    #define pq priority_queue
    const int N=1e5+5,MD=1e9+7,INF=0x3f3f3f3f;
    const ll LL_INF=0x3f3f3f3f3f3f3f3f;
    const double eps=1e-9,e=exp(1),PI=acos(-1.);
    vector<int>V[N];
    int dp[N][505][2],n,k;
    void dfs(int x,int fa)
    {
        dp[x][0][0]=dp[x][0][1]=1;
        if(fa)
        {
            dp[x][1][1]++;
            for(int i=2;i<=k;i++)dp[x][i][1]+=dp[fa][i-1][1]+dp[fa][i-1][0];
        }
        for(auto X:V[x])
        {
            if(X==fa)continue;
            dfs(X,x);
            for(int i=1; i<=k; i++)dp[x][i][0]+=dp[X][i-1][0];
        }
    }
    int main()
    {
        ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
        cin>>n>>k;
        for(int i=1,x,y; i<n; i++)
            cin>>x>>y,V[x].push_back(y),V[y].push_back(x);
        dfs(1,0);
        int ans=0;
        for(int i=1; i<=n; i++)ans+=dp[i][k][1];
        cout<<ans;
        return 0;
    }

    当然也可以直接乘法原理计数

    #include<stdio.h>
    #include<bits/stdc++.h>
    using namespace std;
    #define lson l,(l+r)/2,rt<<1
    #define rson (l+r)/2+1,r,rt<<1|1
    #define dbg(x) cout<<#x<<" = "<< (x)<< endl
    #define pb push_back
    #define fi first
    #define se second
    #define ll long long
    #define sz(x) (int)(x).size()
    #define pll pair<long long,long long>
    #define pii pair<int,int>
    #define pq priority_queue
    const int N=1e5+5,MD=1e9+7,INF=0x3f3f3f3f;
    const ll LL_INF=0x3f3f3f3f3f3f3f3f;
    const double eps=1e-9,e=exp(1),PI=acos(-1.);
    vector<int>V[N];
    int dp[N][505],n,k,ans;
    void dfs(int x,int fa)
    {
        dp[x][0]=1;
        for(auto X:V[x])
        {
            if(X==fa)continue;
            dfs(X,x);
            for(int i=0; i<k; i++)ans+=dp[x][i]*dp[X][k-i-1];
            for(int i=1; i<=k; i++)dp[x][i]+=dp[X][i-1];
        }
    }
    int main()
    {
        ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
        cin>>n>>k;
        for(int i=1,x,y; i<n; i++)
            cin>>x>>y,V[x].push_back(y),V[y].push_back(x);
        dfs(1,0);
        cout<<ans;
        return 0;
    }

    H - Balanced Substring

     CodeForces - 873B

    You are given a string s consisting only of characters 0 and 1. A substring [l, r]of s is a string slsl + 1sl + 2... sr, and its length equals to r - l + 1. A substring is called balanced if the number of zeroes (0) equals to the number of ones in this substring.

    You have to determine the length of the longest balanced substring of s.

    Input

    The first line contains n (1 ≤ n ≤ 100000) — the number of characters in s.

    The second line contains a string s consisting of exactly n characters. Only characters 0 and 1 can appear in s.

    Output

    If there is no non-empty balanced substring in s, print 0. Otherwise, print the length of the longest balanced substring.

    Examples

    Input
    8
    11010111
    Output
    4
    Input
    3
    111
    Output
    0

    Note

    In the first example you can choose the substring [3, 6]. It is balanced, and its length is 4. Choosing the substring [2, 5] is also possible.

    In the second example it's impossible to find a non-empty balanced substring.

    这个题我有一个巧妙的做法就是把他转换为那个7的倍数的问题(送你个题目,这个思想我已经AC了好几个题目了)

    我们01可以变为+1和-1的,这样就能确保序列合法十分容易查询

    #include<stdio.h>
    #include<bits/stdc++.h>
    using namespace std;
    #define lson l,(l+r)/2,rt<<1
    #define rson (l+r)/2+1,r,rt<<1|1
    #define dbg(x) cout<<#x<<" = "<< (x)<< endl
    #define pb push_back
    #define fi first
    #define se second
    #define ll long long
    #define sz(x) (int)(x).size()
    #define pll pair<long long,long long>
    #define pii pair<int,int>
    #define pq priority_queue
    const int N=1e5+5,MD=1e9+7,INF=0x3f3f3f3f;
    const ll LL_INF=0x3f3f3f3f3f3f3f3f;
    const double eps=1e-9,e=exp(1),PI=acos(-1.);
    char s[N];
    unordered_map<int,int>S;
    int main()
    {
        int n,sum=0;
        scanf("%d%s",&n,s+1);
        int ans=0;
        S[0]=0;
        for(int i=1; i<=n; i++)
        {
            sum+=(s[i]=='1'?1:-1);
            if(S.count(sum))
            {
                ans=max(ans,i-S[sum]);
            }
            else
            {
               S[sum]=i;
            }
        }
        cout<<ans;
        return 0;
    }

    I - Jersey Politics

     POJ - 2454 

    In the newest census of Jersey Cows and Holstein Cows, Wisconsin cows have earned three stalls in the Barn of Representatives. The Jersey Cows currently control the state's redistricting committee. They want to partition the state into three equally sized voting districts such that the Jersey Cows are guaranteed to win elections in at least two of the districts. 

    Wisconsin has 3*K (1 <= K <= 60) cities of 1,000 cows, numbered 1..3*K, each with a known number (range: 0..1,000) of Jersey Cows. Find a way to partition the state into three districts, each with K cities, such that the Jersey Cows have the majority percentage in at least two of districts. 

    All supplied input datasets are solvable.

    Input

    * Line 1: A single integer, K 

    * Lines 2..3*K+1: One integer per line, the number of cows in each city that are Jersey Cows. Line i+1 contains city i's cow census.

    Output

    * Lines 1..K: K lines that are the city numbers in district one, one per line 

    * Lines K+1..2K: K lines that are the city numbers in district two, one per line 

    * Lines 2K+1..3K: K lines that are the city numbers in district three, one per line

    Sample Input

    2
    510
    500
    500
    670
    400
    310

    Sample Output

    1
    2
    3
    6
    5
    4

    Hint

    Other solutions might be possible. Note that "2 3" would NOT be a district won by the Jerseys, as they would be exactly half of the cows.
    这个I要构造两个组,其实直接rand分组就好了,我们的全部rand比较奇葩
    只有C++才能AC,G++是RE的
    #include<stdio.h>
    #include<set>
    #include<algorithm>
    #include<time.h>
    #include<string.h>
    using namespace std;
    
    struct p
    {
        int x,pos;
    }a[10005];
    bool cmp(p a,p b)
    {
        return a.x>b.x;
    }
    int vis[10005];
    int main()
    {
        int n,m;
        srand(time(NULL));
    
        scanf("%d",&n);m=n;
        for(int i=0;i<3*n;i++)
            scanf("%d",&a[i].x),a[i].pos=i+1;
        sort(a,a+3*n,cmp);
    
        for(int i=2*n;i<3*n;i++)
            printf("%d
    ",a[i].pos);
    
        int kum=0;
        for(int i=0;i<2*n;i++)kum+=a[i].x;
    
        while(1)
        {
            int sum=0;
            memset(vis,0,sizeof vis);
            for(int i=0;i<n;)
            {
                int pos=rand()%(2*n);
                if(vis[pos])continue;
                sum+=a[pos].x;
                vis[pos]=1;
                i++;
            }
            if(sum>500*m&&kum-sum>500*m)
            {
                for(int i=0;i<2*n;i++)
                    if(!vis[i])
                        printf("%d
    ",a[i].pos);
                for(int i=0;i<2*n;i++)
                    if(vis[i])
                        printf("%d
    ",a[i].pos);
                break;
            }
        }
        return 0;
    }

    J - Sunscreen

     POJ - 3614

    To avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2500) cows must cover her hide with sunscreen when they're at the beach. Cow i has a minimum and maximum SPF rating (1 ≤ minSPFi ≤ 1,000; minSPFi ≤ maxSPFi ≤ 1,000) that will work. If the SPF rating is too low, the cow suffers sunburn; if the SPF rating is too high, the cow doesn't tan at all........

    The cows have a picnic basket with L (1 ≤ L ≤ 2500) bottles of sunscreen lotion, each bottle i with an SPF rating SPFi (1 ≤ SPFi ≤ 1,000). Lotion bottle i can cover coveri cows with lotion. A cow may lotion from only one bottle.

    What is the maximum number of cows that can protect themselves while tanning given the available lotions?

    Input

    * Line 1: Two space-separated integers: C and L
    * Lines 2..C+1: Line i describes cow i's lotion requires with two integers: minSPFiand maxSPFi 
    * Lines C+2..C+L+1: Line i+C+1 describes a sunscreen lotion bottle i with space-separated integers: SPFi and coveri

    Output

    A single line with an integer that is the maximum number of cows that can be protected while tanning

    Sample Input

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

    Sample Output

    2

    这题用贪心(奈何我们队没人会)

    防晒霜与上限尽量小的牛匹配更优,其实我还是不懂,具体可以问taozi

    #include<stdio.h>
    #include<set>
    #include<algorithm>
    #include<time.h>
    #include<string.h>
    #include<queue>
    using namespace std;
    
    struct p
    {
        int l,r;
    }a[3000],b[3000];
    bool cmp(p a,p b)
    {
        if(a.r==b.r)return a.l<b.l;
        return a.r<b.r;
    }
    bool cmp1(p a,p b)
    {
        return a.l<b.l;
    }
    int main()
    {
        int c,l;
        scanf("%d%d",&c,&l);
        for(int i=0;i<c;i++)scanf("%d%d",&a[i].l,&a[i].r);
        for(int i=0;i<l;i++)scanf("%d%d",&b[i].l,&b[i].r);
        sort(a,a+c,cmp);
        sort(b,b+l,cmp1);
        int cnt=0;
        for(int i=0;i<c;i++)
            for(int j=0;j<l;j++)
                if(b[j].r&&a[i].l<=b[j].l&&b[j].l<=a[i].r)
                    {b[j].r--;cnt++;break;}
        printf("%d
    ",cnt);
        return 0;
    }
  • 相关阅读:
    QR code 乱谈(一)
    用JAVA实现数字水印(可见)
    ctf总结
    Unix/Linux常用命令
    C语言概述
    C语言发发展历史
    为什么要学习C语言
    计算机应用领域
    计算机发展趋势
    如何学习计算机
  • 原文地址:https://www.cnblogs.com/BobHuang/p/9420131.html
Copyright © 2011-2022 走看看