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 题意:给你一个字符串,由‘.’和小写字母组成.把两个相邻的‘.’替换成一个‘.’,算一次变换.现在给你一些 ...
随机推荐
- AJ学IOS(55)多线程网络之图片下载框架之SDWebImage
AJ分享,必须精品 效果: 代码: - (NSArray *)apps { if (!_apps) { NSArray *dictArray = [NSArray arrayWithContentsO ...
- C#多线程(6):线程通知
目录 AutoRestEvent 类 常用方法 一个简单的示例 解释一下 复杂一点的示例 解释 回顾一下,前面 lock.Monitor 部分我们学习了线程锁,Mutex 部分学习了进程同步,Sema ...
- 前端学习笔记 --ES6新特性
前言 这篇博客是我在b站进行学习es6课程时的笔记总结与补充. 此处贴出up主的教程视频地址:深入解读ES6系列(全18讲) 1.ES6学习之路 1.1 ES6新特性 1. 变量 2. 函数 3. 数 ...
- Reward 杭电 2647
Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he ...
- redis: 持久化(十二)
RDB配置 RDB 是 Redis 默认的持久化方案.在指定的时间间隔内,执行指定次数的写操作,则会将内存中的数据写入到磁盘中.即在指定目录下生成一个dump.rdb文件.Redis 重启会通过加载d ...
- vue+element-ui中引入阿里播放器
1.在public文件下的index.html文件中插入以下代码: <link rel="stylesheet" href="https://g.alicdn.co ...
- HTTPS之密钥知识与密钥工具Keytool和Keystore-Explorer
1 简介 之前文章<Springboot整合https原来这么简单>讲解过一些基础的密码学知识和Springboot整合HTTPS.本文将更深入讲解密钥知识和密钥工具. 2 密钥知识-非对 ...
- css中的宽和高
width width表示宽 height height表示高 max-width.min-width max-width表示最大宽度 min-width表示最小宽度 max-height.min-h ...
- Windows VHD Create, Attach, 获得Disk序号
// create_vhd.cpp : Defines the entry point for the console application. // #include "stdafx.h& ...
- 动画图解Git命令
Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理,是目前使用范围最广的版本管理工具 尽管Git是一个非常强大的工具,但我认为大多数人都会同意我的说法,即它也可以 ...