2017 Qinhuangdao Site CCPC E&&M

E - String of CCPC ZOJ - 3985

传送门:https://cn.vjudge.net/contest/227061#problem/E

题意:

给出字符串,判断CCPC的个数以及判断是否添一个数能够构成CCPC.
(由原题得出 最多只能得一分 不然就是负分 画个坐标轴很好想)

分析:

CCPC这个是直接可以的分的 CCP CPC CCC是可以得一分的,这里的关键问题是解决CCPC和其子序列的公用字母问题。可以想到的是在CCPC中P是唯一的所有如果CCPC,我们直接i+=2,跳过了CCP CPC。再特判一下CCC出现是否会构成CCPC.. 就好啦

emmmm 注意大小写!

#include<bits/stdc++.h>
using namespace std;
string ccpc="CCPC";
string cpc="CPC";
string ccp="CCP";
string ccc="CCC";
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        string s;
        cin>>s;
        int cnt=0;
        bool flag=0;
        for(int i=0; i<n; i++)
        {
            if(s.substr(i,4)==ccpc)
            {
                cnt++;
                i+=2;
                continue;
            }
            if(flag) continue;
            string tmp = s.substr(i,3);
            if(tmp==ccp||tmp==cpc||tmp==ccc)
            {
                if(tmp==ccc &&s.substr(i+1, 4)==ccpc)
                    continue;
                cnt++;
                flag = 1;
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

M - Safest Buildings ZOJ - 3993

传送门:https://cn.vjudge.net/contest/227061#problem/M

题意:

有圆心为(0,0) 半径为R的安全区域,会缩小为半径为r的安全区(始终包含或相切在原来圆中) 有n个房子,概率最大的安全的房子有几个并输出编号。

分析:

1.R<=2r时候 存在必然安全圈
2.R>2r 存在等价安全圈
3.[|R-2r|,R],概率会不断的减小

平方处理的话不需要分类讨论了
我的代码

#include <bits/stdc++.h>
using namespace std;
const int maxn=11111;
int d[maxn];
struct ss
{
    double dis;
    int id;
} a[maxn];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,dd,tt;
        cin>>n;
        int R,r;
        cin>>R>>r;
        dd=R-2*r;
        tt=-1;
        int cnt=0;
        int x,y;
        for(int i=1; i<=n; i++)
        {
            cin>>x>>y;
            a[i].dis=x*x+y*y;
            a[i].id=i;
            if(a[i].dis<=dd*dd)
                d[cnt++]=i;
            if(tt==-1||a[i].dis<=tt)
                tt=a[i].dis;
        }
        if(cnt==0)
        {
            for(int i=1; i<=n; i++)
                if(a[i].dis==tt)
                    d[cnt++]=i;
        }

        cout<<cnt<<endl;
        for(int i=0; i<cnt; i++)
        {
            if(i==cnt-1) cout<<d[i]<<endl;
            else cout<<d[i]<<' ';
        }
    }
    return 0;
}

队友的代码
感觉有点麻烦了
但是upper_bound是真的好用

#include <bits/stdc++.h>
using namespace std;

const int maxn = 11111;
struct bd{
    int dis;
    int id;
    bool operator < (const bd & bd1) const{
        return dis < bd1.dis;
    }
}a[maxn];

bool cmp(bd bd1, bd bd2){
    return bd1.id < bd2.id;
}

int main() {
    int t;
    scanf("%d", &t);
    while(t--) {
        int n;
        scanf("%d", &n);
        int R, r;
        scanf("%d %d", &R, &r);
        int dd = R - 2 * r;
        for(int i = 1; i <= n; ++i) {
            int x, y;
            scanf("%d %d", &x, &y);
            a[i].dis = (x * x + y * y);
            a[i].id = i;
        }
        bd tmp;
        tmp.dis = dd * dd;
//        printf("%d\n", tmp.dis);
        sort(a + 1, a + 1 + n);
//        for(int i = 1; i <= n; i++){
//            printf("%d %d\n", a[i].id, a[i].dis);
//        }
        int pos = upper_bound(a + 1, a + 1 + n, tmp) - a - 1;
        tmp.dis = a[1].dis;
        if(pos == 0)
            pos = upper_bound(a + 1, a + 1 + n, tmp) - a - 1;
        printf("%d\n", pos);
        sort(a + 1, a + 1 + pos, cmp);
        for(int i = 1; i <= pos; i++) {
            if(i != 1)
                printf(" %d", a[i].id);
            else
                printf("%d", a[i].id);
        }
        putchar('\n');
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/muvseea/p/8997354.html
今日推荐