POJ 2976 Dropping tests (二分+贪心)
2024-08-29 12:32:23
题意:给定 n 个分数,然后让你去年 m 个分数,使得把剩下的所有的分子和分母都相加的分数最大。
析:这个题并不是分子越大最后结果就越大,也不是整个分数越大,最后结果就越大的,我们可以反过来理解,要去掉 m 个分数,那么就是要选 n-m个分数,
那么就是 sigma(分子) / sigma(分母) 尽量大,那么最大是多大啊?这个我们可以通过二分来解决,也就是sigma(分子) / sigma(分母) >= x,
因为分子和分母都是正数,所以可以得到 sigma(分子) - sigma(分母)* x >= 0,也就是 sigma(分子 - x * 分母) >= 0(前n-m项),我们就可以按这个进行排序,
看看前 n-m 项成不成立。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
double c; struct Node{
double a, b;
bool operator < (const Node &p) const{
return a - b * c > p.a - p.b * c;
}
};
Node a[maxn]; bool judge(){
sort(a, a + n);
double ans = 0;
for(int i = 0; i < n-m; ++i) ans += a[i].a- a[i].b * c;
return ans >= 0.0;
} double solve(){
double l = 0, r = INF;
for(int i = 0; i < 100; ++i){
c = (l+r) / 2;
if(judge()) l = c;
else r = c;
}
return l;
} int main(){
while(scanf("%d %d", &n, &m) == 2 && n+m){
for(int i = 0; i < n ; ++i) scanf("%lf", &a[i].a);
for(int i = 0; i < n ; ++i) scanf("%lf", &a[i].b);
printf("%.f\n", solve()*100);
}
return 0;
}
最新文章
- SunSonic 3.0 ORM开源框架的学习
- Java Web Cookie
- POJ2288 Islands and Bridges(TSP:状压DP)
- 使pre的内容自动换行
- Microsoft Office 2007的ContentType
- PV是什么意思
- C# 正则表达式、Json
- 创建dblink遇到一系列问题
- 手动开发动态资源之servlet初步
- allegro把formate symbol文件从一个文件拷入另一个文件的方法
- 在linux和windows用c++编写c接口的动态库
- Jquery+asp.net实现Ajax方式文件下载实例代码
- sql语句查询结果排序
- 【巷子】---redux---【react】
- 事件触发器-----dispatchEvent
- 阿里云腾讯云服务器ubuntu多域名配置
- 2016-2017-2 20155338 实验二《Java面向对象程序设计》实验报告
- Latex中为作者添加多个单位属性(IEEE模板)
- jQuery实际案例⑤——仿京东侧边栏(楼层)
- 【剑指offer-25】合并两个单调递增的链表,C++实现(链表)
热门文章
- ES5中的类与继承
- windows下python安装face_recognition模块
- POJ - 1611 The Suspects 【并查集】
- 05-树8 File Transfer(25 point(s)) 【并查集】
- 吴恩达机器学习笔记(七) —— K-means算法
- 关于Dubbo
- System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration sect
- RStudio安装package时出现错误
- npm使用淘宝镜像安装包
- apache-tomcat 及对应eclipse下载地址for mac