|洛谷|贪心|P1208 [USACO1.3]混合牛奶 Mixing Milk

http://www.luogu.org/problem/show?pid=1208

贪心地优先选择最便宜的牛奶

#include
#include
#include
#include
#define ms(i,j) memset(i,j, sizeof i);
using namespace std;
int p[5005], a[5005];
void qs(int l, int r) {  
    int i=l, j=r;  
    int x=p[(l+r)/2], y;  
    do {   
        while (p[i]x) j--;  
        if (i<=j) {  
            y = p[i], p[i] = p[j], p[j] = y;  
            y = a[i], a[i] = a[j], a[j] = y;  
            i++, j--;  
        }  
    }while (i<=j);  
    if (l=n)
        {
           int sy = n - tot;
           money += sy * p[i];
           break;
        } 
        else
        {
            tot += a[i];
            money += a[i] * p[i];
        }
    }
    printf("%d\n", money);
    return 0;
}


你可能感兴趣的:(贪心,洛谷)