답안 #125533

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
125533 2019-07-05T17:18:48 Z Retro3014 영역 (JOI16_ho_t4) C++17
15 / 100
158 ms 13268 KB
#include <bits/stdc++.h>

#define pb push_back
#define all(v) ((v).begin(), (v).end())
#define sortv(v) sort(all(v))
#define sz(v) ((int)(v).size())
#define uniqv(v) (v).erase(unique(all(v)), (v).end())
#define umax(a, b) (a)=max((a), (b))
#define umin(a, b) (a)=min((a), (b))
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define rep(i,n) FOR(i,1,n)
#define rep0(i,n) FOR(i,0,(int)(n)-1)
#define FI first
#define SE second
#define INF 2000000000
#define INFLL 1000000000000000000LL


using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int N, K;
string str;
vector<pii> vt;



map<pii, vector<int> > mp; 

vector<pii> mrg;
vector<int> tmp;
vector<pii> mrg2;
vector<pii> mrg3;

ll ans;

void merge(){
	//cout<<"****************"<<endl;
	mrg2.clear();
	for(int i=0; i<tmp.size(); i++){
		mrg2.pb({tmp[i], tmp[i] + K-1});
	//	cout<<tmp[i]<<" "<<tmp[i]+K-1<<endl;
	}
	if(mrg.empty() || mrg2.empty()){
		mrg.clear(); return;
	}
	//cout<<"========="<<endl;
	//for(int i=0; i<mrg.size(); i++){
	//	cout<<mrg[i].first<<" "<<mrg[i].second<<endl;
	//}
	//cout<<"========="<<endl;
	int t1 = mrg[0].first; int t2 = mrg2[0].first;
	int n1 = 0, n2 = 0;
	int i1=0, i2=0;
	int cnt1=0, cnt2=0;
	int now = -INF, prv=-INF;
	while(1){
		//cout<<"n "<<t1<<" "<<t2<<endl;
		if(t1<t2 || (t1==t2 && n1==0)){
			now = max(now, t1);
			if(n1!=1){
				cnt1++;
				t1 = mrg[i1].second;
				n1 = 1;
			}else{
				n1 = 0;
				cnt1--;
				i1++;
				if(i1>=mrg.size()){
					if(prv!=-INF){
						mrg3.pb({prv, now});
					}
					break;
				}
				t1 = mrg[i1].first;
			}
		}else{
			now = max(now, t2);
			if(n2!=1){
				cnt2++;
				while(i2+1<mrg2.size() && mrg2[i2+1].first<=mrg2[i2].second){
					i2++;
				}
				t2 = mrg2[i2].second;
				n2 = 1;
			}else{
				n2 = 0;
				cnt2--;
				i2++;
				if(i2>=mrg2.size()){
					if(prv!=-INF){
						mrg3.pb({prv, now});
					}
					break;
				}
				t2 = mrg2[i2].first;
			}
		}
		if(cnt1>0 && cnt2>0){
			if(prv==-INF){
				prv = now;
			}
		}else{
			if(prv!=-INF){
				mrg3.pb({prv, now});
				prv = -INF;
			}
		}
	}
	mrg = mrg3;
	mrg3.clear();
	//for(int i=0; i<mrg.size(); i++){
	//	cout<<mrg[i].first<<" "<<mrg[i].second<<endl;
	//}
	return;
}

