zoukankan      html  css  js  c++  java
  • Codeforces Round #364 (Div. 2)->A. Cards

    A. Cards
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    There are n cards (n is even) in the deck. Each card has a positive integer written on it. n / 2 people will play new card game. At the beginning of the game each player gets two cards, each card is given to exactly one player.

    Find the way to distribute cards such that the sum of values written of the cards will be equal for each player. It is guaranteed that it is always possible.

    Input

    The first line of the input contains integer n (2 ≤ n ≤ 100) — the number of cards in the deck. It is guaranteed that n is even.

    The second line contains the sequence of n positive integers a1, a2, ..., an (1 ≤ ai ≤ 100), where ai is equal to the number written on the i-th card.

    Output

    Print n / 2 pairs of integers, the i-th pair denote the cards that should be given to the i-th player. Each card should be given to exactly one player. Cards are numbered in the order they appear in the input.

    It is guaranteed that solution exists. If there are several correct answers, you are allowed to print any of them.

    Examples
    input
    6
    1 5 7 4 4 3
    output
    1 3
    6 2
    4 5
    input
    4
    10 10 10 10
    output
    1 2
    3 4
    Note

    In the first sample, cards are distributed in such a way that each player has the sum of numbers written on his cards equal to 8.

    In the second sample, all values ai are equal. Thus, any distribution is acceptable.

    题意:n是偶数,组成n/2组人,每人一张牌,使得每组的牌总和差值尽可能小

    思路:排序,然后第一个跟最后一个组合,第二个跟倒数第二个组合,以此类推,输出牌原来的位置

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 struct Node{
     4     int id;
     5     int num;
     6 }a[101];
     7 bool cmp(Node a,Node b){
     8     return a.num<b.num;
     9 }
    10 int main(){
    11     int n;
    12     cin>>n;
    13     for(int i=0;i<n;i++){
    14             cin>>a[i].num;
    15             a[i].id=i+1;
    16     }
    17     sort(a,a+n,cmp);
    18     int i,j;
    19     for(i=0,j=n-1;i<n/2,j>=n/2;i++,j--)
    20     {
    21         printf("%d %d
    ",a[i].id,a[j].id);
    22     }
    23     return 0;
    24 }
  • 相关阅读:
    《花好月圆夜》
    关于Url重写
    三大WEB服务器对比分析(apache ,lighttpd,nginx)
    APC 和 Memcache 有什么区别,哪个更好效率更高?
    URL优化不仅仅是静态化重写URL
    php中的静态变量和动态变量的区别框架加载变量时运用
    msicuu.exe (msizap.exe),程序的作用
    显示器接口针脚定义(Dsub15)
    图文教程:DIY全屏开机LOGO详解
    ASP.NET中的媒体播放
  • 原文地址:https://www.cnblogs.com/zhien-aa/p/5800958.html
Copyright © 2011-2022 走看看