CF161BDiscounts
CF161B
题目大意;要购买\(n\)件物品,有\(A\)\(B\)两种类型,要求分成\(k\)组,其中如果其中一组含有\(A\)类物品,那么这一组最便宜的一件物品就会半价
怎么分组最小化代价?
我们应该尽量优惠的幅度尽量大
对于一个\(A\)类物品,假设他的价格为\(w\),那么我们绝对不会选择价值比它更小的,因为这样会让我们优惠的代价变小
我们选择贵的又对优惠的价格没有影响,所以
我们能够选择一个比较优的分组方案
先按照价格排序
把最贵的前\(k\)个A(不足\(k\)个就全部)分成一组
然后如果不够组数,就把剩下的尽量补全\(k\)组,然后把每个\(A\)类塞到小于他的最大的\(A\)的那组,如果不存在这样的\(A\),就塞到最后一组
来保证优惠价格的最大化
#include<cstdio>
#include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cctype>
#include<vector>
#include<ctime>
#define LL long long
#define pii pair<int,int>
#define mk make_pair
#define fi first
#define se second
using namespace std;
const int N = 2e5 + 3;
inline int read(){
int v = 0,c = 1;char ch = getchar();
while(!isdigit(ch)){
if(ch == '-') c = -1;
ch = getchar();
}
while(isdigit(ch)){
v = v * 10 + ch - 48;
ch = getchar();
}
return v * c;
}
struct node{
int ti;
int ci;
int id;
}a[N];
bool book[N],used[N];
vector <int> G[N];
int n,k;
double ans;
inline bool cmp(node x,node y){
return x.ci > y.ci;
}
inline bool cmpp(node x,node y){
return x.id < y.id;
}
int main(){
int sum = 0,cnt;
n = read(),k = read();
for(int i = 1;i <= n;++i){
a[i].ci = read(),a[i].ti = read(),sum += (a[i].ti == 1);
a[i].id = i;
}
sort(a + 1,a + n + 1,cmp);
int need = max(0,k - sum),now = 1;
for(int i = 1;i <= n;++i){
if(a[i].ti == 1){
book[now] = 1;
if(now < k) G[now++].push_back(a[i].id);
else G[now].push_back(a[i].id);
}
else{
if(need && now < k) G[now++].push_back(a[i].id),need--;
else G[now].push_back(a[i].id);
}
}
sort(a + 1,a + n + 1,cmpp);
for(int i = 1;i <= now;++i){
for(int j = 0;j < (int)G[i].size();++j){
if(j == G[i].size() - 1 && book[i]) ans += (double)a[G[i][j]].ci * 0.5;
else ans += a[G[i][j]].ci;
}
}
printf("%.1lf\n",ans);
for(int i = 1;i <= now;++i){
printf("%d ",G[i].size());
for(int j = 0;j < (int)G[i].size();++j) printf("%d ",G[i][j]);
putchar('\n');
}
return 0;
}
CF161BDiscounts的更多相关文章
随机推荐
- Handler, AsyncTask用法简单示例
package com.jim.testapp; import android.app.Activity; import android.os.AsyncTask; import android.os ...
- Please upgrade the installed version of powershell to the minimum required version and run the command again.
版权声明:本文为博主原创文章,转载请注明出处.谢谢 https://blog.csdn.net/cow66/article/details/77993908 我的系统是windows 7 安装了vag ...
- Python学习之路2☞数据类型与变量
变量 变量作用:保存状态:说白了,程序运行的状态就是状态的变化,变量是用来保存状态的,变量值的不断变化就产生了运行程序的最终输出结果 一:声明变量 #!/usr/bin/env python # -* ...
- hdu2176 尼姆博弈
如果 a1^a2^a3........^an=0,必败态. 如果 a1^a2^a3........^an!=0,必胜态. 对于必胜态,若a1^a2^a3........^an=k,要让对方为必败态,所 ...
- python 异常处理与流程控制
- 通过反射拿到构造方法 Day25
package com.sxt.constructor; /* * 反射 * Class类拿到构造方法 */ import java.lang.reflect.Constructor; public ...
- oracle函数 TO_CHAR(x[[,c2],C3])
[功能]将日期或数据转换为char数据类型 [参数] x是一个date或number数据类型. c2为格式参数 c3为NLS设置参数 如果x为日期nlsparm=NLS_DATE_LANGUAGE 控 ...
- 图表echarts折线图,柱状图,饼状图
总体就是有折线图相关图标的设置,x,y轴的设置,x,y轴或者数据加上单位的设置.饼状图如何默认显示几个数据中的某个数据 折线图:legend(小标题)中间默认是圆圈 改变成直线 在legend设置的时 ...
- CSS引入的方式有哪些? link和@import的区别是?
CSS引入的方式包括内联 内嵌 外链 导入 link和@import的区别是 : ①link属于XHTML标签,除了加载CSS外,还能 用于定义RSS, 定义rel连接属性等作用:而@import是C ...
- Hammersley-Clifford定理证明
Proof of Hammersley-Clifford TheoremProof of Hammersley-Clifford Theorem依赖知识定义1定义2证明过程反向证明(吉布斯分布=> ...