您的位置:首页 > 其它

Codeforces Beta Round #1 (水题 + 进制模拟 + 计算几何)

2018-03-07 15:30 239 查看
之后想从CF上扒点contest下来做,之前有从后往前补题,所以做contest时想从前往后开。之后的要求是每套题所有题必须补完(flag就立在这里了 hhh)

A. Theatre SquareTheatre Square in the capital city of Berland has a rectangular shape with the size n × m meters. On the occasion of the city's anniversary, a decision was taken to pave the Square with square granite flagstones. Each flagstone is of the size a × a.What is the least number of flagstones needed to pave the Square? It's allowed to cover the surface larger than the Theatre Square, but the Square has to be covered. It's not allowed to break the flagstones. The sides of flagstones should be parallel to the sides of the Square.InputThe input contains three positive integer numbers in the first line: n,  m and a (1 ≤  n, m, a ≤ 109).OutputWrite the needed number of flagstones.ExamplesinputCopy
6 6 4
output
4

A 题,用a*a的地砖覆盖m*n的地面,可以超过边界但不允许打破地砖,问最少需要多少地砖。
解: 只需要计算覆盖一条边所需的地砖数量,就好,如果地板除(整型除法)不能完全覆盖就对该边所需地砖+1。水题

B. SpreadsheetsIn the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23.Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.InputThe first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .OutputWrite n lines, each line should contain a cell coordinates in the other numeration system.ExamplesinputCopy
2
R23C55
BC23
output
BC23
R23C55
B题,模拟Excel表格的计数方式,第1列是A,第26列是Z,第27列是AA以此类推。也可以用R行数C列数表示。然后对于输入的两种计数方式进行互换。
可以发现行数不需要操作,列数的转换类似于26进制(A-Z),只不过Z+1=AA比较特殊。这个26进制的每一位上都不会出现0(前导0忽略)。所以字符转数字表示时按照正常的26进制计算,在每一次计算后末位+1。数字转字符表示时先减1再进行除余取位计算即可。#include<bits/stdc++.h>
using namespace std;

char a[110],s[4][110],ans[110];

int tol[4];

int main()
{
int T;
cin>>T;
while(T--)
{
cin>>a;
int lena = strlen(a);
memset(tol, 0, sizeof tol);

for(int i=0; i<lena; i++) {
if(!tol[1]) {
if(isalpha(a[i])) s[0][tol[0]++] = a[i];
else s[1][tol[1]++] = a[i];
}

else if(!tol[2]) {
if(isdigit(a[i])) s[1][tol[1]++] = a[i];
else s[2][tol[2]++] = a[i];
}

else if(!tol[3]) {
if(isalpha(a[i])) s[2][tol[2]++] = a[i];
else s[3][tol[3]++] = a[i];
}

else s[3][tol[3]++] = a[i];
}

if(!tol[3]) {
int x = 0, y = 0;
for(int i=0; i<tol[0]; i++)
{
x = x*26 + s[0][i] - 'A' + 1;
}
for(int i=0; i<tol[1]; i++)
{
y = y*10 + s[1][i] - '0';
}
printf("R%dC%d\n",y,x);
}

else {
int x = 0, y = 0;
for(int i=0; i<tol[1]; i++) {
x = x*10 + s[1][i] - '0';
}
for(int i=0; i<tol[3]; i++) {
y = y*10 + s[3][i] - '0';
}

//printf("##%s %s\n",s[1],s[3]);
//printf("**%d %d\n",x,y);
int tol=0;
//y += 1;
while(y)
{
y--;
ans[tol++] = y%26 + 'A';
y /= 26;
}

for(int i=tol-1; i>=0; i--)
{
printf("%c",ans[i]);
}
printf("%d\n",x);
}
}
}
(为什么代码没有高亮了,不开心 -.- )
附带一个python3做这道题的代码,因为发现python处理起来别有一番风味所以之后也想用py做一做这种类似的题(水平不够啊喂)import re
import string

def num_to_col(num):
col =''
while num > 0:
num, mod = divmod(num - 1, 26)
col = chr(mod+65) + col
return col

def col_to_num(col):
num = 0
for c in col:
num = num * 26 + (ord(c) - ord('A')) + 1
return num

if __name__ == '__main__':
n = int(input())

