xdu 1203 - put on make up 二分答案

简介:

  写了这么多题,总想不起来二分答案这种神方法。附带下sort的默认比较函数

sort 中的比较函 数

equal_to 相等
not_equal_to 不相等
less 小于
greater 大于
less_equal 小于等于
greater_equal 大于等于

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
double a[10005],b[10005],t[10005];
int n,k;
bool ok(double mid)
{
    int i;
    double ans=0;
    for(i=0;i<n;i++)
        t[i]=a[i]-mid*b[i];
    sort(t,t+n,greater<double>());
    for(i=0;i<k;i++) ans+=t[i];
    return ans>0;
}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        int i;
        double t;
        for(i=0;i<n;i++)
            scanf("%lf",&t),a[i]=log(t);
        for(i=0;i<n;i++)
            scanf("%lf",&t),b[i]=log(t);
        double l=1.0,r=3.0,mid;
        while(r-l>1e-8)
        {
            mid=(l+r)/2.0;
            if(ok(mid))l=mid;
            else r=mid;
        }
        printf("%.3f\n",l);
    }
}


目录
相关文章
|
1月前
|
存储
CMake中遍历元素的技巧:foreach命令详解
CMake中遍历元素的技巧:foreach命令详解
43 1
|
7月前
|
机器学习/深度学习 人工智能 算法
CF1550A Find The Array(贪心算法)
CF1550A Find The Array(贪心算法)
22 0
|
7月前
|
机器学习/深度学习
CF1552A Subsequence Permutation(string排序大法)
CF1552A Subsequence Permutation(string排序大法)
25 0
|
9月前
|
算法 IDE 开发工具
【Python语法】类型提示(self, nums: List[int]) -> List[int],报错NameError: name ‘List‘ is not defined解决
【Python语法】类型提示(self, nums: List[int]) -> List[int],报错NameError: name ‘List‘ is not defined解决
三道题教你快速掌握Map和Set
Map 和 Set 都是接口类,都不能不能直接实例化对象,如果要实例化只能实例化其对于实现类!
三道题教你快速掌握Map和Set
|
存储 算法 JavaScript
📖Map和Set巧解力扣算法问题
📖Map和Set巧解力扣算法问题
77 1
📖Map和Set巧解力扣算法问题
|
人工智能 BI
Codeforces 1324 D-Pair of Topics(思维+二分 || 双指针)
Codeforces 1324 D-Pair of Topics(思维+二分 || 双指针)
101 0
PHP:array_diff求取两个数组的差集
PHP:array_diff求取两个数组的差集
|
人工智能 vr&ar
Atcoder--Candy Distribution II--前缀和+map
题目描述 There are N boxes arranged in a row from left to right. The i-th box from the left contains Ai candies. You will take out the candies from some consecutive boxes and distribute them evenly to M children. Such being the case, find the number of the pairs (l,r) that satisfy the following:
77 0
|
存储 算法 Python
[Leetcode][Python]Linked List Cycle/Linked List Cycle II/环形链表/环形链表 II
Linked List Cycle 题目大意 判断一个链表中是否存在着一个环,能否在不申请额外空间的前提下完成? 解题思路 哈希表 快慢指针
69 0

热门文章

最新文章