【挑战程序设计竞赛】- 2.2贪心法(硬币最少、区间覆盖、字典序最小、标记最少、木板切割)

四年前犯的错再做一遍还是会犯。
四年前不看presentation要求,四年后依然PE。
四年前忘记longlong,四年后还是会忘。

2.2 贪心法

核心思想:不断选取最优策略。

例题1-硬币:有1、5、10、50、100、500面值的硬币各 c 1 c_1 c1 c 5 c_5 c5 c 10 c_{10} c10 c 50 c_{50} c50 c 100 c_{100} c100 c 500 c_{500} c500枚。需要支付A元,问最少需要硬币几枚。

解决思路:尽可能从多到少的使用硬币。

const int V[6]={1,5,10,50,100,500};
int C[6]; // 输入的硬币数量
int A;
void solve(){
    int ans = 0;
    for(int i=5; i>=0; i--){
        int t=min(A/V[i], C[i]);
        A-=t;
        ans+=t;
    }
    printf("%d\n", ans);
}

例题2-区间问题:有n项工作,每项分别在 s i s_i si开始、在 t i t_i ti结束。对每项工作可以选择参与与否,如果参与必须全程参与。另外工作时间段不可重叠(时间点也不可)。目标是尽可能多地参与工作,最多能多少?

解决思路:

  1. 总是选择开始最早的工作:如果持续时间长到覆盖所有那就GG。X

  2. 总是选择时间最短的工作。X

  3. 总是选择结束最早的工作。√

const int NMAX = 100010;
int N,S[NMAX],T[NMAX];
pair<int,int> itv[NMAX];//用来排序
void solve(){
    for(int i=0; i<N; i++){
        itv[i].first = T[i];
        itv[i].second = S[i];
    }
    sort(itv, itv+N);
    int ans = 0, t = 0;
    for(int i=0; i<N; i++){
        if(itv[i].first>t){
            ans++;
            t = itv[i].first;
        }
    }
    printf("%d\n", ans);
}

例题3:字典序最小问题 POJ3617

给定长度为N的字符串S,要构造一个长度为N的字符串T。最初T是空串,然后反复进行下列操作:
从S头部删除一个字符,加到T尾部;
从S尾部删除一个字符,加到T尾部。

目标是构造字典序尽可能小的字符串T。
1<=N<=2000,且S只有大写英文字母。

思路:每次比较S的前后开始和末尾字母,首选小的。

但是对于字母相同的情况:我们需要比较下一个字符大小。

#include <cstdio>
#include <cstring>
using namespace std;
const int NMAX=2010;
int N;
char S[NMAX];
void solve(){
    int l = 0, r = N-1, cnt = 0;
    while(l<=r){
        bool lef = false;
        for(int i=0; l+i<=r; i++){
            if(S[l+i]<S[r-i]){
                lef = true;
                break;
            }
            else if(S[l+i]>S[r-i]){
                break;
            }
        }
        if(lef){
            putchar(S[l++]);
        }
        else{
            putchar(S[r--]);
        }
        cnt++;
        //原题中的presentation要求
        if(cnt%80==0)
            putchar('\n');
    }
    if(cnt%80)
        putchar('\n');
}
int main(){
    while(~scanf("%d",&N)){
        for(int i=0; i<N; i++){
            scanf(" %c",&S[i]);
        }
        solve();
    }
    return 0;
}

例题4: 标记最少 POJ3069

直线上有N个点,每个点位置为 X i X_i Xi
从这N个点中选出最少数量的标记点以保证:对任意点,距离R内必有标记点。
问至少要选几个点?

1<=N<=1000, 0<=R<=1000, 0<= X i X_i Xi<=1000

思路:从左往右,距离起点R范围内最右侧点做标记点,然后再往右R找到第一个不包含的点。
对于这个新点作为起点相同操作,不断重复。

#include <cstdio>
#include <algorithm>
using namespace std;
const int NMAX = 1011;
int N,R,X[NMAX];
void solve(){
    sort(X,X+N);
    int i = 0, ans = 0;
    while( i<N ){
        int s = X[i++];
        while( i<N && X[i]<=s+R ){i++;}
        //i跳出时是正好过了1位,因此--
        int p = X[i-1]; //标记点
        ans++;
        while( i<N && X[i]<=p+R ){i++;}
        //i跳出时正好是下个起点位置
    }
    printf("%d\n",ans);
}
int main(){
    while(~scanf("%d%d",&R,&N)){
        if(R==-1&&N==-1)break;
        for(int i=0; i<N; i++){
            scanf("%d", &X[i]);
        }
        solve();
    }
    return 0;
}

例题5 木板切割 POJ3253

将一块很长的木板切割成N块。一次切割开销等于切割后的两块木板长度和,切割过程认为没有损耗。问达成切割目标的最小开销。
1<=N<=20000, 0<= L i L_i Li<=50000

思路:逆向考虑,就是每次合并长度最短的两块木板。

#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
ll N, x, ans;

int main(){
    while(~scanf("%lld",&N)){
        ans = 0;
        priority_queue<ll, vector<ll>, greater<ll> > pq;//小顶堆,默认是大顶堆
        for(int i=0;i<N;i++){
            scanf("%lld",&x);
            pq.push(x);
        }
        while(pq.size()>1){
            int now1 = pq.top();
            pq.pop();
            int now2 = pq.top();
            pq.pop();
            int nex=now1+now2;
            ans+=nex;
            pq.push(nex);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

四年前犯的错再做一遍还是会犯。四年前忘记longlong,四年后还是会忘。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值