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. golang 在 Mac , Linux , Windows 下交叉编译详解

    一. 前言 Golang 支持交叉编译, 在一个平台上生成然后再另外一个平台去执行. 而且编译的工具[build]这个工具是Golang 内置的,不需要你去下载第三方的包啥的,贼方便. 二. 交叉编译 ...

  2. 2020年iOS进阶面试题总结(一)

    准备找工作的你,可以看看,复习复习!! 1.说一下OC的反射机制 在动态运行下我们可以构建任何一个类,然后我们通过这个类知道这个类的所有的属性和方法,并且如果我们创建一个对象,我们也可以通过对象找到这 ...

  3. AJ学IOS(11)UI之图片自动轮播

    AJ分享,必须精品 先看效果 代码 #import "NYViewController.h" #define kImageCount 5 @interface NYViewCont ...

  4. 分布式 and 集群

    集群是个物理形态,强调个体和群体之间的联系: 同一个业务部署在多个服务器上,形成的逻辑上的整体. 分布式是个工作方式.强调请求和处理直接的分发状况: 一个业务分拆多个子业务,部署在不同的服务器上,通过 ...

  5. CSS也能计算:calc

    举个例子ul li适配屏幕,如果加个border:1px,不用border-content得情况下,每个li多加了2px: <ul><li></li><li& ...

  6. JAVA—线程(Thread)

    1.线程的状态有哪些 我记得在操作系统原理的书上有一张具体的图,暂时找不到书... new:新建状态,被创建出来后未启动时的线程状态. runnable:就绪状态,表示可以运行. blocked:阻塞 ...

  7. L24 word2vec

    词嵌入基础 我们在"循环神经网络的从零开始实现"一节中使用 one-hot 向量表示单词,虽然它们构造起来很容易,但通常并不是一个好选择.一个主要的原因是,one-hot 词向量无 ...

  8. 不使用tomcat,仅适用javaSE手写服务器--模拟登陆

    1.搭建框架 我们只是简单模拟,框架简单分三个模块 a,服务器端server包 b,servlet,根据不同的请求url,利用反射生产对应的servlet c,IO工具包,用来关闭IO流 d,编写we ...

  9. 2020年必须掌握的硬核技能k8s

    Kubernetes 是一个软件系统,使你在数以万计的电脑节点上运行软件时就像 所有节点是以单个大节点一样, 它将底层基础设施抽象,这样做同时简化了应用开发.部署,以及对开发和运维团队的管理. Kub ...

  10. ES6中不得不说的关键字const

    上一节讲了let关键字,它是用来声明一个变量,只在块级作用域起作用.这一节我们来学习ES6新增的另一个关键字const. const 的作用 const是constant(常量)的缩写,const和 ...