期望得分:100+60+70=230

实际得分:0+60+0=60

 

T1

 

2017 清北济南考前刷题Day 2 afternoon

 

可以证明如果一对括号原本就匹配,那么这对括号在最优解中一定不会被分开

所以用栈记录下没有匹配的括号

最后栈中一定是 一堆右括号然后一堆左括号

ans=栈中右括号/2 上取整 + 栈中左括号 /2 上取整

 

#include<cstdio>
#include<cstring>

using namespace std;

#define N 100001

int top;
char st[N];

char s[N];

int main()
{
    freopen("shower.in","r",stdin);
    freopen("shower.out","w",stdout);
    scanf("%s",s+1);
    int len=strlen(s+1);
    for(int i=1;i<=len;i++)
        if(s[i]=='(') st[++top]='(';
        else if(top && st[top]=='(') top--;
        else st[++top]=')';
    int ans=0;
    int i;
    for(i=1;i<=top;i++)
        if(st[i]!=')') break;
    i--;
    printf("%d",(i+1)/2+(top-i+1)/2);
}
View Code

相关文章: