bzoj 2430: [Poi2003]Chocolate 贪心

发现每一次切割都会使另一方向所有切割次数++.
而每一刀的代价就是 cost*切割次数,故贪心按照cost从大到小排序即可.

#include <bits/stdc++.h> 
#define N 200000    
#define LL long long 
using namespace std;  
void setIO(string s) 
{
    string in=s+".in"; 
    freopen(in.c_str(),"r",stdin);  
}
struct node 
{ 
    LL val;  
    int f;    
}t[N];      
bool cmp(node a,node b) { return a.val>b.val;  }
int f[N];    
int main() 
{ 
    // setIO("input");    
    int n,m,i,j;  
    scanf("%d%d",&n,&m); 
    --n,--m;   
    for(i=1;i<=n;++i) scanf("%lld",&t[i].val),t[i].f=0; 
    for(i=1;i<=m;++i) scanf("%lld",&t[n+i].val),t[n+i].f=1;   
    sort(t+1,t+1+n+m,cmp);        
    f[0]=f[1]=1;        
    LL ans=0ll;  
    for(i=1;i<=n+m;++i) 
    {
        ans+=t[i].val*f[t[i].f];   
        ++f[t[i].f^1];      
    }
    printf("%lld
",ans);   
    return 0;  
}
原文地址:https://www.cnblogs.com/guangheli/p/11753292.html