C - Ordering Pizza

CodeForces - 867C

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 贪心 经典的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 【Codeforces Round #437 (Div. 2) C】 Ordering Pizza

    [链接]h在这里写链接 [题意]     给你参赛者的数量以及一个整数S表示每块披萨的片数.     每个参数者有3个参数,si,ai,bi;     表示第i个参赛者它要吃的披萨的片数,以及吃一片第 ...

  4. Codeforces Round #437 C. Ordering Pizza

    题意: n个人吃披萨,总共有两种披萨,每种披萨都是有S块,给出每个人要吃的块数,吃第一种披萨能获得的happy值,吃第二种披萨能获得的happy值,问你,在购买的披萨数最少的情况下能获得的最大的总的h ...

  5. CodeForces - 893D 贪心

    http://codeforces.com/problemset/problem/893/D 题意 Recenlty Luba有一张信用卡可用,一开始金额为0,每天早上可以去充任意数量的钱.到了晚上, ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Codeforces 570C 贪心

    题目:http://codeforces.com/contest/570/problem/C 题意:给你一个字符串,由‘.’和小写字母组成.把两个相邻的‘.’替换成一个‘.’,算一次变换.现在给你一些 ...

随机推荐

  1. 大数据篇:Spark

    大数据篇:Spark Spark是什么 Spark是一个快速(基于内存),通用,可扩展的计算引擎,采用Scala语言编写.2009年诞生于UC Berkeley(加州大学伯克利分校,CAL的AMP实验 ...

  2. nginx配置虚拟主机、反向代理和负载均衡

    为了实现这个功能,需要修改nginx的配置文件,将nginx.conf清理一下,使结构更清晰. worker_processes ; events { worker_connections ; } h ...

  3. 自己写的一个HTML的小网页

    上次在上直播课的时候,教员提到了html这种标记语言.自己就在W3school上面学了一点点关于html的一些皮毛,自己动手写了一个小网页,同时自己对CTF这一块比较感兴趣,但是自己还是一个干干净净的 ...

  4. Linux学习笔记(四)帮助命令

    帮助命令 man info help --help man 英文原意:format and display the on-line manual pages 功能:显示联机帮助手册 语法:man 选项 ...

  5. CSS 中的伪类和伪元素

    伪类(Pseudo classes) 由于状态的变化是非静态的,所以元素达到一个特定状态时,它可能得到一个伪类的样式:当状态改变时,它又会失去这个样式.由此可以看出,它的功能和 class 有些类似, ...

  6. 初探Redis-基础类型String

    Redis存在五种基础类型:字符串(String).队列(List).哈希(Hash).集合(Set).有序集合(Sorted Set).String的出镜率算是最高的.本次列举出String的常用操 ...

  7. JDBC中的时间处理

    MySQL中常用的时间类有: java.sql.Date, Time, Timestamp 用的比较多的是ava.sql.Date和TimeStamp: 先看表结构 CREATE TABLE `t_u ...

  8. Idea上tomcat部署细节

    ​ 一.On Update action: (1)Update resources:更新项目变更的.jsp,.xml文件等资源文件,而不会更新源码文件:(仅修改项目的JS文件.JSP文件.CSS文件推 ...

  9. 2019-2020-1 20199308《Linux内核原理与分析》第九周作业

    <Linux内核分析> 第八章 可执行程序工作原理进程的切换和系统的一般执行过程 8.1 知识点 进程调度的时机 ntel定义的中断类型主要有以下几种 硬中断(Interrupt) 软中断 ...

  10. 2019-2020-1 20199303《Linux内核原理与分析》第六周作业

    系统调用的三层机制 首先是为系统增加新的命令 运行脚本自动生成文件系统 其中有一个显示时间的功能 编辑test.c文件,增加一个hello函数用来显示学号,再次使用make roofts自动编译,调用 ...