题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1148

很常见的排序贪心题。。。。。。
假设我们得到了一个最优序列,记s[n]=w[1]+w[2]+...+w[n]
对于第n个和第n+1个,剩余容量为:
min(c[n]-s[n-1],c[n+1]-s[n-1]-w[n])
如果交换第n个和第n+1个,剩余容量为:
min(c[n+1]-s[n-1],c[n]-s[n-1]-w[n+1])
因为我们得到的是一个最优序列,所以不交换比交换优,所以:
min(c[n]-s[n-1],c[n+1]-s[n-1]-w[n])  > min(c[n+1]-s[n-1],c[n]-s[n-1]-w[n+1])
容易知道c[n]-s[n-1]>c[n]-s[n-1]-w[n+1],c[n+1]-s[n-1]-w[n]<c[n+1]-s[n-1]
所以就是:
c[n+1]-s[n-1]-w[n]>c[n]-s[n-1]-w[n+1]
c[n]+w[n]<c[n+1]+w[n+1]
所以就是按ci+wi从小到大排序。
我们先按ci+wi从小到大排序,然后一个一个取,如果不能去,就在已取的中找质量最大的,看替换或会不会更优,这个可以用优先队列。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
//#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj using namespace std; typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef complex<DB> CP; #define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define re(i,a,b) for(i=a;i<=b;i++)
#define red(i,a,b) for(i=a;i>=b;i--)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define SF scanf
#define PF printf
#define two(k) (1<<(k)) template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;} const DB EPS=1e-;
inline int sgn(DB x){if(abs(x)<EPS)return ;return(x>)?:-;}
const DB Pi=acos(-1.0); inline int gint()
{
int res=;bool neg=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){neg=;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return (neg)?-res:res;
}
inline LL gll()
{
LL res=;bool neg=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){neg=;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return (neg)?-res:res;
} const int maxN=; int N;
struct Tdata{LL C,W;}data[maxN+]; inline bool cmp(Tdata a,Tdata b){return a.C+a.W<b.C+b.W;} struct cmp2{inline bool operator()(Tdata a,Tdata b){return a.W<b.W;}};
priority_queue<Tdata,vector<Tdata>,cmp2> Q;
LL ans; int main()
{
freopen("bzoj1148.in","r",stdin);
freopen("bzoj1148.out","w",stdout);
int i;
N=gint();
re(i,,N)data[i].C=LL(gint()),data[i].W=LL(gint());
sort(data+,data+N+,cmp);
re(i,,N)
if(ans<=data[i].C)
{
ans+=data[i].W;
Q.push(data[i]);
}
else
if(data[i].W<Q.top().W && ans-Q.top().W<=data[i].C)
{
ans-=Q.top().W;
Q.pop();
ans+=data[i].W;
Q.push(data[i]);
}
cout<<Q.size()<<endl;
cout<<ans<<endl;
return ;
}

bzoj1148的更多相关文章

随机推荐

  1. 关于Python中的for循环控制语句

    #第一个:求 50 - 100 之间的质数 import mathfor i in range(50, 100 + 1):    for j in range(2, int(math.sqrt(i)) ...

  2. lua中的坑

    在工作中使用lua也有一年了,代码也写了不少,踩过不少坑,这里记录一下. table.sort table.sort是lua自带的排序函数,数据量小时,也还是不错的.不过要注意你传入的compare函 ...

  3. BeanFactory调用getbean()对象

    Spring通过资源加载器加载相应的XML文件,使用读取器读取资源加载器中的文件到读取器中,在读取过程中,解析相应的xml文件元素,转化为spring定义的数据结BeanDefinition,把相应的 ...

  4. mongodb授权登录

    mongodb版本为3.2(目前最新),演示的是linux下的mongodb授权认证 第一次登录不启动授权(mongo默认不启动) ./mongod --dbpath=/home/db/data -- ...

  5. (原)Mac下Apache添加限制IP线程模块:mod_limitipconn.so

    1.下载:http://dominia.org/djao/limit/mod_limitipconn-0.24.tar.bz2 解压到一个目录:我的是~/Download/mod_limitipcon ...

  6. 我对Laravel ThinkPHP Yii symfony2 CI cakephp 的看法

    这是我的真心体会,在尝试使用Laravel.ThinkPHP.Yii.symfony2.CI.cakephp.Yii2 之后的真实想法(default7#zbphp.com). 1)ThinkPHP ...

  7. rhel Linux系统yum的配置

    yum是一个很方便的linux系统软件管理工具,但是很多新手还是不会配置yum,下面详细的介绍下yum的配置方法,其实很简单. 1.首先确保系统ISO镜像已经成功挂载,可以用df -h命令查看.2.创 ...

  8. java对象Integer不能引用传递

    java对象Integer不能引用传递 /** * The value of the <code>Integer</code>. * * @serial */ private ...

  9. HttpWebRequest多线程抓取17Track的物流信息

    公司的一个系统需要去抓17Track的物流信息,贴上代码有需要的朋友可以参考一下↓ //17Track的抓取地址以及开启的线程数量 <add key="url" value= ...

  10. (转) C# Activator.CreateInstance()方法使用

    C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...