AcWing1221。4つの平方和(2つのポイント、yの総合的な思考を学ぶ)


タイトル意味:d d = na ab bc cを直接激しく使用します。時間計算量は、n ^ 3、タイムアウトです。
したがって、t = na a + b b&sum.y = c c + ddを使用します最初にcとdの合計を見つけ、辞書を並べ替えます。cとdのsum []。y配列でt = na abbの最初の添え字を見つけます。これが答えの
要点
です。1。1つの数値は2200であり、2200の3倍であるため、最大2つの数値を列挙します。正方形が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;
}

yTotalコード、私はBlue Bridge Cupに参加しているので、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