C - Ordering Pizza CodeForces - 867C 贪心 经典
C - Ordering Pizza
C - Ordering Pizza
这个是最难的,一个贪心,很经典,但是我不会,早训结束看了题解才知道怎么贪心的。
这个是先假设每个人都可以吃到他喜欢的,就是先求出答案,然后按照b-a 排序,分别放入两个优先队列里面,
如果b>a 那就吃第二块,否则就吃第一块,求出num,注意优先队列按照从小到达排序。
num1记录第一块吃的数量,num2记录第二块吃的数量。
num1%=s,num2%=s 如果num1+num2的数量大于等于了s 则说明可以满足。
如果小于s了,说明他们只能选一块吃,这个时候在求出答案后贪心,贪心取最少的代价使得只吃一块。
两边都利用优先队列进行贪心。
很好的一个题目。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <iostream>
#include <string>
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
const int maxn = 1e5 + ;
typedef long long ll;
struct node {
int num, happy;
node(int num = , int happy = ) :num(num), happy(happy) {}
bool operator< (const node &a)const
{
return a.happy < happy;
}
};
priority_queue<node>v1, v2;
int a[maxn], b[maxn];
int main()
{
ll n, s;
scanf("%lld%lld", &n, &s);
ll num1 = , num2 = , ans = ;
for(int i=;i<=n;i++)
{
int num;
scanf("%d%d%d", &num, &a[i], &b[i]);
if (a[i] > b[i]) num1 += num, v1.push(node(num, a[i]-b[i]));
else num2 += num, v2.push(node(num, b[i]-a[i]));
ans += num * 1ll * max(a[i], b[i]);
}
num1 %= s, num2 %= s;
if (num1 + num2 > s) printf("%lld\n", ans);
else
{
// printf("num1=%lld num2=%lld ans=%lld\n", num1, num2, ans);
ll res1 = , cost1 = , res2 = , cost2 = ;
while(!v1.empty())
{
node u = v1.top(); v1.pop();
int num = u.num;
if(res1+num>=num1)
{
cost1 += (num1 - res1)*u.happy;
break;
}
res1 += num;
cost1 += num * u.happy;
}
while(!v2.empty())
{
node u = v2.top(); v2.pop();
int num = u.num;
if(res2+num>=num2)
{
cost2 += (num2 - res2)*u.happy;
break;
}
res2 += num;
cost2 += num * u.happy;
// printf("cost2=")
}
// printf("cost2=%lld\n", cost2);
ans = max(ans - cost1, ans - cost2);
printf("%lld\n", ans);
}
return ;
}
贪心
C - Ordering Pizza CodeForces - 867C 贪心 经典的更多相关文章
- Codeforce 867 C. Ordering Pizza (思维题)
C. Ordering Pizza It's another Start[c]up finals, and that means there is pizza to order for the ons ...
- cf 865 B. Ordering Pizza
B. Ordering Pizza It's another Start[c]up finals, and that means there is pizza to order for the ons ...
- 【Codeforces Round #437 (Div. 2) C】 Ordering Pizza
[链接]h在这里写链接 [题意] 给你参赛者的数量以及一个整数S表示每块披萨的片数. 每个参数者有3个参数,si,ai,bi; 表示第i个参赛者它要吃的披萨的片数,以及吃一片第 ...
- Codeforces Round #437 C. Ordering Pizza
题意: n个人吃披萨,总共有两种披萨,每种披萨都是有S块,给出每个人要吃的块数,吃第一种披萨能获得的happy值,吃第二种披萨能获得的happy值,问你,在购买的披萨数最少的情况下能获得的最大的总的h ...
- CodeForces - 893D 贪心
http://codeforces.com/problemset/problem/893/D 题意 Recenlty Luba有一张信用卡可用,一开始金额为0,每天早上可以去充任意数量的钱.到了晚上, ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 828D) - 贪心
Arkady needs your help again! This time he decided to build his own high-speed Internet exchange poi ...
- CodeForces - 93B(贪心+vector<pair<int,double> >+double 的精度操作
题目链接:http://codeforces.com/problemset/problem/93/B B. End of Exams time limit per test 1 second memo ...
- Codeforces 570C 贪心
题目:http://codeforces.com/contest/570/problem/C 题意:给你一个字符串,由‘.’和小写字母组成.把两个相邻的‘.’替换成一个‘.’,算一次变换.现在给你一些 ...
随机推荐
- python 字节码死磕
前言: 如果你跟我一样,对python的字节码感兴趣,想了解python的代码在内存中到底是怎么去运行的,那么你可以继续往下看,如果你是python新手,我建议你移步它处,本文适合有点基础的pyth ...
- web网页html基础
列表标签 ul:定义一个列表 li:定义列表中的条目 图片标签 Img:用来向网页中插入一张图片 Src:指定图片所在的路径 Width:指定图片宽度 (% px) Height:指定图片高度 (% ...
- myvue 模拟vue核心原理
// js部分index.js class Myvue{ constructor(options){ this.data = options.data; this.dep = new Dep(); v ...
- 如何使用python,才能像人民日报的“点亮”武汉景点
如何使用python,才能像人民日报的“点亮”武汉景点 前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:Allen P ...
- Spark SQL源码剖析(一)SQL解析框架Catalyst流程概述
Spark SQL模块,主要就是处理跟SQL解析相关的一些内容,说得更通俗点就是怎么把一个SQL语句解析成Dataframe或者说RDD的任务.以Spark 2.4.3为例,Spark SQL这个大模 ...
- 2016NOIP普及组T2回文日期
回文日期 分类:枚举,函数 [题目描述] 日常生活中,通过年.月.日这三个要素可以表示出一个唯一确定的日期. 牛牛习惯用8位数字表示一个日期,其中,前4位代表年份,接下来2位代表月份,最后2位代表日期 ...
- 简单了解下CAP定理与BASE定理
分布式环境下的各种问题 通信异常 网络不可用风险高,消息丢失.消息延迟非常普遍 网络分区(脑裂) 网络发生异常情况,延迟增加,导致所有组成分布式系统的节点中,只有部分节点之间能够正常通信,而另一些 ...
- leetcode-0101 对称二叉树
题目地址 https://leetcode-cn.com/problems/symmetric-tree/ 1.递归 本题最简单的思路是递归,可以假设两棵一模一样的树在进行镜像对比.他们之间的关系满足 ...
- 微信小程序标签常见知识点归纳整理
1. <image src='/images/logo.png' mode='widthFix'></image> mode 属性表示图片随着指定的宽度自动拉伸以显示原图的正确 ...
- IIS WebDAV安全配置
本文为转载,原文地址:http://www.2cto.com/article/201307/228165.html IIS WebDAV安全配置 2013-07-16 12:13:00 作者:瞌睡龙收 ...