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

题目
题意: 直接暴力使用 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;
}

猜你喜欢

转载自blog.csdn.net/qq_47874905/article/details/114999348
今日推荐