AcWing 1221. 四平方和 (二分,学习y总思维)

题目
关于二分的体现:
如果求的答案只有一个原则上两个模板都可以,但是check判断得是满足题意的方向走的(防止出现答案减一才是正确答案的情况)。
如果答案求出的是多个,例如本题,满足条件可以是多个数组元素,这时必须用固定的模板。

题意: 直接暴力使用 dd=n-aa-bb-cc 公式了话,时间复杂度为n^3,超时。
所以使用t=n-aa+bb & sum.y=cc+dd 。先求出c和d的sum,进行字典排序。在c和d中的sum[].y数组中找 t=n-aa-bb 第一次出现的下标,这就是答案
重点:
1.最多枚举两个数,因为一个数是2200,2200的三次方已经超过了1亿
2.用空间换时间(本题重点思路)
3.用二分找一个数 是否在 之前的数中出现过
3.如何找到字典序最小的数组

自己的超时代码,与y总的代码没有太大却别,时间复杂度都是 O(N^2logN)

#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

const int N = 2500010;

struct Sum//定义结构体 
{
    int y, e, s;
}sum[N];


int young(Sum a,Sum b){   //结构体排序算法 
    if(a.y > b.y){
        return 1;
    }
 else if(a.y < b.y){
        return -1;
    }
 else{
        if(a.e > b.e){
            return 1;
        }
  else if(a.e < b.e){
            return -1;
        }
	 else{
            if(a.s > b.s){
                return 1;
            }
   else if(a.s < b.s){
                return -1;
            }
            else return 0;
        }
    }
}

int n, m;

int main()
{
    cin >> n;

    for (int c = 0; c * c <= n; c ++ )
        for (int d = c; c * c + d * d <= n; d ++ )
        {
        	sum[m ].y= c * c + d * d;
            sum[m ].e= c;
            sum[m ++ ].s =d; //这样的 m++ 正好可以使 ,最后的m变成数组sum[]的个数 
		}
           
//对结构体进行排序 
    for(int i=0;i<m;i++){
 		for(int j=i;j<m;j++){
  			if(young(sum[i],sum[j])>0){//从小到大排序 
  				 swap(sum[i],sum[j]);
  			}
 		}
 	} 

    for (int a = 0; a * a <= n; a ++ )
        for (int b = a; a * a + b * b <= n; b ++ )
        {
            int t = n - a * a - b * b;// 此时算出的 t 是最大值,因为t=c*c+d*d。满足题意: a<b<c<d 
            int l = 0, r = m - 1;
            while (l < r)//用于找到下边最小的 t (因为下标最小,则它的后面会满足 c<d ) 
            {
                int mid = l + r >> 1;
                if (sum[mid].y >= t) r = mid;
                else l = mid + 1;
            }
            if (sum[l].y == t)
            {
                printf("%d %d %d %d\n", a, b, sum[l].e, sum[l].s);
                return 0;
            }
        }

    return 0;
}

y总的代码,因为参加蓝桥杯,所以尽量不要用 std+11 协议,自己学习一下就可以了

#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

const int N = 2500010;

struct Sum
{
    int s, c, d;
    bool operator< (const Sum &t)const
    {
        if (s != t.s) return s < t.s;
        if (c != t.c) return c < t.c;
        return d < t.d;
    }
}sum[N];

int n, m;

int main()
{
    cin >> n;

    for (int c = 0; c * c <= n; c ++ )
        for (int d = c; c * c + d * d <= n; d ++ )
            sum[m ++ ] = {c * c + d * d, c, d};

    sort(sum, sum + m);

    for (int a = 0; a * a <= n; a ++ )
        for (int b = 0; a * a + b * b <= n; b ++ )
        {
            int t = n - 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\n", a, b, sum[l].c, sum[l].d);
                return 0;
            }
        }

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值