Codeforces Round #198 (Div. 2)C,D题解
接着是C,D的题解
C. Tourist Problem
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.
3
2 3 5
22 3
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个数,以样例来看,求出所有的全排列,然后在序列前面加一个0,
再求出序列中每个相邻的数的差值的绝对值之和(大概根据样例意会一下)
然后做法的话,我选择了直接推公式
首先n个数的全排列一共有n!种
如果我们抛去第一个数和0的差值,只计算后面的值
就比如 0 2 3 5, 只计算|3 – 2| + |5 – 3| = 3
那么每一种排列一共有n-1个间隔,那么总间隔数就是n!*(n-1)
其次这n个数一共可以组成n*(n-1)/2种不同的间隔
因此每一种不同的间隔一共会有n!*(n-1)/n/(n-1)*2=(n-1)!*2种
最后我们在加上每个数和0的差值,每个差值的个数为n!/n=n-1
我们用2 3 5来举例
[2, 3, 5]: |2 – 0| + |3 – 2| + |5 – 3|
[2, 5, 3]: |2 – 0| + |5 – 2| + |3 – 5|
[3, 2, 5]: |3 – 0| + |2 – 3| + |5 – 2|
[3, 5, 2]: |3 – 0| + |5 – 3| + |2 – 5|
[5, 2, 3]: |5 – 0| + |2 – 5| + |3 – 2|
[5, 3, 2]: |5 – 0| + |3 – 5| + |2 – 3|
首先不同的间隔为(2,3),(2,5),(3,5),每种间隔一共有(n-1)!*2=4种
然后和0之间的间隔(0,2),(0,3),(0,5),一共有n-1=2种
因此将所有的间隔和相加就行了
***另外你需要用O(a[i])的复杂度而不是O(n^2)的
#include<bits/stdc++.h>
#define ll long long
using namespace std;
#define N 10000010
ll a[N],sum=,s=,ans=,x; int n;
bool bo[N];
int main(){
scanf("%d",&n);
memset(a,,sizeof(a));
memset(bo,,sizeof(bo));
for(int i=;i<=n;++i)
scanf("%lld",&x),s+=x,a[x+]++,bo[x]=;
for (int i=;i<=N;++i){
a[i]+=a[i-];
if (bo[i]) ans+=a[i]*i-sum+abs(i*(n-a[i])-(s-sum)),sum+=i;
}
ll x=ans+s,y=n,gcd=__gcd(x,y);
x/=gcd; y/=gcd;
printf("%lld %lld",x,y);
}
D. Bubble Sort Graph
Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode).
procedure bubbleSortGraph()
build a graph G with n vertices and 0 edges
repeat
swapped = false
for i = 1 to n - 1 inclusive do:
if a[i] > a[i + 1] then
add an undirected edge in G between a[i] and a[i + 1]
swap( a[i], a[i + 1] )
swapped = true
end if
end for
until not swapped
/* repeat the algorithm as long as swapped value is true. */
end procedure
For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph.
3
3 1 2
2
Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2].
一句话题意:O(nlogn)求出最长不下降子序列
下面我们来解释为什么是最长不下降子序列
它所给的这段程序,大意是将将所有i<j,a[i]>a[j]中的i和j连上一条边,那么我们想如果没有连上的
是不是就是i<j,a[i]<=a[j]的所有序号,那么这不就是裸的最长严格不下降子序列,
***因为n为1e5,所以你得用O(nlogn)来处理,我是用树状数组来维护
简单的讲一下,就是从前往后add(a[i],f[i]),那么ask时就是求从1到a[i]的前缀和,
因为1到a[i]内的数都是比a[i]小的QAQ
#include<bits/stdc++.h>
using namespace std;
int ans,a[],c[],n,t;
void add(int pos,int x){
while (pos<=n)
c[pos]=max(c[pos],x),pos+=pos&-pos;
} int ask(int pos){
int res=;
while (pos)
res=max(res,c[pos]),pos-=pos&-pos;
return res;
} int main(){
scanf("%d",&n);
for (int i=;i<=n;++i){
scanf("%d",&a[i]); t=ask(a[i]-)+;
ans=max(ans,t); add(a[i],t);
}
printf("%d",ans);
}
Codeforces Round #198 (Div. 2)C,D题解的更多相关文章
- Codeforces Round #198 (Div. 2)A,B题解
Codeforces Round #198 (Div. 2) 昨天看到奋斗群的群赛,好奇的去做了一下, 大概花了3个小时Ak,我大概可以退役了吧 那下面来稍微总结一下 A. The Wall Iahu ...
- Codeforces Round #612 (Div. 2) 前四题题解
这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...
- Codeforces Round #672 (Div. 2) A - C1题解
[Codeforces Round #672 (Div. 2) A - C1 ] 题目链接# A. Cubes Sorting 思路: " If Wheatley needs more th ...
- Codeforces Round #198 (Div. 2)E题解
E. Iahub and Permutations Iahub is so happy about inventing bubble sort graphs that he's staying all ...
- 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 #614 (Div. 2) A-E简要题解
链接:https://codeforces.com/contest/1293 A. ConneR and the A.R.C. Markland-N 题意:略 思路:上下枚举1000次扫一遍,比较一下 ...
- Codeforces Round #610 (Div. 2) A-E简要题解
contest链接: https://codeforces.com/contest/1282 A. Temporarily unavailable 题意: 给一个区间L,R通有网络,有个点x,在x+r ...
- Codeforces Round #611 (Div. 3) A-F简要题解
contest链接:https://codeforces.com/contest/1283 A. Minutes Before the New Year 题意:给一个当前时间,输出离第二天差多少分钟 ...
- 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 ...
随机推荐
- AI 的会议总结(by南大周志华)
原文链接:http://blog.csdn.net/akipeng/article/details/6533897 这个列的更详细:http://www.cvchina.info/2010/08/31 ...
- Lazarus开发环境编译选项配置
Lazarus的环境配置让人有点犯晕,对于刚从delphi转到lazarus上的我来说,每次新建工程都会遇到一堆Can't find unit xxxx used by xxxx的问题,问题虽然不大, ...
- (转)基于Metronic的Bootstrap开发框架经验总结(9)--实现Web页面内容的打印预览和保存操作
http://www.cnblogs.com/wuhuacong/p/5147368.html 在前面介绍了很多篇相关的<Bootstrap开发框架>的系列文章,这些内容基本上覆盖到了我这 ...
- Vue2 封装的 Quill 富文本编辑器组件 Vue-Quill-Editor
1.安装 npm install vue-quill-editor --save 2.使用 import { quillEditor } from 'vue-quill-editor' 3.组件中 & ...
- python tips:类的绑定方法(bound)和非绑定方法(unbound)
类属性只有类及其实例能够访问,可以理解为一个独立的命名空间. Python中类属性的引用方式有两种: 1. 通过类的实例进行属性引用,称为绑定方法(bound method),可以理解为方法与实例绑定 ...
- PAT_A1155#Heap Paths
Source: PAT A1155 Heap Paths (30 分) Description: In computer science, a heap is a specialized tree-b ...
- GETDATE()
定义和用法 GETDATE() 函数从 SQL Server 返回当前的时间和日期. 语法 GETDATE() 实例 例子 1 使用下面的 SELECT 语句: SELECT GETDATE() AS ...
- Linux内核tracepoints
Linux内核tracepoints 简单介绍 内核中的每个tracepoint提供一个钩子来调用probe函数. 一个tracepoint可以打开或关闭.打开时,probe函数关联到tracepoi ...
- [jzoj 5781]【NOIP提高A组模拟2018.8.8】秘密通道 (最短路)
传送门 Description 有一副nm的地图,有nm块地,每块是下列四种中的一种: 墙:用#表示,墙有4个面,分别是前面,后面,左面,右面. 起点:用C表示,为主角的起点,是一片空地. 终点:用F ...
- MySQL中的注释符号的使用
前言 在学习MySQL的过程中,因为目前接触的语法简单,所以实在没有想到过加入注释.在写博客使用Markdown时,突然想用注释语句,所以便百度了一下,引用了这一篇转载博客. MySQL中的注释符号有 ...