5道贪心算法 + 1道思维 + 1道搜索


Links:https://vjudge.net/contest/144390#overview


A - Safe Or Unsafe HDU - 2527

统计频率求哈夫曼编码后所需的码长,小于给定要求的即为yes。
用优先队列或者multiset或者自己维护一个有序队列都可以。
我这里用的set
要注意只有一个字母时的处理。

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<set>
#include<string>
#include<string.h>
using namespace std;
int n;
multiset<int> s;
int acc[27];
int main(){
    int t;
    string str;
    scanf("%d", &t);
    while (t--){
        memset(acc, 0, sizeof(acc));
        s.clear();
        scanf("%d", &n);
        cin >> str;
        int len = str.length();
        for (int i = 0; i < len; i++){
            acc[str[i] - 'a']++;
        }
        for (int i = 0; i <= 26; i++){
            if (acc[i]>0){ s.insert(acc[i]); }
        }
        int ans = 0;
        if (s.size() == 1){ ans = *s.begin(); }
        else{
            while (s.size() != 1){
                int a = *s.begin();
                s.erase(s.begin());
                int b = *s.begin();
                s.erase(s.begin());
                ans += a + b;
                s.insert(a + b);
            }
        }
        if (ans <= n){ printf("yes\n"); }
        else{ printf("no\n"); }
    }
    return 0;
}

B - Repair the Wall

贪心,从最大的开始选起,就可以用最少的数量修墙。

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int l, n;
int len[605];

bool cmp(int a, int b){
    return a > b;
}

int main(){
    while (scanf("%d%d", &l, &n) != EOF){
        for (int i = 0; i < n; i++){
            scanf("%d", &len[i]);
        }
        sort(len, len + n,cmp);
        int ans = 0;
        for (int i = 0; i < n; i++){
            if (len[i] >= l){
                ans++;
                l = 0;
                break;
            }
            else{
                l -= len[i];
                ans++;
            }
        }
        if (l != 0){
            printf("impossible\n");
        }
        else{
            printf("%d\n", ans);
        }
    }
    return 0;
}

C - The Highest Mark

之前写过的的贪心dp题

D - Radar Installation

也是之前写过的贪心题了

E - Crossing River

一开始不会做的贪心题,题解方法是选择1.最快和最慢的过去,最快的回来,最快而次慢的过去,最快的回来。2.最快的和次快的过去,最快的回来,最慢和次慢的过去,次快的的回来,这样可以把最慢两个人送过去,是问题规模减小2人,同时还得注意1,2,3人时的处理。

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int p[1005];

int min(int a, int b){
    return a < b ? a : b;
}

int cal(int n){
    int ans = 0;
    if (n == 0){ return 0; }
    if (n == 1 || n == 2){ ans = p[n - 1]; }
    else if (n == 3){ ans=p[2] + p[1]+p[0]; }
    else{
        ans += min(p[1] + p[n - 1] + p[1] + p[0], p[n-1] + p[0] + p[n - 2] + p[0]);
        ans += cal(n - 2);
    }
    return ans;
}

int main(){
    int t,n;
    cin >> t;
    while (t--){
        cin >> n;
        int ans = 0;
        for (int i = 0; i < n; i++){
            scanf("%d", &p[i]);
        }
        sort(p, p + n);
        ans = cal(n);
        printf("%d\n", ans);
    }
    return 0;
}

F - Back to Underworld

两国打架,给出的所有数字对中,两个数字分别就是两个国家的人,但不知道到底是哪个国家的人,要求求出两个国家中某个国家的最大人数可能值。
一开始用dfs然后就超时了,后来用了连通块染色,记得每次要清空那些数组。

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<set>
#include<vector>
#include<string.h>
using namespace std;
int color[20002];
int n;
vector<int>e[20002];
bool exist[20002];

int acount[3];

void dfs(int cnt,int k){
    color[cnt] = k;
    acount[k]++;
    int total = e[cnt].size();
    for (int i = 0; i < total; i++){
        int newp = e[cnt][i];
        if (exist[newp]&&color[newp] == 0){
            //acount[3 - k]++;
            dfs(newp, 3 - k);
        }
    }
}

int main(){
    int t,x,y;
    scanf("%d", &t);
    for (int k = 1; k <= t; k++){
        int num = 0;
        memset(color, 0, sizeof(color));
        memset(exist, 0, sizeof(exist));
        scanf("%d", &n);
        for (int i = 1; i <= 20000; i++){ e[i].clear(); }
        for (int i = 0; i < n;i++){
            scanf("%d%d", &x, &y);
            exist[x] = 1, exist[y] = 1;
            e[x].push_back(y);
            e[y].push_back(x);
            num = max(num, max(x, y));
        }

        int ans = 0;
        for (int i = 1; i <= num; i++){
            if (exist[i]&&color[i] == 0){
                acount[1] = 0;
                acount[2] = 0;
                dfs(i,1);
                ans += max(acount[1], acount[2]);
            }
        }
        printf("Case %d: %d\n", k, ans);
    }
    return 0;
}

G - I can do it!

n个有序对,要嘛是有序对的左边加入左数组,要嘛是有序对的右边加入右数组,求两个数组中的最大值之和的最小值。
如果当前有序对中的一边的数值已经小于了数组的最大值,肯定就把他加入那边的数组,题解的做法是将有序对按左边从小到大排序,然后从末尾遍历回去,这样有序对的左边不断递减,同时维护右边的最小值,便可以得到答案。

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<string.h>
using namespace std;

struct pp{
    int a, b;
}num[100005];

bool cmp(pp a, pp b){
    return a.a < b.a;
}

int main(){
    int t,x,y,n;
    scanf("%d", &t);
    for (int k = 1; k <= t; k++){
        scanf("%d", &n);
        for (int i = 0; i < n; i++){
            scanf("%d%d", &num[i].a, &num[i].b);
        }
        sort(num, num + n, cmp);
        int ans = num[n - 1].a + num[n - 1].b,r=num[n-1].b;
        for (int i = n - 2; i >= 0; i--){
            ans = min(ans, num[i].a + r);
            r = max(r, num[i].b);
        }
        printf("Case %d: %d\n", k, ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值