Submission #12112593


Source Code Expand

Copy
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
template<typename T>
struct hungarian{//n<=m
	const T inf=numeric_limits<T>::max();
	int n, m, max_match, root;
	T max_cost;
	vector<vector<T>> cost;
	vector<T> lx, ly, slack;
	vector<int> xy, yx, prev, slackx;
	vector<bool> s, t;
	void update_labels(){
		T delta=inf;
		for(int y=0; y<m; y++) if(!t[y]) delta=min(delta, slack[y]);
		for(int x=0; x<n; x++) if(s[x]) lx[x]-=delta;
		for(int y=0; y<m; y++) if(t[y]) ly[y]+=delta;
		for(int y=0; y<m; y++) if(!t[y]) slack[y]-=delta;
	}
	void add_to_tree(int x, int prevx){
		s[x]=true;
		prev[x]=prevx;
		for(int y=0; y<m; y++){
			if(lx[x]+ly[y]-cost[x][y]<slack[y]){
				slack[y]=lx[x]+ly[y]-cost[x][y];
				slackx[y]=x;
			}
		}
	}
	void augment(){
		if(max_match==n) return;
		fill(s.begin(), s.end(), false);
		fill(t.begin(), t.end(), false);
		fill(prev.begin(), prev.end(), -1);
		queue<int> que;
		for(int x=0; x<n; x++){
			if(xy[x]==-1){
				root=x;
				que.push(x);
				prev[x]=-2;
				s[x]=true;
				break;
			}
		}
		for(int y=0; y<m; y++){
			slack[y]=lx[root]+ly[y]-cost[root][y];
			slackx[y]=root;
		}
		int x, y;
		while(1){
			while(!que.empty()){
				x=que.front(); que.pop();
				for(y=0; y<m; y++){
					if(cost[x][y]==lx[x]+ly[y] && !t[y]){
						if(yx[y]==-1) break;
						t[y]=true;
						que.push(yx[y]);
						add_to_tree(yx[y], x);
					}
				}
				if(y<m) break;
			}
			if(y<m) break;
			update_labels();
			for(y=0; y<m; y++){
				if(!t[y] && slack[y]==0){
					if(yx[y]==-1){
						x=slackx[y];
						break;
					}else{
						t[y]=true;
						que.push(yx[y]);
						add_to_tree(yx[y], slackx[y]);
					}
				}
			}
			if(y<m) break;
		}
		if(y<m){
			max_match++;
			for(int cx=x, cy=y, ty; cx!=-2; cx=prev[cx], cy=ty){
				ty=xy[cx];
				yx[cy]=cx, xy[cx]=cy;
			}
			augment();
		}
	}
	hungarian(const vector<vector<T>> &cost):max_match(0), max_cost(0), cost(cost), n(cost.size()), m(cost[0].size()), lx(n, -inf), ly(m), xy(n, -1), yx(m, -1), s(n), t(m), prev(n), slack(m), slackx(m){
		for(int x=0; x<n; x++) for(int y=0; y<m; y++) lx[x]=max(lx[x], cost[x][y]);
		augment();
		for(int x=0; x<n; x++) max_cost+=cost[x][xy[x]];
	}
};
int main()
{
    int n;
    cin>>n;
    ll a[2020];
    for(int i=0; i<n; i++) cin>>a[i];
    vector<vector<ll>> c(n, vector<ll>(n));
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            c[i][j]=a[i]*abs(i-j);
        }
    }
    hungarian<ll> h(c);
    cout<<h.max_cost<<endl;
	return 0;
}

Submission Info

Submission Time
Task E - Active Infants
User chocorusk
Language C++ (GCC 9.2.1)
Score 0
Code Size 3098 Byte
Status TLE
Exec Time 2207 ms
Memory 66660 KB

Compile Error

./Main.cpp: In instantiation of ‘hungarian<T>::hungarian(const std::vector<std::vector<_Tp> >&) [with T = long long int]’:
./Main.cpp:130:22:   required from here
./Main.cpp:33:20: warning: ‘hungarian<long long int>::cost’ will be initialized after [-Wreorder]
   33 |  vector<vector<T>> cost;
      |                    ^~~~
./Main.cpp:31:6: warning:   ‘int hungarian<long long int>::n’ [-Wreorder]
   31 |  int n, m, max_match, root;
      |      ^
