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);
    }
}


目录
相关文章
|
30天前
|
存储
CMake中遍历元素的技巧:foreach命令详解
CMake中遍历元素的技巧:foreach命令详解
37 1
|
7月前
|
机器学习/深度学习
CF1552A Subsequence Permutation(string排序大法)
CF1552A Subsequence Permutation(string排序大法)
25 0
三道题教你快速掌握Map和Set
Map 和 Set 都是接口类,都不能不能直接实例化对象,如果要实例化只能实例化其对于实现类!
三道题教你快速掌握Map和Set
|
人工智能 算法
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
|
存储 算法 JavaScript
📖Map和Set巧解力扣算法问题
📖Map和Set巧解力扣算法问题
77 1
📖Map和Set巧解力扣算法问题
|
人工智能 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
|
Sentinel
HDOJ/HDU 1113 Word Amalgamation(字典顺序~Map)
HDOJ/HDU 1113 Word Amalgamation(字典顺序~Map)
88 0
# Leetcode 14:Longest Common Prefix 最长公共前缀
公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". 编写一个函数来查找字符串数组中的最长公共前缀。
976 0