Submission #547058

#TimeUsernameProblemLanguageResultExecution timeMemory
547058nonsensenonsense1Sightseeing in Kyoto (JOI22_kyoto)C++17
100 / 100
564 ms23272 KiB
#include <cstdio>
#include <set>

struct frac {
	int a, b;
	frac(int a_ = 0, int b_ = 0) {
		a = a_;
		b = b_;
	}
};

bool operator<(frac a, frac b) {
	return (long long)a.a * b.b < (long long)b.a * a.b;
}

const int N = 100000;
int h, w, a[N], b[N];
bool rmx[N], rmy[N];
std::set<std::pair<frac, int> > st;
std::set<int> x, y;

int main() {
	scanf("%d%d", &h, &w);
	for (int i = 0; i < h; ++i) {
		x.insert(i);
		scanf("%d", a + i);
		if (i) st.insert(std::make_pair(frac(a[i - 1] - a[i], 1), i));
	}
	for (int i = 0; i < w; ++i) {
		y.insert(i);
		scanf("%d", b + i);
		if (i) st.insert(std::make_pair(frac(b[i - 1] - b[i], 1), h + i));
	}
	long long ans = 0;
	while (st.size()) {
		int pos = st.begin()->second;
		st.erase(st.begin());
		if (pos < h) {
			if (!rmx[pos]) {
				rmx[pos] = 1;
				x.erase(pos);
				std::set<int>::iterator it = x.lower_bound(pos);
				if (it == x.end()) ans += (long long)b[*prev(y.end())] * (pos - *prev(it));
				else st.insert(std::make_pair(frac(a[*prev(it)] - a[*it], *it - *prev(it)), *it));
			}
		} else {
			pos -= h;
			if (!rmy[pos]) {
				rmy[pos] = 1;
				y.erase(pos);
				std::set<int>::iterator it = y.lower_bound(pos);
				if (it == y.end()) ans += (long long)a[*prev(x.end())] * (pos - *prev(it));
				else st.insert(std::make_pair(frac(b[*prev(it)] - b[*it], *it - *prev(it)), h + *it));
			}
		}
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message (stderr)

kyoto.cpp: In function 'int main()':
kyoto.cpp:23:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |  scanf("%d%d", &h, &w);
      |  ~~~~~^~~~~~~~~~~~~~~~
kyoto.cpp:26:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |   scanf("%d", a + i);
      |   ~~~~~^~~~~~~~~~~~~
kyoto.cpp:31:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |   scanf("%d", b + i);
      |   ~~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...