codeforces 416C C. Booking System(贪心)
2015-09-18 14:53
423 查看
题目链接:
codeforces 416C题目大意:
给出n个请求,每个请求包括客人数量和支付金额,再给出m个桌子,包括桌子大小,问如何安排才能最大盈利。给出最大盈利和一个能够最大盈利的方案。题目分析:
采取如下的贪心策略,首先将请求按照支付金额从大到小排序,如果支付金额相同,按照人数从小到大排序,然后我们选取桌子中符合要求最小的桌子,这样就能得到最优方案。那么对于我们选取的这张桌子,是我们选取当前请求的最小的桌子,所以当前请求之后的请求不会出现比当前金额大的请求能够用这张桌子,所以这样选择一定能够得到最优方案。
AC代码:
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <set> #include <vector> #define MAX 1007 using namespace std; struct Node { int c,p,id; bool operator < ( const Node & a ) const { if ( p == a.p ) return c < a.c; return p > a.p; } }p[MAX]; struct PP { int a,b; PP ( int x , int y ) { a = x; b = y; } bool operator < ( const PP &x ) const { return a < x.a; } }; int n,k,r,m; multiset<PP> table; vector<pair<int,int> > pp; int main ( ) { while ( ~scanf ( "%d" , &n ) ) { for ( int i = 0 ; i < n ; i++ ) { scanf ( "%d%d" , &p[i].c , &p[i].p ); p[i].id = i+1; } sort ( p , p+n ); scanf ( "%d" , &m ); for ( int i = 0 ; i < m ; i++ ) { scanf ( "%d" , &r ); table.insert ( PP ( r , i+1 ) ); } int ans = 0; multiset<PP>::iterator it; for ( int i = 0 ; i < n ; i++ ) { it = table.lower_bound ( PP ( p[i].c , 1 ) ); if ( it == table.end() ) continue; ans += p[i].p; table.erase ( it ); pp.push_back ( make_pair ( p[i].id , it->b ) ); } printf ( "%d %d\n" , (int)pp.size() ,ans ); for ( int i = 0 ; i < pp.size() ; i++ ) printf ( "%d %d\n" , pp[i].first , pp[i].second ); } }
相关文章推荐
- 软测试总结
- Zigbee系列 学习笔记三(初始化程序解析)
- OGRE中的四元数与旋转
- Linear regression(线性回归)
- 分享一个volley支持 multipart上传文件
- 访问者模式
- MFC__Dialog去掉右上角的关闭按钮
- Android跨进程访问(四大组件)- Broadcast Receiver跨进程访问
- log4j2配置实例[按小时记录日志文件]
- 一.视图动画
- OS开发之旅之App的生命周期【转载】
- Jquery Ajax表单提交插件jquery form用法
- 转 iOS使用ffmpeg播放rstp实时监控视频数据流
- Android 开源项目分类汇总
- Xcode7 处理HTTP请求报错
- 发送有序广播
- 五种Java多线程同步的方法
- android:installLocation简析
- windows下安装setuptools,pip,virtualenv
- 在tableView中设置手势,获取手势点击位置