【假期自学】| 算法竞赛入门竞赛经典训练指南

算法设计基础

1.1 思维的体操

Uva 11292 The Dragon of Loowter
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 Specification:
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 mlines 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 Specification:
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
Output for Sample Input:
11
Loowater is doomed!

题意
雇佣一些骑士去杀死一个有n个头的恶龙,能力值为x的骑士可以砍掉一个直径不超过x的恶龙的头。雇佣骑士且花费的金币最少
贪心 策略

#include 
#include 
#include 

using namespace std;

const int maxn = 20005;
int n,k;
int a[maxn],b[maxn];
int ans,cur;


int main(){
    while(scanf("%d%d",&n,&k)==2&&n&&k){
        for(int i = 0;i<n;i++) scanf("%d",&a[i]);
        for(int i = 0;i<k;i++) scanf("%d",&b[i]);
        sort(a,a+n);
        sort(b,b+k);
        ans = 0;
        cur = 0;
        for(int i = 0;i<k;i++){
            if(b[i] >= a[cur]){
                ans += b[i];
                if(++cur == n) break;
            }
        }
        if(cur < n) printf("Loowter is doomed!\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}

Uva 11729 Commando War
“Waiting for orders we held in the wood, word from the front never came
By evening the sound of the gunfire was miles away
Ah softly we moved through the shadows, slip away through the trees
Crossing their lines in the mists in the fields on our hands and our knees
And all that I ever, was able to see The fire in the air, glowing red, silhouetting the smoke on the breeze” There is a war and it doesn’t look very promising for your country. Now it’s time to act. You have a commando squad at your disposal and planning an ambush on an important enemy camp located nearby. You have N soldiers in your squad. In your master-plan, every single soldier has a unique responsibility and you don’t want any of your soldier to know the plan for other soldiers so that everyone can focus on his task only. In order to enforce this, you brief every individual soldier about his tasks separately and just before sending him to the battlefield. You know that every single soldier needs a certain amount of time to execute his job. You also know very clearly how much time you need to brief every single soldier. Being anxious to finish the total operation as soon as possible, you need to find an order of briefing your soldiers that will minimize the time necessary for all the soldiers to complete their tasks. You may assume that, no soldier has a plan that depends on the tasks of his fellows. In other words, once a soldier begins a task, he can finish it without the necessity of pausing in between.
Input
There will be multiple test cases in the input file. Every test case starts with an integer N (1 ≤
N ≤ 1000), denoting the number of soldiers. Each of the following N lines describe a soldier with two
integers B (1 ≤ B ≤ 10000) & J (1 ≤ J ≤ 10000). B seconds are needed to brief the soldier while
completing his job needs J seconds. The end of input will be denoted by a case with N = 0. This case
should not be processed.
Output
For each test case, print a line in the format, ‘Case X: Y ’, where X is the case number & Y is the
total number of seconds counted from the start of your first briefing till the completion of all jobs.
Sample Input
3
2 5
3 2
2 1
3
3 3
4 4
5 5
0
Sample Output
Case 1: 8
Case 2: 15
题意
假设当前有n个部下,每个部下需要完成一项任务。第i个部下需要花Bi分钟交代任务,然后他会立刻独立地、无间断地执行Ji分钟后完成任务。小光需要选择交代任务的顺序,使得所有任务尽早执行完毕(即最后一个执行完的任务应尽早结束)。注意,不能同时给两个部下交代任务,但部下们可以同时执行他们各自的任务。
贪心思想:将完成任务执行时间最长的任务放置最前
证明:
假设交换两个相邻任务x和y交换之前x在y前,交换之后y在x之前,对其它任务没有影响。
对于x和y任务
情况一:
交换之前y比x早结束,最终时间不会变好
情况二:
交换之前x比y先结束,可得到[x].j >= [y].j 贪心依据

#include 
#include 
#include 
#include 

using namespace std;

struct Job
{
    int b,j;
    bool operator < (const Job& x) const
    {
        return j > x.j;
    }
};

int main()
{
    int n,b,j,ans;
    int kase = 1;
    while(scanf("%d",&n)==1&&n)
    {
        vector<Job>v;
        for(int i = 0; i<n; i++)
        {
            scanf("%d%d",&b,&j);
            v.push_back((Job){b,j});
        }
        sort(v.begin(),v.end());
        int s =0;
        ans = 0;
        for(int i = 0; i<n; i++)
        {
            s += v[i].b;
            ans = max(ans,s + v[i].j);
        }
        printf("Case %d:%d\n",kase++,ans);
    }
    return 0;
}

总结
1、不定长数组 vector
2、c++重载运算符
结构体中的重载和非结构体中的重载
3、贪心思想的证明

你可能感兴趣的:(寒假自学)