Codeforces Global Round 9 - A. Sign Flipping (思维,贪心)

题目传送
题意:
给你一个n大小的数组(n一定为奇数),现在你可以改变其中元素的符号,现在要求你在操作后,使得有至少一半的ai <= ai+1,至少有一半ai >= ai+1,现在让你构造出这个数组

思路:
我们直接正负正负的一直下去,由于n为奇数,那么肯定就满足条件了

AC代码

#include <bits/stdc++.h>
inline long long read(){char c = getchar();long long x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x)&(-x)
const int N = 1e5 + 10;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const double EEE = exp(1);
const int mod = 1e9+7;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
signed main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    //    freopen("input.txt","r",stdin);
    //    freopen("output.txt","w",stdout);
    int t;
    cin >> t;
    while(t--)
    {
        ll n;
        cin >> n;
        ll arr[n+5] = {0};
        for(int i = 1;i <= n;i++)
        {
            cin >> arr[i];
            if(i % 2 != 0 && arr[i] < 0)
                arr[i] = arr[i]*-1;
            else if(i % 2 == 0 && arr[i] > 0)
                arr[i] = arr[i]*-1;
        }
        for(int i = 1;i <= n;i++)
            i != n ? cout << arr[i] << " " : cout << arr[i] << endl;
    }
}

猜你喜欢

转载自blog.csdn.net/moasad/article/details/107135699
今日推荐