B - Ternary String CodeForces - 1354B
You are given a string s such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of s such that it contains each of these three characters at least once.
A contiguous substring of string s is a string that can be obtained from s by removing some (possibly zero) characters from the beginning of s and some (possibly zero) characters from the end of s.
Input
The first line contains one integer t (1≤t≤20000) — the number of test cases.
Each test case consists of one line containing the string s (1≤|s|≤200000). It is guaranteed that each character of s is either 1, 2, or 3.
The sum of lengths of all strings in all test cases does not exceed 200000.
Output
For each test case, print one integer — the length of the shortest contiguous substring of s containing all three types of characters at least once. If there is no such substring, print 0 instead.
Example
Input
7
123
12222133333332
112233
332211
12121212
333333
31121
Output
3
3
4
4
0
0
4
#include
#include
#include
#include
using namespace std;
int T,x[5];
char s[200010];
void calc(int i,int flag) {
if(flag == 0) {
if(s[i] == '1') x[1] ++;
if(s[i] == '2') x[2] ++;
if(s[i] == '3') x[3] ++;
} else if(flag == 1) {
if(s[i] == '1') x[1] --;
if(s[i] == '2') x[2] --;
if(s[i] == '3') x[3] --;
}
}
bool check() {
if(x[1] > 0 && x[2] > 0 && x[3] > 0) return true;
return false;
}
int main() {
scanf("%d",&T);
while(T--) {
scanf("%s",s);
memset(x,0,sizeof x);
int len = strlen(s),ans = 0x3f3f3f3f,sum = 0;
int l = 0,r = len;
for(int i = 0; i < len; i ++) {
sum ++;
calc(i,0);
if(check()) {
r = i;
ans = min(ans,sum);
break;
}
}
// for(int j = 1;j < 4;j ++)
// cout << "*" << x[j] << endl;
while(l < r && r < len) {
l ++;
sum --;
calc(l - 1, 1);
if(check()) ans = min(ans,sum);
else {
r ++;
calc(r,0);
sum ++;
if(check()) ans = min(ans,sum);
}
}
if(ans == 0x3f3f3f3f) ans = 0;
printf("%d\n",ans);
}
}
题意:寻找最短包含‘1’,‘2’,‘3’的连续数字段长度
思路:尺取。先从第一个开始寻找符合的数字段,用l指向最左,r指向最右。然后,将l向前移,如果依然满足条件更新答案,否则将r前移,以此循环更新最短长度。
C - Simple Polygon Embedding CodeForces - 1354C1
The statement of this problem is the same as the statement of problem C2. The only difference is that, in problem C1, n is always even, and in C2, n is always odd.
You are given a regular polygon with 2⋅n vertices (it’s convex and has equal sides and equal angles) and all its sides have length 1. Let’s name it as 2n-gon.
Your task is to find the square of the minimum size such that you can embed 2n-gon in the square. Embedding 2n-gon in the square means that you need to place 2n-gon in the square in such way that each point which lies inside or on a border of 2n-gon should also lie inside or on a border of the square.
You can rotate 2n-gon and/or the square.
Input
The first line contains a single integer T (1≤T≤200) — the number of test cases.
Next T lines contain descriptions of test cases — one per line. Each line contains single even integer n (2≤n≤200). Don’t forget you need to embed 2n-gon, not an n-gon.
Output
Print T real numbers — one per test case. For each test case, print the minimum length of a side of the square 2n-gon can be embedded in. Your answer will be considered correct if its absolute or relative error doesn’t exceed 10−6.
Example
Input
3
2
4
200
Output
1.000000000
2.414213562
127.321336469
#include
#include
#define PI 3.1415926535
using namespace std;
int main(){
int T,n;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
double ans = 1.0 / tan(PI / (2 * n));
printf("%.8lf\n",ans);
}
}
题意:给定偶数n,求能容纳n * 2边形的最小正方形的边长。
思路:因为n是偶数,n * 2 边形一定有四条边在正方形边上,又边长为1,所以可以通过角度和勾股定理求出答案。
D - Not So Simple Polygon Embedding CodeForces - 1354C2
The statement of this problem is the same as the statement of problem C1. The only difference is that, in problem C1, n is always even, and in C2, n is always odd.
You are given a regular polygon with 2⋅n vertices (it’s convex and has equal sides and equal angles) and all its sides have length 1. Let’s name it as 2n-gon.
Your task is to find the square of the minimum size such that you can embed 2n-gon in the square. Embedding 2n-gon in the square means that you need to place 2n-gon in the square in such way that each point which lies inside or on a border of 2n-gon should also lie inside or on a border of the square.
You can rotate 2n-gon and/or the square.
Input
The first line contains a single integer T (1≤T≤200) — the number of test cases.
Next T lines contain descriptions of test cases — one per line. Each line contains single odd integer n (3≤n≤199). Don’t forget you need to embed 2n-gon, not an n-gon.
Output
Print T real numbers — one per test case. For each test case, print the minimum length of a side of the square 2n-gon can be embedded in. Your answer will be considered correct if its absolute or relative error doesn’t exceed 10−6.
Example
Input
3
3
5
199
Output
1.931851653
3.196226611
126.687663595
#include
#include
#include
#define PI 3.1415926535
using namespace std;
int main(){
int T,n;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
double l = 1.0,r = 150.0;
double a1 = PI / (n * 1.0);
double a2 = (PI - a1) / 2.0;
double s = sin(a2) / sin(a1) * 2 * cos(PI / (4*n));
printf("%.8lf\n",s);
}
}
题意:给定奇数n,求能容纳n * 2边形的最小正方形的边长。
思路:因为n是奇数,所以要使正方形边长最小,多边形的边不会与正方形重合。(重合的话正方形的边长就是多边形的对角线。要在重合的基础上旋转,有效的旋转角度为(0,PI / 4n】,而旋转后正方形边长变为多边形对角线乘上cos(旋转角度),于是可以得出旋转PI / 4n可以得到答案。