QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#214457#2346. Miniature Golfucup-team2172#AC ✓990ms102192kbC++143.3kb2023-10-14 19:55:442023-10-14 19:55:46

Judging History

This is the latest submission verdict.

  • [2023-10-14 19:55:46]
  • Judged
  • Verdict: AC
  • Time: 990ms
  • Memory: 102192kb
  • [2023-10-14 19:55:44]
  • Submitted

answer

#include <bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
	int ch = 0, f = 0; x = 0;
	for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
	for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
	if(f) x = -x;
}
int p,h;
const int N = 505;
const int M = 55;
int a[N][M];
int vl[N],id[N];
bool cmp(int x,int y){
	return x>y;
}
int check(ll sumi, ll cnti, ll sumj, ll cntj, ll l){
	ll vali = sumi + cnti * l;
	ll valj = sumj + cntj * l;
	if(vali > valj) return 1;
	if(vali == valj) return 0;
	return -1;
}
#define fi first
#define se second
struct node{
	int l,r,v;
	node(){}
	node(int l_,int r_,int v_){l = l_, r = r_, v = v_;}
};
vector<pair<int,int> > vec[N];
int maxx;
int main(){
	read(p), read(h);
	for(int i = 1; i <= p; i++) for(int j = 1; j <= h; j++) read(a[i][j]), maxx = max(maxx,a[i][j]);
	for(int i = 1; i <= p; i++) sort(a[i] + 1, a[i] + 1 + h, cmp);
	for(int i = 1; i <= p; i++){
		for(int j = 1; j <= p; j++){
		 	ll sumi = 0, sumj = 0;
			ll cnti = 0, cntj = 0;
			for(int k = 1; k <= h; k++){
				sumi += a[i][k];
				sumj += a[j][k];
			}
			int pos_i = 0, pos_j = 0;
			int pos = 0;
			while(pos_i < h && pos_j < h){
				if(a[i][pos_i + 1] >= a[j][pos_j + 1]) vl[++pos] = a[i][++pos_i], id[pos] = 0;
				else vl[++pos] = a[j][++pos_j],id[pos] = 1;
			}
			while(pos_i < h) vl[++pos] = a[i][++pos_i], id[pos] = 0;
			while(pos_j < h) vl[++pos] = a[j][++pos_j], id[pos] = 1;
			vl[pos + 1] = 0;
			vector<node> tmp;
			for(int k = 1; k <= pos; k++){
				if(id[k] == 0){
					sumi -= vl[k];
					cnti++;
				}
				else{
					sumj -= vl[k];
					cntj++;
				}
				if(vl[k] == vl[k + 1]) continue;
				int lstate = check(sumi, cnti, sumj, cntj, vl[k]);
				int rstate = check(sumi, cnti, sumj, cntj, vl[k + 1] + 1);
				if(lstate == rstate){
					tmp.push_back(node(vl[k],vl[k + 1] + 1, lstate));
					continue;
				}
				assert(cnti != cntj);
				int xr = abs(sumi - sumj) / abs(cntj - cnti);
				int xl = xr + 1;

				if(abs(sumi - sumj) % abs(cntj - cnti) == 0){
					int x = (sumi - sumj) / (cntj - cnti);
					if(lstate != 0) tmp.push_back(node(vl[k], x + 1, lstate));
					tmp.push_back(node(x, x, 0));
					if(rstate != 0) tmp.push_back(node(x - 1, vl[k + 1] + 1, rstate));
				}
				else{
					tmp.push_back(node(vl[k], xl, lstate));
					assert(check(sumi, cnti, sumj, cntj, xl) == lstate);
					tmp.push_back(node(xr, vl[k + 1] + 1, rstate));
					assert(check(sumi,cnti,sumj,cntj,xr) == rstate);
				}
			}
			int last = tmp[0].v;
			//cout<<i<<" "<<j<<" "<<last<<endl;
			if(last != -1) vec[i].push_back(make_pair(-maxx,1));
			for(int k = 1; k < tmp.size(); k++){
				int now = tmp[k].v;
				if((last != -1) != (now != -1)){
					if(now != -1) vec[i].push_back(make_pair(-tmp[k].l,1));
					else vec[i].push_back(make_pair(-tmp[k].l,-1));
				}
				last = now;
			}
		}
		sort(vec[i].begin(),vec[i].end());
		int sum = 0, res = p;
		for(int k = 0; k < vec[i].size(); k++){
			sum += vec[i][k].se;
			if(k == vec[i].size() - 1 || vec[i][k].fi != vec[i][k + 1].fi) res = min(res, sum);
		}
		printf("%d\n",res);
	}
	return 0;
}

Details

Test #1:

score: 100
Accepted
time: 1ms
memory: 3688kb

Test #2:

score: 0
Accepted
time: 0ms
memory: 3672kb

Test #3:

score: 0
Accepted
time: 0ms
memory: 3644kb

Test #4:

score: 0
Accepted
time: 1ms
memory: 3652kb

Test #5:

score: 0
Accepted
time: 0ms
memory: 3616kb

Test #6:

score: 0
Accepted
time: 1ms
memory: 3736kb

Test #7:

score: 0
Accepted
time: 0ms
memory: 3616kb

Test #8:

score: 0
Accepted
time: 0ms
memory: 3672kb

Test #9:

score: 0
Accepted
time: 3ms
memory: 3744kb

Test #10:

score: 0
Accepted
time: 0ms
memory: 3616kb

Test #11:

score: 0
Accepted
time: 1ms
memory: 3720kb

Test #12:

score: 0
Accepted
time: 5ms
memory: 3732kb

Test #13:

score: 0
Accepted
time: 0ms
memory: 3628kb

Test #14:

score: 0
Accepted
time: 1ms
memory: 3664kb

Test #15:

score: 0
Accepted
time: 2ms
memory: 3748kb

Test #16:

score: 0
Accepted
time: 55ms
memory: 5532kb

Test #17:

score: 0
Accepted
time: 983ms
memory: 102192kb

Test #18:

score: 0
Accepted
time: 990ms
memory: 100328kb

Test #19:

score: 0
Accepted
time: 853ms
memory: 84660kb

Test #20:

score: 0
Accepted
time: 417ms
memory: 49996kb

Test #21:

score: 0
Accepted
time: 408ms
memory: 50020kb

Test #22:

score: 0
Accepted
time: 416ms
memory: 50012kb

Test #23:

score: 0
Accepted
time: 286ms
memory: 7480kb

Test #24:

score: 0
Accepted
time: 298ms
memory: 7476kb

Test #25:

score: 0
Accepted
time: 312ms
memory: 7492kb

Test #26:

score: 0
Accepted
time: 300ms
memory: 7488kb

Test #27:

score: 0
Accepted
time: 294ms
memory: 7496kb