题目链接: 传送门

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;
}

CF 161B Discounts(贪心)的更多相关文章

  1. Codeforces 731B Coupons and Discounts(贪心)

    题目链接 Coupons and Discounts 逐步贪心即可. 若当前位为奇数则当前位的下一位减一,否则不动. #include <bits/stdc++.h> using name ...

  2. CF 949D Curfew——贪心(思路!!!)

    题目:http://codeforces.com/contest/949/problem/D 有二分答案的思路. 如果二分了一个答案,首先可知越靠中间的应该大约越容易满足,因为方便把别的房间的人聚集过 ...

  3. Codeforces 161 B. Discounts (贪心)

    题目链接:http://codeforces.com/contest/161/problem/B 题意: 有n个商品和k辆购物车,给出每个商品的价钱c和类别t(1表示凳子,2表示铅笔),如果一辆购物车 ...

  4. CF Covered Path (贪心)

    Covered Path time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  5. CF 389 E 贪心(第一次遇到这么水的E)

    http://codeforces.com/contest/389/problem/E 这道题目刚开始想的特别麻烦...但是没想到竟然是贪心 我们只需要知道偶数的时候可以对称取的,然后奇数的时候没次取 ...

  6. CF 463A && 463B 贪心 && 463C 霍夫曼树 && 463D 树形dp && 463E 线段树

    http://codeforces.com/contest/462 A:Appleman and Easy Task 要求是否全部的字符都挨着偶数个'o' #include <cstdio> ...

  7. cf 之lis+贪心+思维+并查集

    https://codeforces.com/contest/1257/problem/E 题意:有三个集合集合里面的数字可以随意变换位置,不同集合的数字,如从第一个A集合取一个数字到B集合那操作数+ ...

  8. Codeforces Round #160 (Div. 2)

    A. Roma and Lucky Numbers 暴力计算. B. Roma and Changing Signs 每次取最小值改变正负,优先队列维护. C. Maxim and Discounts ...

  9. 【清真dp】cf1144G. Two Merged Sequences

    成就:赛后在cf使用错误的贪心通过一题 成就:在cf上赛后提交hack数据 成就:在cf上赛后hack自己 题目大意 有一长度$n \le 2\times 10^5$的序列,要求判断是否能够划分为一个 ...

随机推荐

  1. 轻量级iOS蓝牙库:LGBluetooth(项目中用过的)

    LGBluetooth 是个简单的,基于块的,轻量级 CoreBluetooth 库: iOS 6引入了Core Bluetooth,让低功耗蓝牙设备之间的通信变得简单.但如果CoreBluetoot ...

  2. swift-sharesdk集成微信、Facebook第三方登录

    好久没有写博客了.最近忙得没有时间更新博客,很忙很忙. 今天就把自己做过的第三方集成和大家分享一下,请大家多多指教. 第一步: 一.获取AppKey(去官方平台注册) 二.下载SDK 三.快速集成 第 ...

  3. 分享:计算机图形学期末作业!!利用WebGL的第三方库three.js写一个简单的网页版“我的世界小游戏”

    这几天一直在忙着期末考试,所以一直没有更新我的博客,今天刚把我的期末作业完成了,心情澎湃,所以晚上不管怎么样,我也要写一篇博客纪念一下我上课都没有听,还是通过强大的度娘完成了我的作业的经历.(当然作业 ...

  4. 对react的几点质疑

    现在react.js如火如荼,非常火爆,昨天抽了一天来看了下这项技术.可能就看了一天,研究的不深入,但是我在看的过程中发现来了很多疑惑,这里拿出来和那家分享讨论以此共勉. 在我接触的前端以后,让我感觉 ...

  5. [Codevs1403]新三国争霸(MST+DP)

    题目:http://codevs.cn/problem/1403/ 分析: 很容易想到对于某个确定的一天,就是求个最小生成树,又因为数据范围很小,所以可以暴力.但问题的关键是如果相邻两天的方案不同,就 ...

  6. js表单提交,面向对象

    一.js表单验证之后再提交 1.普通按钮onclick函数调用表单的submit()函数 <input type=button name="submit1" value=&q ...

  7. windows设置开机启动项

    一.windows下设置开机启动有如下方法 1 注册表启动项目RUN 2 计划任务,在"windows管理">"计划任务管理器"中新建任务,在操作栏指定要 ...

  8. 为什么我们要给父级元素写overflow:hidden

    有这样的一种情况,有的时候,我们的父级元素设置了高度,一般来说,父级元素的高度是根据子元素的高度来自适应撑开的,如果我们的父级元素也设置了高度,那么其高度就不会随着子元素的的大小而自适应,也许有的时候 ...

  9. Maven的内置变量

    Maven内置变量说明: ${basedir} 项目根目录(即pom.xml文件所在目录) ${project.build.directory} 构建目录,缺省为target目录 ${project. ...

  10. ActiveMQ_日志信息(五)

    activemq的日志信息主要配置两个文件 1.conf/log4j.properties   2.conf/logging.properties   来自为知笔记(Wiz)