E - 贪心

Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

  Have you ever heard the story of Blue.Mary, the great civil engineer? Unlike Mr. Wolowitz, Dr. Blue.Mary has accomplished many great projects, one of which is the Guanghua Building.
  The public opinion is that Guanghua Building is nothing more than one of hundreds of modern skyscrapers recently built in Shanghai, and sadly, they are all wrong. Blue.Mary the great civil engineer had try a completely new evolutionary building method in project of Guanghua Building. That is, to build all the floors at first, then stack them up forming a complete building.
  Believe it or not, he did it (in secret manner). Now you are face the same problem Blue.Mary once stuck in: Place floors in a good way.
  Each floor has its own weight w i and strength s i. When floors are stacked up, each floor has PDV(Potential Damage Value) equal to (Σw j)-s i, where (Σw j) stands for sum of weight of all floors above.
  Blue.Mary, the great civil engineer, would like to minimize PDV of the whole building, denoted as the largest PDV of all floors.
  Now, it’s up to you to calculate this value.
 

Input

  There’re several test cases.
  In each test case, in the first line is a single integer N (1 <= N <= 10 5) denoting the number of building’s floors. The following N lines specify the floors. Each of them contains two integers w i and s i (0 <= w i, s i <= 100000) separated by single spaces.
  Please process until EOF (End Of File).
 

Output

  For each test case, your program should output a single integer in a single line - the minimal PDV of the whole building.
  If no floor would be damaged in a optimal configuration (that is, minimal PDV is non-positive) you should output 0.
 

Sample Input

3
10 6
2 3
5 4
2
2 2
2 2
3
10 3
2 5
3 3

Sample Output

1 0 2
题目大意:现在给你N块板子,每一块木板都有着两个属性,一个是重量w,另一个是承受力s,同时每一块木板会有一个
pdv数值,计算方法为(Σw j)-s i,即所有在其上的木板的重量之和减去他的承受力,问你怎么样放置才能使pdv的最大值
(pdv最大的那块木板)最小。
思路分析:首先,题目要求的是pdv最大的那块木板的pdv值,并不是让所有木板的pdv之和最小,读题时一定要注意!
另外,很显然我们需要对这些木板进行排序,但是我们应该怎么排才能使最大的pdv值最小呢?换言之,我们以什么样的
标准来决定哪块木板在上,哪块木板在下。
对于相邻放置的两块板,设两块板为i,j他们上面的重量为sum
1) a=sum-si;b=sum+wi-sj;

交换两个板的位置

2)a'=sum+wj-si;b'=sum-sj;

如果1优于2,求解得有效的条件为wj-si>wi-sj

即wj+sj>wi+si

所以按si+wi的和排序贪心即可。

贪心好奇妙,感觉贪心虽然是人的天性,但是有的人会贪,有的人不会贪,

很多时候并没有什么道理可言,全靠个人直觉,但是得考虑得全面,我感觉

贪心策略的确定基本可以有两个想法,一个是全自己想,然后自己出数据,

不断的使自己的策略接近正确答案。二是可以先搞少的数据,两个或三个,

列公式确定贪心策略。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=100000+100;
struct nod
{
    int w;
    int s;
};
nod f[maxn];
bool cmp(nod a,nod b)
{
    return a.s+a.w<b.s+b.w;
}
int main()
{
   int n;
   while(scanf("%d",&n)!=EOF)
   {
       __int64 ma=0,sum,t;
       for(int i=0;i<n;i++)
       scanf("%d%d",&f[i].w,&f[i].s);
       sort(f,f+n,cmp);
       sum=f[0].w;
       for(int i=1;i<n;i++)
       {
           t=sum-f[i].s;
           if(t>ma) ma=t;
           sum+=f[i].w;
       }
       printf("%I64d\n",ma);
   }
    return 0;
}

 

hdu4296 贪心的更多相关文章

  1. HDU-4296 Buildings 贪心 从相邻元素的相对位置开始考虑

    题目链接:https://cn.vjudge.net/problem/HDU-4296 题意 有很多板子,每一个板子有重量(w)和承重(s)能力 现规定一块板子的PDV值为其上所有板子的重量和减去这个 ...

  2. 【Python3】【贪心】hdu4296 Buildings

    题意: n个板,每个板有重量和强度w和s,还有PDV值(上面的总重量-该板的强度) 对于某种叠放方式,PDV的最大值为其代表值 求该值的最小值   考虑只有两个板的情况:a和b,很显然下面的比上面的容 ...

  3. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  4. HDOJ 1051. Wooden Sticks 贪心 结构体排序

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  5. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]

    1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 786  Solved: 391[Submit][S ...

  7. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  8. 【BZOJ-4245】OR-XOR 按位贪心

    4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 486  Solved: 266[Submit][Sta ...

  9. code vs 1098 均分纸牌(贪心)

    1098 均分纸牌 2002年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解   题目描述 Description 有 N 堆纸牌 ...

随机推荐

  1. ImportError No module named memcache

    ImportError: No module named memcache 没有找到windows下的memcache,我们就用linux下的包来安装 先下载memcache linux下的安装包 f ...

  2. Mac下安装 php+nginx+mysql 开发环境

    一.mysql安装 mysql是安装最简单顺利的 1. 首先去官方网站下载Mac适用的MySQL的dmg包 下载页面 选择图中最下方的dmg包下载进行安装 安装完成后 MySQL的安装目录为/usr/ ...

  3. 校门外的树 - Grids2808

    校门外的树 问题描述: 某校大门外长度为 L 的马路上有一排树,每两棵相邻的树之间的间隔都是1 米.我们 可以把马路看成一个数轴,马路的一端在数轴0 的位置,另一端在L 的位置:数轴上的每 个整数点, ...

  4. php 字符编码转换函数 iconv mb_convert_encoding比较

    在使用PHP处理字符串时,我们经常会碰到字符编码转换的问题,你碰到过iconv转换失败吗? 发现问题时,网上搜了搜,才发现iconv原来有bug ,碰到一些生僻字就会无法转换,当然了配置第二个参数时, ...

  5. 容器 MAP

    1.equal_range pair <myMapDef::iterator,myMapDef::iterator> myresult; myPairDef ps=*MyMap1.begi ...

  6. C程序设计语言练习题1-2

    练习1-2 做个实验,当printf函数的参数字符串中包含\c(其中c是上面的转义字符串序列中未曾列出的某一个字符)时,观察一下会出现什么情况. 代码如下: #include <stdio.h& ...

  7. logstash 各种时间转换

    <pre name="code" class="html">日期格式转换: /***** nginx 访问日志 [elk@zjtest7-front ...

  8. 了解 Windows Azure 存储的可伸缩性、可用性、持久性和计费

    借助 Windows Azure存储,应用程序开发者及其应用程序和用户可以在云中使用可用性更高.持久性更长.可伸缩性更强的海量存储.开发者可以构建能随时随地高效访问数据的服务,在所需的时间段内存储任意 ...

  9. socket使用TCP协议时,send、recv函数解析以及TCP连接关闭的问题

    Tcp协议本身是可靠的,并不等于应用程序用tcp发送数据就一定是可靠的.不管是否阻塞,send发送的大小,并不代表对端recv到多少的数据. 在阻塞模式下, send函数的过程是将应用程序请求发送的数 ...

  10. c#发送邮件样例

    1.通过gmail邮箱发送邮件 try { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient(& ...