# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
39609 | Hassoony | Retro (COCI17_retro) | C++14 | 500 ms | 117360 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int inf=(1<<30);
const int MX=309;
char a[MX][MX];
int n,m,dp[MX][MX][MX];
int DP(int x,int y,int open){
if(y<=0||y>m||open<0)return -inf;
if(x==0){
if(open)return -inf;
return 0;
}
if(a[x][y]=='*')return -inf;
int &ret=dp[x][y][open];if(ret!=-1)return ret;
vector<pair<char,int> >v;
v.push_back({a[x-1][y-1],y-1});
v.push_back({a[x-1][y],y});
v.push_back({a[x-1][y+1],y+1});
sort(v.begin(),v.end());
for(auto pp:v){
int open1=open,add=0;
if(pp.first=='(')open1++;
if(pp.first==')')open1--,add=2;
ret=max(ret,DP(x-1,pp.second,open1)+add);
ret=max(ret,DP(x-1,pp.second,open));
}
return ret;
}
void FDP(int x,int y,int open){
if(y<=0||y>m||open<0)return;
if(x==0)return;
if(a[x][y]=='*')return;
int &ret=dp[x][y][open];
vector<pair<char,int> >v;
v.push_back({a[x-1][y-1],y-1});
v.push_back({a[x-1][y],y});
v.push_back({a[x-1][y+1],y+1});
sort(v.begin(),v.end());
for(auto pp:v){
int open1=open,add=0;
if(pp.first=='(')open1++;
if(pp.first==')')open1--,add=2;
if(ret==DP(x-1,pp.second,open1)+add){
if(pp.first!='.')printf("%c",pp.first);
FDP(x-1,pp.second,open1);
return;
}
if(ret==DP(x-1,pp.second,open)){
FDP(x-1,pp.second,open);
return;
}
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
}
}
memset(dp,-1,sizeof(dp));
int j;
for(int i=1;i<=m;i++){
if(a[n][i]=='M')j=i;
}
cout<<DP(n,j,0)<<endl;
FDP(n,j,0);
puts("");
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |