题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1475

题意:中文题诶~

思路:看懂题意后,首先想到的是贪心:

按照人数非递增排序,对于当前城市尽量将其排在离首都远的地方,这样得到的人数显然是最大的;

对于最右边可以放两个城市,我一开始是将其余城市都安排好,再从剩下的城市中选择一个人数尽量多的。然而wa了;

举一个反例:

  5 5

  4 1

  3 2

  2 3

  1 4

  1 4

按照这个思路得到的答案是13,但显然正确答案是14;

显然右边多出的一个城市不能最后再选,因为本来多出的那个城市可能会在前面贪心时就被选走了;

解决的方法很简单,既然放后面不行,那先确定多出的那个城市就好了,所以可以先枚举多出那个城市,再按照前面的思路贪心剩下的城市就好了。。。

代码:

 #include <iostream>
#include <algorithm>
#include <map>
#include <string.h>
using namespace std; const int MAXN=1e5+;
map<int, bool> vis;
struct node{
int hi, pi;
}gg[MAXN]; bool cmp(node a, node b){
return a.pi==b.pi?a.hi<b.hi:a.pi>b.pi;
} int main(void){
int n, k, num=;
cin >> n >> k;
for(int i=; i<n; i++){
cin >> gg[i].hi >> gg[i].pi;
}
sort(gg, gg+n, cmp);
for(int j=; j<n; j++){
if(gg[j].hi>=k) continue;
vis.clear();
int ans=gg[j].pi, cnt=;
for(int i=; i<n; i++){
if(i==j||gg[i].hi<gg[j].hi||cnt>=k-gg[j].hi) continue;
int x=k-gg[i].hi;
while(vis[x]&&x>=){
x--;
}
if(x>){
vis[x]=true;
ans+=gg[i].pi;
cnt++;
}
}
num=max(num, ans);
}
cout << num << endl;
return ;
}

51nod1475(贪心&枚举)的更多相关文章

  1. POJ 1018 Communication System 贪心+枚举

    看题传送门:http://poj.org/problem?id=1018 题目大意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m个厂家提供生产,而每个厂家生产的同种设备都 ...

  2. 【搜索】POJ-2718 贪心+枚举

    一.题目 Description Given a number of distinct decimal digits, you can form one integer by choosing a n ...

  3. [APIO2015]巴厘岛的雕塑 --- 贪心 + 枚举

    [APIO2015]巴厘岛的雕塑  题目描述 印尼巴厘岛的公路上有许多的雕塑,我们来关注它的一条主干道. 在这条主干道上一共有\(N\)座雕塑,为方便起见,我们把这些雕塑从 1 到\(N\)连续地进行 ...

  4. hdu 4091 Zombie’s Treasure Chest 贪心+枚举

    转自:http://blog.csdn.net/a601025382s/article/details/12308193 题意: 输入背包体积n,绿宝石体积s1,价值v1,蓝宝石体积s2,价值v2,宝 ...

  5. HDU 5303 Delicious Apples (贪心 枚举 好题)

    Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

  6. 贪心+枚举/哈希表 HDOJ Trouble

    题目传送门 题意:5个集合,每个集合最多200个数字,问是否每个集合挑一个数加起来和为0. 分析:显然n^5的程序果断超时,甚至n^3logn的二分也过不了.想n^3的方法,既然判断有没有,那么可以将 ...

  7. Delicious Apples (hdu 5303 贪心+枚举)

    Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

  8. HDU 5303 Delicious Apples (2015多校第二场 贪心 + 枚举)

    Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

  9. Codeforces C. Elections(贪心枚举三分)

    题目描述: C. Elections time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. Use Apache HBase™ when you need random, realtime read/write access to your Big Data.

    Apache HBase™ is the Hadoop database, a distributed, scalable, big data store. Use Apache HBase™ whe ...

  2. 简单老式Java对象 横切关注点 最小侵入性编程 声明式编程 避免强迫类继承和接口实现

    Spring In Action data injection aspect-oriented programming Plain Old Java Object 依赖注入能让相互协作的软件组件保持松 ...

  3. Local Response Normalization 60 million parameters and 500,000 neurons

    CNN是工具,在图像识别中是发现图像中待识别对象的特征的工具,是剔除对识别结果无用信息的工具. ImageNet Classification with Deep Convolutional Neur ...

  4. mount available

    Mount (computing), the process of making a file system accessible mount (Unix), the utility in Unix- ...

  5. The JSP specification requires that an attribute name is preceded by whitespace--异常

    异常信息:org.apache.jasper.JasperException: /pages/selectedCourse.jsp (line: 4, column: 39) The JSP spec ...

  6. ubuntu 查看网卡 数据包处理 速度

    ubuntu 查看网卡 数据包处理 速度 sar -l 1 10 首先要安装sar .使用 apt-get install atsar sar 命令中的 "-l"參数是 net-i ...

  7. px dp 互转

    /** * * px dip 转换 */ public class DensityUtil { /** * 根据手机分辨率 dip 转 px */ static public int dip2px(C ...

  8. Linux-正则表达式学习(精)

    正则表达式30分钟入门教程 来园子之前写的一篇正则表达式教程,部分翻译自codeproject的The 30 Minute Regex Tutorial. 由于评论里有过长的URL,所以本页排版比较混 ...

  9. Alsa中PCM参数设置⭐⭐

    1) PCM设备的句柄.2) 指定同时可供回放或截获的PCM流的方向3) 提供一些关于我们想要使用的设置选项的信息,比如缓冲区大小,采样率,PCM数据格式等4) 检查硬件是否支持设置选项.   4.1 ...

  10. ffmpeg视频格式转换中关键帧的设置

    在用ffmpeg转换视频到flv过程中,需要设置关键帧的间隔,以便在播放过程中实现精确定位.在网上查找了不少,最后发现这个指令有效: -g 1 -keyint_min 2 . http://blog. ...