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 题意:给你一个字符串,由‘.’和小写字母组成.把两个相邻的‘.’替换成一个‘.’,算一次变换.现在给你一些 ...
随机推荐
- tf.nn.softmax_cross_entropy_with_logits 分类
tf.nn.softmax_cross_entropy_with_logits(logits, labels, name=None) 参数: logits:就是神经网络最后一层的输出,如果有batch ...
- 使用Jmeter测试java请求
1.性能测试过程中,有时候开发想对JAVA代码进行性能测试,Jmeter是支持对Java请求进行性能测试,但是需要自己开发.打包好要测试的代码,就能在Java请求中对该java方法进行性能测试2.本文 ...
- 美化你的终端利器Iterm2
Iterm2是特别好用的一款终端,支持自定义字体和高亮,让日常开发,充满愉悦. 安装iterm2(mac版) brew tap caskroom/cask brew cask install iter ...
- PHP函数:debug_backtrace
debug_backtrace() - 产生一条 PHP 的回溯跟踪(backtrace). 说明: debug_backtrace ([ int $options = DEBUG_BACKTRAC ...
- Android App安全渗透测试(一)
一. 实验环境搭建 1. 安装JDK 2. 安装Android Studio 3. 模拟器或真机 我的是夜神模拟器和nexus 工具 Apktool ...
- java 容器(collection)--ArrayList 常用方法分析 源码分析
ArrayList 介绍 打开jdk源码看看官方文档的介绍 粗糙的翻译下大致意思是: List接口的可调整大小的数组实现.实现了所有可选的列表操作,并允许所有元素,包括 null .除了实现List接 ...
- [Abp vNext 入坑分享] - 4.JWT授权的接入
一.感想 在写这一系列文章之前,本来以为写这个之前已经搭建好的框架描述会比较简单,但是慢慢写下来才发现.写这个真的不简单额,本来以为图文一起,一个晚上应该能输出一篇吧...结果:现实真的骨感,一个星期 ...
- php--static用法
static关键字声明一个属性或方法是和类相关的,而不是和类的某个特定的实例相关,因此,这类属性或方法也称为“类属性”或“类方法”. 如果访问控制权限允许,可不必创建该类对象而直接使用类名加两个冒号“ ...
- Windows VHD Create, Attach, 获得Disk序号
// create_vhd.cpp : Defines the entry point for the console application. // #include "stdafx.h& ...
- Vue-cli4脚手架搭建
一:要安装Node.js:安装路径要默认安装(node-v12.16.2-x64.msi-长支持 二:要安装cnpm 1)说明:npm(node package manager)是nodejs的包管理 ...