for i in range(n):
line_str = input()
m = re.match('R([0-9]+)C([0-9]+)',line_str)
if m:
col = num_to_col(int(m.group(2)))
print(col + m.group(1))
else:
m = re.match("([A-Z]+)([0-9]+)",line_str)
num = str(col_to_num(m.group(1)))
print('R' + m.group(2) + 'C' + num)C. Ancient Berland CircusNowadays all circuses in Berland have a round arena with diameter 13 meters, but in the past things were different.In Ancient Berland arenas in circuses were shaped as a regular (equiangular) polygon, the size and the number of angles could vary from one circus to another. In each corner of the arena there was a special pillar, and the rope strung between the pillars marked the arena edges.Recently the scientists from Berland have discovered the remains of the ancient circus arena. They found only three pillars, the others were destroyed by the time.You are given the coordinates of these three pillars. Find out what is the smallest area that the arena could have.
InputThe input file consists of three lines, each of them contains a pair of numbers –– coordinates of the pillar. Any coordinate doesn't exceed 1000 by absolute value, and is given with at most six digits after decimal point.OutputOutput the smallest possible area of the ancient arena. This number should be accurate to at least 6 digits after the decimal point. It's guaranteed that the number of angles in the optimal polygon is not larger than 100.ExamplesinputCopy
0.000000 0.000000
1.000000 1.000000
0.000000 1.000000
output
1.00000000
C题,给你三个点,求三个点作为定点的正多边形,面积最小是多少。
看到这道题的第一反应是正多边形的三个点一定共圆,而给出三个点就可以计算出对应三角形的外心,圆唯一确定之后,我们又可知,当边数越大,多边形越接近圆,即面积越大,则我们要求边数最少的情况。
在验证过思路正确之后就开始着手实现了,在实现过程中,我是先确定外心,再确定半径以及三个角度。然后枚举边数看是否整除(边数不超过100),WA了。后来发现在确认三个角度时会出现很多问题。
思路不变改变实现方法,不算外心直接根据海伦公式和外接圆公式确定半径,根据边与半径的比计算弧度,然后对三个弧度求gcd得到最大弧度,对应最小边数,然后计算多边形大小。#include<bits/stdc++.h>
using namespace std;

const double eps = 0.01;
const double PI = acos(-1.0);
int sgn(double x)
{
if(fabs(x) < eps) return 0;
if(x < 0) return -1;
else return 1;
}

double fgcd(double x, double y)
{
return !sgn(y) ? x : fgcd(y, fmod(x,y));
}

int main()
{
double p[3][2];
for(int i=0; i<3; i++)
for(int j=0; j<2; j++)
cin>>p[i][j];

double a = sqrt((p[0][0]-p[1][0])*(p[0][0]-p[1][0]) + (p[0][1]-p[1][1])*(p[0][1]-p[1][1]));
double b = sqrt((p[0][0]-p[2][0])*(p[0][0]-p[2][0]) + (p[0][1]-p[2][1])*(p[0][1]-p[2][1]));
double c = sqrt((p[2][0]-p[1][0])*(p[2][0]-p[1][0]) + (p[2][1]-p[1][1])*(p[2][1]-p[1][1]));
double m = (a + b + c) / 2;
double s = sqrt(m*(m-a)*(m-b)*(m-c));
double r = a*b*c / (4*s);
if(a>c) swap(a, c);
if(b>c) swap(b, c);
double A = 2 * asin(a/(2*r)) ;
double B = 2 * asin(b/(2*r)) ;
double C = 2 * PI - A - B ;

m = fgcd(A, B);
m = fgcd(m, C);

//printf("%f %f %f\n", A, B, C) ;
//printf("%f %f\n", r, m) ;

printf("%.8f\n",PI*r*r*sin(m)/m);
}这里需要注意的是,一开始eps设置为1e-8结果一直WA,后来发现因为边长不大于100,弧度最小值不小于0.06,如果我们把eps设置的过于小,则所求得的gcd可能会是一个极小的值,导致计算错误(我们是通过对余数的判断来决定是否精度足够的,eps过小会使得我们需要计算更多次,所得到的数也会越来越小)。
同时也学会了一种新的方法,浮点数的最大公约数,用到了浮点数除余的方法fmod()。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: