CodeForces - 1419D1

题目链接:Problem - 1419D1 - Codeforces

因为题目说了只可能是最优选择, 所以能获取的冰球数量稍微推理一下就能得到, 公式是(n-1)/2, 注意就算n为偶数也遵守这个公式, 因为最左最右的冰球都不能买.

关于冰球的排列, 我们按照样例把他拆分, 输出样例是

2
3 1 4 2 5 

观察偶数位是: 1 2

观察奇数位是: 3 4 5

显而易见, 将原序列排序后的规律就是 偶数序列+奇数序列 , 之后将这两个序列放在同一个数组中即可ac

ac代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <string>
#include <stack>
#include <deque>
#include <map>
#include <set>
using namespace std;
#define ll long long
#define endl "\n"
#define rep(i, a, b) for (ll i = (a); i <= (b); i++)
#define repr(i, a, b) for (ll i = (a); i < (b); i++)
#define rrep(i, a, b) for (ll i = (b); i >= (a); i--)
#define rrepr(i, a, b) for (ll i = (b); i > (a); i--)
#define min(a,b) (a)<(b)?(a):(b)
#define max(a,b) (a)>(b)?(a):(b)

ll cnt,n,m,t,ant;
const int N=1e5+10;
ll arr[N];
ll ans[N];
string str;

inline ll read()
{
    char c = getchar();int 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;
}


void solve()
{
    n=read();
    rep(i,1,n) arr[i]=read();
    sort(arr+1,arr+1+n);
    cnt=(n-1)/2;
    cout<<cnt<<endl;
    //先填小的数字在偶数位
    //再填大的数字在奇数位
    ant=1;
    for(ll i=2;i<=n;i+=2)
        ans[i]=arr[ant++];
    for(ll i=1;i<=n;i+=2)
        ans[i]=arr[ant++];
    rep(i,1,n) cout<<ans[i]<<" ";
    puts("");
    
    return;
}

int main()
{
    solve();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值