贪心算法

第三周贪心算法题(一)

A-A


题目:Farmer John has received a noise complaint from his neighbor, Farmer Bob, stating that his cows are making too much noise.
FJ’s N cows (1 <= N <= 10,000) all graze at various locations on a long one-dimensional pasture. The cows are very chatty animals. Every pair of cows simultaneously carries on a conversation (so every cow is simultaneously MOOing at all of the N-1 other cows). When cow i MOOs at cow j, the volume of this MOO must be equal to the distance between i and j, in order for j to be able to hear the MOO at all. Please help FJ compute the total volume of sound being generated by all N*(N-1) simultaneous MOOing sessions.

Input
Line 1: N
Lines 2…N+1: The location of each cow (in the range 0…1,000,000,000).

Output
There are five cows at locations 1, 5, 3, 2, and 4.

Sample Input
5
1
5
3
2
4

Sample Output
40

提示:这运用了很基本的算法,我认为只要弄清楚其中的b[i]=b[i-1]+(i-2)(a[i]-a[i-1])-((n-i)(a[i]-a[i-1]));此处的算式,顺利AC没问题。

代码如下:

#include<iostream>
#include<algorithm>
using namespace std;
long long a[10000],b[10000];
long long sum;
int main()
{
     int n;
     cin>>n;
     int i;
     for(i=1;i<=n;i++)
        {
             cin>>a[i];
        }
         sort(a+1,a+n+1);
              b[1]=0;
         for(i=2;i<=n;i++)
            {
                 b[1]+=a[i]-a[1];
             }
        sum=b[1];
        for(i=2;i<=n;i++)
       {
           b[i]=b[i-1]+(i-2)*(a[i]-a[i-1])-((n-i)*(a[i]-a[i-1]));
           sum+=b[i];
         }
        cout<<sum<<endl;
        return  0;
        }

D-D

题目:Farmer John has a problem: the dirt road from his farm to town has suffered in the recent rainstorms and now contains (1 <= N <= 10,000) mud pools.
Farmer John has a collection of wooden planks of length L that he can use to bridge these mud pools. He can overlap planks and the ends do not need to be anchored on the ground. However, he must cover each pool completely.
Given the mud pools, help FJ figure out the minimum number of planks he needs in order to completely cover all the mud pools.

Input
Line 1: Two space-separated integers: N and L
Lines 2…N+1: Line i+1 contains two space-separated integers: s_i and e_i (0 <= s_i < e_i <= 1,000,000,000) that specify the start and end points of a mud pool along the road. The mud pools will not overlap. These numbers specify points, so a mud pool from 35 to 39 can be covered by a single board of length 4. Mud pools at (3,6) and (6,9) are not considered to overlap.

Output
Line 1: The miminum number of planks FJ needs to use.
Sample Input
3 3
1 6
13 17
8 12
Sample Output
5

提示:可转换成区间长度问题。

#include<iostream>
#include<algorithm>


using namespace std;
struct pool
{
    int e,s;
}p[10000];
bool cmp(pool a,pool b)
{
    return a.e<b.e;
}
int main()
{
   int N,L,i;
   cin>>N>>L;
   for(i=0;i<N;i++)
   {
     cin>>p[i].s>>p[i].e;
   }
   sort(p,p+N,cmp);
    int sum=0,h=0;
    for(i=0;i<N;i++)
    {
    if(h>=p[i].e)
    continue;
    h=max(p[i].s,h);
    while(h<p[i].e)
    {
    sum++;
    h+=L;
    }
    }
    cout<<sum;
}

F-F

题目:FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.

Input
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1’s. All integers are not greater than 1000.

Output
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
Sample Input
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1

Sample Output
13.333
31.500

提示:可以按照性价比排序。

#include<iostream>
#include<algorithm>
using namespace std;
struct bang
{
double F,J,x;
}b[1000];
bool cmp(bang a,bang b)
{
return a. x>b. x;
}
int main()
{
    int M,N,i;
    while(scanf("%d%d",&M,&N)&&(M!=-1||N!=-1))
{
     for(i=0;i<N;i++)
    {scanf("%lf%lf",&b[i].J,&b[i].F);
    b[i]. x=1.0*b[i].J/b[i]. F;
    }
    sort(b,b+N,cmp);
    double d=0;
    for(i=0;i<N;i++)
    {
    if(M>=b[i].F)
    {
    d+=b[i].J;
    M-=b[i].F;
    }
    else
    {
        d+=1.0*M*b[i].x;break;
    }
    }
    if(N=0)
    printf("%.3lf\n",N);
    else
    printf("%.3lf\n",d);
}

}

