题目链接: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. Pipeline inbound(netty源码7)

    netty源码死磕7  Pipeline 入站流程详解 1. Pipeline的入站流程 在讲解入站处理流程前,先脑补和铺垫一下两个知识点: (1)如何向Pipeline添加一个Handler节点 ( ...

  2. xorm

    https://github.com/go-xorm/xorm Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3, ...

  3. Geoffrey E. Hinton

    https://www.cs.toronto.edu/~hinton/ Geoffrey E. Hinton I am an Engineering Fellow at Google where I ...

  4. Nothing but the key 属性全部依赖于主键 third norm form

    全依赖 Designs that Violate 1NF CustomerCustomer ID First Name Surname Telephone Number123 Pooja Singh ...

  5. ElasticSearch(十三) bulk api奇特的json格式的原因

    bulk api的语法 正常的语法: {"action": {"meta"}}\n {"data"}\n {"action&quo ...

  6. github 工具命令集

  7. Machine Learning in Action(2) 决策树算法

    决策树也是有监督机器学习方法. 电影<无耻混蛋>里有一幕游戏,在德军小酒馆里有几个人在玩20问题游戏,游戏规则是一个设迷者在纸牌中抽出一个目标(可以是人,也可以是物),而猜谜者可以提问题, ...

  8. [shell] Bash编程总结

    由于工作需要,之前的几个月写了一些Bash脚本,主要完成自动测试.打包.安装包等.虽然相比C++编程,要简单.傻瓜,但其在类Unix系统中可以大大提高工作的效率.所以在此对脚本编程过程中一些注意事项进 ...

  9. 2016 Al-Baath University Training Camp Contest-1 I. March Rain —— 二分

    题目链接:http://codeforces.com/problemset/gymProblem/101028/I I. March Rain time limit per test 2 second ...

  10. 从Github上下载了项目,导入Android Studio,gradle 报错,应该怎么修改

    一.从Github上获取源代码 我这里是直接下载ZIP文件 二.在本机的Android Studio上新建一个空白项目,目的主要是与刚从Github上下载的项目文件结构做对比 三.替换gradle文件 ...