Arthur and Brackets

n<605设计出n对夸号  给出n个条件每个条件为[l,r] 表示第i对夸号右夸号离左夸号的距离,然后夸号的右夸号出现的顺序必须按照给的顺序 出现, 那么如果存在就输出否则输出impossilbe , 我们知道如果一个夸号在 L位置,那么 另一个夸号就在 L+cnt 这个位置, 那么我们就可以知道在L=1 和L+cnt-1 这之间的夸号只要合法就可以了

#include <iostream>
#include <cstdio>
#include<algorithm>
#include <string.h>
using namespace std;
int dp[605][605],L[605],R[605];
int get[605][605];
int dfs(int l, int r){
   if(dp[l][r]!=0) return dp[l][r];
   if(l==r) return dp[l][r]=1;
   for(int k=L[l]; k<=R[l]; k+=2){
       int left=k/2;
       if( left + l + 1 > r ) break;
       if(dfs(l+1,l+left+1)==1&&dfs(l+left+1,r)==1){
          get[l][r]=left;
          return dp[l][r]=1;
       }
   }
  return dp[l][r]=2;
}
void print(int l, int r){
   if(l==r) return ;
   printf("(");
    print(l+1,l+get[l][r]+1);
    printf(")");
    print(l+get[l][r]+1,r);
}
int main()
{
    int n;
    while(scanf("%d",&n)==1){
         memset(dp,0,sizeof(dp));
         for(int i=0; i<n; ++i){
             scanf("%d%d",&L[i],&R[i]);
             if(L[i]%2==0) L[i]++;
         }
         if(dfs(0,n)==1){print(0,n); printf("\n");}
         else puts("IMPOSSIBLE");

    }
    return 0;
}
#include <iostream>
#include <cstdio>
#include<algorithm>
#include <string.h>
using namespace std;
int dp[605][605],L[605],R[605];
int get[605][605];
int dfs(int l, int r){
   if(dp[l][r]!=0) return dp[l][r];
   if(l==r) return dp[l][r]=1;
   for(int k=L[l]; k<=R[l]; k+=2){
       int left=k/2;
       if( left + l + 1 > r ) break;
       if(dfs(l+1,l+left+1)==1&&dfs(l+left+1,r)==1){
          get[l][r]=left;
          return dp[l][r]=1;
       }
   }
  return dp[l][r]=2;
}
void print(int l, int r){
   if(l==r) return ;
   printf("(");
    print(l+1,l+get[l][r]+1);
    printf(")");
    print(l+get[l][r]+1,r);
}
int main()
{
    int n;
    while(scanf("%d",&n)==1){
         memset(dp,0,sizeof(dp));
         for(int i=0; i<n; ++i){
             scanf("%d%d",&L[i],&R[i]);
             if(L[i]%2==0) L[i]++;
         }
         if(dfs(0,n)==1){print(0,n); printf("\n");}
         else puts("IMPOSSIBLE");

    }
    return 0;
}
时间: 2024-10-05 12:39:29

Arthur and Brackets的相关文章

codeforces 508 E. Arthur and Brackets

题目链接: http://codeforces.ru/problemset/problem/508/E 题意: 有n对括号,按顺序,给出每对括号长度的范围,输出一种情况. 限制: 1 <= n <= 600 思路: 贪心:能匹配的先匹配. 括号匹配问题,果断先思考用栈能不能做. C++ Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3

Codeforces 508E Arthur and Brackets

题意: 给出括号序列中每个右括号可能离对应左括号多远  求这个括号序列 思路: 记忆化搜索解决  用f[l][r]表示对于第l个左括号到第r个左括号区间最前面的左括号与其对应右括号的距离 状态只有n^2个  不用担心TLE 求f[l][r]的方法为  如果最前的左括号可以包住l+1~r个括号就尝试包起来  否则将l~r分治为l~x和x+1~r两个子问题 代码: #include<cstdio> #include<iostream> #include<cstring> #

CF149D. Coloring Brackets[区间DP !]

不知道为什么居中了,先把代码放这 #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int N=705,MOD=1e9+7; char s[N]; long long n,f[N][N][5][5]; int st[N],top=0,m[N]; void match(){ for(int i=1;i<=

POJ 2955 Brackets (动规)

Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2999   Accepted: 1536 Description We give the following inductive definition of a "regular brackets" sequence: the empty sequence is a regular brackets sequence, if s is a reg

Codeforces 552E Vanya and Brackets(贪心 + 表达式计算)

题目链接 Vanya and Brackets 题目大意是给出一个只由1-9的数.乘号和加号组成的表达式,若要在这个表达式中加上一对括号,求加上括号的表达式的最大值. 我们发现,左括号的位置肯定是最左端或者某个乘号右边,右括号的位置肯定是最右段或者某个乘号左边. 而乘号最多只有15个,那么暴力枚举就可以了. #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b);

POJ3682 King Arthur&#39;s Birthday Celebration

King Arthur is an narcissist who intends to spare no coins to celebrate his coming K-th birthday. The luxurious celebration will start on his birthday and King Arthur decides to let fate tell when to stop it. Every day he will toss a coin which has p

POJ 2955 Brackets

Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6622   Accepted: 3558 Description We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets sequence, if s is a regular

前端开发利器-Brackets IDE

是什么? http://brackets.io/ A modern, open source text editor that understands web design. 现代, 开源的文本编辑器, 最懂得web设计. With focused visual tools and preprocessor support, Brackets is a modern text editor that makes it easy to design in the browser. 专注可视化工具

POJ1141 Brackets Sequence

Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a regular sequence, then (S) and [S] are both regular sequences. 3. If A and B are regular sequences, then AB is a regular