K-K

“今年暑假不AC?”
“是的。”
“那你干什么呢?”
“看世界杯呀,笨蛋!”
“@#$%^&*%…”
确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了。
作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目)

Input
输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。

Output
对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。
Sample Input
12
1 3
3 4
0 7
3 8
15 19
15 20
10 15
8 18
6 12
5 10
4 14
2 9
0

Sample Output
5

提示:先按照结束时间升序,再观察结束时间和开始时间的关系。

#include<iostream>
using namespace std;
#include<cstring>
#include<algorithm>
struct program
{
    int s,e;
}p[100];
bool cmp(program a,program b)
{
    return a.e<b.e;
}
int main()
{

     int n;

    while( cin>>n&&n!=0)
    {
    for(int i=0;i<n;i++)
    {cin>>p[i].s>>p[i].e;}
    sort(p,p+n,cmp);
    int t=p[0].e,sum=1;
    for(int i=0;i<n;i++)
    {
       if(t<=p[i].s)
      {sum++;
       t=p[i]. e;}
    }
    cout<<sum<<endl;
    }
    return 0;
}

X-X

题目:In one of the countries of Caribbean basin all decisions were accepted by the simple majority of votes at the general meeting of citizens (fortunately, there were no lots of them). One of the local parties, aspiring to come to power as lawfully as possible, got its way in putting into effect some reform of the election system. The main argument was that the population of the island recently had increased and it was to longer easy to hold general meetings.
The essence of the reform is as follows. From the moment of its coming into effect all the citizens were divided into K (may be not equal) groups. Votes on every question were to be held then in each group, moreover, the group was said to vote “for” if more than half of the group had voted “for”, otherwise it was said to vote “against”. After the voting in each group a number of group that had voted “for” and “against” was calculated. The answer to the question was positive if the number of groups that had voted “for” was greater than the half of the general number of groups.
At first the inhabitants of the island accepted this system with pleasure. But when the first delights dispersed, some negative properties became obvious. It appeared that supporters of the party, that had introduced this system, could influence upon formation of groups of voters. Due to this they had an opportunity to put into effect some decisions without a majority of voters “for” it.
Let’s consider three groups of voters, containing 5, 5 and 7 persons, respectively. Then it is enough for the party to have only three supporters in each of the first two groups. So it would be able to put into effect a decision with the help of only six votes “for” instead of nine, that would .be necessary in the case of general votes.
You are to write a program, which would determine according to the given partition of the electors the minimal number of supporters of the party, sufficient for putting into effect of any decision, with some distribution of those supporters among the groups.

Input
The input of this problem contains two lines. In the first line an only natural number K <= 101 — a quantity of groups — is written. In the second line there are written K natural numbers, separated with a space. Those numbers define a number of voters in each group. In order to simplify the notion of “the majority of votes” we’ll say that the number of groups also as the number of voters in each group is odd. You may also consider, that the population of the island does not exceeds 10001 persons.

Output
You should write an only natural number — a minimal quantity of supporters of the party, that can put into effect any decision.

Sample Input
3
5 7 5

Sample Output
6

#include<iostream>
#include<algorithm>
using namespace std;
struct person
{
int p;
}s[10001];
bool cmp(person a,person b)
{
return a. p<b. p;
}
int main()
{
     int k,i;
    while(cin>>k&&k>=0&&k<=10001)
    {
    for(i=0;i<k;i++)
    cin>>s[i]. p;
    sort(s,s+k,cmp);
    int sum=0;
    for(i=0;i<k/2+1;i++)
    {sum+=(s[i].p)/2+1;}
    cout<<sum;
    }
}

总结

如果一个问题可以同时用几种方法解决,贪心算法应该是最好的选择之一,这是最近做的题中较为简单的题目,在当中也了解了许多函数,这将会写在下一篇中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值