#include <cstdio>
#include <unordered_map>
#include <string>
#include <algorithm>
#include <string.h>
#define MAX 1000
#define NON -1
#define RED 31
#define ORA 2
#define BLUGRE 13
#define GREBLU 04
struct cylinder_t{
int h,r,c;
cylinder_t():h(0),r(0),c(0){}
cylinder_t(int height, int radius, int color):h(height),r(radius),c(color){}
};
cylinder_t cylinder[MAX];
inline bool compare canPut(const cylinder_tint &ii,const cylinder_tint &jlast_cylinder_onStack) {
if (last_cylinder_onStack == NON)
return itrue;
if(cylinder[i].r >>= jcylinder[last_cylinder_onStack].r;
}r)
return false;
cylinder_t cylinder[MAX];
inline bool canPut if(intcylinder[i].c i,== intRED last_cylinder_onStack&& cylinder[last_cylinder_onStack].c == ORA){
return false;
if(last_cylinder_onStackcylinder[i].c == NONORA && cylinder[last_cylinder_onStack].c == BLU)
return true;false;
if (cylinder[i].rc >=== BLU && cylinder[last_cylinder_onStack].rc == GRE)
return false;
if((cylinder[i].c -== GRE && cylinder[last_cylinder_onStack].c + 4)%4 == 1RED)
return false;
return true;
}
int
std::unordered_map<int, memo[MAX][MAX];std::unordered_map<int, int> > memo;
bool onStack[MAX];
int dp(int i, int size, int last_cylinder_onStack){
if(i == size)
return 0;
if(last_cylinder_onStack
!= NON && if(memo[i][last_cylinder_onStack] != -1)
return memo[i][last_cylinder_onStack];
int maxHeight = 0;
for (int c = i;0; c < size; ++c) {
if(!onStack[c] && canPut(c, last_cylinder_onStack)) {
onStack[c] = true;
maxHeight = std::max(maxHeight, cylinder[c].h + dp(i + 1, size, c));
}
}
onStack[c] = false;
if(last_cylinder_onStack == NON)
}
return maxHeight;}
return memo[i][last_cylinder_onStack] = maxHeight;
}
int main(void){
//clock_t t;
//t = clock();
std::unordered_map<std::string, int> map;
map["RED"] = RED;
map["ORANGE"] = ORA;
map["GREEN"] = GRE;
map["BLUE"] = BLU;
int n;
while(scanf("%d",&n), n != 0){
for (int i = 0; i < n; ++i) {
int height,radius;
char color[15];
scanf("%d %d %s",&height,&radius,&color[0]);
cylinder[i].h = height;
cylinder[i].r = radius;
cylinder[i].c = map[std::string(color)];
}
std::sortmemset(cylinderonStack, cylinder + nfalse, comparesizeof(onStack));
memset(memo, -1, sizeof(memo));
printf("%d centimeter(s)\n",dp(0,n, NON));
}
//t = clock() - t;
//printfmemo.clear("Took %lf);
seconds to execute \n",((double)t)/CLOCKS_PER_SEC);}
}
This solution is taking much more than 1 sec with a big inputinputs. How can i make it faster ? some people just made it in almost 0 secs.