CODEVS 必做题:3149、2821、1531、3369、1230

3149 爱改名的小融 2

 

 时间限制: 2 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold
 
 
 
题目描述 Description

Wikioi上有个人叫小融,他喜欢改名。
现在他的要求变了,只要是英文字母就是他的名字。
先给你N个名字,请你一一判断是不是小融。
本题还加强了测试数据

输入描述 Input Description

N
N行名字(全部为字符)

输出描述 Output Description

 N行,YES或NO(大写)

样例输入 Sample Input

3
&6*14315
Rinkement
micsloox

样例输出 Sample Output

NO

YES

YES

数据范围及提示 Data Size & Hint

对于40%的数据 N≤10 名字长度≤100
对于100%的数据 N≤50 名字长度≤100000

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define N 100010
char str[N];int n;
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%s",str);
        int flag=0,len=strlen(str);
        for(int j=0;j<len;j++){
            if((str[j]>='a'&&str[j]<='z')||(str[j]>='A'&&str[j]<='Z'));
            else {flag=1;break;}
        }
        printf("%s
",flag?"NO":"YES");
    }
    return 0;
}

2821 天使之城

 

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold
 
 
题目描述 Description

天使城有一个火车站,每辆火车都从A方向驶入车站,
再从B方向驶出车站。

为了调度火车,火车站设有停放轨道,可存放5辆火车。已知从A进入车站顺序为1、2、3……。现在给你一个调度方案,判断是否可行,如果可行,输出出站顺序。
有以下几种调度方法:
A. 将A上的头一辆车驶出B方向
B. 将A上的头一辆车停入暂停轨道
C. 将暂停轨道上最外面的车驶出B方向

输入描述 Input Description

输入第一行一个整数N(n<30)表示调度方案步骤数目。
下一行一个字符串,有N个大写字母,表示调度方法。

输出描述 Output Description

输出若不可行(暂停站满了还停车、暂停站空了还出车),则输出一行“No”。
若可行,输出一行“Yes”,再输出若干行,每行一个整数,表示车出站序列。

样例输入 Sample Input

[样例输入1]
6
ABBCCA
[样例输入2]
5
BACAC

样例输出 Sample Output

[样例输出1]
Yes
1
3
2
4
[样例输出2]
No

数据范围及提示 Data Size & Hint

如题

AC代码:

C1

