結果

問題 No.596 郵便配達
ユーザー 👑 kmjpkmjp
提出日時 2017-11-11 03:04:16
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2,259 ms / 5,000 ms
コード長 2,071 bytes
コンパイル時間 1,737 ms
コンパイル使用メモリ 161,132 KB
実行使用メモリ 63,872 KB
最終ジャッジ日時 2024-05-03 15:26:46
合計ジャッジ時間 11,819 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 344 ms
11,136 KB
testcase_01 AC 344 ms
11,136 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 701 ms
23,424 KB
testcase_05 AC 2,259 ms
61,696 KB
testcase_06 AC 143 ms
7,680 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 410 ms
17,024 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 1,504 ms
63,872 KB
testcase_21 AC 1,533 ms
63,872 KB
testcase_22 AC 1,500 ms
63,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,M;
int X[505050];
int miy[505050],may[505050];

int L[7070707];
int R[7070707];

int add[7]={0,1,1,2,3,3,0};
int lef[7]={0,0,1,1,1,1,0};
int rig[7]={0,1,0,1,1,1,0};
// before, ->, <- , <-> , -><-->, <-->-<, done
// 1-non 2-start 3-end
int mat[7][7]={
	{1,2,3,1,2,3,1},
	{0,1,0,3,1,0,3},
	{0,0,1,2,0,1,2},
	{0,2,3,1,0,0,1},
	{0,1,0,0,1,0,3},
	{0,0,1,0,0,1,2},
	{0,0,0,0,0,0,1},
};
int from[7][2][2];
int to[7][2][2];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>M;
	int miX=N,maX=0;
	
	FOR(i,M) {
		cin>>X[i]>>x;
		miy[i]=may[i]=X[i];
		while(x--) {
			cin>>y;
			miy[i]=min(miy[i],y);
			may[i]=max(may[i],y);
		}
		L[miy[i]]++;
		L[X[i]]--;
		R[X[i]]++;
		R[may[i]]--;
		miX=min(miX,miy[i]);
		maX=max(maX,may[i]);
	}
	
	FOR(i,7) FOR(x,2) FOR(y,2) from[i][x][y]=1<<30;
	from[0][0][0]=0;
	FOR(i,N) {
		FOR(j,7) FOR(x,2) FOR(y,2) to[j][x][y]=1<<30;
		
		FOR(x,7) FOR(y,7) {
			if(mat[x][y]==0) continue;
			if(L[i]&&lef[y]==0) continue;
			if(R[i]&&rig[y]==0) continue;
			FOR(j,2) FOR(k,2) {
				if(from[x][j][k]>=1<<30) continue;
				int j2=j+(mat[x][y]==2);
				int k2=k+(mat[x][y]==3);
				if(j2==2 || k2==2) continue;
				to[y][j2][k2]=min(to[y][j2][k2],from[x][j][k]+add[y]);
			}
		}
		
		if(i>=miX) to[0][0][0]=1<<30;
		if(i+1<=maX) to[6][0][0]=to[6][1][1]=1<<30;
		
		L[i+1]+=L[i];
		R[i+1]+=R[i];
		swap(from,to);
	}
	
	cout<<min(from[6][0][0],from[6][1][1])<<endl;
	
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0