hdu 4882 ZCC Loves Codefires (贪心 推导)
做题的时候凑的规律,其实可以 用式子推一下的。
题意:n对数,每对数有e,k, 按照题目的要求(可以看下面的Hint就明白了)求最小的值。
分析:假设现在总的是sum, 有两个e1 k1 e2 k2
则先选e1 为 (sum+e1)*k1+(sum+e1+e2)*k2
先e2: (sum+e2)*k2 + (sum+e1+e2)*k1.
比较两个式子发现不同的部分分别是 e1*k2 e2*k1; 比较大小移向 e1/k1 e2/k2, 那个小,就选那个,能达到最小。
官方题解:
考察序列中相邻的两题i, j(i在前)。交换它们后,解出它们之前的题目所带来的时间对答案的贡献是不变的,它们对
它们后面的题目的贡献也是不变的,其他题目之间对答案的贡献自然也是不变的。唯一的变化就是,原来的EiKj一项变
成了EjKi一项。那么,为了使答案变优,需要满足的条件是EjKi≤EiKj。也即Ei/Ki≥Ej/Kj。
那么,最优解序列Ai一定满足,EAi/KAi是递增的。
排序一遍即可。
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
const int maxn = +;
int n;
struct node
{
int e, k;
double x;
}p[maxn]; bool cmp(node a, node b)
{
return a.x < b.x;
}
int main()
{
int i;
__int64 sum, ans;
while(~scanf("%d", &n))
{
for(i = ; i < n; i++)
{
scanf("%d", &p[i].e);
}
for(i = ; i < n; i++)
{
scanf("%d", &p[i].k);
p[i].x = (double)(p[i].e*1.0/p[i].k*1.0);
}
sort(p, p+n, cmp);
sum = ; ans = ;
for(i = ; i < n; i++)
{
sum += p[i].e;
ans += sum*p[i].k;
}
cout<<ans<<endl;
}
return ;
}
hdu 4882 ZCC Loves Codefires (贪心 推导)的更多相关文章
- HDU 4882 ZCC Loves Codefires(贪心)
ZCC Loves Codefires Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- hdu 4882 ZCC Loves Codefires(数学题+贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4882 ------------------------------------------------ ...
- HDU 4882 ZCC Loves Codefires (贪心)
ZCC Loves Codefires 题目链接: http://acm.hust.edu.cn/vjudge/contest/121349#problem/B Description Though ...
- 2014多校第二场1011 || HDU 4882 ZCC Loves Codefires (贪心)
题目链接 题意 : 给出n个问题,每个问题有两个参数,一个ei(所要耗费的时间),一个ki(能得到的score).每道问题需要耗费:(当前耗费的时间)*ki,问怎样组合问题的处理顺序可以使得耗费达到最 ...
- hdu 4882 ZCC Loves Codefires(贪心)
# include<stdio.h> # include <algorithm> # include <string.h> using namespace std; ...
- HDU-4882 ZCC Loves Codefires
http://acm.hdu.edu.cn/showproblem.php?pid=4882 ZCC Loves Codefires Time Limit: 2000/1000 MS (Java/Ot ...
- 2014---多校训练2(ZCC Loves Codefires)
ZCC Loves Codefires Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 4876 ZCC loves cards(暴力剪枝)
HDU 4876 ZCC loves cards 题目链接 题意:给定一些卡片,每一个卡片上有数字,如今选k个卡片,绕成一个环,每次能够再这个环上连续选1 - k张卡片,得到他们的异或和的数,给定一个 ...
- hdu 4873 ZCC Loves Intersection(大数+概率)
pid=4873" target="_blank" style="">题目链接:hdu 4873 ZCC Loves Intersection ...
随机推荐
- google api , the problem of null refresh token
http://stackoverflow.com/questions/10827920/google-oauth-refresh-token-is-not-being-received The ref ...
- Android ADB 端口占用问题解决方案
问题描述: The connection to adb is down, and a severe error has occured. You must restart adb and Eclips ...
- jquery插件dataTables添加序号列
官网方法实例: $(document).ready(function() { var t = $('#example').DataTable({ "columnDef ...
- 1934: [Shoi2007]Vote 善意的投票 - BZOJ
Description幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可以 ...
- FZU 2016 summer train I. Approximating a Constant Range 单调队列
题目链接: 题目 I. Approximating a Constant Range time limit per test:2 seconds memory limit per test:256 m ...
- Problem 1016 咒文卷轴 优先队列+前缀和+rmq
题目链接: 题目 Problem 1016 咒文卷轴 Time Limit: 3000 mSec Memory Limit : 131072 KB 问题描述 小Y 是一个魔法师,有一天他获得了一卷神秘 ...
- JQuery,UIbootstrap风格弹出层
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <met ...
- 2013 Asia Regional Changchun
Hard Code http://acm.hdu.edu.cn/showproblem.php?pid=4813 #include<cstdio> ]; int main(){ int t ...
- [设计模式] 8 组合模式 Composite
DP书上给出的定义:将对象组合成树形结构以表示“部分-整体”的层次结构.组合使得用户对单个对象和组合对象的使用具有一致性.注意两个字“树形”.这种树形结构在现实生活中随处可见,比如一个集团公司,它有一 ...
- 【Unity3D】iOS 推送实现
原地址:http://www.iappfan.com/%E3%80%90unity3d%E3%80%91ios-%E6%8E%A8%E9%80%81%E5%AE%9E%E7%8E%B0/ #impor ...