zoukankan      html  css  js  c++  java
  • BZOJ2802: [Poi2012]Warehouse Store

    2802: [Poi2012]Warehouse Store

    Time Limit: 10 Sec  Memory Limit: 64 MBSec  Special Judge
    Submit: 121  Solved: 65
    [Submit][Status]

    Description


    有一家专卖一种商品的店,考虑连续的n天。
    第i天上午会进货Ai件商品,中午的时候会有顾客需要购买Bi件商品,可以选择满足顾客的要求,或是无视掉他。
    如果要满足顾客的需求,就必须要有足够的库存。问最多能够满足多少个顾客的需求。

    Input

    第一行一个正整数n (n<=250,000)。
    第二行n个整数A1,A2,...An (0<=Ai<=10^9)。
    第三行n个整数B1,B2,...Bn (0<=Bi<=10^9)。

    Output

    第一行一个正整数k,表示最多能满足k个顾客的需求。
    第二行k个依次递增的正整数X1,X2,...,Xk,表示在第X1,X2,...,Xk天分别满足顾客的需求。

    Sample Input

    6
    2 2 1 2 1 0
    1 2 2 3 4 4

    Sample Output

    3
    1 2 4

    HINT

    Source

    题解:
    一直想怎么用线段树来优化DP,结果发现状态太多。。。
    然后看到此题的贪心解法简直惊呆了!
    算法基于这样的事实:如果i>j,且b[i]<b[j],而且只能买一个,不如买i而放弃j。具体的实现可以:
    从第一天开始每次判断当前的东西可不可以卖。如果可卖就卖掉并压入堆。如果卖不掉(缺货),去之前的堆中找最大值比较一下并决定是否卖。
    orz!贪心是王道!
    代码:
     1 #include<cstdio>
     2 
     3 #include<cstdlib>
     4 
     5 #include<cmath>
     6 
     7 #include<cstring>
     8 
     9 #include<algorithm>
    10 
    11 #include<iostream>
    12 
    13 #include<vector>
    14 
    15 #include<map>
    16 
    17 #include<set>
    18 
    19 #include<queue>
    20 
    21 #include<string>
    22 
    23 #define inf 1000000000
    24 
    25 #define maxn 250000+5
    26 
    27 #define maxm 500+100
    28 
    29 #define eps 1e-10
    30 
    31 #define ll long long
    32 
    33 #define pa pair<int,int>
    34 
    35 #define for0(i,n) for(int i=0;i<=(n);i++)
    36 
    37 #define for1(i,n) for(int i=1;i<=(n);i++)
    38 
    39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
    40 
    41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
    42 
    43 #define mod 1000000007
    44 
    45 using namespace std;
    46 
    47 inline int read()
    48 
    49 {
    50 
    51     int x=0,f=1;char ch=getchar();
    52 
    53     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    54 
    55     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
    56 
    57     return x*f;
    58 
    59 }
    60 int n,a[maxn],b[maxn];
    61 bool v[maxn];
    62 priority_queue<pa,vector<pa> >q;
    63 
    64 int main()
    65 
    66 {
    67 
    68     freopen("input.txt","r",stdin);
    69 
    70     freopen("output.txt","w",stdout);
    71 
    72     n=read();
    73     for1(i,n)a[i]=read();
    74     for1(i,n)b[i]=read();
    75     ll now=0;
    76     for1(i,n)
    77     {
    78         now+=a[i];
    79         if(now>=b[i])q.push(pa(b[i],i)),now-=b[i],v[i]=1;
    80         else if(!q.empty())
    81         {
    82             int x=q.top().first,y=q.top().second;
    83             if(x>b[i])q.pop(),q.push(pa(b[i],i)),now+=x-b[i],v[i]=1,v[y]=0;
    84         }
    85     }
    86     int ans=0;
    87     for1(i,n)if(v[i])ans++;
    88     printf("%d
    ",ans);
    89     bool flag=1;
    90     for1(i,n)if(v[i]){if(flag)printf("%d",i),flag=0;else printf(" %d",i);}
    91 
    92     return 0;
    93 
    94 } 
    View Code
  • 相关阅读:
    哈夫曼树
    MongoDB
    Node.js 搭建Web
    Node.js
    HDFS编程
    scp
    MapRecude
    级数
    (转)MySQL百万级数据库优化
    ssh
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/4108857.html
Copyright © 2011-2022 走看看