Codeforces 239D Boring Partition【思维】

来源:互联网 发布:计算量子电路矩阵 编辑:程序博客网 时间:2024/05/16 12:26

D. Boring Partition
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

This problem is the most boring one you've ever seen.

Given a sequence of integers a1, a2, ..., an and a non-negative integer h, our goal is to partition the sequence into two subsequences (not necessarily consist of continuous elements). Each element of the original sequence should be contained in exactly one of the result subsequences. Note, that one of the result subsequences can be empty.

Let's define function f(ai, aj) on pairs of distinct elements (that is i ≠ j) in the original sequence. If ai and aj are in the same subsequence in the current partition then f(ai, aj) = ai + aj otherwise f(ai, aj) = ai + aj + h.

Consider all possible values of the function f for some partition. We'll call the goodness of this partiotion the difference between the maximum value of function f and the minimum value of function f.

Your task is to find a partition of the given sequence a that have the minimal possible goodness among all possible partitions.

Input

The first line of input contains integers n and h (2 ≤ n ≤ 105, 0 ≤ h ≤ 108). In the second line there is a list of n space-separated integers representing a1, a2, ..., an (0 ≤ ai ≤ 108).

Output

The first line of output should contain the required minimum goodness.

The second line describes the optimal partition. You should print n whitespace-separated integers in the second line. The i-th integer is1 if ai is in the first subsequence otherwise it should be 2.

If there are several possible correct answers you are allowed to print any of them.

Examples
input
3 21 2 3
output
11 2 2 
input
5 100 1 0 2 1
output
32 2 2 2 2 
Note

In the first sample the values of f are as follows: f(1, 2) = 1 + 2 + 2 = 5f(1, 3) = 1 + 3 + 2 = 6 and f(2, 3) = 2 + 3 = 5. So the difference between maximum and minimum values of f is 1.

In the second sample the value of h is large, so it's better for one of the sub-sequences to be empty.


题目大意:
给出N个数,以及一个常数H,现在让我们将这个数组分成两个子部分,使得Max[F(ai,aj)]-Min[F(ai,aj)]的值最小。
这里F(ai,aj)=如果ai和aj在一个部分中:ai+aj,否则为:ai+aj+H
子部分可以为空

思路:

假设我们有一个长度为7的序列,此时我将其排序按照从大到小排序有:
A1 A2 A3 A4 A5 A6 A7


①我们挪动任意一个数A1~An5 之内的数都是只能使得结果maxn更大,而且并不能使得minn增大。


②当我们挪动两个数到另一个序列的时候也是同理。


③那么我们考虑只挪动最后两个数,当我们挪动倒数第二个数的时候


minn=min(A5+A7,A6+A7+H);显然是增大了minn的。
maxn=max(A1+A2,A1+A6+H);显然maxn是没有增大的,除非H很大。所以能够使得结果更优。


④如果我们挪动最后一个数的时候:
minn=min(A5+A6,A7+A6+H);显然A5+A6>=A5+A7。
maxn=max(A1+A2,A1+A7+H);显然A1+A7+H<=A1+A6+H;


所以挪动最后一个数肯定比挪动第一个数更好,所以我们这个问题一共有两种分配序列的方法得到最优:
1.一个空集合,另外一个就是序列本身
2.两个集合都非空,第一个集合只有序列中最小的元素min[ai],另外一个集合是剩下的所有元素


Ac代码:

#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>using namespace std;struct node{    int val,pos;}a[150000];int cmp(node a,node b){    return a.val<b.val;}int main(){    int n,h;    while(~scanf("%d%d",&n,&h))    {        for(int i=1;i<=n;i++)scanf("%d",&a[i].val),a[i].pos=i;        if(n==2)        {            printf("0\n");            printf("1 1\n");            continue;        }        sort(a+1,a+1+n,cmp);        int ans1=a[n].val+a[n-1].val-(a[1].val+a[2].val);        int ans2=max(a[n].val+a[1].val+h,a[n].val+a[n-1].val)-min(a[1].val+a[2].val+h,a[2].val+a[3].val);        printf("%d\n",min(ans1,ans2));        if(ans1<ans2)        {            for(int i=1;i<=n;i++)printf("1 ");            printf("\n");        }        else        {            for(int i=1;i<=n;i++)            {                if(i==a[1].pos)printf("2 ");                else printf("1 ");            }            printf("\n");        }    }}