(BIT小学期)(16-20)

               一个一个写着太麻烦了,每天有限量,整合到一起写了。

16.括号匹配

代码如下:

#include <stdio.h>  
#include <string.h>  
long long L[100010]={0},R[100010]={0},sum=0,record;  
int match(char c1,char c2)  
{  
    return c1=='('&&c2==')'?1:0;  
}  
void simplify(char s[])  
{  
    char stack[100010];  
    int count,top=-1;  
    for(int i=0;i<strlen(s);i++)  
    {  
        if(top!=-1&&match(stack[top],s[i]))  top--;  
        else  stack[++top]=s[i];  
    }  
    if (top==-1)  
    {  
        sum++;  
        return;  
    }  
    char c=stack[top];  
    count=top+1;  
    for(;top>=0;top--)   
    {  
        if(stack[top]!=c)  return;  
    }  
    if (c=='(')  L[count]++;  
    else  R[count]++;  
}  
int main()  
{  
    int n,i;  
    char str[100010];  
    scanf("%d\n",&n);  
    for(i=0;i<n;i++)  
    {  
        scanf("%s",str);  
        simplify(str);  
    }  
    record=sum/2;  
    for(i=0; i<100010; i++)  record+=L[i]<R[i]?L[i]:R[i];  
    printf("%lld\n",record);  
}

17. 看楼房

Description

小张在暑假时间进行了暑期社会调查。调查的内容是楼房的颜色如何影响人们的心情。于是他找到了一个楼房从左到右排成一排的小区,这个小区一共有n栋楼房,每个楼房有一个颜色c_i和一个高度h_i。小张调查的内容为每次他站在第i栋楼和第i+1栋楼之间向左看,他记录下此时他看到的楼房颜色数作为他的调查结果。

由于小张在暑假时间沉迷游戏来不及做实地调查,只好拜托你将调查结果告诉他。

Input

本题有多组数据。

每组数据第一行一个整数n。表示有n栋楼房从左到右排成一排。

第二行n个数,表示每个楼房的颜色(1 \leq c_i \leq 10^6 )

第三行n个数,表示每个楼房的高度(1 \leq c_i \leq 10^9 )

数据保证所有组数据的\sum{n} \leq 1000000

Output

每组数据输出n个数,第i个数表示他站在第i栋楼和第i+1栋楼之间向左看,能够看到的楼房颜色数。

Notes

在从左向右看楼房的时候,左边较矮的楼房会被右边较高的楼房挡住。

测试输入期待的输出时间限制内存限制额外进程
测试用例 1以文本方式显示
  1. 2↵
  2. 5↵
  3. 1 2 3 4 5↵
  4. 1 3 4 2 5↵
  5. 5↵
  6. 1 2 4 4 5↵
  7. 1 3 4 2 5↵
以文本方式显示
  1. 1 1 1 2 1↵
  2. 1 1 1 1 1↵
1秒64M0
测试用例 5以文本方式显示
  1. 1↵
  2. 1000000↵
  3. 210894 163116 160080 147818 149341 182487 47691 125431 166628 66786 126057 200700 137717 98602 140619 92537 102889 156231 68679 114080 157904 93696 38769 7804 129770 45661 204057 37615 856 103262 93884 140584 198480 23847 17513 14915 38494 29791 177935 39755 184152 136545 93142 197906 15027 162566 183049 28091 156593 123729 150865 199551 104819 34482 158665 54827 124961 26337 61727 14651 9188 163144 43688 107614 129606 2310 211872 194589 186685 21098 202160 200098 12478 109407 11159 87094 185644 35895 196518 189298 192129 165236 45550 78827 209013 113906 134385 111098 49910 9668 151568 78011 136402 170946 36058 196974 147552 139978 121334 151780 162817 135327 121725 83773 132784 22940 8280 69937 138789 1158 35948 117150 3791 211172 35357 93901 120335 209961 12180 152105 70044 101248 30397 34496 179404 56621 126541 132172 75359 200615 98020 146517 113303 37415 11599 97790 93061 26565 73803 106871 27597 5012 89464 159927 79212 187429 171423 194322 36322 160011 215349 59136 131780 61954 115304 113900↵
