概述
Description
Input
Output
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);
}
}
Description
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 is terminated by a line containing pair of zeros
Output
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);
}
}
Description
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 is terminated by a line with two zeros.
Output
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);
}
}
Description
(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
Output
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);
}
}
Description
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
* Lines 2..N+1: Line i+1 contains two space-separated integers: C_i and Y_i.
Output
Sample Input
4 5 88 200 89 400 97 300 91 500
Sample Output
126900
Hint
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);
}
Description
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 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
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);
}
}
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 toM rocks (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
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
Sample Input
25 5 2 2 14 11 21 17
Sample Output
4
Hint
#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));
}
Description
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
Output
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贪心经典题目所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复