VK Cup 2012 Round 1161B Discounts(贪心)

题目链接: 传送门

Discounts

time limit per test:3 second     memory limit per test:256 megabytes

Description

One day Polycarpus stopped by a supermarket on his way home. It turns out that the supermarket is having a special offer for stools. The offer is as follows: if a customer's shopping cart contains at least one stool, the customer gets a 50% discount on the cheapest item in the cart (that is, it becomes two times cheaper). If there are several items with the same minimum price, the discount is available for only one of them!
Polycarpus has k carts, and he wants to buy up all stools and pencils from the supermarket. Help him distribute the stools and the pencils among the shopping carts, so that the items' total price (including the discounts) is the least possible.
Polycarpus must use all k carts to purchase the items, no shopping cart can remain empty. Each shopping cart can contain an arbitrary number of stools and/or pencils.

Input

The first input line contains two integers n and k (1 ≤ k ≤ n ≤ 103) — the number of items in the supermarket and the number of carts, correspondingly. Next n lines describe the items as "ci ti" (without the quotes), where ci (1 ≤ ci ≤ 109) is an integer denoting the price of the i-th item, ti (1 ≤ ti ≤ 2) is an integer representing the type of item i (1 for a stool and 2 for a pencil). The numbers in the lines are separated by single spaces.

Output

In the first line print a single real number with exactly one decimal place — the minimum total price of the items, including the discounts.
In the following k lines print the descriptions of the items in the carts. In the i-th line print the description of the i-th cart as "t b1 b2 ... bt" (without the quotes), where t is the number of items in the i-th cart, and the sequence b1, b2, ..., bt (1 ≤ bj ≤ n) gives the indices of items to put in this cart in the optimal distribution. All indices of items in all carts should be pairwise different, each item must belong to exactly one cart. You can print the items in carts and the carts themselves in any order. The items are numbered from 1 to n in the order in which they are specified in the input.
If there are multiple optimal distributions, you are allowed to print any of them.

Sample Input

3 2
2 1
3 2
3 1

4 3
4 1
1 2
2 2
3 2

Sample Output

5.5
2 1 2
1 3

8.0
1 1
2 4 2
1 3

解题思路:

题目大意:商场打折,如果购买的商品包含凳子,则购物车中最便宜的商品能打五折,如果有多件价格一样低,仅只一件能打折,同时K辆购物车必须均有物品,问最少花费。
一开始无脑的分情况,分到最后写得自己都乱了,才开始仔细回想这个购物过程。其实不管怎么买,一定要让凳子最优打折。比如,凳子的价格比铅笔的高,那么凳子肯定要自己一辆购物车,因为再进来一件商品,购物车中最低价就变小,能打折的优惠就少了,另外,凳子的价格比铅笔低,那么无论是自己放还是和铅笔一起放,折扣也在凳子这。最后需要分类的也变得简单,就是分凳子的总数比K大还是小就行了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const double INF = 0x3f3f3f3f;

struct Node
{
    double val;
    int id;
};

bool cmp(Node x,Node y)
{
	return x.val > y.val;
}

int main()
{
    int N,K;
    while (~scanf("%d%d",&N,&K))
    {
        double tmp,sum = 0,smin = INF,smax = 0,pmin = INF,pmax = 0,sum1 = 0,sum2 = 0;
        int opt;
        Node stool[1005],pencil[1005];
        memset(stool,0,sizeof(stool));
        memset(pencil,0,sizeof(pencil));
        int s = 0, p = 0;
        for (int i = 1; i <= N; i++)
        {
            scanf("%lf %d",&tmp,&opt);
            if (opt == 1)
            {
                stool[s].id = i;
                stool[s++].val = tmp;
                smin = min(smin,tmp);
                smax = max(smax,tmp);
                sum1 += tmp;
            }
            else
            {
                pencil[p].id = i;
                pencil[p++].val = tmp;
                pmax = max(pmax,tmp);
                pmin = min(pmin,tmp);
                sum2 += tmp;
            }
        }
        if (s >= K)
        {
            sort(stool,stool+s,cmp);
			for (int i = 0; i < K - 1; i++)
            {
                sum += stool[i].val;
            }
            sum *= 0.5;
            for (int i = K - 1; i < s; i++)
            {
                sum += stool[i].val;
            }
            sum += sum2;
            double minn  = min(pmin,smin);
            sum -= minn*0.5;
            printf("%.1lf\n",sum);
            for (int i = 0; i < K - 1; i++)
            {
                printf("1 %d\n",stool[i].id);
            }
            printf("%d",p+s-K+1);
            for (int i = K - 1; i < s; i++)
            {
                printf(" %d",stool[i].id);
            }
            for (int i = 0; i < p; i++)
            {
                printf(" %d",pencil[i].id);
            }
            printf("\n");
        }
        else
        {
            sum1 *= 0.5;
            sum += sum1 + sum2;
            printf("%.1lf\n",sum);
            for (int i = 0; i < s; i++)
            {
                printf("1 %d\n",stool[i].id);
            }
            for (int i = 0 ; i < K - s - 1; i++)
            {
                printf("1 %d\n",pencil[i].id);
            }
            printf("%d",p-(K-s-1));
            for (int i = K - s - 1; i < p; i++)
        	{
            	printf(" %d",pencil[i].id);
            }
            printf("\n");
        }
    }
    return 0;
}
posted @   zxzhang  阅读(297)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示

目录导航