Submission #527901


Source Code Expand

/*{{{*/
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <functional>
#include <bitset>
#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
typedef int8_t sbyte;
typedef uint8_t byte;
typedef uint16_t ushort;
typedef uint32_t uint;
typedef int64_t i64;
typedef uint64_t u64;
template<class T> static inline T ABS(T x) {return x < 0 ? -x : x;}
template<class T> static inline void MAZ(T &a, const T &b) {if(a < b) a = b;}
template<class T> static inline void MIZ(T &a, const T &b) {if(b < a) a = b;}
/*}}}*/

typedef i64 ll;
static const int AMAX = 300;
static const int BMAX = 300;
class Hun {
	private:
		int A,B,M; i64 cost[AMAX][BMAX];
		int fa[AMAX],fb[BMAX];
		i64 ca[AMAX],cb[BMAX];
		bool d[AMAX], e[BMAX];
		bool dfs(int a) {
			d[a]=1;
			for(int b=0;b<B;b++) {
				if(ca[a]+cb[b]!=cost[a][b])continue;
				if(fb[b]>=0&&(d[fb[b]]||!dfs(fb[b])))continue;
				fa[a]=b;fb[b]=a;return 1;
			}
			return 0;
		}
	public:
		inline i64 *operator[](int a) {return cost[a];}
		inline void init(int a,int b) {assert(0<=a&&a<=b);A=a;B=b;}
		// Hopcroft-Karp algorithm, O(m*sqrt(n))
		int BM() {
			int a; bool flag;
			do {
				flag=false;memset(d,0x00,A);
				for(a=0;a<A;a++)if(fa[a]<0&&!d[a]&&dfs(a)){M++;flag=true;}
			} while(flag);
			return M;
		}
		// Hungarian algorithm, O(a^2*b)
		ll solve() {
			int i,j; i64 k; ll ret=0;
			memset(fa,0xFF,A*sizeof(int));memset(ca,0x00,A*sizeof(i64));
			memset(fb,0xFF,B*sizeof(int));memset(cb,0x00,B*sizeof(i64));
			for(i=0;i<A;i++)for(j=0;j<B;j++)ca[i]=max(ca[i],cost[i][j]);
			for(M=0;BM()!=A;) {
				for(j=0;j<B;j++)e[j]=(fb[j]>=0&&d[fb[j]]);
				for(i=0;i<A;i++)d[i]=!d[i];
				k=LLONG_MAX;
				for(i=0;i<A;i++)if(!d[i])
					for(j=0;j<B;j++)if(!e[j])
						k=min(k,ca[i]+cb[j]-cost[i][j]);
				for(i=0;i<A;i++)if(!d[i])ca[i]-=k;
				for(j=0;j<B;j++)if( e[j])cb[j]+=k;
			}
			for(i=0;i<A;i++)ret+=ca[i];
			for(j=0;j<B;j++)ret+=cb[j];
			return ret;
		}
};

static vector<pair<int, int>> K, A;

static i64 solve() {
	static Hun hun;
	int nk = K.size(), na = A.size();
	if(nk == 0 || na == 0) return 0;
	int AS, BS;
	if(nk <= na) {
		AS = nk;
		BS = na;
		for(int i = 0; i < nk; i++)
			for(int j = 0; j < na; j++)
				hun[i][j] = max((i64)0, -K[i].first + (i64)A[j].first * min(K[i].second, A[j].second));
	} else {
		AS = na;
		BS = nk;
		for(int j = 0; j < na; j++)
			for(int i = 0; i < nk; i++)
				hun[j][i] = max((i64)0, -K[i].first + (i64)A[j].first * min(K[i].second, A[j].second));
	}
	hun.init(AS, BS);
	//for(int i = 0; i < AS; i++) {
	//	for(int j = 0; j < BS; j++) printf("%5lld ", hun[i][j]);
	//	puts("$");
	//}
	auto s = hun.solve();
	//printf("s = %lld\n", s);
	return s;
}

int main() {
	int n;
	scanf("%d", &n);
	vector<pair<int, int>> r(n);
	auto h = 0LL; auto m = 0;
	for(auto i = 0; i < n; i++) {
		scanf("%d%d", &r[i].first, &r[i].second);
		if(r[i].first == 2) {
			h += r[i].second;
			m++;
		}
	}
	for(auto i = 0; i < n; ) {
		if(r[i].first == 2) {
			m--;
			i++;
			continue;
		}
		vector<int> ks, as;
		do {
			if(r[i].first == 0) {
				ks.push_back(r[i].second);
			} else {
				as.push_back(r[i].second);
			}
		} while(++i < n && r[i].first != 2);
		sort(begin(ks), end(ks));
		sort(begin(as), end(as), greater<int>());
		for(auto k : ks) K.emplace_back(k, m);
		for(auto a : as) A.emplace_back(a, m);
	}
	printf("%lld\n", h - solve());
	return 0;
}

Submission Info

Submission Time
Task H - Dungeon
User arosusti
Language C++11 (GCC 4.8.1)
Score 100
Code Size 3804 Byte
Status AC
Exec Time 1961 ms
Memory 1732 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:121:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
                 ^
./Main.cpp:125:43: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &r[i].first, &r[i].second);
                                           ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 3
AC × 58
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All colorful1_01.txt, colorful1_02.txt, colorful1_03.txt, colorful2_01.txt, colorful2_02.txt, colorful2_03.txt, colorful3_01.txt, colorful3_02.txt, colorful3_03.txt, corner_01.txt, corner_02.txt, corner_03.txt, corner_04.txt, corner_05.txt, corner_06.txt, interval_01.txt, interval_02.txt, interval_03.txt, interval_04.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_exp_01.txt, random_exp_02.txt, random_exp_03.txt, random_max_01.txt, random_max_02.txt, random_max_03.txt, random_max_04.txt, random_max_05.txt, random_max_06.txt, random_max_07.txt, random_max_08.txt, random_max_09.txt, random_max_10.txt, random_select_01.txt, random_select_02.txt, random_select_03.txt, random_select_04.txt, random_select_05.txt, random_select_06.txt, random_select_07.txt, random_select_08.txt, random_select_09.txt, random_select_10.txt, random_small_01.txt, random_small_02.txt, random_small_03.txt, random_small_04.txt, random_small_05.txt, random_small_06.txt, random_small_07.txt, random_small_08.txt, random_small_09.txt, random_small_10.txt
Case Name Status Exec Time Memory
colorful1_01.txt AC 135 ms 1348 KB
colorful1_02.txt AC 30 ms 1376 KB
colorful1_03.txt AC 140 ms 1304 KB
colorful2_01.txt AC 1011 ms 1692 KB
colorful2_02.txt AC 62 ms 1692 KB
colorful2_03.txt AC 103 ms 1732 KB
colorful3_01.txt AC 58 ms 1692 KB
colorful3_02.txt AC 69 ms 1688 KB
colorful3_03.txt AC 145 ms 1688 KB
corner_01.txt AC 27 ms 948 KB
corner_02.txt AC 27 ms 1048 KB
corner_03.txt AC 27 ms 1052 KB
corner_04.txt AC 26 ms 1176 KB
corner_05.txt AC 27 ms 1048 KB
corner_06.txt AC 27 ms 1048 KB
interval_01.txt AC 29 ms 1052 KB
interval_02.txt AC 43 ms 1684 KB
interval_03.txt AC 63 ms 1696 KB
interval_04.txt AC 34 ms 1312 KB
random_01.txt AC 43 ms 1692 KB
random_02.txt AC 60 ms 1684 KB
random_03.txt AC 33 ms 1688 KB
random_04.txt AC 33 ms 1692 KB
random_05.txt AC 35 ms 1680 KB
random_06.txt AC 31 ms 1692 KB
random_exp_01.txt AC 27 ms 1052 KB
random_exp_02.txt AC 26 ms 1052 KB
random_exp_03.txt AC 27 ms 1056 KB
random_max_01.txt AC 1544 ms 1688 KB
random_max_02.txt AC 1680 ms 1688 KB
random_max_03.txt AC 1961 ms 1692 KB
random_max_04.txt AC 1333 ms 1688 KB
random_max_05.txt AC 1732 ms 1688 KB
random_max_06.txt AC 1522 ms 1684 KB
random_max_07.txt AC 1774 ms 1688 KB
random_max_08.txt AC 1493 ms 1684 KB
random_max_09.txt AC 1383 ms 1684 KB
random_max_10.txt AC 1154 ms 1692 KB
random_select_01.txt AC 289 ms 1304 KB
random_select_02.txt AC 461 ms 1360 KB
random_select_03.txt AC 376 ms 1296 KB
random_select_04.txt AC 349 ms 1308 KB
random_select_05.txt AC 322 ms 1308 KB
random_select_06.txt AC 349 ms 1304 KB
random_select_07.txt AC 358 ms 1300 KB
random_select_08.txt AC 369 ms 1304 KB
random_select_09.txt AC 230 ms 1308 KB
random_select_10.txt AC 358 ms 1304 KB
random_small_01.txt AC 28 ms 1044 KB
random_small_02.txt AC 27 ms 1044 KB
random_small_03.txt AC 26 ms 1152 KB
random_small_04.txt AC 28 ms 1044 KB
random_small_05.txt AC 28 ms 1048 KB
random_small_06.txt AC 28 ms 1148 KB
random_small_07.txt AC 30 ms 1172 KB
random_small_08.txt AC 32 ms 1176 KB
random_small_09.txt AC 34 ms 1172 KB
random_small_10.txt AC 37 ms 1184 KB
sample_01.txt AC 27 ms 956 KB
sample_02.txt AC 27 ms 1048 KB
sample_03.txt AC 27 ms 1040 KB