CODE FESTIVAL 2016 Final

Submission #994517

Source codeソースコード

// tsukasa_diary's programing contest code template
#include <bits/stdc++.h>
using namespace std;
#define TSUKASA_DIARY_S_TEMPLATE
// define
#define for_(i,a,b) for(int i=(a);i<(b);++i)
#define for_rev(i,a,b) for(int i=(a);i>=(b);--i)
#define allof(a) (a).begin(),(a).end()
#define minit(a,b) memset(a,b,sizeof(a))
#define size_of(a) int((a).size())
#define cauto const auto
// typedef
typedef long long lint;
typedef double Double;
typedef pair< int, int > pii;
template< typename T > using Vec = vector< T >;
template< typename T > using Matrix = Vec< Vec< T > >;
template< typename T > using USet = unordered_set< T >;
template< typename T, class C > using MyUSet = unordered_set< T, C >;
template< typename T, typename F > using UMap = unordered_map< T, F >;
template< typename T, typename F, class C > using MyUMap = unordered_map< T, F, C >;
// hash
class PiiHash { public: size_t operator () (const pii& p) const { return (p.first << 16) | p.second; } };
// popcount
inline int POPCNT(int x) { return __builtin_popcount(x); }
inline int POPCNT(lint x) { return __builtin_popcount(x); }
// inf
const int iINF = 1L << 30;
const lint lINF = 1LL << 60;
// eps
const Double EPS = 1e-9;
const Double PI = acos(-1);
// inrange
template< typename T >
inline bool in_range(T v, T mi, T mx) { return mi <= v && v < mx; }
template< typename T >
inline bool in_range(T x, T y, T W, T H) { return in_range(x,0,W) && in_range(y,0,H); }
// neighbor clockwise
const int DX[4] = {0,1,0,-1}, DY[4] = {-1,0,1,0};
const int DX_[8] = {0,1,1,1,0,-1,-1,-1}, DY_[8] = {-1,-1,0,1,1,1,0,-1};
// variable update
template< typename T > inline void modAdd(T& a, T b, T mod) { a = (a + b) % mod; }
template< typename T > inline void modMul(T& a, T b, T mod) { a = (a * b) % mod; }
template< typename T > inline void minUpdate(T& a, T b) { a = min(a, b); }
template< typename T > inline void maxUpdate(T& a, T b) { a = max(a, b); }
// converter
template< typename F, typename T >
inline void convert(F& from, T& to) {
	stringstream ss;
	ss << from; ss >> to;
}


int N, M, X[100010];
const int MAX_X = 100000;

void solve() {
	Vec< int > cnt(MAX_X + 1, 0);
	for_(i,0,N) cnt[X[i]]++;
	
	Vec< priority_queue< int, Vec< int >, greater< int > > > vque(M);
	//Vec< int > odd_cnt(M, 0);
	for_rev(x,MAX_X,1) {
		if (cnt[x] > 0) {
			if (cnt[x] % 2) {
				vque[x % M].push(1);
				vque[x % M].push(cnt[x] - 1);
				//++odd_cnt[x % M];
			} else {
				vque[x % M].push(cnt[x]);				
			}
		}
	}
	
	int ans = 0;
	
	for_(x,0,M) {
		int a = x, b = (M - a) % M;
		//cerr << a << " " << b << endl;
		if (a == b) {
			/*
			ans += odd_cnt[a] / 2; odd_cnt[a] %= 2;
			for_(i,0,odd_cnt[a]) vque[a].push(1);
			odd_cnt[a] = 0;
			*/
			while (vque[a].size() > 1) {
				int t = vque[a].top(); vque[a].pop();
				int s = vque[a].top(); vque[a].pop();
				ans += t;
				if (s - t > 0) vque[a].push(s - t);
			}
		} else {
			/*
			int add = min(odd_cnt[a], odd_cnt[b]);
			ans += add;
			odd_cnt[a] -= add;
			odd_cnt[b] -= add;
			for_(i,0,odd_cnt[a]) vque[a].push(1);
			for_(i,0,odd_cnt[b]) vque[a].push(1);
			odd_cnt[a] = odd_cnt[b] = 0;
			*/
			while (!vque[a].empty() && !vque[b].empty()) {
				int ta = vque[a].top(); vque[a].pop();
				int tb = vque[b].top(); vque[b].pop();
				int sub = min(ta, tb);
				ans += sub;
				if (ta - sub > 0) vque[a].push(ta - sub);
				if (tb - sub > 0) vque[b].push(tb - sub);
			}
		}
		//cerr << "#" << ans << endl;
	}
	
	for_(x,0,M) {
		while (!vque[x].empty()) {
			ans += vque[x].top() / 2;
			vque[x].pop();
		}
	}
	
	cout << ans << endl;
}

int main() {
	cin >> N >> M;
	for_(i,0,N) cin >> X[i];
	solve();
}




Submission

Task問題 D - Pair Cards
User nameユーザ名 tsukasa_diary
Created time投稿日時
Language言語 C++14 (GCC 5.4.1)
Status状態 AC
Score得点 700
Source lengthソースコード長 3748 Byte
File nameファイル名
Exec time実行時間 49 ms
Memory usageメモリ使用量 7296 KB

Test case

Set

Set name Score得点 / Max score Cases
sample - sample-01.txt,sample-02.txt
all 700 / 700 sample-01.txt,sample-02.txt,01-01.txt,01-02.txt,01-03.txt,01-04.txt,01-05.txt,01-06.txt,01-07.txt,01-08.txt,01-09.txt,01-10.txt,01-11.txt,01-12.txt,01-13.txt,01-14.txt,01-15.txt,01-16.txt,01-17.txt,01-18.txt,01-19.txt,01-20.txt,01-21.txt,01-22.txt,01-23.txt,01-24.txt,01-25.txt,01-26.txt,01-27.txt,01-28.txt,01-29.txt,01-30.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
01-01.txt AC 3 ms 640 KB
01-02.txt AC 40 ms 1660 KB
01-03.txt AC 40 ms 1660 KB
01-04.txt AC 39 ms 1664 KB
01-05.txt AC 40 ms 1536 KB
01-06.txt AC 39 ms 1664 KB
01-07.txt AC 38 ms 1664 KB
01-08.txt AC 37 ms 1664 KB
01-09.txt AC 37 ms 1664 KB
01-10.txt AC 40 ms 3584 KB
01-11.txt AC 44 ms 6144 KB
01-12.txt AC 31 ms 1024 KB
01-13.txt AC 33 ms 2432 KB
01-14.txt AC 46 ms 2044 KB
01-15.txt AC 46 ms 1992 KB
01-16.txt AC 42 ms 2304 KB
01-17.txt AC 39 ms 2048 KB
01-18.txt AC 39 ms 2688 KB
01-19.txt AC 38 ms 2432 KB
01-20.txt AC 49 ms 7296 KB
01-21.txt AC 31 ms 1024 KB
01-22.txt AC 31 ms 1024 KB
01-23.txt AC 31 ms 1024 KB
01-24.txt AC 31 ms 1152 KB
01-25.txt AC 33 ms 2048 KB
01-26.txt AC 36 ms 4224 KB
01-27.txt AC 27 ms 5504 KB
01-28.txt AC 9 ms 3840 KB
01-29.txt AC 5 ms 1920 KB
01-30.txt AC 7 ms 3712 KB
sample-01.txt AC 3 ms 640 KB
sample-02.txt AC 3 ms 640 KB