Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <list>
- #include <map>
- #include <set>
- #include <queue>
- #include <deque>
- #include <stack>
- #include <bitset>
- #include <algorithm>
- #include <functional>
- #include <numeric>
- #include <utility>
- #include <sstream>
- #include <iostream>
- #include <iomanip>
- #include <cstdio>
- #include <cmath>
- #include <cstdlib>
- #include <cstring>
- #include <queue>
- #include <ctime>
- #include <cassert>
- #include <climits>
- #include <limits>
- using namespace std;
- typedef vector <int> vi;
- typedef pair< int ,int > ii;
- #define S(a) scanf("%d",&(a))
- #define P(a) printf("%d",(a))
- #define PS printf(" ")
- #define NL printf("\n")
- #define SL(a) scanf("%lld",&(a))
- #define PL(a) printf("%lld",(a))
- #define LL long long int
- #define FOR(I,A,B) for(int I= (A); I<(B); ++I)
- #define all(c) c.begin(), c.end()
- #define stop system("pause")
- #define pb push_back
- #define mp make_pair
- #define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
- #define INDEX(arr,ind) (lower_bound(all(arr),ind)-arr.begin())
- #define ff first
- #define ss second
- #define imax numeric_limits<int>::max()
- #define imin numeric_limits<int>::min()
- #define lmax numeric_limits<ll>::max()
- #define lmin numeric_limits<ll>::min()
- int ar[1010];
- struct node{
- int x;
- int y;
- int val;
- };
- vector<node> dist;
- vector<node> store[25];
- map<int,int> mark;
- void init(){
- mark.clear();
- dist.clear();
- FOR(i,0,25)
- store[i].clear();
- }
- void do_sort(){ //linear-time sorting
- int sz = dist.size();
- FOR(i,0,sz){
- store[dist[i].val].pb(dist[i]);
- }
- dist.clear();
- FOR(i,0,25){
- int sz = store[i].size();
- FOR(j,0,sz){
- dist.pb(store[i][j]);
- }
- }
- }
- int main(){
- int n;
- int flag = 0;
- while(S(n)==1){
- if(flag)
- NL;
- init();
- FOR(i,0,n){
- S(ar[i]);
- mark[ar[i]]++; //mark stores the frequency of each element
- }
- FOR(i,0,n){
- FOR(j,i+1,n){
- int value = min(abs(ar[i]-ar[j]),24-abs(ar[i]-ar[j]));
- node temp;
- temp.x = ar[i];
- temp.y = ar[j];
- temp.val = value;
- dist.pb(temp);
- }
- }
- do_sort(); //sort the distances
- int ct = 0;
- int ans = 0;
- int i = 0;
- while(ct<n/2){ //select N/2 distances
- if(mark[dist[i].x]==0 || mark[dist[i].y]==0 || (dist[i].x == dist[i].y && mark[dist[i].x]<=1) ){
- i++;
- }
- else{
- ans +=dist[i].val;
- mark[dist[i].x]--;
- mark[dist[i].y]--;
- ct++;
- i++;
- }
- }
- P(ans);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement