CodeForces Round 198
总体感觉这次出的题偏数学,数学若菜表示果断被虐.不过看起来由于大家都被虐我2题居然排到331,rating又升了74.
Div2-A
A. The Wall
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Iahub and his friend Floyd have started painting a wall. Iahub is painting the wall red and Floyd is painting it pink. You can consider the wall being made of a very large number of bricks, numbered 1, 2, 3 and so on.
Iahub has the following scheme of painting: he skips x - 1 consecutive bricks, then he paints the x-th one. That is, he'll paint bricks x, 2·x, 3·x and so on red. Similarly, Floyd skips y - 1 consecutive bricks, then he paints the y-th one. Hence he'll paint bricks y, 2·y, 3·y and so on pink.
After painting the wall all day, the boys observed that some bricks are painted both red and pink. Iahub has a lucky number a and Floyd has a lucky number b. Boys wonder how many bricks numbered no less than a and no greater than b are painted both red and pink. This is exactly your task: compute and print the answer to the question.
Input
The input will have a single line containing four integers in this order: x, y, a, b. (1 ≤ x, y ≤ 1000, 1 ≤ a, b ≤ 2·109, a ≤ b).
Output
Output a single integer — the number of bricks numbered no less than a and no greater than b that are painted both red and pink.
Sample test(s)
Input
2 3 6 18
Output
3
Note
Let's look at the bricks from a to b (a = 6, b = 18). The bricks colored in red are numbered 6, 8, 10, 12, 14, 16, 18. The bricks colored in pink are numbered 6, 9, 12, 15, 18. The bricks colored in both red and pink are numbered with 6, 12 and 18.
题意:两个人刷墙,一个人刷编号是x的倍数的,另一个刷编号是y倍数的.求在[a,b]上有多少个墙被两个人同时刷了.
思路:第一题不水没道理.被同时刷的墙是x和y最小公倍数的倍数.
#include<stdio.h>
int gcd(int a,int b)
{
if (a==) return b;
return gcd(b%a,a);
}
int main()
{
int x,y;
__int64 a,b;
scanf("%d%d%I64d%I64d",&x,&y,&a,&b);
int M=x/gcd(x,y)*y;
__int64 l=a/M,r=b/M;
if (l*M<a) l++;
printf("%I64d\n",r-l+);
return ;
}
Div2-B
B. Maximal Area Quadrilateral
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Iahub has drawn a set of n points in the cartesian plane which he calls "special points". A quadrilateral is a simple polygon without self-intersections with four sides (also called edges) and four vertices (also called corners). Please note that a quadrilateral doesn't have to be convex. A special quadrilateral is one which has all four vertices in the set of special points. Given the set of special points, please calculate the maximal area of a special quadrilateral.
Input
The first line contains integer n (4 ≤ n ≤ 300). Each of the next n lines contains two integers: xi, yi ( - 1000 ≤ xi, yi ≤ 1000) — the cartesian coordinates of ith special point. It is guaranteed that no three points are on the same line. It is guaranteed that no two points coincide.
Output
Output a single real number — the maximal area of a special quadrilateral. The answer will be considered correct if its absolute or relative error does't exceed 10 - 9.
Sample test(s)
Input
50 00 44 04 42 3
Output
16.000000
Note
In the test example we can choose first 4 points to be the vertices of the quadrilateral. They form a square by side 4, so the area is 4·4 = 16.
题意:给出一些点,从其中找出4个点构成四边形,求面积最大的四边形的面积.
思路:一开始被吓尿了,想dfs发现那是不可能的事,后来想能不能把四边形分成两个三角形来处理.经过观察发现不论四个点怎么摆必会发生这样的情况:中间一条线段,线段左右两边各有一个点.于是思路就有了:先O(N^2)枚举这样的线段,然后O(N)枚举左右两边的点.总时间复杂度为O(N^3).不过要特别注意的是有可能出现一条线段某一侧没有点的情况.
#include<math.h>
#include<stdio.h>
struct vect
{
int x,y;
};
double x[],y[];
int mult(vect v1,vect v2)
{
return v1.x*v2.y-v2.x*v1.y;
}
int main()
{
int N;
scanf("%d",&N);
for (int i=;i<=N;i++) scanf("%lf%lf",&x[i],&y[i]);
double Max=;
for (int i=;i<=N;i++)
for (int j=;j<=N;j++)
if (i!=j)
{
vect seg,tmp;
seg.x=x[i]-x[j];
seg.y=y[i]-y[j];
double l=,r=;
for (int k=;k<=N;k++)
if (k!=i && k!=j)
{
tmp.x=x[k]-x[j];
tmp.y=y[k]-y[j];
int dir=mult(seg,tmp);
if (dir<)
{
double S=0.5*fabs(x[i]*y[j]+y[i]*x[k]+x[j]*y[k]-y[j]*x[k]-y[i]*x[j]-x[i]*y[k]);
if (S>l) l=S;
}
else
{
double S=0.5*fabs(x[i]*y[j]+y[i]*x[k]+x[j]*y[k]-y[j]*x[k]-y[i]*x[j]-x[i]*y[k]);
if (S>r) r=S;
}
}
if (l+r>Max && l> && r>) Max=l+r;
}
printf("%.9lf\n",Max);
return ;
}
Div2-C/Div1-A
C. Tourist Problem
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place.
Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination.
The distance between destination located at kilometer x and next destination, located at kilometer y, is |x?-?y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once.
Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him.
Input
The first line contains integer n (2?≤?n?≤?105). Next line contains n distinct integers a1, a2, ..., an (1?≤?ai?≤?107).
Output
Output two integers — the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible.
Sample test(s)
Input
32 3 5Output
22 3Note
Consider 6 possible routes:
[2, 3, 5]: total distance traveled: |2 – 0| + |3 – 2| + |5 – 3| = 5;
[2, 5, 3]: |2 – 0| + |5 – 2| + |3 – 5| = 7;
[3, 2, 5]: |3 – 0| + |2 – 3| + |5 – 2| = 7;
[3, 5, 2]: |3 – 0| + |5 – 3| + |2 – 5| = 8;
[5, 2, 3]: |5 – 0| + |2 – 5| + |3 – 2| = 9;
[5, 3, 2]: |5 – 0| + |3 – 5| + |2 – 3| = 8.
The average travel distance is = = .
题意:一条路上n个景点,可以按任意顺序全部访问一遍,求走路的平均长度.
思路:这是官方题解
Despite this is a math task, the only math formula we'll use is that number of permutations with n elements is n!. From this one, we can deduce the whole task.
The average formula is sum_of_all_routes / number_of_routes. As each route is a permutation with n elements, number_of_routes is n!. Next suppose you have a permutation of a: p1, p2, …, pn. The sum for it will be p1 + |p2 – p1| + … + |pn – pn-1|. The sum of routes will be the sum for each possible permutation.
We can calculate sum_of_all routes in two steps: first time we calculate sums like “p1” and then we calculate sums like “|p2 – p1| + … + |pn – pn-1|” for every existing permutation.
First step Each element of a1, a2, …, an can appear on the first position on the routes and needs to be added as much as it appears. Suppose I fixed an element X for the first position. I can fill positions 2, 3, .., n – 1 in (n – 1)! ways. Why It is equivalent to permuting n – 1 elements (all elements except X). So sum_of_all = a1 * (n – 1)! + a2 * (n – 1)! + … * an * (n – 1)! = (n – 1)! * (a1 + a2 + … + an).
Second step For each permutation, for each position j between 1 and n – 1 we need to compute |pj — p(j?+?1)|. Similarly to first step, we observe that only elements from a can appear on consecutive positions. We fix 2 indices i and j. We’re interested in how many permutations do ai appear before aj. We fix k such as on a permutation p, ai appears on position k and aj appears on a position k + 1. In how many ways can we fix this n – 1 ways (1, 2, …, n – 1). What’s left A sequence of (n – 2) elements which can be permuted independently. So the sum of second step is |ai?-?aj| * (n – 1) * (n – 2)!, for each i != j. If I note (a1 + a2 + … + an) by S1 and |ai?-?aj| for each i != j by S2, the answer is (N – 1)! * S1 + (N – 1)! * S2 / N!. By a simplification, the answer is (S1 + S2) / N.
The only problem remained is how to calculate S2. Simple iteration won’t enter in time limit. Let’s think different. For each element, I need to make sum of differences between it and all smaller elements in the array a. As well, I need to make sum of all different between bigger elements than it and it. I’ll focus on the first part. I sort increasing array a. Suppose I’m at position i. I know that (i – 1) elements are smaller than ai. The difference is simply (i — 1) * ai — sum_of_elements_before_position_i. Sum of elements before position i can be computed when iterating i. Let’s call the obtained sum Sleft. I need to calculate now sum of all differences between an element and bigger elements than it. This sum is equal to Sleft. As a proof, for an element ai, calculating the difference aj — ai when aj > ai is equivalent to calculating differences between aj and a smaller element of it (in this case ai). That’s why Sleft = Sright.
As a conclusion, the answer is (S1 + 2 * Sleft) / N. For make fraction irreducible, you can use Euclid's algorithm. The complexity of the presented algorithm is O(N?*?logN), necessary due of sorting. Sorting can be implemented by count sort as well, having a complexity of O(maximalValue), but this is not necessary.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define N 100005
long long a[N];
long long gcd(long long a,long long b)
{
return b gcd(b,a%b):a;
}
int cmp(const void *a,const void *b)
{
return *(long long*)a-*(long long *)b;
}
int main()
{
long long n,sum=,p,q,d,tot;
while(~scanf("%I64d",&n))
{
for(int i=;i<n;i++)
scanf("%I64d",a+i);
for(int i=;i<n;i++)
sum+=a[i];
qsort(a,n,sizeof(a[]),cmp);
p=;q=n-;
tot=n;
while(p<q)
{
sum+=*(tot-)*(a[q]-a[p]);
p++;
q--;
tot-=;
}
d=gcd(sum,n);
sum/=d,n/=d;
printf("%I64d %I64d\n",sum,n);
}
return ;
}
CodeForces Round 198的更多相关文章
- Codeforces Round #198 (Div. 2)A,B题解
Codeforces Round #198 (Div. 2) 昨天看到奋斗群的群赛,好奇的去做了一下, 大概花了3个小时Ak,我大概可以退役了吧 那下面来稍微总结一下 A. The Wall Iahu ...
- [置顶] Codeforces Round #198 (Div. 1)(A,B,C,D)
http://codeforces.com/contest/341 赛后做的虚拟比赛,40分钟出了3题,RP爆发. A计数问题 我们可以对每对分析,分别对每对<a, b>(a走到b)进行统 ...
- Codeforces Round #198 (Div. 2) E. Iahub and Permutations —— 容斥原理
题目链接:http://codeforces.com/contest/340/problem/E E. Iahub and Permutations time limit per test 1 sec ...
- Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*
D. Iahub and Xors Iahub does not like background stories, so he'll tell you exactly what this prob ...
- Codeforces Round #198 (Div. 2)
A.The Wall 题意:两个人粉刷墙壁,甲从粉刷标号为x,2x,3x...的小块乙粉刷标号为y,2y,3y...的小块问在某个区间内被重复粉刷的小块的个数. 分析:求出x和y的最小公倍数,然后做一 ...
- Codeforces Round #198 (Div. 1) B,C 动态规划
比赛时,开了大号去做,算了半天发现不会做A,囧.于是跑去看B,发现很水?于是很快敲完了,但是A不会,没敢交.于是去看C,一直找规律啊,后来总算调了出来,看了一下榜,发现还是算了吧,直接去睡觉了.第二天 ...
- Codeforces Round #198 (Div. 2) —— D
昨天想了一下D题,有点思路不过感觉很麻烦,就懒得去敲了: 今天上午也想了一下,还是没有结果,看了一下官方题解,证明得很精彩: 这道题目其实就是一道裸地最大上升子序列的题: 看到这里,直接怒码···· ...
- Codeforces Round #198 (Div. 2) —— C
C题很容易看懂题目,不过两个循环肯定会TLE,所以得用点小聪明: 首先排好序,因为是全排列,乱序和顺序的结果是一样的: 然后呢···· 如果是数列 1 2 3 4 5 元素1 被 2 3 4 5每个减 ...
- Codeforces Round #198 (Div. 2) —— B
B题是一个计算几何的题,虽然以前看过计算几何的ppt,但一直都没有写过: 昨晚比赛的时候本来想写的,但是怕不熟练浪费时间,太可惜了! 其实没必要选出一个最大的矩形: 以矩形的一条对角线为轴,向上或者向 ...
随机推荐
- Sum Root to Leaf Numbers
int sumNumbers(TreeNode *root) { ); } int dfs(TreeNode *root, int sum) { ; if (root->left == null ...
- RO05 - 如何编写RemObjects SDK服务端 (Delphi Version)
转载:http://blog.csdn.net/henreash/article/details/2261134 本文档向你展示如何使用RemObjects(Delphi版)创建第一个服务.读了本文档 ...
- HDU 4435 charge-station () bfs图论问题
E - charge-station Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- Bellman-Ford算法
#include<stdio.h> #define max 0xffffff ][]; //图的邻接矩阵 ]; int n;//顶点个数 int m;//边个数 struct Edge { ...
- HDU2084基础DP数塔
数塔 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...
- google maps js v3 api教程(3) -- 创建infowindow
原文地址 前面我们学习了地图和标记的创建.那么今天我们来学习怎样在地图上显示一个窗口(infowindow) infowindow构造函数为:InfoWindow(opts?:InfoWindowOp ...
- 引水入城(codevs 1066)
题目描述 Description 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政 区划十分特殊,刚好构成一个N行M列的矩形,如上图所示,其中每个格子都代表一座城 市,每座 ...
- 小吃(codevs 3231)
3231 小吃 时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 这里简直是吃货的天堂,小吃太多了. ...
- 【读书笔记】读《JavaScript设计模式》之装饰者模式
一.定义 装饰者模式可用来透明地把对象包装在具有同样接口的另一个对象之中.这样一来,你可以给一个方法添加一些行为,然后将方法调用传递给原始对象.相对于创建子类来说,使用装饰者对象是一种更灵活的选择(装 ...
- linux环境下配置虚拟主机域名
linux环境下面配置虚拟主机域名 第一步:在root目录下面(即根目录)ls(查看文件)cd进入etc目录find hosts文件vi hosts 打开hosts文件并进行编辑在打开的文件最下面添加 ...