文章标题
来源:互联网 发布:二选一数据选择器 编辑:程序博客网 时间:2024/05/16 14:06
很简单的贪心,写一个结构体,按j/f的比例从大到小排,有限选择比例大的就行,注意要用double,float可能精度不够第一次WA了。
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <map>#include <algorithm>#include <cmath>#include <iomanip>#include <vector>#include <stack>#include <cstdlib>#include <queue>#include <set>#include <cctype>using namespace std;#define loop(i,u,v) for(int i = u;i <= v;i++)#define rloop(i,u,v) for(int i = u;i >= v;i--)#define eps 0.00000001typedef long long ll;struct stu{ int x; int y;} jf[10001];bool cmp(stu a,stu b){ float rate1 = (float)a.x / a.y; float rate2 = (float)b.x / b.y; return rate1 > rate2;}int main(){ int n,m; while(cin >> n >> m) { if(n == -1) break; loop(i,1,m) scanf("%d%d",&jf[i].x,&jf[i].y); sort(jf+1,jf+1+m,cmp); double sum = 0; loop(i,1,m) { if(n <= 0) break; else if((double)n >= jf[i].y) { sum += (double)jf[i].x; n -= jf[i].y; } else { sum += (double) jf[i].x / jf[i].y * n; break; } } printf("%.3lf\n",sum ); } return 0;}
0 0
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 114. Flatten Binary Tree to Linked List
- HDU1002
- linux ipc之匿名管道
- 字符数组动态开辟空间和静态开辟空间
- windows::gvim配置pydiction详细过程
- 文章标题
- Leetcode 124. Binary Tree Maximum Path Sum
- 【莫比乌斯反演】[BZOJ3529]数表
- Matplotlib 如何显示中文
- nginx上设置禁止通过ip访问服务器,只能通过域名访问
- Leetcode 207. Course Schedule
- linq jion多张表 group by
- Leetcode 210. Course Schedule II
- OMAP3 DSS 初始化过程(1)总线关系