HDU2199,HDU2899,HDU1969,HDU2141--(简单二分)
二分是一种很有效的减少时间开销的策略, 我觉得单列出二分专题有些不太合理, 二分应该作为一中优化方法来考虑
这几道题都是简单的使用了二分方法优化, 二分虽然看似很简单, 但一不注意就会犯错. 在写二分时, 会遇到很多选择题, 很多"分叉路口", 要根据实际情况选择合适的"路"
HDU2199
Can you solve this equation?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1195 Accepted Submission(s): 565 Problem DescriptionNow,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its solution between 0 and 100;
Now please try your lucky. InputThe first line of the input contains an integer
T(1<=T<=100) which means the number of test cases. Then T lines
follow, each line has a real number Y (fabs(Y) <= 1e10); OutputFor each test case, you should just output one
real number(accurate up to 4 decimal places),which is the solution of
the equation,or “No solution!”,if there is no solution for the equation
between 0 and 100. Sample Input2
100
-4 Sample Output1.6152
No solution! AuthorRedow Recommend
|
|
注意看AC代码,循环的条件是 l+eps<r ,不能用 f(mid)==res 否则会一直循环下去(注意看循环内部, 有可能边界(l或r)就为正确的值, 这样的话永远不会搜索到)
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<iomanip>
#define INF 0x7ffffff
#define MAXN 10000
using namespace std;
const double eps=1e-;
double js(double x)
{
return 8.0*x*x*x*x + 7.0*x*x*x + 2.0*x*x + 3.0*x + 6.0;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
std::ios::sync_with_stdio(false);
std::cin.tie();
double y;
double x;
double r,l;
int t;
cin>>t;
while(t--){
cin>>y;
if(y<||y>js()){
cout<<"No solution!"<<endl;
}
else{
l=; r=;
double mid,res;
while(l+eps<r){
mid=(l+r)/;
if(y<js(mid)){
r=mid;
}
else{
l=mid;
}
}
cout<<fixed<<setprecision()<<mid<<endl;
}
}
return ;
}
HDU2899
Strange fuction
| Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
| Total Submission(s): 666 Accepted Submission(s): 549 |
|
Problem Description
Now, here is a fuction:
F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100) Can you find the minimum value when x is between 0 and 100. |
|
Input
The first line of the input contains an integer
T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has only one real numbers Y.(0 < Y <1e10) |
|
Output
Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100. |
|
Sample Input
2 |
|
Sample Output
-74.4291 |
|
Author
Redow
|
|
Recommend
lcy
|
和上一道题一模一样,只不过这道题目拐了个弯, 需要进行求导
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<iomanip>
#define INF 0x7ffffff
#define MAXN 10000
using namespace std;
const double eps=1e-;
double fd(double x)
{
return *pow(x,)+*pow(x,)+*pow(x,)+*x;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
std::ios::sync_with_stdio(false);
std::cin.tie();
int t;
double y;
double l,r,mid;
cin>>t;
while(t--){
cin>>y;
l=;
r=;
while(r-l>eps){
mid=(l+r)/;
if(fd(mid)>y){
r=mid;
}
else l=mid;
}
cout<<fixed<<setprecision()<<*pow(mid,)+*pow(mid,)+*pow(mid,)+*mid*mid-y*mid<<endl;
}
}
HDU1969
Pie
| Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
| Total Submission(s): 939 Accepted Submission(s): 348 |
|
Problem Description
My birthday is coming up and traditionally I'm serving pie. Not
just one pie, no, I have a number N of them, of various tastes and of various sizes. F of my friends are coming to my party and each of them gets a piece of pie. This should be one piece of one pie, not several small pieces since that looks messy. This piece can be one whole pie though. My friends are very annoying and if one of them gets a What is the |
|
Input
One line with a positive integer: the number of test cases. Then for each test case:
---One line with two integers N and F with 1 <= N, F <= 10 000: the number of pies and the number of friends. ---One line with N integers ri with 1 <= ri <= 10 000: the radii of the pies. |
|
Output
For each test case, output one line with the largest possible
volume V such that me and my friends can all get a pie piece of size V. The answer should be given as a floating point number with an absolute error of at most 10^(-3). |
|
Sample Input
3 |
|
Sample Output
25.1327 |
|
Source
NWERC2006
|
|
Recommend
wangye
|
这道题没有固定的函数关系, 每一个待定的面积值都要去除每块pie的面积, 为了加快搜索到结果的速度, 可以采用二分的方法(可分成的块数大体上是相对于面积递增的)
代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std ;
#define PI 3.1415926535898
#define eqs 1e-5
double s[] ;
int n , m ;
double f(double x)
{
int k = (x+eqs) * ;
x = k * 1.0 / ;
return x ;
}
int solve(double x)
{
int i , j , num = ;
for(i = n- ; i >= && (s[i]-x) > eqs ; i--)
{
j = s[i] / x ;
num += j ;
if( num >= m+ )return ;
}
if( num >= m+ ) return ;
return ;
}
int main()
{
int t , i , k ;
double low , mid , high , last ;
while( scanf("%d", &t) != EOF )
{
while(t--)
{
scanf("%d %d", &n, &m) ;
for(i = ; i < n ; i++)
scanf("%lf", &s[i]) ;
sort(s,s+n) ;
for(i = ; i < n ; i++)
s[i] = s[i]*s[i]*PI ;
low = ;
high = s[n-] ;
while( (high-low) > eqs )
{
mid = (low+high)/2.0 ;
if( solve(mid) )
{
low = mid ;
last = mid ;
}
else
high = mid ;
}
printf("%.4lf\n", last) ;
}
}
return ;
}
HDU2141
Can you find it?
| Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others) |
| Total Submission(s): 1157 Accepted Submission(s): 373 |
|
Problem Description
Give you three sequences of numbers A, B, C, then we give you a
number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X. |
|
Input
There are many cases. Every data case is described as followed:
In the first line there are three integers L, N, M, in the second line there are L integers represent the sequence A, in the third line there are N integers represent the sequences B, in the forth line there are M integers represent the sequence C. In the fifth line there is an integer S represents there are S integers X to be calculated. 1<=L, N, M<=500, 1<=S<=1000. all the integers are 32-integers. |
|
Output
For each case, firstly you have to print the case number as the
form "Case d:", then for the S queries, you calculate if the formula can be satisfied or not. If satisfied, you print "YES", otherwise print "NO". |
|
Sample Input
3 3 3 |
|
Sample Output
Case 1: |
|
Author
wangye
|
|
Source
HDU 2007-11 Programming Contest
|
|
Recommend
威士忌
|
一样的题目, 不多说了
代码:
#include<stdio.h>
#include<algorithm>
using namespace std;
#define K 505
int LN[K*K];
int BinarySearch(int LN[],int h,int t)/*二分查找*/
{
int left,right,mid;
left=;
right=h-;
mid=(left+right)/;
while(left<=right)
{
mid=(left+right)/;
if(LN[mid]==t)
return ;
else if(LN[mid]>t)
right=mid-;
else if(LN[mid]<t)
left=mid+;
}
return ;
}
int main()
{
int i,j,count=,q;
__int32 L[K],N[K],M[K],S,n,m,l;
while(scanf("%d%d%d",&l,&n,&m)!=EOF)
{
int h=;
for(i=;i<l;i++)
scanf("%d",&L[i]);
for(i=;i<n;i++)
scanf("%d",&N[i]);
for(i=;i<m;i++)
scanf("%d",&M[i]);
for(i=;i<l;i++)
for(j=;j<n;j++)
LN[h++]=L[i]+N[j];/*合并L和N*/
sort(LN,LN+h); /*对LN数组排序*/
scanf("%d",&S);
printf("Case %d:\n",count++);
for(i=;i<S;i++)
{
scanf("%d",&q);/*q即为题目中的x*/
int p=; /*p为标记,0为找不到,1为能找到*/
for(j=;j<m;j++)
{
int a=q-M[j]; /*因为L[i]+N[j]+M[k]==q,所以q-M[k]=LN[h]*/
if(BinarySearch(LN,h,a)) /*在LN数组中查找到a*/
{
printf("YES\n");
p=;
break;
}
}
if(!p) /*找不到a*/
printf("NO\n");
}
}
return ;
}
HDU2199,HDU2899,HDU1969,HDU2141--(简单二分)的更多相关文章
- 一些简单二分题,简单的hash,H(i),字符串题
说在前面: 题是乱七八糟的. 几个二分的题. (但是我的做法不一定是二分,有些裸暴力. 1. Equations HDU - 1496 输入a,b,c,d问你这个方程有多少解.a*x1^2+b*x2^ ...
- hdu-4185.loiol_skimming(简单二分匹配模型)
/************************************************************************* > File Name: hdu-4185. ...
- POJ2239简单二分匹配
题意: 一周有7天,每天可以上12节课,现在给你每科课的上课时间,问你一周最多可以上几科课,一科课只要上一节就行了. 思路: 简单题目,直接二分就行了,好久没写二分匹配了,练习 ...
- CF 706B 简单二分,水
1.CF 706B Interesting drink 2.链接:http://codeforces.com/problemset/problem/706/B 3.总结:二分 题意:给出n个数,再给 ...
- poj2785 简单二分
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 19243 Accep ...
- poj 3273 Monthly Expence 简单二分
/** 大意: 有连续的n天,每一天有一定的花费,将其分成m份,每一份占一天或者连续的几天,求这m份中的最大值 思路: 二分其最大上限,看在此最大上线,能分成多少份,若大于m份,说明上限过小,需要扩大 ...
- Codeforces 846D Monitor(简单二分+二维BIT)
D. Monitor time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- SPOJ-COLONY - Linearian Colony!简单二分思想
COLONY - Linearian Colony 一道很水的题却坑我两天!在CF上做过类似的题,用递归可以找到答案,但感觉不会这么麻烦,于是看看有没有什么规律,对Y分奇偶貌似可以找到规律,但WA了三 ...
- 二维数组中的查找 - Java版 -简单二分查找 -<<剑指Offer>> -水题
如题 (总结) -认真读题, 还WA了一次, https://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e?tpId=13&am ...
随机推荐
- HTML+CSS D09 定位
1.定位 (1)相对定位 如果对一个元素进行相对定位,它将出现在它所在的位置上.然后,可以通过设置垂直或水平位置,让这个元素“相对于”它的起点进行移动. #box_relative { positi ...
- php 验证码类
<?php class Vcode { private $width; //宽 private $height; //高 private $num; //数量 private ...
- Webdriver如何解决页面元素过期:org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
当运行Webdriver时抛出如下异常:org.openqa.selenium.StaleElementReferenceException: Element not found in the cac ...
- svn地址如何更改
1.先进入平时放的更新文件的位置 2.然后右键 选中TortoiseSVN中的Relocate修改里面的完了之后输入账号和密码就好了当然你要记得密码和账号
- [Python] spides
摘要:本文将介绍用Python进行的爬虫的各种练习以及涉及到的各种知识,包括Http协议,cookie等等 工具 Fiddler Python默认不用代理,所以fiddler不能截取它的包.如果想用用 ...
- HOSTS文件修改后不起作用的原因
如果是通过记事本修改,其实是没有问题的,如果有问题,网上搜到的是ipconfig /flushdns之类的 如果是批量程序写的,那就要小心了, 一定要ANSI(美标格式的,忘了英文是什么来着) 保存后 ...
- 《JS权威指南学习总结--4.9.3in和instanceof运算符》
内容要点: 一.in运算符 in运算符希望它的左操作数是一个字符串或可以转换为字符串,希望它的右操作数是一个对象.如果右侧的对象拥有一个名为左操作数数值的属性名,那么表达式返回true. 例如: va ...
- thinkphp 实现微信公众号开发(二)--实现自定义菜单
IndexController.class.php <?php namespace Home\Controller; use Think\Controller; class IndexContr ...
- hdu_5802_Windows 10(贪心)
题目链接:hdu_5802_Windows 10 题意: 给你两个音量a,b,要让你将音量a变到音量b,up:每秒只能一次加1的音量,down:如果连续按x秒,那么就会减2x-1的音量 题解: 对于a ...
- WisKey的眼神
WisKey的眼神 Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...