zoukankan      html  css  js  c++  java
  • 牛客2020 第九场 B-Groundhog and Apple Tree (贪心+sort

    题意:https://ac.nowcoder.com/acm/contest/5674/B

    n个东西,要拿它你必须大于ai,不够得借钱,买下它能获得bi的钱,问你最少借多少钱。

    思路:

    假设现在你手上有x钱,对于总的来说会加钱的肯定能拿就拿,借钱也要拿(这个因为加钱有利于你以后拿那些亏钱的东西,可以少借一点)

    然后对于铁亏钱的东西,讨论两个东西的顺序

    第一个东西:要x1,完事获得y1

    第二个东西:要x2,完事获得y2

    发现两个拿的顺序:x1+x2-y1,和,x2+x1-y2

    总的来说就是y排一下序就行了

      1 struct EDGE
      2 {
      3     int to,next,cost;
      4 }edge[N<<1];
      5 int etot;
      6 int head[N];
      7 void Init(int n)
      8 {
      9     etot=0;
     10     for(int i=0;i<=n;++i)
     11         head[i]=0;
     12 }
     13 void add(int from,int to,int cost)
     14 {
     15     ++etot;
     16     edge[etot].to=to;
     17     edge[etot].cost=cost;
     18     edge[etot].next=head[from];
     19     head[from]=etot;
     20 }//for(int i=head[u];i;i=edge[i].next)
     21 
     22 
     23 
     24 struct node
     25 {
     26     int dn,up;
     27 };
     28 bool cmp(node a,node b)
     29 {
     30     return a.dn<b.dn;
     31 }
     32 bool cmp2(node a,node b)
     33 {
     34     return a.up>b.up;
     35 }
     36 
     37 int a[N];
     38 
     39 
     40 node dfs(int u,int f)
     41 {
     42     vector<node>A,D;
     43     for(int i=head[u];i;i=edge[i].next)
     44     {
     45         int to=edge[i].to;
     46         if(to==f)continue;
     47         node ans=dfs(to,u);
     48         ans.dn+=edge[i].cost;
     49         if(ans.up<edge[i].cost)
     50         {
     51             ans.dn+=edge[i].cost-ans.up;
     52             ans.up=0;
     53         }
     54         else
     55         {
     56             ans.up-=edge[i].cost;
     57         }
     58         if(ans.up-ans.dn>=0)A.push_back(ans);
     59         else D.push_back(ans);
     60     }
     61     int sz1=A.size();
     62     int sz2=D.size();
     63     node res=node();
     64     res.up=a[u];
     65     sort(A.begin(),A.end(),cmp);
     66     sort(D.begin(),D.end(),cmp2);
     67     for(int i=0;i<sz1;)
     68     {
     69         while(res.up>=A[i].dn)
     70         {
     71             res.up+=A[i].up-A[i].dn;
     72             i++;
     73             if(i==sz1)break;
     74         }
     75         if(i==sz1)break;
     76         res.dn+=A[i].dn-res.up;
     77         res.up=A[i].up;
     78         i++;
     79     }
     80     for(int i=0;i<sz2;++i)
     81     {
     82         node to=D[i];
     83         if(res.up<to.dn)
     84         {
     85             res.dn+=to.dn-res.up;
     86             res.up=to.up;
     87         }
     88         else
     89         {
     90             res.up-=to.dn;
     91             res.up+=to.up;
     92         }
     93     }
     94     return res;
     95 }
     96 
     97 void solve()
     98 {
     99     int n;
    100     sc("%lld",&n);
    101     Init(n);
    102     for(int i=1;i<=n;++i)sc("%lld",&a[i]);
    103     for(int i=1;i<n;++i)
    104     {
    105         int u,v,cost;
    106         sc("%lld%lld%lld",&u,&v,&cost);
    107         add(u,v,cost);
    108         add(v,u,cost);
    109     }
    110     node ans=dfs(1,0);
    111 //    pr("up: %lld dn: %lld
    ",ans.up,ans.dn);
    112     pr("%lld
    ",ans.dn);
    113 }
    114 
    115 signed main()
    116 {
    117 //    q.push({1,1});
    118 //    q.push({2,2});
    119 //    pr("%lld
    ",q.top().up);
    120     int T;
    121     sc("%lld",&T);
    122     while(T--)solve();
    123     return 0;
    124 }
  • 相关阅读:
    内存对齐
    类和对象
    C++ 各种继承方式的类内存布局
    静态变量static
    程序内存布局
    String类
    C++与C的区别
    命名空间
    C
    D
  • 原文地址:https://www.cnblogs.com/--HPY-7m/p/13460415.html
Copyright © 2011-2022 走看看