이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
///Dynamic Programming
///Determine the maximum amount of coal we can get from sending food
int n;
int memo[100005][3][3][3][3];
string s;
int convert(char c) ///Converts a character into a code
{
if(c=='M') return 0;
if(c=='F') return 1;
if(c=='B') return 2;
}
int production(vector<int>& prev,int mine,int next) ///Determines how many coal are produced with a given shipments
{
int a,b;
if(mine==1)
a=0,b=2;
else
a=2,b=4;
short used[4];
memset(used,0,sizeof(used));
int total=0;
for(int i=a;i<b;i++){
if(prev[i]==-1) continue;
int food=prev[i];
if(!used[food]){
used[food]=1;
total++;
}
}
if(!used[next])
total++;
return total;
}
int coal(int i,vector<int> prev)
{
if(i==n)
return 0;
bool negative=false;
for(int i=0;i<prev.size();i++){
if(prev[i]==-1)
negative=true;
}
int maximum=0;
vector<int> next=prev;
int food=convert(s[i]);
if(negative){ //If there are -1 does not store the answer
int k=production(prev,1,food); //Obtains the amount of coal if the food was shipped to mine 1
next[0]=prev[1];
next[1]=food;
maximum=max(maximum,coal(i+1,next)+k); //And we get the answer of that mine
next[0]=prev[0];
next[1]=prev[1];
next[2]=prev[3];
next[3]=food;
k=production(prev,2,food); //Obtains the amount of coal if the food was shipped to mine 2
maximum=max(maximum,coal(i+1,next)+k);
}
else{
if(memo[i][ prev[0] ][ prev[1] ][ prev[2] ][ prev[3] ]==0){ //Same but storing the values
int k=production(prev,1,food);
next[0]=prev[1];
next[1]=food;
maximum=max(maximum,coal(i+1,next)+k);
next[0]=prev[0];
next[1]=prev[1];
next[2]=prev[3];
next[3]=food;
k=production(prev,2,food);
maximum=max(maximum,coal(i+1,next)+k);
memo[i][ prev[0] ][ prev[1] ][ prev[2] ][ prev[3] ]=maximum;
}
maximum=memo[i][ prev[0] ][ prev[1] ][ prev[2] ][ prev[3] ];
}
return maximum;
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n;
cin>>s;
vector<int> A;
int food=convert(s[0]);
A.push_back(-1);
A.push_back(food);
A.push_back(-1);
A.push_back(-1); //We always send food 1 to mine 1 because we obtain the same results sending it to both mines
int total=coal(1,A)+1; //Because that we add one to the answer
cout<<total<<'\n';
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
miners.cpp: In function 'int coal(int, std::vector<int>)':
miners.cpp:41:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<prev.size();i++){
~^~~~~~~~~~~~
miners.cpp: In function 'int convert(char)':
miners.cpp:13:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |