我是靠谱客的博主 文艺蚂蚁,最近开发中收集的这篇文章主要介绍(省赛训练系列)贪心的说 poj贪心经典题目,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Crossing River
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  POJ 1700
Appoint description: 

Description

A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arrangement must be arranged in order to row the boat back and forth so that all people may cross. Each person has a different rowing speed; the speed of a couple is determined by the speed of the slower one. Your job is to determine a strategy that minimizes the time for these people to get across.

Input

The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. The first line of each case contains N, and the second line contains N integers giving the time for each people to cross the river. Each case is preceded by a blank line. There won't be more than 1000 people and nobody takes more than 100 seconds to cross.

Output

For each test case, print a line containing the total number of seconds required for all the N people to cross the river.

Sample Input

1
4
1 2 5 10

Sample Output

17

H:过河问题
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[1005];
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);

        sort(a,a+n);

        if(n<=2)
        {
             printf("%dn",a[n-1]);
             continue;
        }

        int sum=a[1];//最后一定是 a[0].a[1]回去

        while(n>3)
        {
            if(a[0]+a[n-1]+a[0]+a[n-2]>=a[0]+a[1]+a[n-1]+a[1])
                sum+=a[0]+a[1]+a[n-1]+a[1];
            else
                sum+=a[0]+a[n-1]+a[0]+a[n-2];
            n-=2;
        }
        if(n==3)
            sum+=(a[0]+a[2]);
        printf("%dn",sum);
    }
}

Radar Installation
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  POJ 1328
Appoint description: 

Description

Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d. 

We use Cartesian coordinate system, defining the coasting is the x-axis. The sea side is above x-axis, and the land side below. Given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. Note that the position of an island is represented by its x-y coordinates. 

Figure A Sample Input of Radar Installations


Input

The input consists of several test cases. The first line of each case contains two integers n (1<=n<=1000) and d, where n is the number of islands in the sea and d is the distance of coverage of the radar installation. This is followed by n lines each containing two integers representing the coordinate of the position of each island. Then a blank line follows to separate the cases. 

The input is terminated by a line containing pair of zeros 

Output

For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. "-1" installation means no solution for that case.

Sample Input

3 2
1 2
-3 1
2 1

1 2
0 2

0 0

Sample Output

Case 1: 2
Case 2: 1
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
struct ac
{
    double l;
    double r;
}p[1500];

bool cmp(ac x,ac y)
{
    return (x.r!=y.r)?(x.r<y.r):(x.l>y.l);
}

int main()
{
    int n,rad;
    int d=1;
    while(~scanf("%d%d",&n,&rad),(n&&rad))
    {
        int x,y;
        int f=1;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            if(rad>=y)
            {
                p[i].l=x-sqrt((double)rad*rad-(double)y*y);
                p[i].r=x+sqrt((double)rad*rad-(double)y*y);
            }
            else
            {
                f=0;
            }
        }
        if(!f)
        {
            printf("Case %d: -1n",d++);
            continue;
        }
        sort(p,p+n,cmp);

        double End=p[0].r;
        int cnt=1;
        for(int i=1;i<n;i++)
        {
            if(End<p[i].l)
            {
                cnt++;
                End=p[i].r;
            }
        }
        printf("Case %d: %dn",d++,cnt);
    }
}
Game Prediction
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  POJ 1323
Appoint description: 

Description

Suppose there are M people, including you, playing a special card game. At the beginning, each player receives N cards. The pip of a card is a positive integer which is at most N*M. And there are no two cards with the same pip. During a round, each player chooses one card to compare with others. The player whose card with the biggest pip wins the round, and then the next round begins. After N rounds, when all the cards of each player have been chosen, the player who has won the most rounds is the winner of the game. 



Given your cards received at the beginning, write a program to tell the maximal number of rounds that you may at least win during the whole game. 

Input

The input consists of several test cases. The first line of each case contains two integers m (2?20) and n (1?50), representing the number of players and the number of cards each player receives at the beginning of the game, respectively. This followed by a line with n positive integers, representing the pips of cards you received at the beginning. Then a blank line follows to separate the cases. 

The input is terminated by a line with two zeros. 

Output

For each test case, output a line consisting of the test case number followed by the number of rounds you will at least win during the game. 

Sample Input

2 5
1 7 2 10 9

6 11
62 63 54 66 65 61 57 56 50 53 48

0 0

Sample Output

Case 1: 2
Case 2: 4

N*M发排问题、
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,m;
int a[1005];


int main()
{
    int d=1;
    while(~scanf("%d%d",&n,&m),(n&&m))
    {
        int lose=0;
        int win=0;
        for(int i=1;i<=m;i++)
            scanf("%d",&a[i]);
        sort(a+1,a+1+m);
        for(int i=n*m;i>=1&&m>0;i--)
        {
            if(a[m]!=i)
                lose++;
            else
            {
                if(lose)
                {
                    lose--;
                }
                else
                {
                    win++;
                }
                m--;
            }
        }
        printf("Case %d: %dn",d++,win);
    }
}

Wooden Sticks
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  POJ 1065
Appoint description: 

Description

There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to prepare processing a stick. The setup times are associated with cleaning operations and changing tools and shapes in the machine. The setup times of the woodworking machine are given as follows: 
(a) The setup time for the first wooden stick is 1 minute. 
(b) Right after processing a stick of length l and weight w , the machine will need no setup time for a stick of length l' and weight w' if l <= l' and w <= w'. Otherwise, it will need 1 minute for setup. 
You are to find the minimum setup time to process a given pile of n wooden sticks. For example, if you have five sticks whose pairs of length and weight are ( 9 , 4 ) , ( 2 , 5 ) , ( 1 , 2 ) , ( 5 , 3 ) , and ( 4 , 1 ) , then the minimum setup time should be 2 minutes since there is a sequence of pairs ( 4 , 1 ) , ( 5 , 3 ) , ( 9 , 4 ) , ( 1 , 2 ) , ( 2 , 5 ) .

Input

The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case consists of two lines: The first line has an integer n , 1 <= n <= 5000 , that represents the number of wooden sticks in the test case, and the second line contains 2n positive integers l1 , w1 , l2 , w2 ,..., ln , wn , each of magnitude at most 10000 , where li and wi are the length and weight of the i th wooden stick, respectively. The 2n integers are delimited by one or more spaces.

Output

The output should contain the minimum setup time in minutes, one per line.

Sample Input

3 
5 
4 9 5 2 2 1 3 5 1 4 
3 
2 2 1 1 2 2 
3 
1 3 2 2 3 1 

Sample Output

2
1
3
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;

struct Rat
{
    int x,y;//l w
} p[5005];
bool vis[5005];

bool cmp(Rat a,Rat b)
{
    return (a.x==b.x)?(a.y<b.y):(a.x<b.x);
}

int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0; i<n; i++)
            scanf("%d%d",&p[i].x,&p[i].y);

        memset(vis,0,sizeof(vis));

        sort(p,p+n,cmp);

        int cnt=0;
//        int End=p[0].y;
        for(int i=0; i<n; ++i)
        {
            int End=p[i].y;
            if(!vis[i])
            {
                for(int j=i+1; j<n; ++j)
                {
                    if(!vis[j]&&End<=p[j].y)
                    {
                        vis[j]=1;
                        End=p[j].y;
                    }
                }
                cnt++;
            }
        }
        printf("%dn",cnt);
    }
}

Yogurt factory
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  POJ 2393
Appoint description: 

Description

The cows have purchased a yogurt factory that makes world-famous Yucky Yogurt. Over the next N (1 <= N <= 10,000) weeks, the price of milk and labor will fluctuate weekly such that it will cost the company C_i (1 <= C_i <= 5,000) cents to produce one unit of yogurt in week i. Yucky's factory, being well-designed, can produce arbitrarily many units of yogurt each week. 

Yucky Yogurt owns a warehouse that can store unused yogurt at a constant fee of S (1 <= S <= 100) cents per unit of yogurt per week. Fortuitously, yogurt does not spoil. Yucky Yogurt's warehouse is enormous, so it can hold arbitrarily many units of yogurt. 

Yucky wants to find a way to make weekly deliveries of Y_i (0 <= Y_i <= 10,000) units of yogurt to its clientele (Y_i is the delivery quantity in week i). Help Yucky minimize its costs over the entire N-week period. Yogurt produced in week i, as well as any yogurt already in storage, can be used to meet Yucky's demand for that week.

Input

* Line 1: Two space-separated integers, N and S. 

* Lines 2..N+1: Line i+1 contains two space-separated integers: C_i and Y_i.

Output

* Line 1: Line 1 contains a single integer: the minimum total cost to satisfy the yogurt schedule. Note that the total might be too large for a 32-bit integer.

Sample Input

4 5
88 200
89 400
97 300
91 500

Sample Output

126900

Hint

OUTPUT DETAILS: 
In week 1, produce 200 units of yogurt and deliver all of it. In week 2, produce 700 units: deliver 400 units while storing 300 units. In week 3, deliver the 300 units that were stored. In week 4, produce and deliver 500 units. 

D:酸奶dp
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
int n,s;
int c[10005],y[10005];
int main()
{
    while(~scanf("%d%d",&n,&s))
    {
        for(int i=0;i<n;++i)
            scanf("%d%d",&c[i],&y[i]);
        for(int i=1;i<n;++i)
            c[i]=min(c[i],c[i-1]+s);
        long long ans=0;
        for(int i=0;i<n;i++)
            ans+=(c[i]*y[i]);
        printf("%lldn",ans);
    }

 Pie
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  POJ 3122
Appoint description: 

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
pie 最大最小值问题 二分
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
double v[10005];
#define esp 1e-6
#define pi acos(-1.0)

int n,f;

double mid_sort(double l,double r)
{
    double mid;
    while(r-l>esp)
    {
        int cnt=0;
        mid=(l+r)/2;
        for(int i=0;i<n;i++)
        {
            cnt+=(int)v[i]/mid;
        }
        if(cnt<f)
            r=mid;
        else
            l=mid;
    }
    return mid;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&f);
        f++;
        double Ms=0;
        for(int i=0;i<n;i++)
        {
            scanf("%lf",&v[i]);
            v[i]*=v[i];
            if(Ms<v[i])
                Ms=v[i];
        }
        double k=mid_sort(0.0,Ms);
        printf("%.4lfn",k*pi);
    }
}


River Hopscotch
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  POJ 3258
Appoint description: 

Description

Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, L units away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).

To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.

Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up torocks (0 ≤ M ≤ N).

FJ wants to know exactly how much he can increase the shortest distance *before* he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks.

Input

Line 1: Three space-separated integers:  LN, and  M
Lines 2..  N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.

Output

Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing  M rocks

Sample Input

25 5 2
2
14
11
21
17

Sample Output

4

Hint

Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;

int a[50005];

int l,n,m;

int b_s(int l,int r)
{
    int mid;
    while(l<=r)
    {
        mid=(l+r)/2;
        int cnt=0,last=0;
        for(int i=1; i<=n+1; i++)
            if(mid>=a[i]-a[last]) cnt++;
            else last=i;
        if(cnt>m)
            r=mid-1;
        else
            l=mid+1;
    }
    return l;
}

int main()
{
    scanf("%d%d%d",&l,&n,&m);
    a[0]=0;
    a[n+1]=l;
    for(int i=1; i<=n; i++)
        scanf("%d",&a[i]);
    sort(a,a+n+1);
    printf("%dn",b_s(0,l));

}
Persistent Numbers
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  POJ 2325
Appoint description: 

Description

The multiplicative persistence of a number is defined by Neil Sloane (Neil J.A. Sloane in The Persistence of a Number published in Journal of Recreational Mathematics 6, 1973, pp. 97-98., 1973) as the number of steps to reach a one-digit number when repeatedly multiplying the digits. Example: 
679 -> 378 -> 168 -> 48 -> 32 -> 6.

That is, the persistence of 679 is 6. The persistence of a single digit number is 0. At the time of this writing it is known that there are numbers with the persistence of 11. It is not known whether there are numbers with the persistence of 12 but it is known that if they exists then the smallest of them would have more than 3000 digits. 
The problem that you are to solve here is: what is the smallest number such that the first step of computing its persistence results in the given number?

Input

For each test case there is a single line of input containing a decimal number with up to 1000 digits. A line containing -1 follows the last test case.

Output

For each test case you are to output one line containing one integer number satisfying the condition stated above or a statement saying that there is no such number in the format shown below.

Sample Input

0
1
4
7
18
49
51
768
-1

Sample Output

10
11
14
17
29
77
There is no such number.
2688


最后

以上就是文艺蚂蚁为你收集整理的(省赛训练系列)贪心的说 poj贪心经典题目的全部内容,希望文章能够帮你解决(省赛训练系列)贪心的说 poj贪心经典题目所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(33)

评论列表共有 0 条评论

立即
投稿
返回
顶部