int main(){
	scanf("%d%d", &N, &K);
	cin>>str;
	int x = 0, y = 0;
	for(int i=1; i<=N+1; i++){
		vt.pb({x, y});
		if(i==N+1)	break;
		if(str[i-1]=='N'){
			y++;
		}else if(str[i-1]=='E'){
			x++;
		}else if(str[i-1]=='W'){
			x--;
		}else{
			y--;
		}
	}
	if(x==0){
		int tmp = x; x = y; y = tmp;
		for(int i=0; i<vt.size(); i++){
			tmp = vt[i].first; vt[i].first = vt[i].second; vt[i].second = tmp;
		}
	}
	//cout<<x<<" "<<y<<endl;
	for(int i=0; i<vt.size(); i++){
		int t;
		if(x==0){
			t = 0;
		}else if(x<0){
			if(vt[i].first==0){
				t = 0;
			}
			else if(vt[i].first>0){
				t = vt[i].first / x;
			}else{
				t = vt[i].first / x + (((vt[i].first/x) * x ==vt[i].first)?0:1);
			}
		}else{
			if(vt[i].first==0){
				t = 0;
			}else if(vt[i].first>0){
				t = vt[i].first/x;
			}else{
				t = vt[i].first / x - (((vt[i].first/x) * x ==vt[i].first)?0:1);
			}
		}
		vt[i].first -= x * t;
		vt[i].second -= y * t;
		//cout<<"a "<<vt[i].first<<" "<<vt[i].second<<" "<<t<<endl;
		mp[vt[i]].pb(t);
		if(vt[i].first==0){
			if(x==0)	continue;
			if(x>0){
				vt[i].first+=x; vt[i].second+=y;
				//cout<<"a "<<vt[i].first<<" "<<vt[i].second<<" "<<t-1<<endl;
				mp[vt[i]].pb(t-1);
			}else{
				vt[i].first-=x; vt[i].second-=y;
				//cout<<"a "<<vt[i].first<<" "<<vt[i].second<<" "<<t+1<<endl;
				mp[vt[i]].pb(t+1);
			}
		}
	}
	//cout<<"**"<<endl;
	for(map<pii, vector<int> >::iterator it = mp.begin(); it!=mp.end(); it++){
		tmp = it->second;
		sort(tmp.begin(), tmp.end());
		mp[it->first] = tmp;
	}
	for(map<pii, vector<int> >::iterator it = mp.begin(); it!=mp.end(); it++){
		pii p = it->first;
		//if(p.first==x)	continue;
		//cout<<"+"<<p.first<<" "<<p.second<<endl;
		//if(x==0){
			if(mp.find({(p.first+1), p.second})==mp.end() || mp.find({p.first, p.second+1})==mp.end() || mp.find({(p.first+1), p.second+1})==mp.end())	continue;
			mrg.clear(); mrg2.clear();
			//cout<<p.first<<" "<<p.second<<endl;
			mrg.pb({-INF, INF}); tmp = mp[p]; merge();
			tmp = mp[{p.first+1, p.second}]; merge();
			tmp = mp[{p.first, p.second+1}]; merge();
			tmp = mp[{p.first+1, p.second+1}]; merge();
			for(int i=0; i<mrg.size(); i++){
				ans += mrg[i].second - mrg[i].first + 1;
				//cout<<'*'<<mrg[i].first<<" "<<mrg[i].second<<endl;
			}
		/*}else{
			if(mp.find({p.first+1, p.second})==mp.end() || mp.find({p.first, p.second+1})==mp.end() || mp.find({p.first+1, p.second+1})==mp.end())	continue;
			mrg.clear(); mrg2.clear();
			mrg.pb({1, K}); tmp = mp[p]; merge();
			tmp = mp[{(p.first+1)%x, p.second}]; merge();
			tmp = mp[{p.first, p.second+1}]; merge();
			tmp = mp[{(p.first+1)%x, p.second+1}]; merge();
			for(int i=0; i<mrg.size(); i++){
				ans += mrg[i].second - mrg[i].first;
				cout<<'*'<<mrg[i].first<<" "<<mrg[i].second<<endl;
			}
		}*/
	}
	cout<<ans;
	return 0;
}

Compilation message

