HHTC_学校集训编程题目(5)(贪心)

HHTC_学校集训编程题目(5)(贪心)

The Dragon of Loowater

Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem.

The shores of Rellau Creek in central Loowater had always been a prime breeding ground for geese. Due to the lack of predators, the geese population was out of control. The people of Loowater mostly kept clear of the geese. Occasionally, a goose would attack one of the people, and perhaps bite off a finger or two, but in general, the people tolerated the geese as a minor nuisance.

One day, a freak mutation occurred, and one of the geese spawned a multi-headed fire-breathing dragon. When the dragon grew up, he threatened to burn the Kingdom of Loowater to a crisp. Loowater had a major problem. The king was alarmed, and called on his knights to slay the dragon and save the kingdom.

The knights explained: “To slay the dragon, we must chop off all its heads. Each knight can chop off one of the dragon’s heads. The heads of the dragon are of different sizes. In order to chop off a head, a knight must be at least as tall as the diameter of the head. The knights’ union demands that for chopping off a head, a knight must be paid a wage equal to one gold coin for each centimetre of the knight’s height.”

Would there be enough knights to defeat the dragon? The king called on his advisors to help him decide how many and which knights to hire. After having lost a lot of money building Mir Park, the king wanted to minimize the expense of slaying the dragon. As one of the advisors, your job was to help the king. You took it very seriously: if you failed, you and the whole kingdom would be burnt to a crisp!

Input

The input contains several test cases. The first line of each test case contains two integers between 1 and 20000 inclusive, indicating the number n of heads that the dragon has, and the number m of knights in the kingdom. The next n lines each contain an integer, and give the diameters of the dragon’s heads, in centimetres. The following m lines each contain an integer, and specify the heights of the knights of Loowater, also in centimetres.

The last test case is followed by a line containing:
0 0

Output

For each test case, output a line containing the minimum number of gold coins that the king needs to pay to slay the dragon. If it is not possible for the knights of Loowater to slay the dragon, output the line:

Loowater is doomed!

Sample Input

2 3
5
4
7
8
4
2 1
5
5
10
0 0

Sample Output

11
Loowater is doomed!

题意很简单,就是找花费最少并且刚好能杀龙的骑士去杀龙,
直接对两个都排序,然后找骑士即可
AC代码:

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;

int a[20005];
int b[20005];
int vis[20005];

int main()
{
    int n,m;
    while(cin>>n>>m && (n !=0 || m != 0)){
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++)
            cin>>a[i];
        for(int i=0;i<m;i++)
            cin>>b[i];
        if(m < n){
            cout<<"Loowater is doomed!"<<endl;
            continue;
        }
        sort(a,a+n);
        sort(b,b+m);
        int k = 0,sum = 0;
        for(int i=0;i<n;i++){
            for(int j=k;j<m;j++){
                if(a[i] <= b[j]){
                    k = j + 1;
                    vis[i] = 1;
                    sum += b[j];
                    break;
                }
            }
        }
        if(vis[n-1] == 0)
            cout<<"Loowater is doomed!"<<endl;
        else
            cout<<sum<<endl;
    }
    return 0;
}

Game Prediction

Suppose there are M people, including you, playing a special card game. At the beginning, each player receives N cards. The pip of a card is a positive integer which is at most N*M. And there are no two cards with the same pip. During a round, each player chooses one card to compare with others. The player whose card with the biggest pip wins the round, and then the next round begins. After N rounds, when all the cards of each player have been chosen, the player who has won the most rounds is the winner of the game.

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 consists of several test cases. The first line of each case contains two integers m (2<=20) and n (1<=50), representing the number of players and the number of cards each player receives at the beginning of the game, respectively. This followed by a line with n positive integers, representing the pips of cards you received at the beginning. Then a blank line follows to separate the cases.

The input is terminated by a line with two zeros.

Output

For each test case, output a line consisting of the test case number followed by the number of rounds you will at least win during the game.

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,求你至少要赢得几把~
比如第一个样例:
最大点数为10,但是10和9都在你手里,所以你至少要赢两把
所以我们从大的开始往后找,我们没有的牌就算做别人的,
当别人没有牌比我们大时,我们绝对赢
AC代码:

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;

bool a[1005];

int main()
{
    int n,m,x,k = 1;
    while(cin>>n>>m && (n !=0 || m != 0)){
        memset(a,false,sizeof(a));
        for(int i=0;i<m;i++){
            cin>>x;
            a[x] = true;
        }
        int num = 0,sum = 0;
        for(int i = n*m; i > 0; i--){
            if(!a[i])
                num++;
            else{
                if(num == 0)
                    sum++;
                else
                    num--;
            }
        }
        cout<<"Case "<<k++<<": "<<sum<<endl;
    }
    return 0;
}

Cow Acrobats

Farmer John’s N (1 <= N <= 50,000) cows (numbered 1…N) are planning to run away and join the circus. Their hoofed feet prevent them from tightrope walking and swinging from the trapeze (and their last attempt at firing a cow out of a cannon met with a dismal failure). Thus, they have decided to practice performing acrobatic stunts.

The cows aren’t terribly creative and have only come up with one acrobatic stunt: standing on top of each other to form a vertical stack of some height. The cows are trying to figure out the order in which they should arrange themselves ithin this stack.

Each of the N cows has an associated weight (1 <= W_i <= 10,000) and strength (1 <= S_i <= 1,000,000,000). The risk of a cow collapsing is equal to the combined weight of all cows on top of her (not including her own weight, of course) minus her strength (so that a stronger cow has a lower risk). Your task is to determine an ordering of the cows that minimizes the greatest risk of collapse for any of the cows.

Input

  • Line 1: A single line with the integer N.
  • Lines 2…N+1: Line i+1 describes cow i with two space-separated integers, W_i and S_i.

Output

  • Line 1: A single integer, giving the largest risk of all the cows in any optimal ordering that minimizes the risk.

Sample Input

3
10 3
2 5
3 3

Sample Output

2

Hint

OUTPUT DETAILS:
Put the cow with weight 10 on the bottom. She will carry the other two cows, so the risk of her collapsing is 2+3-3=2. The other cows have lower risk of collapsing.

对于每头牛而言,视他与他上面的牛为一个整体,总重为sum,
那么对于这头牛的危险值为sum-w-s,要想sum-(w+s)的值最小,
w+s的值就应该最大,由此可见取最优化,wi+si越大越应该在下面
注意不要用cin否则会超时,,,,
AC代码:

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;

struct node{
    int w,s;
}d[50010];

bool cmp(node a, node b){
    return a.w + a.s > b.w + b.s;
}

int main()
{
    int n;
    long long sum;
    while(scanf("%d",&n) != EOF){
        sum = 0;
        for(int i=0;i<n;i++){
            scanf("%d %d",&d[i].w,&d[i].s);
            sum += d[i].w;
        }
        sort(d,d+n,cmp);
        long long maxx = -999999999999;
        for(int i=0;i<n;i++){
            sum -= d[i].w;
            maxx = max(maxx,sum - d[i].s);
        }
        printf("%lld\n",maxx);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值