結果

問題 No.608 God's Maze
ユーザー lyozlyoz
提出日時 2017-12-08 02:31:19
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 3,363 bytes
コンパイル時間 2,711 ms
コンパイル使用メモリ 221,312 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-19 22:29:08
合計ジャッジ時間 4,824 ms
ジャッジサーバーID
(参考情報)
judge10 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 3 ms
4,380 KB
testcase_30 AC 7 ms
4,384 KB
testcase_31 AC 4 ms
4,376 KB
testcase_32 AC 5 ms
4,380 KB
testcase_33 AC 7 ms
4,384 KB
testcase_34 AC 5 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 3 ms
4,380 KB
testcase_37 AC 3 ms
4,384 KB
testcase_38 AC 5 ms
4,376 KB
testcase_39 AC 2 ms
4,384 KB
testcase_40 AC 4 ms
4,380 KB
testcase_41 AC 3 ms
4,380 KB
testcase_42 AC 7 ms
4,380 KB
testcase_43 AC 3 ms
4,380 KB
testcase_44 AC 6 ms
4,376 KB
testcase_45 AC 6 ms
4,380 KB
testcase_46 AC 3 ms
4,376 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 AC 6 ms
4,376 KB
testcase_49 AC 3 ms
4,376 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 1 ms
4,376 KB
testcase_52 AC 1 ms
4,376 KB
testcase_53 AC 2 ms
4,380 KB
testcase_54 AC 2 ms
4,380 KB
testcase_55 AC 1 ms
4,376 KB
testcase_56 AC 1 ms
4,380 KB
testcase_57 AC 2 ms
4,380 KB
testcase_58 AC 1 ms
4,380 KB
testcase_59 AC 1 ms
4,380 KB
testcase_60 AC 2 ms
4,380 KB
testcase_61 AC 1 ms
4,380 KB
testcase_62 AC 2 ms
4,380 KB
testcase_63 AC 2 ms
4,380 KB
testcase_64 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define dump(...) do{print_vars(cout<<"# "<<#__VA_ARGS__<<'=',__VA_ARGS__);cout<<endl;}while(0)
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple

using uint=unsigned;
using ll=long long;
using ull=unsigned long long;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using vd=vector<double>;
using vvd=vector<vd>;
using vs=vector<string>;

template<typename T,typename U>bool chmin(T& a,const U& b){return a>b?(a=b,1):0;}
template<typename T,typename U>bool chmax(T& a,const U& b){return a<b?(a=b,1):0;}

void print_vars(ostream&){}
template<typename Car,typename... Cdr>
void print_vars(ostream& os,const Car& car,const Cdr&... cdr){
	print_vars(os<<car<<(sizeof...(cdr)?",":""),cdr...);
}

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}

template<int I,typename Tuple>
void print_tuple(ostream&,const Tuple&){}
template<int I,typename Car,typename... Cdr,typename Tuple>
void print_tuple(ostream& os,const Tuple& t){
	os<<get<I>(t)<<(sizeof...(Cdr)?",":"");
	print_tuple<I+1,Cdr...>(os,t);
}
template<typename... Args>
ostream& operator<<(ostream& os,const tuple<Args...>& t){
	print_tuple<0,Args...>(os<<'(',t);
	return os<<')';
}

template<typename Ch,typename Tr,typename C>
basic_ostream<Ch,Tr>& operator<<(basic_ostream<Ch,Tr>& os,const C& c){
	os<<'[';
	for(auto i=begin(c);i!=end(c);++i)
		os<<(i==begin(c)?"":" ")<<*i;
	return os<<']';
}

constexpr int INF=1e9;
constexpr int MOD=1e9+7;
constexpr double EPS=1e-9;

int solve_naive(vi a,int i)
{
	queue<tuple<vi,int,int>> q;
	q.emplace(a,i,0);
	set<tuple<vi,int>> vis;
	while(q.size()){
		auto [ca,ci,cc]=q.front(); q.pop();
		if(count(all(ca),1)==0) return cc;
		if(!vis.emplace(ca,ci).second) continue;
		if(0<ci){
			ca[ci-1]^=1;
			q.emplace(ca,ci-1,cc+1);
			ca[ci-1]^=1;
		}
		if(ci+1<a.size()){
			ca[ci+1]^=1;
			q.emplace(ca,ci+1,cc+1);
			ca[ci+1]^=1;
		}
	}
	return -1;
}

int calc(vi a,int i)
{
	int n=a.size();

	int l=n;
	rep(j,n) if(a[j]) chmin(l,j);

	int res=0;
	for(;l<i;res++)
		a[--i]^=1;

	int r=l;
	per(j,n) if(a[j]) chmax(r,j);

	for(;i<r;){
		a[++i]^=1;
		res++;
		if(a[i]){
			a[++i]^=1;
			a[--i]^=1;
			res+=2;
			if(i==r-1)
				break;
		}
	}

	return res;
}

int solve(vi a,int i)
{
	int n=a.size();

	if(count(all(a),1)==1&&a[i])
		return 3;

	int l=INF,r=-INF;
	rep(i,n) if(a[i]) chmin(l,i),chmax(r,i);

	if(i<=l){
		int res=0;
		if(i==l){
			a[++i]^=1;
			a[--i]^=1;
			res+=2;
			r=-INF;
			rep(i,n) if(a[i]) chmax(r,i);
		}
		for(;i<r;){
			a[++i]^=1;
			res++;
			if(a[i]){
				a[++i]^=1;
				a[--i]^=1;
				res+=2;
				if(i==r-1)
					break;
			}
		}
		return res;
	}
	if(r<=i){
		reverse(all(a));
		i=n-1-i;
		return solve(a,i);
	}

	assert(l<i&&i<r);

	int res1=calc(a,i);
	int res2=calc(vi(rbegin(a),rend(a)),n-1-i);
	return min(res1,res2);
}

int main()
{
	#ifndef _GLIBCXX_DEBUG
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	constexpr char endl='\n';
	#endif

	for(int i;cin>>i&&i;){
		i--;
		vi a;
		{
			string s; cin>>s;
			for(char c:s) a.push_back(c=='#');
		}
		cout<<solve(a,i)<<endl;
	}
}
0