codeforce C. Success Rate
写完这道题目才发现自己对二分的理解太浅了 这题是典型的利用二分“假定一个问题可行并求最优解”
二分是通过不断缩小区间来缩小解的范围,最终得出解的算法 我们定义一个c(x) 表示判断函数
如果对任意y>=x 当x满足条件的时候 y也满足条件 那么我们就一个不断缩小区间范围来确定最后的解
好扯了这么多犊子 来说下这道题目。。
我们的目的是对A,B 有 (x+A)/(y+B) == (p/q) 其中A<=B
那么可以转换为 A+x=k*p B+y=k*q;
既 A=k*p-x, B=k*q-y;(A>=0,B>=0)
这里可以验证 对任意k >= x 当x满足条件的时候 k也满足条件(自己模拟一下就知道了)
ok 二分开搞
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <algorithm>
#include <queue>
#include <string>
#include <cmath>
using namespace std;
const int inf=1000000000;
int main()
{
int t;
cin>>t;
long long int x,y,p,q;
while(t--)
{
cin>>x>>y>>p>>q;
long long int temp=-1;
long long int l=1;
long long int r=inf;
while(l <= r)
{
long long int mid=(l+r)/2;
long long int A=mid*p-x;
long long int B=mid*q-y;
if(A>=0 && B>=0 && A<=B)
{
temp=mid;
r=mid-1;
}
else l=mid+1;
}
if(temp==-1) cout<<"-1"<<endl;
else cout<<temp*q-y<<endl;
}
return 0;
}
codeforce C. Success Rate的更多相关文章
- Success Rate CodeForces - 807C (数学+二分)
You are an experienced Codeforces user. Today you found out that during your activity on Codeforces ...
- Codeforces Round #412 C. Success Rate
C. Success Rate time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces807 C. Success Rate 2017-05-08 23:27 91人阅读 评论(0) 收藏
C. Success Rate time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces 807 C. Success Rate
http://codeforces.com/problemset/problem/807/C C. Success Rate time limit per test 2 seconds memory ...
- AC日记——Success Rate codeforces 807c
Success Rate 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> ...
- CodeForce-807C Success Rate(二分数学)
Success Rate CodeForces - 807C 给你4个数字 x y p q ,要求让你求最小的非负整数b,使得 (x+a)/(y+b)==p/q,同时a为一个整数且0<=a< ...
- C. Success Rate
Success Rate 题目链接 题意 给你两个分数形式的数,然后有两种变化方式 上下都+1 仅下面部分+1 让你求第一个分数变化到第二个分数的最小步数. 思路 有几种特殊情况分类讨论一下. 首先我 ...
- The Trip PC/UVa IDs: 110103/10137, Popularity: B, Success rate: average Level: 1
#include<cstdio> #include<iostream> #include<string> #include<algorithm> #in ...
- Minesweeper PC/UVa IDs: 110102/10189, Popularity: A,Success rate: high Level: 1
#include<cstdio> #include<iostream> #include<string> #include<algorithm> #in ...
随机推荐
- 最初学习mysql的一些操作留存
一:数据库的初始话操作 mysql -u root -p //数据库的登陆 show databases: //展现数据哭中存储的所有文件 use 数据库名: //进入当前要 ...
- springboot 配置跨越访问
import org.springframework.stereotype.Component; import javax.servlet.*; import javax.servlet.http.H ...
- git push and git pull
原文链接 git push 通常对于一个本地的新建分支,例如git checkout -b develop, 在develop分支commit了代码之后,如果直接执行git push命令,develo ...
- HearthBuddy 复生 reborn
https://hearthstone.gamepedia.com/Reborn Reborn is an ability that causes a minion to be resummoned ...
- matlab处理矩阵
1.提取大矩阵的一列.一行元素:一列元素: A(:,j)表示提取A矩阵的第j列全部元素一行元素: A(i,:)表示提取A矩阵的第i行元素,于是我们有,A(i, j)表示提取A矩阵的第i行第j列的元 ...
- H5和原生APP之间的区别
最近项目中因各种客观因素,移动端都是默认用的纯H5 APP,感受最深的就是各种坑啊,好大的坑啊.产品上线后,带着各种坑后的总结原因方发现很多人都说纯H5 APP一次编写就能支持android和IOS两 ...
- Scala API - 泛型
- Linux Shell 自动备份脚本
写一个使用shell脚本增量备份系统文件,顺便复习一下shell脚本相关的命令,这个脚本可以根据自己的需求来备份不同的文件或者文件夹,进行完整备份和增量备份. 参考的网址:http://blog.51 ...
- DevOps - 构建本地开发环境
1 - 构建个人本地开发环境 在个人计算机中搭建一个精简版的.与生产环境基本一致的本地开发环境,既不会占用团队公共环境的资源,也可以缩短等待时间,从整体上提高效率. 本地开发环境的适用场景: 从应用程 ...
- Linux 查看修改SWAP大小
1 查看swap 空间大小(总计): # free -m 默认单位为k, -m 单位为M total used fre ...