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 ...
随机推荐
- Leetcode题目160.相交链表(简单)
题目描述 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 输入:intersectVal = 8, listA = [4,1,8,4,5], listB = ...
- 微服务RESTful 接口设计规范
1.RESTful发展背景及简介 网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备......).因此,必须有一种统一的机制,方便不同的前 ...
- POCO C++库笔记 【1.Foundation基础库的结构】
Foundation库是POCO的基础库,提供了一些C++编程中常用的功能的抽象封装,主要由以下这些部分组成: Core -- 这部分除了建立跨平台库的基础头文件外,最有意义的部分是分装了原子计数的 ...
- GitHub代码下载和同步
1.下载git客户端https://git-scm.com/ssh-keygen -C "your@email.address" -t rsa 2. 把下面文件的内容复制到 htt ...
- error: atomic: 没有那个文件或目录
Linux下编译的时候遇到一个问题,就是提示 error: atomic: 没有那个文件或目录 执行的命令是gcc -o myCXXLog myCXXLog.c 经过网上搜索,解决方法有二 (1 ...
- 关于Server2008 R2日志的查看
Server 2008 r2通过 系统事件查看器 分析日志: 查看 系统 事件: 事件ID号: 审计目录服务访问 4934 - Active Directory 对象的属性被复制 4935 -复制失败 ...
- MySQL数据库同步工具的设计与实现
一.背景 在测试过程中,对于不同的测试团队,出于不同的测试目的,我们可能会有多套测试环境.在产品版本迭代过程中,根据业务需求,会对数据库的结构进行一些修改,如:新增表.字段.索引,修改表.字段索引等操 ...
- 第一个React Native项目
1>下图操作创建第一个React Native项目: 用Xcode运行界面如下: 记住: 在使用项目文件期间,终端记住不要关闭的哟!!! 改变了程序代码,需要刷新运行,使用快捷键: Comman ...
- hostname -i 出现0.0.0.0解决
[root@hostnametest4 ~]# hostname -i 0.0.0.0 原因:是因为四个节点中其中一个节点没有配置hosts解析,必须每个节点写全这四个ip,只要掉一个ip就会出现0. ...
- Dell 12V/18A电源适配器接口改造
手头有几个航模用的充电器,原来一直用实验室电源,不方便移动,为了便携省地方,就想配个合适的电源.在网上找了下,航模专用的适配器价格太高,国产的杂牌适配器功率虚标严重并且可靠性是个问题,工业用的电源基本 ...