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

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;
}

猜你喜欢

转载自www.cnblogs.com/wlw-x/p/12652087.html