2016_ho_t4.cpp: In function 'void merge()':
2016_ho_t4.cpp:42:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<tmp.size(); i++){
               ~^~~~~~~~~~~
2016_ho_t4.cpp:71:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(i1>=mrg.size()){
        ~~^~~~~~~~~~~~
2016_ho_t4.cpp:83:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(i2+1<mrg2.size() && mrg2[i2+1].first<=mrg2[i2].second){
           ~~~~^~~~~~~~~~~~
2016_ho_t4.cpp:92:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(i2>=mrg2.size()){
        ~~^~~~~~~~~~~~~
2016_ho_t4.cpp: In function 'int main()':
2016_ho_t4.cpp:139:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0; i<vt.size(); i++){
                ~^~~~~~~~~~
2016_ho_t4.cpp:144:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<vt.size(); i++){
               ~^~~~~~~~~~
2016_ho_t4.cpp:201:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int i=0; i<mrg.size(); i++){
                 ~^~~~~~~~~~~
2016_ho_t4.cpp:121:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &K);
  ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 380 KB Output is correct
11 Correct 12 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 380 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 3 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 380 KB Output is correct
11 Correct 12 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 380 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 3 ms 256 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 4 ms 476 KB Output is correct
28 Correct 9 ms 888 KB Output is correct
29 Correct 26 ms 2452 KB Output is correct
30 Correct 32 ms 2540 KB Output is correct
31 Correct 97 ms 13268 KB Output is correct
32 Correct 47 ms 3816 KB Output is correct
33 Correct 53 ms 4204 KB Output is correct
34 Correct 53 ms 4464 KB Output is correct
35 Correct 50 ms 3976 KB Output is correct
36 Correct 42 ms 3304 KB Output is correct
37 Correct 50 ms 4076 KB Output is correct
38 Correct 49 ms 3948 KB Output is correct
39 Correct 50 ms 4080 KB Output is correct
40 Correct 47 ms 3820 KB Output is correct
41 Correct 33 ms 2540 KB Output is correct
42 Correct 40 ms 3176 KB Output is correct
43 Correct 50 ms 4036 KB Output is correct
44 Correct 48 ms 3944 KB Output is correct
45 Correct 51 ms 3832 KB Output is correct
46 Correct 88 ms 6764 KB Output is correct
47 Correct 158 ms 12520 KB Output is correct
48 Correct 25 ms 4076 KB Output is correct
49 Correct 102 ms 12268 KB Output is correct
50 Correct 99 ms 12268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 380 KB Output is correct
11 Correct 12 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 380 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 3 ms 256 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Incorrect 2 ms 376 KB Output isn't correct
28 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 380 KB Output is correct
11 Correct 12 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 380 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 3 ms 256 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 4 ms 476 KB Output is correct
28 Correct 9 ms 888 KB Output is correct
29 Correct 26 ms 2452 KB Output is correct
30 Correct 32 ms 2540 KB Output is correct
31 Correct 97 ms 13268 KB Output is correct
32 Correct 47 ms 3816 KB Output is correct
33 Correct 53 ms 4204 KB Output is correct
34 Correct 53 ms 4464 KB Output is correct
35 Correct 50 ms 3976 KB Output is correct
36 Correct 42 ms 3304 KB Output is correct
37 Correct 50 ms 4076 KB Output is correct
38 Correct 49 ms 3948 KB Output is correct
39 Correct 50 ms 4080 KB Output is correct
40 Correct 47 ms 3820 KB Output is correct
41 Correct 33 ms 2540 KB Output is correct
42 Correct 40 ms 3176 KB Output is correct
43 Correct 50 ms 4036 KB Output is correct
44 Correct 48 ms 3944 KB Output is correct
45 Correct 51 ms 3832 KB Output is correct
46 Correct 88 ms 6764 KB Output is correct
47 Correct 158 ms 12520 KB Output is correct
48 Correct 25 ms 4076 KB Output is correct
49 Correct 102 ms 12268 KB Output is correct
50 Correct 99 ms 12268 KB Output is correct
51 Correct 2 ms 376 KB Output is correct
52 Incorrect 2 ms 376 KB Output isn't correct
53 Halted 0 ms 0 KB -