701A - Cards - CodeForces Solution


greedy implementation *800

Please click on ads to support us..

Python Code:

card_number = int(input())
card_values = list(map(int, input().split() ))
sorted_card_values = sorted(card_values)
answer_list = []
for i in range(card_number//2):
    answer_list.append(str(card_values.index(sorted_card_values[i])+1))
    card_values[card_values.index(sorted_card_values[i])] = 0
    answer_list.append(str(card_values.index(sorted_card_values[-(i+1)])+1))
    card_values[card_values.index(sorted_card_values[-(i+1)])] = 0
    print(" ".join(answer_list))
    answer_list.clear()

C++ Code:

// Problem: A. Cards
// Contest: Codeforces - Codeforces Round #364 (Div. 2)
// URL: https://codeforces.com/contest/701/problem/A
// Memory Limit: 256 MB
// Time Limit: 1000 ms

/*
	Author: MikiMiku
	
	Observation:
	
	Idea: 
		
*/

#include <bits/stdc++.h>
using namespace std;

//#define int long long
#define endl '\n'

#define sza(x) ((int)x.size())
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back 
#define eb emplace_back 
#define mp make_pair

#define isOn(S, j) (S & (1 << j))
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define LSB(S) (S & (-S))
#define setAll(S, n) (S = (1 << n) - 1)
#define modulo(S, N) ((S) & (N - 1))   // returns S % N, where N is a power of 2
#define isPowerOfTwo(S) (!(S & (S - 1)))
#define nearestPowerOfTwo(S) ((int)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5)))
#define turnOffLastBit(S) ((S) & (S - 1))
#define turnOnLastZero(S) ((S) | (S + 1))
#define turnOffLastConsecutiveBits(S) ((S) & (S + 1))
#define turnOnLastConsecutiveZeroes(S) ((S) | (S - 1))

typedef complex<double> point;

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;

const int MAX_N = 1e5 + 5;
const ll MOD = 1e9 + 7;
const int FMOD = 998244353; 
const ll INF = 1e9;
const ld EPS = 1e-9;
const double PI=acos(-1);

#define fi first
#define se second
typedef pair<int, int> ii;  
typedef vector<ii> vii;
typedef vector<int> vi;
typedef map<int,int> mii; 

//FLOAT PRECISION SETTINGS
/*
cout.setf(ios::fixed,ios::floatfield);
cout.precision(3);
*/
//FILE IO
void setIO(string s) {
	freopen((s + ".in").c_str(), "r", stdin);
	freopen((s + ".out").c_str(), "w", stdout);
}

//........................................................................

signed main() {
	ios_base::sync_with_stdio(0);cin.tie(nullptr); cout.tie(nullptr);
	int n;
	cin >> n;
	
	map<int, vector<int>> mi;
	int sum = 0;
	for(int i = 0; i < n; ++i) {
		int ai;
		cin >> ai; sum += ai;
		mi[ai].pb(i);
	}
	
	sum /= (n/2);
	while(!mi.empty()) {
		auto it = mi.begin();
		int cur = (*it).fi;
		
		int id1 = mi[cur].back(); mi[cur].pop_back(); if(sza(mi[cur]) == 0) mi.erase(cur);
		int id2 = mi[sum - cur].back(); mi[sum - cur].pop_back(); if(sza(mi[sum - cur]) == 0) mi.erase(sum - cur);
		
		cout << id1 + 1 << " " << id2 + 1 << endl;
	}
	
	return 0;
}


Comments

Submit
0 Comments
More Questions

448A - Rewards
1622A - Construct a Rectangle
1620A - Equal or Not Equal
1517A - Sum of 2050
620A - Professor GukiZ's Robot
1342A - Road To Zero
1520A - Do Not Be Distracted
352A - Jeff and Digits
1327A - Sum of Odd Integers
1276A - As Simple as One and Two
812C - Sagheer and Nubian Market
272A - Dima and Friends
1352C - K-th Not Divisible by n
545C - Woodcutters
1528B - Kavi on Pairing Duty
339B - Xenia and Ringroad
189A - Cut Ribbon
1182A - Filling Shapes
82A - Double Cola
45A - Codecraft III
1242A - Tile Painting
1663E - Are You Safe
1663D - Is it rated - 3
1311A - Add Odd or Subtract Even
977F - Consecutive Subsequence
939A - Love Triangle
755A - PolandBall and Hypothesis
760B - Frodo and pillows
1006A - Adjacent Replacements
1195C - Basketball Exercise