码迷,mamicode.com
首页 > 其他好文 > 详细

8.9 - 8. 贪心专题

时间:2015-08-10 21:32:41      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

---恢复内容开始---

1.HDU 2037

  给定每个电视节目Ti的时间段[s,e],问最多能收看几个完整节目。

选择不相交区间  

  明显可以得出的是,如果节目1的时间区间在节目2的时间区间内,就不用考虑节目2了。

  然后需要选择最大节目数,先按结束时间e排序,如果e相同,s靠后的在前。然后选取e最小的节目。因为没有节目在T0之前,用它和T1比较,因为T1的结束时间靠后所以T1对后面的节目的“影响”更大(shenmegui)所以T0在前~

技术分享
#include<bits/stdc++.h>

using namespace std;
struct l{
    int a;
    int b;
};

bool cmp(l x, l y){
    return x.b < y.b;

}

int main()
{
    int n;
    while(scanf("%d", &n) && n)
    {
            
        int ai, bi, sum = 1, k=0;//ai当前左端点  bi当前右端点 
        l x[100];
        for(int i=0; i<n; i++)
        {
            scanf("%d%d", &x[i].a, &x[i].b);
        }
        
        sort(x,x+n,cmp);
        
        ai = x[0].a;
        bi = x[0].b;
            
        for(int i=k; i<n+k; i++)
        {
            if(x[i].a >= bi)
            {
                ai = x[i].a;
                bi = x[i].b;
                sum++;
            }
        }
        if(n == 0) sum--;
        printf("%d\n",sum);
        
    }
    return 0;
}
View Code

   --遇到的问题--

  一开始不会写cmp函数QAQQ  另外不用保存a端点 没改了

 2.HDU 1009

  部分背包问题,类似还有 XDOJ 1087

技术分享
#include<bits/stdc++.h>

using namespace std;

struct f{
    float a;
    float b;
    float div;
} x[1010];

bool cmp(f a, f b)
{
    return a.div > b.div;
}

int main()
{
    int m, n;
    while(scanf("%d%d", &m, &n) && m != -1 && n != -1)
    {
        double sum = 0.0;
        for(int i = 0; i < n; i++)
        {
            scanf("%f%f", &x[i].a, &x[i].b);
            x[i].div = x[i].a/x[i].b;
        }
        
        sort(x, x+n, cmp);
        
        for(int i = 0; i < n; i++)
        {
            if(m >= x[i].b)
            {
                sum += x[i].a;
                m -= x[i].b;
            } 
            else
            {
                sum += m*x[i].div;
                break;
            }
        }
        printf("%.3lf\n", sum);
    }
    return 0; 
}
View Code

  --遇到的问题--

  注意浮点数类型转换

 3.POJ 3646

  对于每一个高度的头 尽量用高度足够的骑士中最低的杀死

技术分享
#include<stdio.h>
#include<algorithm>
using namespace std;


int main()
{
    int m, n;
    while(scanf("%d%d", &n, &m) && (n || m))
    {
        int d[20158], k[20158];
        int sum = 0, j = 0;
        
        for(int i = 0; i < n; i++)
            scanf("%d", &d[i]);
        for(int i = 0; i < m; i++)
            scanf("%d", &k[i]);
        
        sort(d, d+n);
        sort(k, k+m);
        
        for(int i = 0; i < n; i++)
        {
            while(j <= m)//这里为什么不是小于呢  因为如果是  并且刚好m-1和n-1配对  那么也会出现j==m 
            {
                if(k[j++] >= d[i]) 
                {
                    sum += k[j-1];
                    break;    
                }
            }
        }
        
        if(j > m) printf("Loowater is doomed!\n");
        else printf("%d\n", sum);
    }
}
View Code

 4.POJ 2709

  买尽可能少的“颜料套装”,来满足你需要的这N+1种颜料。

  对于多种颜色,每种取出一定的量来调出灰色。相当于把灰色颜料加到每种颜色需要的量上,最后用最多的那种+49/50。

  用很暴力的方法,每1ml灰色颜料加到 现在所需最少的三种之上,然后重新排序直到灰色全部调出。

技术分享
#include<stdio.h>
#include<algorithm>
using namespace std;

int main()
{
    int n;
    while(scanf("%d", &n) && n)
    {
        int color[15];
        int grey, d;
        
        for(int i = 0; i < n; i++)
            scanf("%d", &color[i]);
        
        scanf("%d", &grey);
        
        sort(color, color+n);
        
        if(n > 3)
        {
            while(grey--)
            {
                color[0] ++; 
                color[1] ++; 
                color[2] ++; 
                
                sort(color, color+n); 
            }     
        }
        else color[2] += grey;
            
        printf("%d\n", (color[n-1] + 49)/50);
    }
}
View Code

  --遇到的问题--

  这样是可以过,但是数据大一些怎么办呢?

 5.HDU 4296

  盖房子,某一层的PDV等于这一层之上所有重量之和(Σw j)减去这一层的可承重s i

  因为两层之间对调对于其他层没有影响,对于相邻两层a,b

  若a在上 用sa减去wb 剩下的s来承受sumw

  若b在上 用sb减去wa 剩下的s来承受sumw

 

技术分享
#include<stdio.h>
#include<algorithm>

using namespace std;

struct f{
    int w, s;
} flr[201508];

bool cmp(f a, f b)
{
    return a.s - b.w > b.s - a.w;
}

int main()
{
    int n;
    while(scanf("%d", &n) != EOF)
    {
        int t;
        
        for(int i = 0; i < n; i++)
            scanf("%d%d", &flr[i].w, &flr[i].s);
        
        sort(flr, flr+n, cmp);
        
        long long sumw = 0, PDV = -flr[0].w;
        
        for(int i = n - 1; i > -1; i--)
        {
            if(sumw - flr[i].s > PDV)
                PDV = sumw - flr[i].s;
            sumw += flr[i].w;
        }
        //Êä³ö×î´ó·çÏÕ¡£¡£¡£¡£¡£¡£ 
        printf("%I64d\n", PDV);
    }
}
View Code
技术分享
#include<stdio.h>
#include<algorithm>

using namespace std;

struct cow{
    int a, b;
} c[3000];

struct sunscreen{
    int spf, surplus;
} l[3000];

bool cmp1(cow x, cow y)
{
    if(x.b == y.b) return x.a < y.a;
    return x.b < y.b;
}
bool cmp2(sunscreen x, sunscreen y)
{
    return x.spf < y.spf;
}
int main()
{
    int n, m, k = 0, sum = 0;
    scanf("%d%d", &n, &m);
    
    for(int i = 0; i < n; i++)
    {
        scanf("%d%d", &c[i].a, &c[i].b);
    }
    for(int i = 0; i < m; i++)
    {
        scanf("%d%d", &l[i].spf, &l[i].surplus);
    }
    
    sort(c, c + n, cmp1);
    sort(l, l + n, cmp2);
    
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(l[i].surplus && l[i].spf => c[i].a && l[i].spf <= c[i].b)
            {
                l[i].surplus--;
                sum++;
                break;
            }
        }
    }
    printf("%d\n", sum);
    return 0;
}
还没过

 

8.9 - 8. 贪心专题

标签:

原文地址:http://www.cnblogs.com/nanf/p/4719165.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!