ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Daliy Algorithm(二分&前缀和) -- day 51

2020-04-07 11:01:38  阅读:329  来源: 互联网

标签:二分 Daliy Algorithm int sum 51 mid return include


Nothing to fear

those times when you get up early and you work hard; those times when you stay up late and you work hard; those times when don’t feel like working — you’re too tired, you don’t want to push yourself — but you do it anyway. That is actually the dream. That’s the dream. It’s not the destination, it’s the journey. And if you guys can understand that, what you’ll see happen is that you won’t accomplish your dreams, your dreams won’t come true, something greater will. mamba out


那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~

2020.4.6


四平方和

思路先牺牲部分空间将所有两两可能存在的结果枚举出来
大致是O(n^2)级别 多了就不划算了
然后再去进行枚举a , b
得到 n - a*a - b * b
二分查表观察其是否存在
注意若要二分答案那么答案的序列一定是单调的

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int N = 2500010;
int x ; 
struct Sum
{
	int s, c , d;
	bool operator <(const Sum &x)const{
		if(s != x.s)return s < x.s;
		if(c != x.c)return c < x.c;
		return d < x.d;
	}
}sum[N];
int main()
{
	cin >> x;
	int m = 0;
	for(int c = 0;c * c <= x ;c++)
	{
		for(int d = c;c*c + d*d <= x; d++)
		{
			sum[m++] = {c*c + d*d,c,d};
		}
	}
	sort(sum, sum + m);
	for(int a = 0;a * a <= x ;a++)
	{
		for(int b = a; b*b <= x-a*a;b++)
		{
			int t = x - a*a - b*b;
			int l = 0, r= m - 1;
			while(l < r)
			{
				int mid = l + r >> 1;
				if(sum[mid].s >= t)r = mid;
				else l = mid + 1;
			}
			if(sum[l].s == t)
			{
				printf("%d %d %d %d",a ,b ,sum[l].c,sum[l].d);
				return 0;
			}
		}
	}

	return 0;
}

K倍区间

先推导出来数学规律
再利用数学规律进行优化(大多数情况下都是恒等变换)
通常带有取模性质得运算都可以进行一些等价变化
进行优化时间复杂度得效果

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>

using namespace std;
const int N = 100005;
typedef long long ll;
ll sum[N] , res[N] ,a[N],ans;
int n , k;
int main()
{
	cin >> n >> k;
	for(int i = 1;i <= n ;i ++)
	{
		cin >> a[i];
		sum[i] = (sum[i-1] + a[i]) % k;
		ans += res[sum[i]];
		res[sum[i]]++;
	}
	cout << ans + res[0]<< endl;
	return 0;
}

机器人跳跃问题

二分答案

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>

using namespace std;
const int N = 100005;
int n , maxc;
int h[N];

bool check(int e)
{
	int t = e;
	for(int i  = 1;i <= n; i++)
	{
		if(h[i] <= t)t += (t - h[i]);
		else t -= (h[i] - t);
		if(t < 0)return false;
		if(t >= maxc)return true;   //这里要防止 t 一直增大溢出
	}
	return true;
}

int main()
{
	cin >> n;
	for(int i = 1;i <= n ;i ++)
	{
		scanf("%d",&h[i]);
		maxc = max(maxc , h[i]);
	}
	int l = 0,r = maxc;
	while(l < r)
	{
		int mid = l + r >> 1;
		if(check(mid))r = mid;
		else l = mid + 1;
	}
	cout << l << endl;
	return 0;
}

数得范围

标准二分模板

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;
const int N = 100005;
int n , q , a[N];

// 找到数列中存在
void merge(int k)
{
	int l = 0, r = n - 1;
	while(l < r)
	{
		int mid = l + r >> 1;
		if(a[mid] >= k)r = mid;   // 找到第一个等于该数的
		else l = mid + 1;
	}
	if(a[l] != k)
	{
		cout << -1 << " " << -1 << endl;
		return;
	}else {
		cout << l << " ";
		l = 0, r = n - 1;
		while(l < r)
		{
			int mid = l + r + 1>> 1;
			if(a[mid] <= k)l = mid;  //找到最后一个等于该位置的数
			else r = mid - 1;
		}
		cout << l << endl;
	}
}
int main()
{
	cin >> n >> q;

	for(int i = 0;i < n;i ++)
		scanf("%d",&a[i]);
	int k = 0;
	while(q--)
	{
		cin >> k;
		merge(k);
	}
	return 0;
}

数得三次方根

对于实数域二分

  1. mid 不能采用位运算加速
  2. 若答案存在且唯一 > 和 >= 效果一致
  3. 写二分之前一定要圈定好区间和每次减半的范围
  4. 如果 r = mid; l + r >> 1;
    如果 l = mid l + r + 1 >> 1
    例子 1 2
    1 + 1 / 2 = 2 若 更新 l则不会发生改变产生死循环
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>

using namespace std;

// 实数域二分
double f(double x)
{
	return x * x * x;
}
int main()
{
	double n;
	cin >> n;
	double l = -10000, r = 10000;
	while(r - l >= 1e-7)
	{
		double mid = (l + r) / 2;
		if(f(mid) >= n)r = mid;
		else l = mid;
	}
	printf("%.6f",l);
	return 0;
}

标签:二分,Daliy,Algorithm,int,sum,51,mid,return,include
来源: https://www.cnblogs.com/wlw-x/p/12652087.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有