ARC074 E RGB sequence(800)

高次元DPをやる

dp[300][300][300]を作るのは雰囲気でそうなるが、工夫しないとMLEをするので頑張る

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<vector>
  4. #include<queue>
  5. #define mod 1000000007;
  6. #define lol(i,n) for(int i=0;i<n;i++)
  7. typedef long long ll;
  8. using namespace std;
  9.  
  10. ll dp[303][303][303];
  11. int rest[310][4];
  12.  
  13. bool Cando(int a,int b,int c){
  14. int e1=max(a,max(b,c));
  15. int e3=min(a,min(b,c));
  16. int e2=a+b+c-e1-e3;
  17. if(rest[e1][1]<=e2&&e2<rest[e1][0]&&rest[e1][3]<=e3&&e3<rest[e1][2]){
  18. return true;
  19. }
  20. return false;
  21. }
  22.  
  23. int main(){
  24. int n,m;
  25. cin>>n>>m;
  26. lol(i,n+1){
  27. rest[i][0]=mod;//left1
  28. rest[i][2]=mod;//left2
  29. rest[i][1]=-1;//right2
  30. rest[i][3]=-1;//right3
  31. }
  32. lol(i,m){
  33. int a,b,c;
  34. cin>>a>>b>>c;
  35. if(c==1)rest[b][0]=min(rest[b][0],a);
  36. if(c==3)rest[b][3]=max(rest[b][3],a);
  37. if(c==2){
  38. rest[b][1]=max(rest[b][1],a);
  39. rest[b][2]=min(rest[b][2],a);
  40. }
  41. }
  42. lol(i,n+1)lol(j,n+1)lol(k,n+1)dp[i][j][k]=0;
  43. dp[0][0][0]=1;
  44. for(int len=0;len<=n;len++){
  45. for(int i=0;i<max(1,len);i++){
  46. for(int j=0;j<max(1,i);j++){
  47. int R=len,G=i,B=j;
  48. if(Cando(R,G,B)==false)continue;
  49. int a=len+1,b=G,c=B;
  50. int e1=max(a,max(b,c));
  51. int e3=min(a,min(b,c));
  52. int e2=a+b+c-e1-e3;
  53. dp[e1][e2][e3]+=dp[R][G][B];
  54. dp[e1][e2][e3]%=mod;
  55. a=R,b=len+1,c=B;
  56. e1=max(a,max(b,c));
  57. e3=min(a,min(b,c));
  58. e2=a+b+c-e1-e3;
  59. dp[e1][e2][e3]+=dp[R][G][B];
  60. dp[e1][e2][e3]%=mod;
  61. a=R,b=G,c=len+1;
  62. e1=max(a,max(b,c));
  63. e3=min(a,min(b,c));
  64. e2=a+b+c-e1-e3;
  65. dp[e1][e2][e3]+=dp[R][G][B];
  66. dp[e1][e2][e3]%=mod;
  67. }
  68. }
  69. }
  70. ll ans=0;
  71. for(int i=0;i<n;i++){
  72. for(int j=0;j<max(i,1);j++){
  73. if(Cando(n,i,j)){
  74. ans+=dp[n][i][j];
  75. ans%=mod;
  76. }
  77. }
  78. }
  79. ans%=mod;
  80. cout<<ans<<endl;
  81. return 0;
  82. }

無理にコードを短くしないようにする

 (愚直に分かりやすく書く)