A.The Wall

题意:两个人粉刷墙壁,甲从粉刷标号为x,2x,3x...的小块乙粉刷标号为y,2y,3y...的小块问在某个区间内被重复粉刷的小块的个数。

分析:求出x和y的最小公倍数,然后做一个一维的区间减法就可以了。

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; typedef long long LL;
int x, y, a, b; int main() {
scanf("%d %d %d %d", &x, &y, &a, &b);
int d = __gcd(x, y);
int lcm = x / d * y;
printf("%d\n", b/lcm - (a-)/lcm);
return ;
}

B.Maximal Area Quadrilateral

题意:给定若干个点,求任选四个点后组成的最大的多边形面积。

分析:不能够枚举四个点的组合情况,可以把任何一个四边形看作是一条直线作为边的两个三角形组成的,这样两边的三角形分别求最值就可以了,时间复杂度O(n^3)。

#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int n, x[], y[];
const int inf=<<;
int main( )
{
scanf("%d", &n);
for( int i=; i<n; ++i ){
scanf("%d%d", &x[i], &y[i]);
}
int Max=, m1, m2, n1, n2,t;
for( int i=; i<n; ++ i ){
for( int j=i+; j<n; ++ j ){
m1=-inf, m2=-inf, n1=inf, n2=inf;
for( int k=; k<n; ++ k ){
if(k==i || k==j) continue;
t=(x[j]-x[i])*(y[k]-y[i]) - (x[k]-x[i])*(y[j]-y[i]);
if(t<){
m1=max(m1, -t );
n1=min(n1, -t);
}
else{
m2=max(m2, t);
n2=min(m2, t);
}
}
Max=max(Max, m1+m2);
}
}
printf("%.6lf", 1.0*Max/);
return ;
}

C.Tourist Problem

题意:给定一个N个点在一维直线的坐标,现在有按照 N! 种不同的排列方式来访问这些点,问平均每步的距离为多少,定义两位置的距离为坐标差的绝对值。

分析:单独考虑每个元素在所有排列中的总贡献。对于每一个元素只考虑其前一个元素和其的距离,那么一共有N-1个元素与其匹配,对于每个数该数对存在的位置共有N-1种,加之剩下的数的排列数(N-2)!则每个数对共有(N-1)!种情况,计算出任何一个数与其余N-1个数的绝对值之差之和,然后乘上(N-2)!就是总路程和了。由于要求的平均距离,因此还应除以N!,刚好 (N-1)! 和 N! 的可以约掉。

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std; const int N = ;
typedef long long LL;
int seq[N];
LL sum[N];
int n; int main() {
scanf("%d", &n);
LL a = , b = ;
for (int i = ; i <= n; ++i) {
scanf("%d", &seq[i]);
}
sort(seq+, seq++n);
for (int i = ; i <= n; ++i) {
sum[i] = sum[i-] + seq[i];
}
for (int i = ; i <= n; ++i) {
a += 1LL*(i-)*seq[i]-sum[i-]+ sum[n]-sum[i-]-1LL*(n-i)*seq[i];
}
b = n;
LL d = __gcd(a, b);
a /= d, b /= d;
printf("%I64d %I64d\n", a, b);
return ;
}

D.Bubble Sort Graph

题意:给定一个1-N的全排列,定义逆序对为ai > aj when i < j。对于所有的逆序对连边,求最后形成图的最大独立子集。

分析:初看起来确实是无从下手,但是可以发现一个性质那就是所有的最后的独立子集一定会是原序列中一个单调非递减的子序列,因此求最长单调非递减子序列即可。

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std; const int N = ;
int seq[N];
int n;
typedef vector<int> v_int; void solve() {
v_int vt;
v_int::iterator it;
for (int i = ; i < n; ++i) {
it = upper_bound(vt.begin(), vt.end(), seq[i]);
if (it == vt.end()) vt.push_back(seq[i]);
else *it = seq[i];
}
printf("%d\n", (int)vt.size());
} int main() {
scanf("%d", &n);
for (int i = ; i < n; ++i) {
scanf("%d", &seq[i]);
}
solve();
return ;
}

E.Iahub and Permutations

题意:给定一个1-N的全排列,不过有些位置是被-1代替的,现在要求可能的原序列有多少种,方案数 mod 10^9+7。题目给定一个限制那就是 ai != i。

分析:由于 ai != i 这完全符合错排的定义,不过稍有不同的此处已经给出了某些元素的位置,那么考虑到错排运用容斥定理的推理过程可知此处正好能够应对这一变种。根据输入统计出有多少个数字的安排是有限制的,多少是没有限制的。然后枚举有限制的数字放在限制位置的个数,容斥一下即可。

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std; typedef long long LL;
const int N = ;
const int mod = int(1e9)+;
int n, x, y; // x表示自由放置的数字的数量,y表示有限制的数字
char hav[N];
char fre[N];
int fac[N]; void pre() {
fac[] = ;
for (int i = ; i <= ; ++i) {
fac[i] = 1LL*fac[i-]*i%mod;
}
} int pow(int a, int b) {
int ret = ;
while (b) {
if (b & ) ret = 1LL*ret*a%mod;
b >>= ;
a = 1LL*a*a%mod;
}
return ret;
} void solve() {
int ret = ;
for (int i = ; i <= y; ++i) {
LL sign = i & ? - : ;
ret = (1LL*ret+sign*fac[x+y-i]*pow(1LL*fac[i]*fac[y-i]%mod, mod-)%mod)%mod;
ret = (ret + mod) % mod;
}
printf("%d\n", 1LL*ret*fac[y]%mod);
} int main() {
pre();
int a;
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
scanf("%d", &a);
if (a != -) hav[a] = , fre[i] = ;
}
for (int i = ; i <= n; ++i) {
if (!hav[i]) {
if (fre[i]) ++x;
else ++y;
}
}
solve();
return ;
}

Codeforces Round #198 (Div. 2)的更多相关文章

  1. Codeforces Round #198 (Div. 2)A,B题解

    Codeforces Round #198 (Div. 2) 昨天看到奋斗群的群赛,好奇的去做了一下, 大概花了3个小时Ak,我大概可以退役了吧 那下面来稍微总结一下 A. The Wall Iahu ...

  2. [置顶] Codeforces Round #198 (Div. 1)(A,B,C,D)

    http://codeforces.com/contest/341 赛后做的虚拟比赛,40分钟出了3题,RP爆发. A计数问题 我们可以对每对分析,分别对每对<a, b>(a走到b)进行统 ...

  3. 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 ...

  4. 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 ...

  5. Codeforces Round #198 (Div. 1) B,C 动态规划

    比赛时,开了大号去做,算了半天发现不会做A,囧.于是跑去看B,发现很水?于是很快敲完了,但是A不会,没敢交.于是去看C,一直找规律啊,后来总算调了出来,看了一下榜,发现还是算了吧,直接去睡觉了.第二天 ...

  6. Codeforces Round #198 (Div. 2) —— D

    昨天想了一下D题,有点思路不过感觉很麻烦,就懒得去敲了: 今天上午也想了一下,还是没有结果,看了一下官方题解,证明得很精彩: 这道题目其实就是一道裸地最大上升子序列的题: 看到这里,直接怒码···· ...

  7. Codeforces Round #198 (Div. 2) —— C

    C题很容易看懂题目,不过两个循环肯定会TLE,所以得用点小聪明: 首先排好序,因为是全排列,乱序和顺序的结果是一样的: 然后呢···· 如果是数列 1 2 3 4 5 元素1 被 2 3 4 5每个减 ...

  8. Codeforces Round #198 (Div. 2) —— B

    B题是一个计算几何的题,虽然以前看过计算几何的ppt,但一直都没有写过: 昨晚比赛的时候本来想写的,但是怕不熟练浪费时间,太可惜了! 其实没必要选出一个最大的矩形: 以矩形的一条对角线为轴,向上或者向 ...

  9. Codeforces Round #198 (Div. 2) —— A

    最水的题,可惜当时赶时间没有注意数据范围:暴力超时了! 其实应该用x,y的最大公约数来判断: 代码: #include<iostream> using namespace std; int ...

随机推荐

  1. Java 基本数据类型 sizeof 功能【转】

    转自:http://blog.csdn.net/sunboy_2050/article/details/7310008 版权声明:本文为博主原创文章,未经博主允许不得转载. Java基本数据类型int ...

  2. IE6不支持CSS的属性选择器

    input[type="text"] { width: 50px; } 测试IE6不生效,而IE7以上浏览器则没问题

  3. 如何获取各大平台的播放地址(获得优酷的m3u8播放地址)为例

    1.打开safari 2.在顶部(黑色小苹果)旁边,点击safari. 3.打开里面的 偏好设置. 4.在高级 中 找到 “在菜单栏中显示开发菜单” 并打勾. 5.关闭窗口后,可以发现safari 顶 ...

  4. java中的内存一般分成几部分?

    java中的内存被分成以下四部分: ①.代码区  ②.栈区  ③.堆区   ④.静态区域 栈区:由编译器自动分配释放,存放函数的参数值.局部变量的值等:具体方法执行结束后,系统自动释放JVM内存资源 ...

  5. 每日一九度之 题目1076:N的阶乘

    时间限制:3 秒 内存限制:128 兆 特殊判题:否 提交:7601 解决:2749 题目描述: 输入一个正整数N,输出N的阶乘. 输入: 正整数N(0<=N<=1000) 输出: 输入可 ...

  6. c#委托之最大

    public delegate int ceshi(object o1, object o2); static void Main(string[] args) { string[] a = { &q ...

  7. CaronteFX插件简介

    CaronteFX是一个基于烘培的unity物理增强插件,而不是实时的. 也正因为如此可以在Cutscene中使用,而不用切到maya之类得到软件里去做,以提高效率 Adam demo里一些角色布料( ...

  8. shell脚本判断文件类型

    转自:http://www.cnblogs.com/sunyubo/archive/2011/10/17/2282047.html 1. shell判断文件,目录是否存在或者具有权限 2. #!/bi ...

  9. 2016年11月16日 星期三 --出埃及记 Exodus 20:7

    2016年11月16日 星期三 --出埃及记 Exodus 20:7 "You shall not misuse the name of the LORD your God, for the ...

  10. 2016年10月20日 星期四 --出埃及记 Exodus 19:4

    2016年10月20日 星期四 --出埃及记 Exodus 19:4 `You yourselves have seen what I did to Egypt, and how I carried ...