CodeForces 149C - Division into Teams(贪心)

来源:互联网 发布:七月算法 百度云 编辑:程序博客网 时间:2021/10/28 21:33

C. Division into Teams
time limit per test
2 seconds
memory limit per test
256 megabytes
standard input
standard output

Petya loves football very much, especially when his parents aren't home. Each morning he comes to the yard, gathers his friends and they play all day. From time to time they have a break to have some food or do some chores (for example, water the flowers).

The key in football is to divide into teams fairly before the game begins. There are n boys playing football in the yard (including Petya), each boy's football playing skill is expressed with a non-negative characteristic ai (the larger it is, the better the boy plays).

Let's denote the number of players in the first team as x, the number of players in the second team as y, the individual numbers of boys who play for the first team as pi and the individual numbers of boys who play for the second team as qi. Division n boys into two teams is considered fair if three conditions are fulfilled:

  • Each boy plays for exactly one team (x + y = n).
  • The sizes of teams differ in no more than one (|x - y| ≤ 1).
  • The total football playing skills for two teams differ in no more than by the value of skill the best player in the yard has. More formally:

Your task is to help guys divide into two teams fairly. It is guaranteed that a fair division into two teams always exists.


The first line contains the only integer n (2 ≤ n ≤ 105) which represents the number of guys in the yard. The next line contains n positive space-separated integers, ai (1 ≤ ai ≤ 104), the i-th number represents the i-th boy's playing skills.


On the first line print an integer x — the number of boys playing for the first team. On the second line print x integers — the individual numbers of boys playing for the first team. On the third line print an integer y — the number of boys playing for the second team, on the fourth line print y integers — the individual numbers of boys playing for the second team. Don't forget that you should fulfil all three conditions: x + y = n|x - y| ≤ 1, and the condition that limits the total skills.

If there are multiple ways to solve the problem, print any of them.

The boys are numbered starting from one in the order in which their skills are given in the input data. You are allowed to print individual numbers of boys who belong to the same team in any order.

Sample test(s)
31 2 1
21 2 13 
52 3 3 1 1
34 1 3 25 2 

Let's consider the first sample test. There we send the first and the second boy to the first team and the third boy to the second team. Let's check all three conditions of a fair division. The first limitation is fulfilled (all boys play), the second limitation on the sizes of groups (|2 - 1| = 1 ≤ 1) is fulfilled, the third limitation on the difference in skills ((2 + 1) - (1) = 2 ≤ 2) is fulfilled.



#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;struct player{    int num;    int value;}s[111111];bool cmp(player a,player b){    return a.value>b.value;}int main(){    int n,va=0,vb=0,a[55555],b[55555],flag=1,ok=0;    cin>>n;    for(int i=0;i<n;i++)    {        scanf("%d",&s[i].value);        s[i].num=i+1;    }    sort(s,s+n,cmp);    int j=0,k=0;    for(int i=1;i<n;i++)    {        if(flag)        {            va+=s[i].value;            a[j]=s[i].num;            j++;        }        else        {            vb+=s[i].value;            b[k]=s[i].num;            k++;        }        flag=!flag;    }    if(flag)    {        if(va<vb)        {            a[j]=s[0].num;            ok=1;        }        else b[k]=s[0].num;    }    else b[k]=s[0].num;    if(ok)    {        cout<<(n+1)/2<<endl;        for(int i=0;i<=j;i++)            cout<<a[i]<<" ";        cout<<endl;        cout<<n/2<<endl;        for(int i=0;i<k;i++)            cout<<b[i]<<" ";        cout<<endl;    }    else    {        cout<<(n+1)/2<<endl;        for(int i=0;i<=k;i++)            cout<<b[i]<<" ";        cout<<endl;        cout<<n/2<<endl;        for(int i=0;i<j;i++)            cout<<a[i]<<" ";        cout<<endl;    }    return 0;}