【假期自學】| 算法競賽入門競賽經典訓練指南

算法設計基礎

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 <stdio.h>
#include <iostream>
#include <algorithm>

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 <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>

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、貪心思想的證明

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章