20200315贪心训练总结

1.Pearl Pairing
描述
At Bessie’s recent birthday party, she received N (2 <= N <= 100,000; N%2 == 0) pearls, each painted one of C different colors (1 <= C <= N).

Upon observing that the number of pearls N is always even, her creative juices flowed and she decided to pair the pearls so that each pair of pearls has two different colors.

Knowing that such a set of pairings is always possible for the supplied testcases, help Bessie perform such a pairing. If there are multiple ways of creating a pairing, any solution suffices.输入* Line 1: Two space-separated integers: N and C

  • Lines 2…C + 1: Line i+1 tells the count of pearls with color i: C_i
  • 输出* Lines 1…N/2: Line i contains two integers a_i and b_i indicating that Bessie can pair two pearls with respective colors a_i and b_i.
  • 样例输入
  • 8 3
    2
    2
    4样例输出
    1 3
    1 3
    2 3
    3 2
    提示
    INPUT DETAILS:

There are 8 pearls and 3 different colors. Two pearls have color I; two have color II; four have color III.

OUTPUT DETAILS:

Bessie pairs each pearl of color III with one of color I and II.
总体来讲就是共N朵花,C种颜色,两两配对并且这两种花一定为不同颜色。若每一朵花都能够被配对,则对特定的一种颜色的花来说,数量最多不超过n/2朵,我的想法是将每朵花的颜色用数组记录下来,无须排序(输入时就已经按顺序输入了),将花分为两组,前n/2朵为一组,每次同时输出每组的第i朵即可。AC代码如下

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int n,c,*s,a,r;
int main()
{
    cin>>n>>c;
    s=new int[n+5];
    for(int i=1;i<=c;i++)
    {
        cin>>a;
        for(int j=1;j<=a;j++)
        {
            s[r]=i;
            r++;
        }
    }
    for(int i=0;i<n/2;i++)
        cout<<s[i]<<" "<<s[i+n/2]<<endl;
    return 0;
}

2.Dragons
Kirito is stuck on a level of the MMORPG he is playing now. To move on in the game, he’s got to defeat all n dragons that live on this level. Kirito and the dragons have strength, which is represented by an integer. In the duel between two opponents the duel’s outcome is determined by their strength. Initially, Kirito’s strength equals s.If Kirito starts duelling with the i-th (1 ≤ i ≤ n) dragon and Kirito’s strength is not greater than the dragon’s strength xi, then Kirito loses the duel and dies. But if Kirito’s strength is greater than the dragon’s strength, then he defeats the dragon and gets a bonus strength increase by yi.Kirito can fight the dragons in any order. Determine whether he can move on to the next level of the game, that is, defeat all dragons without a single loss.
Input
The first line contains two space-separated integers s and n(1 ≤ s ≤ 104, 1 ≤ n ≤ 103). Then n lines follow: the i-th line contains space-separated integers xi and yi (1 ≤ xi ≤ 104, 0 ≤ yi ≤ 104) — the i-th dragon’s strength and the bonus for defeating it.
Output
On a single line print “YES” (without the quotes), if Kirito can move on to the next level and print “NO” (without the quotes), if he can’t.
Examples
Input
2 2
1 99
100 0
Output
YES
Input
10 1
100 100
Output
NO
Note
In the first sample Kirito’s strength initially equals 2. As the first dragon’s strength is less than 2, Kirito can fight it and defeat it. After that he gets the bonus and his strength increases to 2 + 99 = 101. Now he can defeat the second dragon and move on to the next level.In the second sample Kirito’s strength is too small to defeat the only dragon and win.
这个题的大意是Kirito打败需要有足够能量才能打败某条龙,打败后又会获得特定的能量,因此这是道典型贪心算法的题目,从小龙开始大,逐步积累能量,若能量不足,则直接输出“NO”
AC代码如下

#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
struct strength{
    int x,y;
}a[1005];
bool cmp(strength p,strength q)
{
    return p.x<q.x;
}
int s,n,i;
int main()
{
    cin>>s>>n;
    for(i=1;i<=n;i++)
        cin>>a[i].x>>a[i].y;
    sort(a+1,a+1+n,cmp);
    for(i=1;i<=n;i++)
    {
        if(s<=a[i].x) break;
        else s+=a[i].y;
    }
    if(i==n+1) cout<<"YES";
    else cout<<"NO";
    return 0;
}
发布了8 篇原创文章 · 获赞 12 · 访问量 181

猜你喜欢

转载自blog.csdn.net/weixin_46434074/article/details/104882929