CERC2016 gym101173C

2 篇文章 0 订阅

Problem C: Convex Contour

题目链接: http://codeforces.com/gym/101173

题意

给你若干个三角形、圆型、正方形摆成一排,问周长。

题解

分上下左右进行计算,注意三角形到圆形的连线是切线。

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
const int SIZE = 1;

char s[25];

const double pi = acos(-1.0);

int main() {
    int n;
    scanf("%d",&n);
    scanf("%s",s);
    int lt = 0, rt = 0;
    for(int i = 0; s[i] == 'T' && i < n; i++) lt++;
    for(int i = n - 1; s[i] == 'T' && i >= 0; i--) rt++;
    if(lt == n) {
        printf("%d\n",2*n+1);
        return 0;
    }

    if(n == 1) {
        if(s[0] == 'S') printf("4\n");
        else if(s[0] == 'C') printf("%.8f\n",pi);
        return 0;
    }

    bool ls, rs;
    int dl, dr;
    for(int i = 0; i < n; i++) {
        if(s[i] == 'T') continue;
        if(s[i] == 'S') {
            ls = true;
        }
        else {
            ls = false;
        }
        dl = i;
        break;
    }
    for(int i = n - 1; i >= 0; i--) {
        if(s[i] == 'T') continue;
        if(s[i] == 'S') {
            rs = true;
        }
        else {
            rs = false;
        }
        dr = i;
        break;
    }
    double res = 0;
    if(s[0] == 'S') {
        res += 3;
    }
    else if(s[0] == 'C') {
        res += pi/2 + 0.5;
    }
    else {
        res += 2;
        double x1 = dl - 0.5, x2 = sqrt(3.0) / 2.0 - 0.5,  x3 = 1.0 - sqrt(3.0)/2.0;
        double th = asin(0.5/sqrt(x2*x2+dl*dl)) - atan(x2/dl);
        if(ls) res += sqrt(x1*x1 + x3*x3);
        else res += th/2.0 + 0.5*tan(pi/2-th-atan(x2/dl));
    }
    if(s[n-1] == 'S') {
        res += 3;
    }
    else if(s[n-1] == 'C') {
        res += pi/2 + 0.5;
    }
    else {
        res += 2;
        int d = n - 1 - dr;
        //printf("d=%d\n",d);
        double x1 = d - 0.5, x2 = sqrt(3.0) / 2.0 - 0.5, x3 = 1.0 - sqrt(3.0)/2.0;
        double th = asin(0.5/sqrt(x2*x2+d*d)) - atan(x2/d);
        //printf("res=%f\n",res);
        if(rs) res += sqrt(x1*x1 + x3*x3);
        else res += th/2.0 + 0.5*tan(pi/2-th-atan(x2/d));
        //printf("----%f %f\n",th/2.0,th/2.0 + 0.5*tan(pi/2-th-atan(x2/d)));

    }
    res += n - 2;
    double up = n - lt - rt;
    if(s[0] == 'S') up-=1;
    else if(s[0] == 'C') up -= 0.5;
    else {
        if(ls) {}
        else {
            up -= 0.5;
        }
    }
    if(s[n-1] == 'S') up-=1;
    else if(s[n-1] == 'C') up -= 0.5;
    else {
        if(rs) {}
        else {
            up -= 0.5;
        }
    }
    res += up;
    //printf("%f\n",up);
    printf("%.8f\n",res);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值