以文本方式显示
  1. 1 1 2 3 2 3 4 3 2 3 3 4 3 4 5 4 5 3 4 2 3 4 5 6 4 5 5 6 5 5 6 7 8 8 9 10 10 6 2 3 3 4 3 3 4 5 5 5 6 5 6 7 7 7 8 7 8 8 9 8 9 8 8 5 6 7 7 8 9 9 1 2 2 3 3 3 4 4 3 4 5 6 5 4 5 2 3 2 3 3 3 3 1 2 2 3 4 4 3 3 4 4 5 6 7 5 5 6 6 6 7 8 7 5 6 6 6 7 5 6 7 7 8 9 4 5 2 3 3 4 3 4 4 5 6 6 3 4 4 5 5 5 5 6 6 6 6 7 8 4 5 5 6 6 7 5 6 6 6 7 8 9 6 7 1 2 3 3 4 4 2 3 4 3 4 5 6 5 6 2 3 4 4 4 5 4 5 6 6 5 6 5 6 5 5 6 6 6 6 7 2 3 3 3 4 3 4 2 3 3 4 5 3 4 4 3 4 5 6 7 8 8 3 4 5 4 5 5 4 5 5 4 5 6 6 5 6 2 3 2 3 4 5 5 3 1 2 2 3 4 4 1 2 3 4 4 5 2 3 4 2 3 3 2 3 4 5 2 3 3 4 4 5 6 5 5 3 4 4 4 4 5 5 4 5 5 5 6 6 7 7 6 7 4 5 5 6 6 2 3 4 3 4 4 5 6 7 5 5 6 6 7 8 9 3 4 5 6 4 5 3 4 5 6 6 7 8 9 5 6 6 7 7 6 7 8 6 7 7 8 8 8 9 8 5 6 6 6 7 7 8 8 9 6 7 7 8 8 8 8 8 9 9 9 10 11 9 10 10 11 11 3 4 4 4 5 6 6 6 6 5 6 6 7 5 6 5 4 5 5 6 7 8 7 6 6 7 8 5 6 7 8 4 5 5 6 5 2 3 4 3 4 4 3 3 4 4 5 4 4 4 5 6 6 4 5 5 5 3 4 5 6 4 3 4 4 5 4 3 4 5 6 4 5 5 5 5 5 5 4 5 6 3 4 5 4 4 4 5 6 6 5 6 7 7 6 7 8 9 7 8 9 8 8 9 9 7 8 9 9 9 9 10 9 10 8 9 6 7 7 8 8 7 8 8 8 8 9 9 10 11 10 9 10 9 ↵
1秒64M0

代码如下: 

#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <stdbool.h>  
#define WIDTH 1000010  
#define HASH_SIZE 1000003  
int T,i,n,temp,color_class,top_color=-1,top_height=-1;  
int color[WIDTH],height[WIDTH],stack_color[WIDTH],stack_height[WIDTH];  
typedef struct HashNode  
{  
    int key;  
    int value;  
    bool used;  
}HashNode;  
HashNode hashTable[HASH_SIZE];  
unsigned int hash(int key)  
{  
    unsigned int h=0;  
    h=key%HASH_SIZE;  
    return h;  
}  
void insert(int key,int value)  
{  
    unsigned int pos=hash(key);  
    while (hashTable[pos].used&&hashTable[pos].key!=key)  pos=(pos+1)%HASH_SIZE;  
    hashTable[pos].key=key;  
    hashTable[pos].value=value;  
    hashTable[pos].used=true;  
}  
int search(int key)  
{  
    unsigned int pos=hash(key);  
    while (hashTable[pos].used&&hashTable[pos].key!=key)  pos=(pos+1)%HASH_SIZE;  
    if (hashTable[pos].used)  return hashTable[pos].value;  
    else  return 0;  
}  
int main()  
{  
    scanf("%d",&T);  
    while (T-->0)  
    {  
        while(top_color!=-1)  
        {  
            top_color--;  
            top_height--;  
        }  
        memset(hashTable,0,sizeof(hashTable));  
        color_class=0;  
        scanf("%d", &n);  
        for(i=0;i<n;i++)  scanf("%d",&color[i]);  
        for(i=0;i<n;i++)  scanf("%d",&height[i]);  
        for(i=0;i<n;i++)  
        {  
            if(top_height==-1||height[i]<stack_height[top_height])  
            {  
                stack_color[++top_color]=color[i];  
                stack_height[++top_height]=height[i];  
                int count=search(color[i])+1;  
                insert(color[i],count);  
                if(count==1)  color_class++;  
            }  
            else  
            {  
                while(top_height!=-1&&height[i]>=stack_height[top_height])  
                {  
                    int count=search(stack_color[top_color])-1;  
                    insert(stack_color[top_color],count);  
                    if(count==0)  color_class--;  
                    top_height--;  
                    top_color--;  
                }     
                if (top_height==-1||height[i]<stack_height[top_height])  
                {  
                    stack_color[++top_color]=color[i];  
                    stack_height[++top_height]=height[i];  
                    int count=search(color[i])+1;  
                    insert(color[i],count);  
                    if(count==1)  color_class++;  
                }  
            }  
            printf("%d%c",color_class,i==n-1?'\n':' ');  
        }  
    }  
    return 0;  
}  

18. 填坑Ⅰ

 

代码如下:

#include <stdio.h>  
#include <stdbool.h>  
#define WIDTH 200001  
int height[WIDTH];  
int face[WIDTH];  
int top=-1;  
void push(int x)  
{  
    face[++top]=x;  
}  
void pop()  
{  
    --top;  
}  
int peek()  
{  
    return face[top];  
}  
int main(void)  
{  
    int n,i,max;  
    while(scanf("%d",&n)!=EOF)  
    {  
        top=-1;  
        max=0;  
        for(i=0;i<n;i++)  
        {  
            scanf("%d",&height[i]);  
            if(height[i]>max)  max=height[i];  
        }  
        for(i=0;i<n;i++)  
            height[i]=(max-height[i])%2;  
        for(i=0;i<n;i++)  
        {  
            (top==-1)?push(height[i]):((height[i]==peek())?pop():push(height[i]));  
        }  
        (top<=0)?printf("YES\n"):printf("NO\n");  
    }  
}

