2017-10-3 清北刷题冲刺班p.m

 

a


【问题描述】
你是能看到第一题的 friends 呢。
——hja
给你一个只有小括号和中括号和大括号的括号序列,问该序列是否合法。
【输入格式】
一行一个括号序列。
【输出格式】
如果合法,输出 OK,否则输出 Wrong。
【样例输入】
[(])
【样例输出】
Wrong
【数据范围与规定】
70%的数据,1 ≤ ? ≤ 100。
对于100%的数据,1 ≤ ? ≤ 10000,所有单词由大写字母组成。

#include<iostream>
#include<cstring>
#include<cstdio>
#define maxn 10010
using namespace std;
int top;
char ch[maxn],st[maxn];
int main(){
    //freopen("Cola.txt","r",stdin);
    freopen("a.in","r",stdin);freopen("a.out","w",stdout);
    scanf("%s",ch+1);
    int len=strlen(ch+1);
    for(int i=1;i<=len;i++){
        if(ch[i]=='('||ch[i]=='['||ch[i]=='{')st[++top]=ch[i];
        else if(ch[i]==')'){
            if(top==0||st[top]!='('){
                printf("Wrong");
                return 0;
            }
            else top--;
        }
        else if(ch[i]==']'){
            if(top==0||st[top]!='['){
                printf("Wrong");
                return 0;
            }
            else top--;
        }
        else if(ch[i]=='}'){
            if(top==0||st[top]!='{'){
                printf("Wrong");
                return 0;
            }
            else top--;
        }
    }
    if(top!=0)printf("Wrong");
    else printf("OK");
    fclose(stdin);fclose(stdout);
    return 0;
}
100分 栈模拟

 


b


【问题描述】
你是能看到第二题的 friends 呢。
——laekov
Yjq 想要将一个长为?宽为?的矩形棺材(棺材表面绝对光滑,所以棺材可
以任意的滑动)拖过一个 L 型墓道。
如图所示,L 型墓道两个走廊的宽度分别是?和?,呈 90°,并且走廊的长
度远大于?。
现在 Hja 想知道对于给定的?,?,?,最大的?是多少,如果无论如何棺材都
不可能通过,则输出"My poor head =("
【输入格式】
第一行三个用空格分隔的整数?,?,?,意义如题目所示。
【输出格式】
输出最大的可能的?,保留七位小数,如果无论如何棺材都不可能通过,则
输出"My poor head =("。
【样例输入 1】
2 2 1
【样例输出 1】
1.0000000
P100 zhxb
第 4 页 共 5 页
【样例输入 2】
2 2 2
【样例输出 2】
2.0000000
【样例输入 3】
2 2 3
【样例输出 3】
1.3284271
【样例输入 4】
2 2 6
【样例输出 4】
My poor head =(
【数据范围与规定】
对于100%的数据,1 ≤ ?,?,? ≤ 10 4 。

#include<cmath>
#include<cstdio>
#include<iostream>
using namespace std;
int a,b,l;
double q,e,ans;
int main(){
    freopen("b.in","r",stdin);freopen("b.out","w",stdout);
    scanf("%d%d%d",&a,&b,&l);
    q=sqrt(a*a+b*b);
    e=l*1.0/2;
    ans=min(double(l),q-e);
    if(l<=b)ans=max(ans,min(double(a),double(l)));
    if(ans<0)printf("My poor head =(");
    else printf("%.7lf",ans);
    fclose(stdin);fclose(stdout);
    return 0;
}
18分 乱搞
/*
    两种情况:
    1.在拐角处被卡:以最左下角的点为原点,用矩形与坐标轴相交的两个点可以求出矩形一边的直线的方程,通过右上方点到这个直线的距离可以求出此时的宽度,然后可以转换成一个以宽度为因变量的函数,这个函数是一个单峰函数,用三分求解 
    2.在拐角处垂直向上移动,直接输出b 
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int a,b,l;
double get(double v1)
{
    double v2=sqrt(l*l-v1*v1);
    if (a*v1+b*v2<v1*v2) return -1e+20;
    else return (a*v1+b*v2-v1*v2)/l;
}
int main()
{
    freopen("b.in","r",stdin);
    freopen("b.out","w",stdout);
    scanf("%d%d%d",&a,&b,&l);
    if (a>=l && b>=l) printf("%d.0000000\n",l);
    else
    {
        if (a>=l) printf("%d.0000000\n",b);
        else
        {
            if (b>=l) printf("%d.0000000\n",a);
            else
            {
                double lv=0.0,rv=l;
                for (int c=1;c<=100;c++)
                {
                    double m1=(rv-lv)/3.0+lv;
                    double m2=lv+rv-m1;
                    if (get(m1)<0.0 || get(m2)<0.0)
                    {
                        printf("My poor head =(\n");
                        return 0;
                    }
                    if (get(m1)<get(m2)) rv=m2;
                    else lv=m1;
                }
                printf("%.7lf\n",get(rv));
            }
        }
    }
    return 0;
}
100分 三分

 

c


【问题描述】
你是能看到第三题的 friends 呢。
——aoao
树是个好东西,删掉树一条边要 1 的代价,随便再加一条边有 1 的代价,求
最小的代价把树变成环。
【输入格式】
第一行一个整数?,代表树的点数。
接下来? − 1行,每行两个数代表树的一条边。
【输出格式】
一行一个整数代表答案。
【样例输入】
4
1 2
2 3
2 4
【样例输出】
3
【数据规模与约定】
3。
60%的数据,1 ≤ ? ≤ 10。
对于100%的数据,1 ≤ ? ≤ 100000。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 100010
using namespace std;
int n,num,ans,root;
int into[MAXN],head[MAXN];
struct node{
    int to,pre;
}e[MAXN*2];
void add(int from,int to){
    e[++num].to=to;
    e[num].pre=head[from];
    head[from]=num;
}
void dfs(int now,int fa){
    for(int i=head[now];i;i=e[i].pre){
        int to=e[i].to;
        if(to!=fa){
            dfs(to,now);
            if(into[to]>2){
                into[now]--;
                ans+=(into[to]-2)*2;
            }
        }
    }
}
int main(){
    freopen("c.in","r",stdin);freopen("c.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<n;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        add(u,v);add(v,u);
        into[u]++;
        into[v]++;
    }
    root=1;
    for(int i=1;i<=n;i++)
        if(into[i]==1){
            root=i;
            break;
        }
    dfs(root,-1);
    cout<<ans+1;
}
100分 贪心+暴力

 

posted @ 2017-10-03 17:00  Echo宝贝儿  阅读(468)  评论(0编辑  收藏  举报