zoukankan      html  css  js  c++  java
  • 最少拦截系统

    hdoj 1257

    解决:贪心 首先按照 每颗导弹到来时的高度排序,若要配备最少的导弹拦截系统,一定是希望一个拦截系统装置可以拦截尽可能多的导弹,但是这些导弹必须满足先后顺序,后来的而且导弹高度低于前一个的可以共用一个系统,这样有多少满足条件的数字序列就是需要多少个导弹拦截系统装置。

    #include <iostream>
    #include <algorithm>
    #include <bitset>
    using namespace std;
    int n;
    struct node
    {
        int id;
        int high;
    };
    node missile[100000];
    bool operator < (const node &a,const node &b)
    {
        return a.high>b.high;
    }
    
    void greedySelect()
    {
        sort(missile,missile+n);
        bitset<100000> b;
        b.reset();
        int cnt=0,i,j,t;
       while(1)
       {
            for(i=0;i<n;i++)
             if(b[missile[i].id]==0)
             {
                 t=i; 
                 break; 
             }
            if(i==n)break;
            b[missile[t].id]=1;
            for(j=1;j<n;j++)
            {
                if(b[missile[j].id]==0 && missile[j].id > missile[t].id && missile[j].high < missile[t].high)
                {
                    t=j;
                    b[missile[j].id]=1;
                }
            }
            cnt++;
        }
        printf("%d\n",cnt);
    }
    int main()
    {
        while(scanf("%d",&n)!=EOF)
        {
            for(int i=0;i<n;i++)
            {
                missile[i].id=i;
                scanf("%d",&missile[i].high);
            }
            greedySelect();
        }
        system("pause");
        return 0;
    }
    

  • 相关阅读:
    B. Shift and Push
    Codeforces Round #392 (Div. 2)
    D. Make a Permutation!
    C. Bus
    B. Polycarp and Letters
    A. Fair Game
    python-随机数的产生random模块
    python的时间处理-time模块
    python-迭代器与生成器
    python-装饰器
  • 原文地址:https://www.cnblogs.com/hpustudent/p/2189597.html
Copyright © 2011-2022 走看看