Codeforces Round #404 (Div. 2) C 二分查找

Codeforces Round #404 (Div. 2)

题意:对于 n and m (1?≤?n,?m?≤?10^18)  找到

  1) [n<= m] cout<<n;

  2) [n>m]最小的 k => (k -m) * (k-m+1) >= (n-m)*2 成立

思路:二分搜索

	#include <bits/stdc++.h>
	#include <map>
	using namespace std;

	#define LL long long
	const long long INF = 1e10;
	long long n, m, del, mix;
	bool cal(long long a){
		return a*(a+1) >= del;
	}

	long long find(long long l, long long r){
		while(l + 1 < r){
			long long mid = (l +r) >> 1;
//			cout<<(l+r)/2<<" "<<l<<" "<<r<<endl;
			if(cal(mid)) r = mid;
			else l = mid;
		}
		return l;
	}

	int main(){
		ios::sync_with_stdio(false); cin.tie(0);
//		freopen("input2.txt", "r", stdin);
		cin>>n>>m;

		if(n <= m) cout<<n<<endl;
		if(n > m){
			del = 2 * (n-m);
			mix = find(0, 1e10);
			while(!cal(mix)) mix++;
			cout<<mix+m<<endl;
		}
		return 0;
	}

  

时间: 2024-07-29 23:49:00

Codeforces Round #404 (Div. 2) C 二分查找的相关文章

Codeforces Round #404 (Div. 2) C 二分,水 D 数学,好题 E 分块

Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale 题意:仓库容量n,每天运来m粮食,第 i 天被吃 i 粮食,问第几天仓库第一次空掉. tags:==SB题 注:二分边界判断,数据范围爆long long判断. // CF404 C #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000&

Codeforces Round #256 (Div. 2)D 二分答案

D. Multiplication Table time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Bizon the Champion isn't just charming, he also is very smart. While some of us were learning the multiplication tabl

Codeforces Round #262 (Div. 2)C(二分答案,延迟标记)

这是最大化最小值的一类问题,这类问题通常用二分法枚举答案就行了. 二分答案时,先确定答案肯定在哪个区间内.然后二分判断,关键在于怎么判断每次枚举的这个答案行不行. 我是用a[i]数组表示初始时花的高度,b[i]表示要达到当前枚举的答案(即mid的值)需要这朵花再涨多少.这两个数组很好算,关键是一次浇连续的w朵花,如何更新区间(暴力的O(n2)的去更新就超时了)?可以用线段树,但是这道题没有涉及区间查询,就是在一个数组上更新区间,用线段树未免小题大做.那么其实这种更新就用延迟标记的思想(懒操作)就

Codeforces Round #404 (Div. 2)

A: Anton and Polyhedrons(水题) B: Anton and Classes(水题) C: Anton and Fairy Tale(二分) 思路:m>=n时 ans=n;m<n时 前m天都能补满,第m+1天时就要开始减少了,n-m-(x+1)*x/2是m天后第x天的剩余,二分n-m<=x*(x+1)/2的最小的x,ans=m+x; D: Anton and School - 2(组合计数) 思路:枚举左括号的最后一个,它之前有n个左括号,右边有m个有括号,那么就C

Codeforces Round #350 (Div. 2) D2 二分

五一期间和然然打的团队赛..那时候用然然的号打一场掉一场...七出四..D1是个数据规模较小的题 写了一个暴力过了 面对数据如此大的D2无可奈何 现在回来看 一下子就知道解法了 二分就可以 二分能做多少个 每次对mid求一下不够的差值 比较差值与m的大小进行l与r的变换 由于自己一向对二分比较迷茫 自己琢磨出来一套神奇的办法面对边界数据 当小于和大于的时候 抛弃mid值 当等于的时候 直接break 然后打一发while试试能否向更好的情况偏移 当然在这个题目中 如果是直接break的时候就不用

Codeforces Round #404 (Div. 2)——ABCDE

题目这里 A.map裸题 #include <bits/stdc++.h> using namespace std; map <string, int> p; string s[] = { "Tetrahedron", "Cube", "Octahedron" , "Dodecahedron", "Icosahedron" }; int ss[] = {4, 6, 8, 12, 20

【Codeforces Round #404 (Div. 2)】题解

A. Anton and Polyhedrons 直接统计+答案就可以了. #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define maxn 1000 #define LL long long using namespace std; char s[10000]; int main() { int n; LL sum=0; scanf("%d&qu

Codeforces Round #404 (Div. 2) ---d

D. Anton and School - 2 这题第一眼一直觉得是dp,然而到最后他竟然是个数学题2333 考虑暴力,我们枚举一个'(',来求选他的情况下方案数有多少,那么事实上我们就是要在该位置左边选取i个'(',在其右边选取i+1个')',可以保证不重不漏.暴力的话On统计即可.那么就是要化简组合数的求和.即C(n,i)*C(m,i+1) (0<=i<=min(n,m-1)) 我们发现C(m,i+1)=C(m,m-i-1) 而i+(m-i-1)是定值. 也就是说变成了现在前n个数中选i个

二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix

题目传送门 1 /* 2 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 using namespace std; 8 9 const int MAXN = 5e2 + 10; 10 const int MAXM = 1e6 + 10; 11 const int INF = 0