Election Time [Jeffrey Wang, 2007]

The cows are having their first election after overthrowing the tyrannical Farmer John, and Bessie is one of N cows (1 <= N <= 50,000) running for President. Before the election actually happens, however, Bessie wants to determine who has the best chance of winning.
    The election consists of two rounds. In the first round, the K cows (1 <= K <= N) cows with the most votes advance to the second round. In the second round, the cow with the most votes becomes President.
    Given that cow i expects to get Ai votes (1 <= Ai <= 1,000,000,000) in the first round and Bi votes (1 <= Bi <= 1,000,000,000) in the second round (if he or she makes it), determine which cow is expected to win the election. Happily for you, no vote count appears twice in the Ai list; likewise, no vote count appears twice in the Bi list.

Дадена е редица от n на брой двойки числа. Измежду най-големите k числа, сортирани по първия елемент, да се намери номера на члена на редицата с най-голям втори елемент.


Домашно: Да се напише програмата на C++ с използване на потокови операции за вход и изход и сортировка от STL. Да се сравнят бързодействията на двете програми.


INPUT FORMAT:
* Line 1: Two space-separated integers: N and K

* Lines 2..N+1: Line i+1 contains two space-separated integers: Ai and Bi

SAMPLE INPUT (file elect.in):
5 3
3 10
9 2
5 6
8 4
6 5

INPUT DETAILS:
There are 5 cows, 3 of which will advance to the second round. The cows expect to get 3, 9, 5, 8, and 6 votes, respectively, in the first round and 10, 2, 6, 4, and 5 votes, respectively, in the second.

OUTPUT FORMAT:
* Line 1: The index of the cow that is expected to win the election.

SAMPLE OUTPUT (file elect.out):
5

OUTPUT DETAILS:
Cows 2, 4, and 5 advance to the second round; cow 5 gets 5 votes in the  second round, winning the election.

Problem where the goal is simply to follow the rules of the task description. This one is slightly complex owing to the requirement for a 'sort' routine. C and C++ programmers have easily used built-in sort routines (e.g., qsort).
Furthermore, the sort routine has to be able to sort one number while carrying two other numbers in the exchanges. The program below is a simple demonstration of how qsort can be used to perform such operations. Java and Pascal programmers had merely to augment their variable-swap routines to swap three variables. Some folks wrote two separate but similar sort routines; some used an if statement to differentiate variable for testing.
Complete credit relied on an O(NlogN) solution; O(N2) sorts were not fast enough to get full points.


#include <stdio.h>
#include <stdlib.h>

struct vote_f {
    int a;
    int b;
    int cownum;
} votes[50000];

comparea (struct vote_f *a, struct vote_f *b) { return b->a - a->a; }
compareb (struct vote_f *a, struct vote_f *b) { return b->b - a->b; }

main() {
    FILE *fin  = fopen ("elect.in", "r");
    FILE *fout = fopen ("elect.out", "w");
    int n, k, i;

    fscanf (fin, "%d %d", &n, &k);
    for (i = 0; i < n; i++) {
        fscanf (fin, "%d %d", &votes[i].a, &votes[i].b);
        votes[i].cownum = i;
    }
    qsort(votes, n, sizeof (struct vote_f), comparea);
    qsort(votes, k, sizeof (struct vote_f), compareb);
    fprintf (fout, "%d\n", votes[0].cownum+1);
    exit (0);
}


Решение на Борис Тодоров на С++:

#include <iostream>

#include <algorithm>
#include <vector>
using namespace std;

class Node;
bool operator>>(istream &in, Node &a);
ostream &operator<<(ostream &out, Node &a);
bool comp1(Node a, Node b);
bool comp2(Node a, Node b);

class Node
{
   public:
      Node(int a1=0, int a2=0)
      { a=a1; b=a2; }
      int get_a()
      { return a; }
      int get_b()
      { return b; }
      void set_a(int a1=0)
      { a=a1; }
      void set_b(int a1=0)
      { b=a1; }
   private:
     int a,b;
};

bool operator>>(istream &in, Node &a)
{
   int c1,c2;
   in >> c1 >> c2;
   if(in.fail()) return false;
   a.set_a(c1);
   a.set_b(c2);
   return true;
}

ostream &operator<<(ostream &out, Node &a)
{
   out << a.get_a() << " " << a.get_b();
   return out;
}

bool comp1(Node a, Node b)
{
   if(a.get_a()<b.get_a())
     return true;
   return false;
}

bool comp2(Node a, Node b)
{
  if(a.get_b()<b.get_b())
     return true;
   return false;
}

int main()
{
  int n,k;
  cin >> n >> k;
  vector<Node> Seq;
  Node current;
  while(cin >> current)
     Seq.push_back(Node(current.get_a(),current.get_b()));

  sort(Seq.begin(), Seq.end(), comp1);

  vector<Node> Seq2;
  int l=0;
  for(int i=Seq.size()-1; l<k; i--,l++)
        Seq2.push_back(Node(Seq[i].get_a(),Seq[i].get_b()));
 
  sort(Seq2.begin(),Seq2.end(),comp2);
 
  cout << endl << Seq2[Seq2.size()-1] << endl; 
  return 0;
}