二分查找 l=4 r=5 卡住的示范, ACM9817 Rangers
来源:互联网 发布:支票管理打印软件 编辑:程序博客网 时间:2024/05/17 09:31
After leaving Canada you all head to Glacier Bay. You and Knave marvel at the beauty of the glaciers while Ark snickers about true beauty being found only in convex polygons and monotonic math functions, whatever that means.
You roll your eyes and follow him into the ranger station. The rangers are getting ready for a several day trip across glacier bay and are getting geared up. They have piles of tents, sleeping bags, etc strewn all over the floor and are arguing about who will go on the expedition.
The issue is that there are rangers in the station and each ranger requires a certain amount of each piece of gear in order to go on the expedition. You suggest that they simply order more gear from Amazon, but there's also a budget for the expedition of dollars.
You decide to help them figure out how to spend their money optimally such that as many rangers as possible are able to go. Given the number of rangers in the station, their current inventory of each piece of gear, the price of each piece of gear, and the gear requirements to send a single ranger can you print out how many pieces of each type they should order and the number of rangers that will be able to go?
Note that all rangers have the same gear requirements, that there is an infinite number of each piece of gear available for purchase, and that shipping is free since you have Amazon prime.
Input Format
The input will begin with 3 of integers, , , and , representing the number of rangers in the station (the maximum number of rangers that could go), the number of pieces of gear that must go with each ranger on the expedition, and the budget for the expedition.
lines follow, each representing a piece of gear. Each line will start with a string containing only lowercase letters, the name of the piece of gear, . This will be followed by the number of that piece of gear that each ranger going requires , the number currently in stock , and the price of buying an additional piece of that gear .
Constraints
for 33% of score
for all cases
All gear names will contain only lowercase English letters.
Output Format
First print a single integer, the maximum number of rangers that will be able to go while staying in budget.
Next print the name of each piece of gear and the number that the ranger station should purchase, with each piece of gear being on a new line. The gear should appear alphabetized by name.
Sample Input 0
5 3 100tent 1 5 10lantern 2 12 6food 4 16 1
Sample Output 0
5food 4lantern 0tent 0
Explanation 0
There is enough room in the budget to add 4 food, in which case all of the rangers in the station can go on the expedition.
#include<bits/stdc++.h>using namespace std;typedef long long ll;struct node{ string nm; ll x, y, z; friend inline bool operator <(const node&a, const node &b){ return a.nm<b.nm; }}a[120];ll n,m,cnt,R,G,D;ll vl, vr, vm, tot;int check(ll mid){tot=0;for (int i=0; i<G; i++){ if ((a[i].x*mid-a[i].y)>0) tot+=(a[i].x*mid-a[i].y)*a[i].z;}if (tot>D) return 1;if (tot==D) return 0;return -1;}vector<char>vec;int main(){cin>>R>>G>>D;for (int i=0; i<G; i++){ cin>>a[i].nm>>a[i].x>>a[i].y>>a[i].z;}vl=0; vr=R;while (vl<vr-1){ tot=0; vm=(vl+vr)>>1; int ret=check(vm); if (ret==1) vr=vm; else if (ret==-1) vl=vm; else { vl=vm; vr=vm; }}int ret2=check(vr);if (ret2<1) vl=vr;cout<<vl<<endl;sort(a, a+G);for (int i=0; i<G; i++){ ll ans=vl*a[i].x-a[i].y; if (ans<0) cout<<a[i].nm<<" "<<0<<endl; else cout<<a[i].nm<<" "<<ans<<endl;}return 0;}
两个注意点,
第一个,整数的二分查找可能会卡住。比如,样例里,如果while (l<r) 然后vl=vm, vr=vm, 那么l取4 r取5 mid 是 4, 总价仍然小, l=vm 还是4。所以只能while (l<r-1)。
对于这种连续函数只能取整数得,可以让r为不合法值,l为合法值, 然后用上述的while,最后check r是否合法。
第二个,function中的全局变量tot要初始化, 不初始化tot不是0.
- 二分查找 l=4 r=5 卡住的示范, ACM9817 Rangers
- ACM9817
- mid=(l+r)/2 mid=l+(r-l+1)/2 区别
- Codeforces #591 D Chip 'n Dale Rescue Rangers(二分查找/转换参考系)
- 分割矩阵 (二分范围[L,R))
- 5-4 二分搜索数的查找
- 关于node N= (node){l,r,sum} 和node N= node(l,r,sum)
- Arithmetic problem | 给定 l, r, k,求在 [l, r] 区间中,所有 f(x) = k 的 x 的和
- 二分 Codeforces591D Chip 'n Dale Rescue Rangers
- {dede:field name='array' /}的简单用法示范
- {dede:field name='array' /}的简单用法示范
- 二分查找和递归的二分查找
- 4种二分查找的C++算法
- Linux中ls -a,-l,-r,-R命令的实现
- 二分查找的思考
- 二分查找的总结
- vector的二分查找
- 二分查找的应用
- PAT乙级1039 到底买不买
- JCE cannot authenticate the provider BC
- java中system.in怎么用?
- JavaScript 正则表达式
- 为什么跑完步了膝盖会疼痛?
- 二分查找 l=4 r=5 卡住的示范, ACM9817 Rangers
- 键盘事件onkeyup练习
- ES6 Proxy杂记
- 多隆:从工程师到合伙人 | 阿里技术人纪录片
- Django开发速成
- Android更换logo之后小米手机和部分三星手机通知栏的小图标未显示正确的图标问题
- 人脸识别之人脸对齐(六)--ERT算法
- 上传文件简单示例
- C语言中各种运算符的优先级