http://acm.hdu.edu.cn/showproblem.php?pid=4163

稳定排序

View Code
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <stack>
#include <queue>
#include <map>
using namespace std;
typedef struct L{
    int price;
    int idx;
}L;
L kk[1000001];
int cmp1(const void*a,const void*b)
{
    L*c=(L*)a;
    L*d=(L*)b;
    if(c->price==d->price)
        return c->idx-d->idx;
    return c->price-d->price;
}
int cmp2(const void*a,const void*b)
{
    L*c=(L*)a;
    L*d=(L*)b;
    return c->idx-d->idx;
}
int cmp3(const void*a,const void*b)
{
    L*c=(L*)a;
    L*d=(L*)b;
    if(c->price==d->price)
        return d->idx-c->idx;
    return d->price-c->price;
}
int cmp4(const void*a,const void*b)
{
    L*c=(L*)a;
    L*d=(L*)b;
    return d->idx-c->idx;
}
int main()
{
    int n,k1,k2;
    int nCase=1;
    while(~scanf("%d%d%d",&n,&k1,&k2),n,k1,k2)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d",&kk[i].price);
            kk[i].idx=i+1;
        }
        printf("Case %d\n",nCase++);
        qsort(kk,n,sizeof(L),cmp1);
        qsort(kk,k1,sizeof(L),cmp2);
        printf("%d",kk[0].idx);
        for(int i=1;i<k1;i++)
            printf(" %d",kk[i].idx);
        putchar('\n');
        qsort(kk,n,sizeof(L),cmp3);
        qsort(kk,k2,sizeof(L),cmp4);
        printf("%d",kk[0].idx);
        for(int i=1;i<k2;i++)
            printf(" %d",kk[i].idx);
        putchar('\n');
    }
    return 0;
}