第k小分数(二分值)
//时间限制:10000ms
//单点时限:1000ms
//内存限制:256MB
//描述
//给定N个不同的质数P1, P2, … PN。用它们作为分目可以组成(P1-1) + (P2-1) + … (PN-1)个分数:
//
//1/P1, 2/P1, 3/P1, …, P1-1/P1, 1/P2, 2/P2, 3/P2, … P2-1/P2, … 1/PN, 2/PN, … PN-1/PN
//
//请帮助小Ho求出其中第K小的分数。
//
//输入
//第一行包含两个整数N和L。
//
//以下N行每行包含一个质数Pi。
//
//对于70%的数据,1 ≤ N ≤ 100, 1 ≤ K ≤ 1000000, 2 ≤ Pi ≤ 100000
//
//对于100%的数据, 1 ≤ N ≤ 1000, 1 ≤ K ≤ 1000000000, 2 ≤ Pi ≤ 1000000000
//
//输出
//输出一个分数表示答案
//
//样例输入
//3 4
//2
//3
//5
//样例输出
//1/2
//题解:
//由于p是质数,所以每个分数都不可约分,由于对于每一个1/P1, 2/P1, 3/P1, …,
//P1-1/P1序列都是递增,我们可以二分答案,算出小于等于二分答案的数有多少个,
//当个数等于k时即二分答案接近要求的答案,记录最接近的分子和分母,即为答案 #include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm> using namespace std;
int main() {
int n, k;
while(scanf("%d%d",&n,&k)==){ long long f[];
memset(f,,sizeof(f));
for( int i = ; i <= n; i++ ) {
scanf("%lld", &f[i] );
}
long long zz , ff;
double l = , r = 1.0, mid;
while(true){
mid = (r+l)/2.0;
long long ans = ;
zz = ff = -; for( int i = ; i <= n; i++ ) { long long x = mid*f[i]; ans += x; if( zz == - ) {
zz = x;
ff = f[i];
}
if((long long)f[i]*zz < (long long)x*ff){
zz = x;
ff = f[i];
}
// cout << zz << "/" << ff<<endl;
}
// cout<<"ans = "<<ans<<endl;
if( ans == k ) break; if( ans > k ) r = mid;
else l = mid; }
cout << zz << "/" << ff<<endl;
}
return ;
}
第k小分数(二分值)的更多相关文章
- [LeetCode] K-th Smallest Prime Fraction 第K小的质分数
A sorted list A contains 1, plus some number of primes. Then, for every p < q in the list, we co ...
- [LeetCode] 786. K-th Smallest Prime Fraction 第K小的质分数
A sorted list A contains 1, plus some number of primes. Then, for every p < q in the list, we co ...
- [LeetCode] Kth Smallest Element in a BST 二叉搜索树中的第K小的元素
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- [Swift]LeetCode230. 二叉搜索树中第K小的元素 | Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- 230. 二叉搜索树中第K小的元素
230. 二叉搜索树中第K小的元素 题意 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. ...
- leetcode 二叉搜索树中第K小的元素 python
二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元 ...
- [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素
题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...
- LeetCode:二叉搜索树中第K小的数【230】
LeetCode:二叉搜索树中第K小的数[230] 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ...
- The UVALIVE 7716 二维区间第k小
The UVALIVE 7716 二维区间第k小 /** 题意:给一个n * n的矩阵,有q个查询 每次查询r,c,s,k表示已(r,c)为右上角 大小为s的正方形中 第k小的元素 n <= 2 ...
随机推荐
- 崩溃!UIAlertController 引起的崩溃
UIAlertController 使用方法很简单,下面贴简单的使用方法: UIAlertController *alert = [UIAlertController alertControllerW ...
- 【Mybatis】参数处理
单个参数:mybatis不会做特殊处理, #{参数名/任意名}:取出参数值. 多个参数:mybatis会做特殊处理. 多个参数会被封装成 一个map, key:param1...paramN,或者参数 ...
- NPOI下载
.吐槽NPOI下载 众所周知我们用NPOI第三方程序集主要的目的就是为了能快捷的操作Excel,但是现在不论是官网(https://archive.codeplex.com/?p=npoi)还是git ...
- keepalived 的进程/usr/sbin/keepalived -D 只有2个
操作系统:openSUSE 11.3 (x86_64) /usr/sbin/keepalived -D 只有2条 日志:ls /var/log/messages* -lrth Can't init ...
- 内核调试工具——strace
简介 strace常用来跟踪进程执行时的系统调用和所接收的信号. 在Linux世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必须由用户态模式切换至内核 ...
- C# 解压gzip文件(.tgz)
1.引用 SharpCompress.dll 2.代码 using System;using System.IO;using System.Text;using SharpCompress.Reade ...
- nginx 源码安装以及后续升级https
事情的来源是,公司要将网站从http升级到https,由于历史遗留原因,才发现现有的nginx是通过源码安装的,并没有安装ssl模块,需要现安装sll模块,这个nginx是整个公司最前端的一个代理,涉 ...
- Python-type函数使用
- Delphi XE7的Splash 功能
Delphi XE5,XE6,XE7编译的程序在Android下启动会有一段时间黑屏,以前需要用Java扩展Activity增加Splash显示, 现在Delphi XE7增加了Splash Imag ...
- scala (7) Set and Tuple
/** * 不可变长Set集合 */ val set0 = Set(1, 2, 3, 4, 5) //++并没有改变原有的set集合,只是将两个set进行合并形成新的set集合 val newSet0 ...