zoukankan      html  css  js  c++  java
  • Luogu P4053 [JSOI2007]建筑抢修

    一道贪心题,看数据范围就知道要套一个数据结构上去。

    别走啊不是什么很高级的数据结构

    考虑最朴素的想法,按建筑的抢修时间排序并先拿小的

    然后随便想想都可以找到一堆反例

    所以我们就直接考虑模拟这个过程,按报废时间排序

    我们扫描到一个建筑时,分情况讨论:

    • 如果可以修好,直接拿去修。并且把这个建筑扔到一个堆里(大根堆)。为后面的操作做准备。
    • 如果不能修好,就将这个建筑的修理时间(t_i)和堆顶的元素的(t_j)比较。若(t_i<t_j),我们就弹出堆顶并选择修理这个建筑(也要扔到堆里)

    贪心的正确性很好证明,我们在面对一个建筑无法修好时,若可以放弃之前的一个占用时间更大的建筑而转修它,肯定会减少总耗时

    CODE

    #include<cstdio>
    #include<cctype>
    #include<queue>
    #include<algorithm>
    using namespace std;
    const int N=150005;
    priority_queue <int> big;
    struct data
    {
    	int t,w;
    }a[N];
    int n,ans; long long tot;
    inline char tc(void)
    {
    	static char fl[100000],*A=fl,*B=fl;
    	return A==B&&(B=(A=fl)+fread(fl,1,100000,stdin),A==B)?EOF:*A++;
    }
    inline void read(int &x)
    {
    	x=0; char ch; while (!isdigit(ch=tc()));
    	while (x=(x<<3)+(x<<1)+ch-'0',isdigit(ch=tc()));
    }
    inline bool cmp(data a,data b)
    {
    	return a.t<b.t;
    }
    int main()
    {
    	//freopen("CODE.in","r",stdin); freopen("CODE.out","w",stdout);
    	register int i; read(n);
    	for (i=1;i<=n;++i)
    	read(a[i].w),read(a[i].t);
    	sort(a+1,a+n+1,cmp);
    	for (i=1;i<=n;++i)
    	{
    		if (tot+a[i].w<=a[i].t) tot+=a[i].w,++ans,big.push(a[i].w); else
    		{
    			if (a[i].w<big.top()) tot-=big.top(),big.pop(),big.push(a[i].w),tot+=a[i].w;
    		}
    	}
    	return printf("%d",ans),0;
    }
    
  • 相关阅读:
    eclipse最有用快捷键整理(转)
    Spring IOC的配置使用(转)
    char varchar varchar2 的区别 (转)
    BigDecimal用法详解(转)
    20160126--springaop
    20160125--Spring
    20150122--接口
    Servlet与JSP的区别
    练习
    151127
  • 原文地址:https://www.cnblogs.com/cjjsb/p/9439267.html
Copyright © 2011-2022 走看看