#include<cstdio>
#include<stack>
#include<queue>
#include<iostream>
using namespace std;
char str[50];int n,cur,wrong;
int main()
{
    scanf("%d",&n);
    scanf("%s",str+1);
    stack<int>s;
    queue<int>q;
    for(int i=1;i<=n;i++){
        char ch=str[i];
        if(ch=='A') q.push(++cur);
        else if(ch=='B'){
            if(s.size()>=5){wrong=1;break;}
            else s.push(++cur);
        }
        else{
            if(!s.size()) {wrong=1;break;}
            else q.push(s.top()),s.pop();
        }
    }
    if(wrong) printf("No
");
    else{
        printf("Yes
");
        while(!q.empty()){
            printf("%d
",q.front());q.pop();
        }
    }
    return 0;
}

 C2

#include<cstdio>
#include<iostream>
#include<stack>
#include<queue>
#include<vector>
#define M 43
using namespace std;
queue<int>q;
stack<int>s;
vector<int>v;
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) q.push(i);
    for(int i=1;i<=n;i++){
        char c;cin>>c;
        if(c=='A'){
            v.push_back(q.front());
            q.pop();
        }
        if(c=='B'){
            if(s.size()>=5){
                printf("No");
                return 0;
            }
            s.push(q.front());
            q.pop();
        }
        if(c=='C'){
            if(!s.size()){
                printf("No");
                return 0;
            }
            v.push_back(s.top());
            s.pop();
        }
    }
    printf("Yes
");
    for(int i=0;i<v.size();i++) printf("%d
",v[i]);
    return 0;
}

1531 山峰

 

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 钻石 Diamond
 
 
 
题目描述 Description

Rocky山脉有n个山峰,一字排开,从西向东依次编号为1, 2, 3, ……, n。每个山峰的高度都是不一样的。编号为i的山峰高度为hi。

小修从西往东登山。每到一座山峰,她就回头观望自己走过的艰辛历程。在第i座山峰,她记录下自己回头能看到的山峰数si。

何谓“能看到”?如果在第i座山峰,存在j<k<i,hj<hk,那么第j座山峰就是不可见的。除了不可见的山峰,其余的山峰都是可见的。

回家之后,小修把所有的si加起来得到S作为她此次旅行快乐值。现在n座山峰的高度都提供给你了,你能计算出小修的快乐值吗?

输入描述 Input Description

第一行一个整数n(n<=15000)。

第i+1(1<=i<=n)行是一个整数hi(hi<=109)。

输出描述 Output Description

仅一行:快乐值。

样例输入 Sample Input

5

2

1

3

5

9

样例输出 Sample Output

5

数据范围及提示 Data Size & Hint

说明:s1=0, s2=1, s3=2, s4=1, s5=1。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
int h[15010],n,g,ans;
stack<int>s;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&h[i]);
        ans+=s.size();
        while(s.size()>0&&h[i]>s.top()){
            s.pop();
        }
        s.push(h[i]);
    }  
    printf("%d
",ans);
    return 0;
}

3369 膜拜

 

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold
 
 
题目描述 Description

神牛有很多…当然…每个同学都有自己衷心膜拜的神牛.
某学校有两位神牛,神牛甲和神牛乙。新入学的N位同学们早已耳闻他们的神话。所以,已经衷心地膜拜其中一位了。
现在,老师要给他们分机房。
但是,要么保证整个机房都是同一位神牛的膜拜者,或者两个神牛的膜拜者人数差不超过M。
另外,现在N位同学排成一排,老师只会把连续一段的同学分进一个机房。老师想知道,至少需要多少个机房。

输入描述 Input Description

输入文件第一行包括N和M。
之后N行,每行一个整数,1表示神牛甲的崇拜者,2表示神牛乙的崇拜者。

输出描述 Output Description

输出一个整数,表示最小需要机房的数量。

样例输入 Sample Input

5 1 

2  


2

样例输出 Sample Output

2

数据范围及提示 Data Size & Hint

对于30%的数据,有1≤N,M≤50;
对于100%的数据,有1≤N,M≤2500

#include<cstdio>
#include<algorithm>
using namespace std;
#define N 3010
int n,m,s1[N],s2[N],f[N];
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1,x;i<=n;i++){
        f[i]=i;
        scanf("%d",&x);
        if(x==1)
            s1[i]=s1[i-1]+1,s2[i]=s2[i-1];
        else
            s1[i]=s1[i-1],s2[i]=s2[i-1]+1;
    }
    for(int i=1;i<=n;i++){
        for(int j=i;j>=0;j--){
            if(s1[i]-s1[j]==i-j||s2[i]-s2[j]==i-j||abs(s1[i]-s1[j]-(s2[i]-s2[j]))<=m){
                f[i]=min(f[i],f[j]+1);
            }
        }
    }
    printf("%d
",f[n]);
    return 0;
}

1230 元素查找

 

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 钻石 Diamond
 
 
题目描述 Description

给出n个正整数,然后有m个询问,每个询问一个整数,询问该整数是否在n个正整数中出现过。

输入描述 Input Description

第一行两个整数 n 和m。

第二行n个正整数(1<=n<= 100000)

第三行m个整数(1<=m<=100000)

输出描述 Output Description

一共m行,若出现则输出YES,否则输出NO

样例输入 Sample Input

4 2

2 1 3 4

1 9

样例输出 Sample Output

YES

NO

数据范围及提示 Data Size & Hint

所有数据都不超过10^8

#include<cstdio>
#include<algorithm>
using namespace std;
#define N 100100
int n,m,a[N],b[N];
int main(){
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%d",a+i);
    }
    for(int i=0;i<m;i++){
        scanf("%d",b+i);
    }
    sort(a,a+n);
    for(int i=0;i<m;i++){
        int p=lower_bound(a,a+n,b[i])-a;
        printf("%s
",a[p]==b[i]?"YES":"NO");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/shenben/p/5619089.html