./Main.cpp:112:2: warning:   when initialized here [-Wreorder]
  112 |  hungarian(const vector<vector<T>> &cost):max_match(0), max_cost(0), cost(cost), n(cost.size()), m(cost[0].size()), lx(n, -inf), ly(m), xy(n, -1), yx(m, -1), s(n), t(m), prev(n), slack(m), slackx(m){
      |  ^~~~~~~~~
./Main.cpp:36:18: warning: ‘hungarian<long long int>::t’ will be initialized after [-Wreorder]
   36 |  vector<bool> s, t;
      |                  ^
./Main.cpp:35:22: warning:   ‘std::vector<int> hungarian<long long int>::prev’ [-Wreorder]
   35 |  vector<int> xy, yx, prev, slackx;
      |                      ^~~~
./Main.cpp:112:2: warning:   when initialized here [-Wreorder]
  112 |  hungarian(const vector<vector<T>> &cost):max_match(0), max_cost(0), cost(cost), n(cost.size()), m(cost[0].size()), lx(n, -inf), ly(m), xy(n, -1), yx(m, -1), s(n), t(m), prev(n), slack(m), slackx(m){
      |  ^~~~~~~~~
./Main.cpp:35:22: warning: ‘hungarian<long long int>::prev’ will be initialized after [-Wreorder]
   35 |  vector<int> xy, yx, prev, slackx;
      |                      ^~~~
./Main.cpp:34:20: warning:   ‘std::vector<long long int> hungarian<long long int>::slack’ [-Wreorder]
   34 |  vector<T> lx, ly, slack;
      |                    ^~~~~
./Main.cpp:112:2: warning:   when initialized here [-Wreorder]
  112 |  hungarian(const vector<vector<T>> &cost):max_match(0), max_cost(0), cost(cost), n(cost.size()), m(cost[0].size()), lx(n, -inf), ly(m), xy(n, -1), yx(m, -1), s(n), t(m), prev(n), slack(m), slackx(m){
      |  ^~~~~~~~~
./Main.cpp: In member function ‘voi...

Judge Result

Set Name Sample FULL
Score / Max Score 0 / 0 0 / 500
Status
AC × 3
AC × 14
TLE × 12
Set Name Test Cases
Sample Sample_01.txt, Sample_02.txt, Sample_03.txt
FULL Sample_01.txt, Sample_02.txt, Sample_03.txt, maxhand_01.txt, maxhand_02.txt, maxhand_03.txt, maxhand_04.txt, maxhand_05.txt, maxrand_01.txt, maxrand_02.txt, maxrand_03.txt, maxrand_04.txt, maxrand_05.txt, minhand_01.txt, minhand_02.txt, minhand_03.txt, minhand_04.txt, minrand_01.txt, minrand_02.txt, minrand_03.txt, minrand_04.txt, ni_01.txt, rand_01.txt, rand_02.txt, rand_03.txt, rand_04.txt
Case Name Status Exec Time Memory
Sample_01.txt AC 2 ms 3512 KB
Sample_02.txt AC 1 ms 3648 KB
Sample_03.txt AC 1 ms 3652 KB
maxhand_01.txt TLE 2207 ms 66460 KB
maxhand_02.txt TLE 2207 ms 66544 KB
maxhand_03.txt TLE 2207 ms 66612 KB
maxhand_04.txt TLE 2207 ms 66496 KB
maxhand_05.txt TLE 2207 ms 66568 KB
maxrand_01.txt TLE 2207 ms 66568 KB
maxrand_02.txt TLE 2207 ms 66660 KB
maxrand_03.txt TLE 2207 ms 66556 KB
maxrand_04.txt TLE 2207 ms 66512 KB
maxrand_05.txt TLE 2207 ms 66544 KB
minhand_01.txt AC 6 ms 3624 KB
minhand_02.txt AC 2 ms 3540 KB
minhand_03.txt AC 2 ms 3616 KB
minhand_04.txt AC 1 ms 3624 KB
minrand_01.txt AC 2 ms 3440 KB
minrand_02.txt AC 2 ms 3484 KB
minrand_03.txt AC 2 ms 3488 KB
minrand_04.txt AC 2 ms 3596 KB
ni_01.txt AC 2 ms 3564 KB
rand_01.txt AC 1627 ms 19740 KB
rand_02.txt TLE 2207 ms 58928 KB
rand_03.txt TLE 2206 ms 39900 KB
rand_04.txt AC 122 ms 6572 KB