【codeforces 779C】Dishonest Sellers

【题目链接】:http://codeforces.com/contest/779/problem/C

【题意】

有n个商品;
打折前买和打折后买的价格不一样;
且必须有至少k个商品在打折前买;
问你买走全部n个商品最少需要多少钱;

【题解】

/*
    每件商品先选择打折前和打折后里面价格较低的;
    然后把价格全部加起来(我好像神经病写了个排序);
    然后看看你选了几个第一种商品;
    少选了,就选一些在打折后买的商品加上差价在打折前买;
    把差价升序排一下,选择大于等于0的加上差价就是答案了
    if (a[i]<b[i])
        {
            c[i].x = a[i];
            c[i].id = 1;
        }
        else
        {
            //a[i]>=b[i]
            c[i].x = b[i];
            c[i].id = 2;
        }
    sort(c+1,c+1+n);
    int ans = 0,num[3];
    rep1(i,1,n)
        ans+=c[i].x,num[c[i].id]++;
    if (num[1]<k)
    {
        rep1(i,1,n)
            c[i].x = b[i].x-a[i].x;
        sort(c);
        rep1(i,1,n)
        if (c[i].x>=0)
        {
            if (num[1]<k)
            {
                ans+=c[i].x;
                num[1]++;
            }
            else
                break;
        }   
    }
*/


【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)

typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 2e5+1000;

struct abc
{
    int x, id;
};

int a[N], b[N],n ,k, num[3];
abc c[N];

bool cmp1(abc a, abc b)
{
    return a.x < b.x;
}

int main()
{
    //freopen("F:\\rush.txt", "r", stdin);
    rei(n), rei(k);
    rep1(i, 1, n)
        rei(a[i]);
    rep1(i, 1, n)
        rei(b[i]);
    rep1(i,1,n)
        if (a[i]<b[i])
        {
            c[i].x = a[i];
            c[i].id = 1;
        }
        else
        {
            //a[i]>=b[i]
            c[i].x = b[i];
            c[i].id = 2;
        }
    sort(c + 1, c + 1 + n,cmp1);
    int ans = 0;
    rep1(i, 1, n)
        ans += c[i].x, num[c[i].id]++;
    if (num[1]<k)
    {
        rep1(i, 1, n)
            c[i].x = a[i] - b[i];
        sort(c+1,c+1+n,cmp1);
        rep1(i, 1, n)
            if (c[i].x >= 0)
            {
                if (num[1]<k)
                {
                    ans += c[i].x;
                    num[1]++;
                }
                else
                    break;
            }
    }
    printf("%d\n", ans);
    return 0;
}
posted @ 2017-10-04 18:45  AWCXV  阅读(271)  评论(0编辑  收藏  举报