Submission #148931

# Submission time Handle Problem Language Result Execution time Memory
148931 2019-09-01T05:25:08 Z お前はもう死んでいる(#3784, kuroni, nvmdava, tfg) Organizing the Best Squad (FXCUP4_squad) C++17
0 / 100
3000 ms 485968 KB
#include "squad.h"

#include <vector>
#include <algorithm>
#include <cassert>

struct PT {
	typedef long long T;
	T x, y;
	PT(T xx = 0, T yy = 0) : x(xx), y(yy){}
	PT operator +(const PT &p) const { return PT(x+p.x,y+p.y); }
	PT operator -(const PT &p) const { return PT(x-p.x,y-p.y); }
	PT operator *(T c)         const { return PT(x*c,y*c);     }
	T operator *(const PT &p)  const { return x*p.x+y*p.y;     }
	T operator %(const PT &p)  const { return x*p.y-y*p.x;     }
	bool operator<(const PT &p)const { return x != p.x ? x < p.x : y < p.y; }
	bool operator==(const PT &p)const{ return x == p.x && y == p.y; }
};

std::vector<PT> ConvexHull (std::vector<PT> pts) {
	if(pts.size() <= 1) return pts;
	std::sort(pts.begin(), pts.end());
	pts.resize(std::unique(pts.begin(), pts.end()) - pts.begin());
	std::vector<PT> ans(2 * pts.size());
	int s = 0;
	for(int i = 0; i < pts.size(); i++) {
		while(s > 1 && (pts[i] - ans[s - 2]) % (ans[s - 1] - ans[s - 2]) >= 0) {
			s--;
		}
		ans[s++] = pts[i];
	}
	for(int i = pts.size() - 1, t = s + 1; i > 0; i--) {
		while(s >= t && (pts[i - 1] - ans[s - 2]) % (ans[s - 1] - ans[s - 2]) >= 0) {
			s--;
		}
		ans[s++] = pts[i - 1];
	}
	ans.resize(s - 1);
	return ans;
}

int maximizeScalarProduct(const std::vector<PT> &hull, PT vec) {
	int ans = 0;
	int n = hull.size();
	if(n < 20) {
		for(int i = 0; i < n; i++) {
			if(hull[i] * vec > hull[ans] * vec) {
				ans = i;
			}
		}
	} else {
		int diff = 1;
		if(hull[0] * vec == hull[1] * vec) {
			int l = 2, r = n - 1;
			while(l != r) {
				int mid = (l + r) / 2;
				if((hull[1] - hull[0]) * (hull[mid] - hull[0]) > 0 && (hull[1] - hull[0]) % (hull[mid] - hull[0]) == 0) {
					l = mid + 1;
				} else {
					r = mid;
				}
			}
			diff = l;
			//diff = 2;
		}
		if(hull[0] * vec < hull[diff] * vec) {
			int l = diff, r = n - 1;
			while(l != r) {
				int mid = (l + r + 1) / 2;
				if(hull[mid] * vec >= hull[mid - 1] * vec && hull[mid] * vec >= hull[0] * vec) {
					l = mid;
				} else {
					r = mid - 1;
				}
			}
			if(hull[0] * vec < hull[l] * vec) {
				ans = l;
			}
		} else {
			int l = diff, r = n - 1;
			while(l != r) {
				int mid = (l + r + 1) / 2;
				if(hull[mid] * vec >= hull[mid - 1] * vec || hull[mid - 1] * vec < hull[0] * vec) {
					l = mid;
				} else {
					r = mid - 1;
				}
			}
			if(hull[0] * vec < hull[l] * vec) {
				ans = l;
			}
		}
	}
	return ans;
}

bool comp(PT a, PT b){
	if((a.x > 0 || (a.x==0 && a.y>0) ) && (b.x < 0 || (b.x==0 && b.y<0))) return 1;
	if((b.x > 0 || (b.x==0 && b.y>0) ) && (a.x < 0 || (a.x==0 && a.y<0))) return 0;
	long long R = a%b;
	if(R) return R > 0;
	return a*a < b*b;
}

std::vector<PT> poly_sum(std::vector<PT> &a, std::vector<PT> &b){
	if(a.empty() || b.empty()) return std::vector<PT>(0);
	if(std::min(a.size(), b.size()) < 2){
		std::vector<PT> ret(0);
		for(int i = 0; i < a.size(); i++) {
			for(int j = 0; j < b.size(); j++) {
				ret.push_back(a[i]+b[j]);
			}
		}
		return ret;
	}
	std::vector<PT> ret;
	ret.push_back(a[0]+b[0]);
	int pa = 0, pb = 0;
	while(pa < a.size() || pb < b.size()){
		PT p = ret.back();
		if(pb == b.size() || (pa < a.size() && comp((a[(pa+1)%a.size()]-a[pa]),(b[(pb+1)%b.size()]-b[pb]))))
			p = p + (a[(pa+1)%a.size()]-a[pa]), pa++;
		else p = p + (b[(pb+1)%b.size()]-b[pb]), pb++;
		ret.push_back(p);
	}
	assert(ret.back() == ret[0]);
	ret.pop_back();
	return ret;
}

std::vector<PT> tot;

void solve(int l, int r, const std::vector<int> &A, const std::vector<int> &B, const std::vector<int> &P) {
	if(r - l <= 1) {
		return;
	}
	int mid = (l + r) / 2;
	solve(l, mid, A, B, P);
	solve(mid, r, A, B, P);
	{
		std::vector<PT> hull[2];
		for(int i = l; i < mid; i++) {
			hull[0].emplace_back(A[i], P[i]);
		}
		for(int i = mid; i < r; i++) {
			hull[1].emplace_back(B[i], P[i]);
		}
		hull[0] = ConvexHull(hull[0]);
		hull[1] = ConvexHull(hull[1]);
		hull[0] = poly_sum(hull[0], hull[1]);
		for(int i = 0; i < hull[0].size(); i++) {
			tot.push_back(hull[0][i]);
		}
	}
	{
		std::vector<PT> hull[2];
		for(int i = l; i < mid; i++) {
			hull[0].emplace_back(B[i], P[i]);
		}
		for(int i = mid; i < r; i++) {
			hull[1].emplace_back(A[i], P[i]);
		}
		hull[0] = ConvexHull(hull[0]);
		hull[1] = ConvexHull(hull[1]);
		hull[0] = poly_sum(hull[0], hull[1]);
		for(int i = 0; i < hull[0].size(); i++) {
			tot.push_back(hull[0][i]);
		}
	}
}

void Init(std::vector<int> A, std::vector<int> D, std::vector<int> P){
	int N = A.size();
	solve(0, N, A, D, P);
	tot = ConvexHull(tot);
}

long long BestSquad(int X, int Y){
	PT cur(X, Y);
	return cur * tot[maximizeScalarProduct(tot, cur)];
}

Compilation message

squad.cpp: In function 'std::vector<PT> ConvexHull(std::vector<PT>)':
squad.cpp:26:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < pts.size(); i++) {
                 ~~^~~~~~~~~~~~
squad.cpp: In function 'std::vector<PT> poly_sum(std::vector<PT>&, std::vector<PT>&)':
squad.cpp:109:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < a.size(); i++) {
                  ~~^~~~~~~~~~
squad.cpp:110:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int j = 0; j < b.size(); j++) {
                   ~~^~~~~~~~~~
squad.cpp:119:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  while(pa < a.size() || pb < b.size()){
        ~~~^~~~~~~~~~
squad.cpp:119:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  while(pa < a.size() || pb < b.size()){
                         ~~~^~~~~~~~~~
squad.cpp:121:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(pb == b.size() || (pa < a.size() && comp((a[(pa+1)%a.size()]-a[pa]),(b[(pb+1)%b.size()]-b[pb]))))
      ~~~^~~~~~~~~~~
squad.cpp:121:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(pb == b.size() || (pa < a.size() && comp((a[(pa+1)%a.size()]-a[pa]),(b[(pb+1)%b.size()]-b[pb]))))
                         ~~~^~~~~~~~~~
squad.cpp: In function 'void solve(int, int, const std::vector<int>&, const std::vector<int>&, const std::vector<int>&)':
squad.cpp:151:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < hull[0].size(); i++) {
                  ~~^~~~~~~~~~~~~~~~
squad.cpp:166:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < hull[0].size(); i++) {
                  ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 11 ms 1020 KB Output is correct
3 Correct 2094 ms 181592 KB Output is correct
4 Correct 1999 ms 181848 KB Output is correct
5 Correct 172 ms 27952 KB Output is correct
6 Execution timed out 3120 ms 485968 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 384 KB Output is correct
2 Correct 17 ms 1400 KB Output is correct
3 Correct 2045 ms 147996 KB Output is correct
4 Correct 1982 ms 147996 KB Output is correct
5 Correct 93 ms 13280 KB Output is correct
6 Correct 2763 ms 403084 KB Output is correct
7 Correct 2882 ms 403208 KB Output is correct
8 Execution timed out 3027 ms 403156 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 11 ms 1020 KB Output is correct
3 Correct 2094 ms 181592 KB Output is correct
4 Correct 1999 ms 181848 KB Output is correct
5 Correct 172 ms 27952 KB Output is correct
6 Execution timed out 3120 ms 485968 KB Time limit exceeded
7 Halted 0 ms 0 KB -