답안 #151691

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
151691 2019-09-04T07:45:58 Z gs14004 포도주 시음 (FXCUP4_wine) C++17
100 / 100
12 ms 1024 KB
#include "bartender.h"
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
using namespace std;
using lint = long long;

namespace {
	lint encode(vector<int> v){
		lint tot = 0;
		int cnt = 0;
		for(int i=0; i<sz(v); i++){
			int rk = 0;
			for(int j=0; j<i; j++){
				if(v[j] < v[i]) rk++;
			}
			cnt++;
			tot = tot * cnt + rk;
		}
		return tot;
	}
	vector<int> decode(lint w, int n){
		vector<int> v(n);
		vector<int> lst(n);
		iota(lst.begin(), lst.end(), 1);
		for(int i=n-1; i>=0; i--){
			int md = w % (i + 1);
			w /= i + 1;
			v[i] = lst[md];
			lst.erase(lst.begin() + md);
		}
		return v;
	}
}

std::vector<int> BlendWines(int K, std::vector<int> R){
	int N = R.size();
	vector<int> tmp;
	for(int i=0; i<N; i++){
		if(R[i] > 12) tmp.push_back(R[i] - 12);
	}
	lint tot = encode(tmp);
	vector<int> ans(N);
	for(int i=0; i<N; i++){
		if(R[i] <= 12) ans[i] = 1 + (tot >> (R[i] - 1)) % 2;
	}
	tot >>= 12;
	for(int i=0; i<N; i++){
		if(R[i] > 12){
			ans[i] = 3 + (tot % 5);
			tot /= 5;
		}
	}
	return ans;
}
#include "taster.h"
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
using namespace std;
using lint = long long;

namespace {
	lint encode(vector<int> v){
		lint tot = 0;
		int cnt = 0;
		for(int i=0; i<sz(v); i++){
			int rk = 0;
			for(int j=0; j<i; j++){
				if(v[j] < v[i]) rk++;
			}
			cnt++;
			tot = tot * cnt + rk;
		}
		return tot;
	}
	vector<int> decode(lint w, int n){
		vector<int> v(n);
		vector<int> lst(n);
		iota(lst.begin(), lst.end(), 1);
		for(int i=n-1; i>=0; i--){
			int md = w % (i + 1);
			w /= i + 1;
			v[i] = lst[md];
			lst.erase(lst.begin() + md);
		}
		return v;
	}
}

vector<int> mis(vector<int> v){
	if(sz(v) <= 1) return v;
	vector<int> LO, HI;
	for(int i=0; i+1<sz(v); i+=2){
		if(Compare(v[i], v[i + 1]) > 0) swap(v[i], v[i + 1]);
		LO.push_back(v[i]);
		HI.push_back(v[i + 1]);
	}
	if(sz(v) & 1) LO.push_back(v.back());
	HI = mis(HI);
	auto find_match_index = [&](int x){
		int pos = find(v.begin(), v.end(), x) - v.begin();
		pos ^= 1;
		if(pos >= sz(v)) return 987654321;
		int ret = find(HI.begin(), HI.end(), v[pos]) - HI.begin();
		return ret;
	};
	sort(LO.begin(), LO.end(), [&](const int &a, const int &b){
		return find_match_index(a) < find_match_index(b);
	});
	HI.insert(HI.begin(), LO[0]); 
	LO.erase(LO.begin());
	for(auto i : {1, 0, 3, 2, 5, 4}){
		if(i >= sz(LO)) continue;
		int thres = find_match_index(LO[i]);
		thres = min(thres, sz(HI));
		int s = 0, e = thres;
		while(s != e){
			int m = (s+e)/2;
			if(Compare(HI[m], LO[i]) > 0) e = m;
			else s = m + 1;
		}
		HI.insert(HI.begin() + s, LO[i]);
	}
	return HI;
}

vector<int> get_order(vector<int> pos, int n){
	pos = mis(pos);
	vector<int> dap(n);
	for(int i=0; i<sz(pos); i++) dap[pos[i]] = i + 1;
	return dap;
}

std::vector<int> SortWines(int K, std::vector<int> A) {
	int N = sz(A);
	lint tot = 0;
	for(int i=N-1; i>=0; i--){
		if(A[i] > 2){
			tot = tot * 5 + (A[i] - 3);
		}
	}
	tot <<= 12;
	vector<int> find_ord;
	for(int i=0; i<N; i++){
		if(A[i] <= 2){
			find_ord.push_back(i);
		}
	}
	vector<int> ord = get_order(find_ord, N);
	vector<int> ans(N);
	for(int i=0; i<N; i++){
		if(A[i] <= 2){
			ans[i] = ord[i];
			if(A[i] == 2) tot += (1 << (ord[i] - 1));
		}
	}
	if(N > 12){
		auto dec = decode(tot, N - 12);
		reverse(dec.begin(), dec.end());
		for(int i=0; i<N; i++){
			if(A[i] > 2){
				ans[i] = dec.back() + 12;
				dec.pop_back();
			}
		}
	}
	return ans;
}

Compilation message

bartender.cpp:21:14: warning: 'std::vector<int> {anonymous}::decode(lint, int)' defined but not used [-Wunused-function]
  vector<int> decode(lint w, int n){
              ^~~~~~

taster.cpp:8:7: warning: 'lint {anonymous}::encode(std::vector<int>)' defined but not used [-Wunused-function]
  lint encode(vector<int> v){
       ^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 644 KB Correct
2 Correct 9 ms 1024 KB Correct
3 Correct 9 ms 772 KB Correct
4 Correct 9 ms 772 KB Correct
5 Correct 10 ms 772 KB Correct
6 Correct 10 ms 908 KB Correct
7 Correct 11 ms 772 KB Correct
8 Correct 9 ms 908 KB Correct
9 Correct 12 ms 644 KB Correct
10 Correct 10 ms 884 KB Correct
11 Correct 10 ms 908 KB Correct
12 Correct 10 ms 644 KB Correct
13 Correct 10 ms 732 KB Correct
14 Correct 9 ms 908 KB Correct
15 Correct 10 ms 644 KB Correct
16 Correct 9 ms 772 KB Correct
17 Correct 8 ms 644 KB Correct
18 Correct 10 ms 772 KB Correct
19 Correct 10 ms 780 KB Correct
20 Correct 10 ms 644 KB Correct
21 Correct 10 ms 772 KB Correct
22 Correct 11 ms 804 KB Correct
23 Correct 9 ms 784 KB Correct
24 Correct 11 ms 772 KB Correct
25 Correct 10 ms 780 KB Correct
26 Correct 10 ms 772 KB Correct
27 Correct 10 ms 1024 KB Correct
28 Correct 10 ms 772 KB Correct
29 Correct 10 ms 780 KB Correct
30 Correct 10 ms 772 KB Correct
31 Correct 10 ms 776 KB Correct
32 Correct 10 ms 1016 KB Correct
33 Correct 10 ms 644 KB Correct
34 Correct 9 ms 992 KB Correct
35 Correct 10 ms 908 KB Correct
36 Correct 10 ms 960 KB Correct
37 Correct 11 ms 792 KB Correct
38 Correct 10 ms 644 KB Correct
39 Correct 9 ms 1020 KB Correct
40 Correct 9 ms 780 KB Correct
41 Correct 10 ms 908 KB Correct
42 Correct 10 ms 676 KB Correct
43 Correct 10 ms 908 KB Correct
44 Correct 10 ms 884 KB Correct
45 Correct 10 ms 772 KB Correct
46 Correct 10 ms 644 KB Correct
47 Correct 9 ms 772 KB Correct
48 Correct 10 ms 908 KB Correct
49 Correct 10 ms 772 KB Correct
50 Correct 9 ms 908 KB Correct
51 Correct 10 ms 780 KB Correct
52 Correct 10 ms 644 KB Correct
53 Correct 9 ms 980 KB Correct
54 Correct 9 ms 644 KB Correct
55 Correct 11 ms 772 KB Correct
56 Correct 10 ms 772 KB Correct
57 Correct 10 ms 772 KB Correct
58 Correct 9 ms 644 KB Correct
59 Correct 8 ms 772 KB Correct
60 Correct 9 ms 772 KB Correct
61 Correct 10 ms 772 KB Correct
62 Correct 9 ms 804 KB Correct
63 Correct 10 ms 908 KB Correct
64 Correct 10 ms 960 KB Correct
65 Correct 10 ms 644 KB Correct
66 Correct 10 ms 1024 KB Correct
67 Correct 10 ms 908 KB Correct
68 Correct 10 ms 964 KB Correct
69 Correct 10 ms 772 KB Correct
70 Correct 9 ms 772 KB Correct
71 Correct 9 ms 772 KB Correct
72 Correct 10 ms 728 KB Correct
73 Correct 11 ms 772 KB Correct
74 Correct 10 ms 772 KB Correct
75 Correct 10 ms 908 KB Correct
76 Correct 10 ms 772 KB Correct
77 Correct 10 ms 644 KB Correct
78 Correct 10 ms 644 KB Correct
79 Correct 11 ms 804 KB Correct
80 Correct 10 ms 772 KB Correct
81 Correct 10 ms 772 KB Correct
82 Correct 10 ms 908 KB Correct
83 Correct 10 ms 772 KB Correct
84 Correct 9 ms 772 KB Correct
85 Correct 10 ms 780 KB Correct
86 Correct 8 ms 772 KB Correct
87 Correct 10 ms 908 KB Correct
88 Correct 10 ms 884 KB Correct
89 Correct 10 ms 624 KB Correct
90 Correct 10 ms 908 KB Correct
91 Correct 11 ms 772 KB Correct
92 Correct 9 ms 772 KB Correct
93 Correct 10 ms 644 KB Correct
94 Correct 10 ms 644 KB Correct
95 Correct 10 ms 908 KB Correct
96 Correct 10 ms 772 KB Correct
97 Correct 10 ms 772 KB Correct
98 Correct 10 ms 772 KB Correct
99 Correct 10 ms 772 KB Correct
100 Correct 10 ms 772 KB Correct
101 Correct 10 ms 772 KB Correct
102 Correct 10 ms 908 KB Correct
103 Correct 9 ms 772 KB Correct
104 Correct 10 ms 732 KB Correct
105 Correct 9 ms 908 KB Correct
106 Correct 9 ms 780 KB Correct
107 Correct 10 ms 940 KB Correct
108 Correct 10 ms 772 KB Correct
109 Correct 9 ms 908 KB Correct
110 Correct 10 ms 772 KB Correct
111 Correct 11 ms 908 KB Correct
112 Correct 10 ms 756 KB Correct
113 Correct 10 ms 644 KB Correct
114 Correct 10 ms 644 KB Correct
115 Correct 10 ms 772 KB Correct
116 Correct 10 ms 1008 KB Correct
117 Correct 9 ms 644 KB Correct
118 Correct 9 ms 908 KB Correct
119 Correct 8 ms 772 KB Correct
120 Correct 10 ms 1008 KB Correct