1 solutions

  • 0
    @ 2022-10-17 8:09:47

    1.题目回顾 求abs函数的最大值,考虑贪心的思想。

    2.思路

    ①最小的放中间,两遍放最大的(顺序无关),然后再在两遍放次小的两个.....放到直到最后一个

    ②最大的放中间,两遍放最小的(顺序无关) 然后再在两遍放次大的两个.....放到直到最后一个

    ③最后一个元素需要考虑顺序的问题,贪心, 比如放的数是 1234 ,已经排好的顺序是 3 1 4,那么2就应该放在4的后面,不应该放在3的前面。

    3.卡你的地方

    ①需要开long long 。

    ②本人不会双端队列,用的vector头插尾插

    AC代码

    #include<iostream>
    #include<vector>
    #include<algorithm>
    #define ll long long
    using namespace std;
    ll n;
    vector<ll> a, b, c;
    
    ll absfun(vector<ll> x)
    {
    	ll ret = 0;
    	for (ll i = 0; i < n - 1; ++i)
    	{
    		ret += abs(x[i] - x[i + 1]);
    	}
    	return ret;
    }
    ll Min(vector<ll> a)
    {
    	ll i1 = 1, i2 = 1,flag = 0;
    	//先小
    	b.insert(b.begin(), a[0]);
    	for (ll i = 1; i < n;)
    	{
    		//先大
    		b.insert(b.begin(), a[n - i1]);
    		flag = 1;
    		i++;
    		i1++;
    		if (i == n - 1) break;
    		b.push_back(a[n - i1]);
    		flag = 2;
    		i1++;
    		i++;
    		if (i == n - 1) break;
    		//后小
    		b.insert(b.begin(), a[i2]);
    		flag = 3;
    		i2++;
    		i++;
    		if (i == n - 1) break;
    		b.push_back(a[i2]);
    		flag = 4;
    		i2++;
    		i++;
    		if (i == n - 1) break;
    	}
    	if (flag == 1)
    	{
    		if (abs(a[n-i1] - b[0]) > abs(a[n-i1] - b[b.size() - 1]))
    		{
    			b.insert(b.begin(), a[n - i1]);
    		}
    		else
    		{
    			b.push_back(a[n - i1]);
    		}
    	}
    	else if (flag == 2)
    	{
    		if (abs(a[i2] - b[0]) > abs(a[i2] - b[b.size() - 1]))
    		{
    			b.insert(b.begin(), a[i2]);
    		}
    		else
    		{
    			b.push_back(a[i2]);
    		}
    	}
    	else if (flag == 3)
    	{
    		if (abs(a[i2] - b[0]) > abs(a[i2] - b[b.size() - 1]))
    		{
    			b.insert(b.begin(), a[i2]);
    		}
    		else
    		{
    			b.push_back(a[i2]);
    		}
    	}
    	else if (flag == 4)
    	{
    		if (abs(a[n - i1] - b[0]) > abs(a[n - i1] - b[b.size() - 1]))
    		{
    			b.insert(b.begin(), a[n - i1]);
    		}
    		else
    		{
    			b.push_back(a[n-i1]);
    		}
    	}
    	return absfun(b);
    }
    
    ll Max(vector<ll> a)
    {
    	ll i1 = 0, i2 = 2, flag = 0;
    	//先大
    	c.insert(c.begin(), a[n-1]);
    	for (ll i = 1; i < n;)
    	{
    		//先小
    		c.insert(c.begin(), a[i1]);
    		flag = 1;
    		i1++;
    		i++;
    		if (i == n - 1) break;
    		c.push_back(a[i1]);
    		flag = 2;
    		i1++;
    		i++;
    		if (i == n - 1) break;
    		//后大
    		c.insert(c.begin(), a[n - i2]);
    		flag = 3;
    		i2++;
    		i++;
    		if (i == n - 1) break;
    		c.push_back(a[n - i2]);
    		flag = 4;
    		i2++;
    		i++;
    		if (i == n - 1) break;
    	}
    
    	if (flag == 1)
    	{
    		if (abs(a[i1] - c[0]) > abs(a[i1] - c[c.size() - 1]))
    		{
    			c.insert(c.begin(), a[i1]);
    		}
    		else
    		{
    			c.push_back(a[i1]);
    		}
    	}
    	else if (flag == 2)
    	{
    		if (abs(a[n-i2] - c[0]) > abs(a[n-i2] - c[c.size() - 1]))
    		{
    			c.insert(c.begin(), a[n-i2]);
    		}
    		else
    		{
    			c.push_back(a[n-i2]);
    		}
    	}
    	else if (flag == 3)
    	{
    		if (abs(a[n - i2] - c[0]) > abs(a[n - i2] - c[c.size() - 1]))
    		{
    			c.insert(c.begin(), a[n - i2]);
    		}
    		else
    		{
    			c.push_back(a[n - i2]);
    		}
    	}
    	else
    	{
    		if (abs(a[i1] - c[0]) > abs(a[i1] - c[c.size() - 1]))
    		{
    			c.insert(c.begin(), a[i1]);
    		}
    		else
    		{
    			c.push_back(a[i1]);
    		}
    	}
    	return absfun(c);
    }
    int main()
    {
    	cin >> n;
    	for (ll i = 0; i < n; ++i)
    	{
    		ll x;
    		cin >> x;
    		a.push_back(x);
    	}
    	sort(a.begin(), a.end());
    	ll ans1 = Min(a);//情况1
    	ll ans2 = Max(a);//情况2
    	cout << (ans1 > ans2 ? ans1 : ans2) << endl;
    	return 0;
    }
    
    • 1

    Information

    ID
    6628
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    8
    Tags
    # Submissions
    25
    Accepted
    6
    Uploaded By