Submission #1074864

#TimeUsernameProblemLanguageResultExecution timeMemory
1074864pawnedNaval battle (CEOI24_battle)C++17
0 / 100
238 ms24348 KiB
#pragma GCC optimize("O1,O2,O3,Ofast,unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;

const char nl = '\n';

void fastIO() {
	ios::sync_with_stdio(false);
	cin.tie(0);
}

ll norm(ll x, ll y) {
	x %= y;
	x += y;
	x %= y;
	return x;
}

ll intersect(pair<int, ii> s1, pair<int, ii> s2) {
//	cout<<"at "<<"("<<s1.fi<<", "<<s1.se.fi<<", "<<s1.se.se<<"); ("<<s2.fi<<", "<<s2.se.fi<<", "<<s2.se.se<<")"<<endl;
	// time if intersect, -1 if not
	if (s1.fi > s2.fi)	// ensure first is less than second
		swap(s1, s2);
	if (s1.fi == s2.fi)	// if same direction, never intersect
		return -1;
	if (s1.fi == 0 && s2.fi == 1) {	// x coord const, first increasing
		if (s1.se.fi != s2.se.fi)
			return -1;
		if (norm(s1.se.se + s2.se.se) == 1 || s1.se.se > s2.se.se)
			return -1;
		return abs(s2.se.se - s1.se.se) / 2;
	}
	if (s1.fi == 2 && s2.fi == 3) {	// y coord const, first increasing
		if (s1.se.se != s2.se.se)
			return -1;
		if (norm(s1.se.fi + s2.se.fi) == 1 || s1.se.fi > s2.se.fi)
			return -1;
		return abs(s2.se.fi - s1.se.fi) / 2;
	}
	// first going N/S, other going E/W
	ii ins = {s1.se.fi, s2.se.se};
//	cout<<"ins: "<<ins.fi<<", "<<ins.se<<endl;
	ll t1 = ins.se - s1.se.se;
	if (s1.fi == 1)
		t1 = -t1;
	ll t2 = ins.fi - s2.se.fi;
//	cout<<"t1, t2: "<<t1<<" "<<t2<<endl;
	if (s2.fi == 3)
		t2 = -t2;
	if (t1 == t2 && t1 > 0)
		return t1;
	return -1;
}

int main() {
	map<char, int> conv = {{'S', 0}, {'N', 1}, {'E', 2}, {'W', 3}};
	fastIO();
	int N;
	cin>>N;
	vector<pair<int, ii>> ships;
	// {type, {x, y}}
	map<ll, set<pair<ii, int>>> alld;	// all on diagonal x+y=i, going down
	// set of {{x, y}, id}
	for (int i = 0; i < N; i++) {
		ll x, y; char d;
		cin>>x>>y>>d;
		ships.pb({conv[d], {x, y}});
		if (conv[d] == 0)
			alld[x + y].insert({{x, y}, i});
	}
/*	cout<<"ships: ";
	for (pair<int, ii> p : ships)
		cout<<"("<<p.fi<<", "<<p.se.fi<<", "<<p.se.se<<"); ";
	cout<<endl;*/
	vector<pair<ll, ii>> sink;
	for (int i = 0; i < N; i++) {
		if (ships[i].fi == 2) {	// first goes right
//			cout<<"at "<<i<<endl;
			ll sr = ships[i].se.fi + ships[i].se.se;
//			cout<<"sr: "<<sr<<endl;
			auto it = alld[sr].lower_bound({{ships[i].se.fi, -1}, -1});
			if (it != alld[sr].end()) {
				int j = (*it).se;
//				cout<<"trying "<<j<<endl;
				ll res = intersect(ships[i], ships[j]);
				if (res != -1)
					sink.pb({res, {i, j}});
			}
		}
	}
	sort(sink.begin(), sink.end());	// comment if TLE
/*	cout<<"sinking: "<<endl;
	for (pair<ll, ii> p : sink)
		cout<<p.fi<<" "<<p.se.fi<<" "<<p.se.se<<endl;*/
	map<ll, vector<ii>> allr;
	for (pair<ll, ii> p : sink) {
		allr[p.fi].pb(p.se);
	}
	vector<bool> alive(N, true);	// still alive
	for (pair<ll, vector<ii>> pr : allr) {
//		cout<<"at "<<pr.fi<<endl;
		set<int> toremove;
		for (ii p : pr.se) {
			if (alive[p.fi] && alive[p.se]) {
				toremove.insert(p.fi);
				toremove.insert(p.se);
			}
		}
		for (int x : toremove) {
//			cout<<"removed "<<x<<endl;
			alive[x] = false;
		}
	}
//	cout<<"ANSWER: "<<endl;
	for (int i = 0; i < N; i++) {
		if (alive[i])
			cout<<(i + 1)<<endl;
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...