19. 填坑Ⅱ

代码如下: 

#include <stdio.h>  
#include <string.h>  
int main()  
{  
    long long n,i,max,cnt,top=-1;  
    long long stk[200010],a[200010]={0};  
    while(scanf("%lld",&n)!=EOF)  
    {  
        memset(a,0,sizeof(a[0]));  
        top = -1;  
        max = 0;  
        for(i=0;i<n;i++)  
        {  
            scanf("%lld",&a[i]);  
            if (a[i]>max)  max=a[i];  
        }  
        for(i=0;i<n;i++)  
        {  
            if(!(top==-1)&&a[i]==stk[top])  top--;  
            else  
            {  
                if(!(top==-1)&stk[top]<a[i])  break;  
                else  stk[++top]=a[i];  
            }  
        }  
        cnt=top+1;  
        if(!cnt)  printf("YES\n");  
        else if(cnt==1&&stk[top]==max)  printf("YES\n");  
        else  printf("NO\n");  
    }  
} 

20. 选举

 在遥远的相簿国有两个种族,分别是冬马族和雪菜族,每个人都属于这两个种族之一。如今,又到了选举元首的时候了,所有 n 个人排成一排等待投票。

    相簿国的选举制度比较奇特,并不是看谁的票数多,而是使用一票否决制度,并且所有人都可以投一张票或者不投票。如果排在前面的人把排在后面的人一票否决了,那么后面的人不能参与竞选,也不能投出他的那一票了。而如果排在后面的人把排在前面的人一票否决了,那么排在前面的人不能参与竞选,但是已经投出的那一票仍然有效。如果一轮投完仍然有不只一个竞选者,那么这些留下的人再投一轮,还没有停止就再投一轮……直到剩下一人为止,但投票的先后顺序不变。

    为了保证每轮投票中都有更多人可以投票,规定竞选者优先投票给在他投票时已经投过票的异族人,每轮投票结束时重置竞选人的投票状态

    所有人都希望自己族的人能当选元首(而不是希望自己当元首),并且会为此采取最优策略。现在给出排队中每个人的种族,请问最后是哪个种族的人会当选元首?

输入

输入第一行一个数 n ,表示相簿国里的人数。

第二行一个长度为 n 的字符串 s ,从前到后依次表示排队投票的人所属的种族。D 表示冬马族,而 X 表示雪菜族。

输出

输出一个字符 D 或 X ,表示是哪一族的人当选。

注意

数据保证1<=n <=200000。

测试用例

用例1.

输入:

5↵

DDXXX↵

输出:

D↵

用例2.

输入:

6↵

DDXXXX↵

输出:

X↵

测试输入期待的输出时间限制内存限制额外进程
测试用例 1以文本方式显示
  1. 5↵
  2. DDXXX↵
以文本方式显示
  1. D↵
1秒64M0
测试用例 2以文本方式显示
  1. 6↵
  2. DDXXXX↵
以文本方式显示
  1. X↵
1秒64M0

代码如下: 

#include <stdio.h>  
#define max 200010  
int main()  
{  
    char S[max],s[max],head;  
    int n,i,num,D=0,X=0,D1=0,X1=0,top=-1;  
    scanf("%d",&n);  
    scanf("%s",s);  
    for(i=0;i<n;i++)  
    {  
        if(s[i]=='D')  
        {  
            if(X==0)  
            {  
                if(top==-1)  
                {  
                    S[++top]='D';  
                    D++;  
                }  
                else  
                {  
                    if(S[top]=='D')  
                    {  
                        S[++top]='D';  
                        D++;  
                    }  
                    else  
                    {  
                        top--;  
                        num=1;  
                        while(i<n-1&&s[i+1]=='D'&&top!=-1&&S[top]=='X')  
                        {  
                            i++;  
                            top--;  
                            num++;  
                        }  
                        while(num--)  S[++top]='D';  
                    }  
                }  
            }  
            else  X--;  
        }  
        else if(s[i]=='X')  
        {  
            if(D==0)  
            {  
                if(top==-1)  
                {  
                    S[++top]='X';  
                    X++;  
                }  
                else  
                {  
                    if(S[top]=='X')  
                    {  
                        S[++top]='X';  
                        X++;  
                    }  
                    else  
                    {  
                        top--;  
                        num=1;  
                        while(i<n-1&&s[i+1]=='X'&&top!=-1&&S[top]=='D')  
                        {  
                            i++;  
                            top--;  
                            num++;  
                        }  
                        while(num--)  S[++top]='X';  
                    }  
                }  
            }  
            else  D--;  
        }  
    }  
    while(top!=-1)  
    {  
        if(top==0)  head=S[top];  
        if(S[top]=='D')  D1++;  
        else  X1++;  
        top--;  
    }  
    if(D1>X1)  printf("D\n");  
    else if(X1>D1)  printf("X\n");  
    else  printf("%c\n",head);  
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值