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;
}