This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
using namespace std;
const int T_MAX = 2001;
int nbBatiment;
int luxury[2 * T_MAX][2];
bool dp[2 * T_MAX][T_MAX][2];
int main()
{
ios::sync_with_stdio(false);
cin >> nbBatiment;
for(int iBat = 1; iBat <= 2 * nbBatiment; iBat++)
{
cin >> luxury[iBat][0];
}
for(int iBat = 1; iBat <= 2 * nbBatiment; iBat++)
{
cin >> luxury[iBat][1];
}
dp[0][0][0] = true;
dp[0][0][1] = true;
for(int iBat = 0; iBat < 2 * nbBatiment; iBat++)
{
for(int nbAVu = 0; nbAVu <= nbBatiment; nbAVu++)
{
for(int curPos = 0; curPos < 2; curPos++)
{
if(dp[iBat][nbAVu][curPos])
{
if(luxury[iBat + 1][curPos] >= luxury[iBat][curPos])
{
dp[iBat + 1][nbAVu + (curPos + 1) % 2][curPos] = true;
}
if(luxury[iBat + 1][(curPos + 1) % 2] >= luxury[iBat][curPos])
{
dp[iBat + 1][nbAVu + curPos][(curPos + 1) % 2] = true;
}
}
}
}
}
//cout << dp[1][1][0] << endl;
if(!dp[2 * nbBatiment][nbBatiment][0] && !dp[2 * nbBatiment][nbBatiment][1])
{
cout << -1 << endl;
return 0;
}
int iBat = 2 * nbBatiment, nbAVu = nbBatiment, curPos = 0;
if(dp[2 * nbBatiment][nbBatiment][1])
{
curPos = 1;
}
vector<char> reponse;
while(iBat > 0)
{
reponse.push_back(char(int('A') + curPos));
//cout << iBat << " " << nbAVu << " " << curPos << endl;
if(luxury[iBat - 1][curPos] <= luxury[iBat][curPos] && dp[iBat - 1][nbAVu - (curPos + 1) % 2][curPos])
{
iBat--;
nbAVu -= (curPos + 1) % 2;
}
else
{
iBat--;
nbAVu -= (curPos + 1) % 2;
curPos++;
curPos %= 2;
}
}
for(int i = reponse.size() - 1; i > -1; i--)
{
cout << reponse[i];
}
cout << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |