BestCoder Round #45 (2/4)

maksyuki 发表于 比赛 分类,标签:
0

A.Dylans loves numbers

Problem Description

Who is Dylans?You can find his ID in UOJ and Codeforces. His another ID is s1451900 in BestCoder. And now today's problems are all about him.Dylans is given a number N.

He wants to find out how many groups of "1" in its Binary representation.

If there are some "0"(at least one)that are between two "1",
then we call these two "1" are not in a group,otherwise they are in a group.

 

Input

In the first line there is a number T.

T is the test number.

In the next T lines there is a number N.

0≤N≤1018,T≤1000

 

Output

For each test case,output an answer.

 

Sample Input

15

Sample Output

2

Source

BestCoder Round #45

 

题目类型:简单模拟

算法分析:直接按照题意将long long型整数转换成二进制存储到数组中,然后扫一遍数组,判断有多少个相连的”1”即可,注意特判!!!!!!

 

B.Dylans loves sequence

Problem Description

Dylans is given N numbers a[1]....a[N]

And there are Q questions.

Each question is like this (L,R)

his goal is to find the “inversions” from number L to number R.

more formally,his needs to find the numbers of pair(x,y),
that L≤x,y≤R and x<y and a[x]>a[y]

 

 

Input

In the first line there is two numbers N and Q.

Then in the second line there are N numbers:a[1]..a[N]

In the next Q lines,there are two numbers L,R in each line.

N≤1000,Q≤100000,L≤R,1≤a[i]≤231−1

 

 

Output

For each query,print the numbers of "inversions”

Sample Input

3 23 2 11 21 3

Sample Output

13 Hint You shouldn't print any space in each end of the line in the hack data.

 

Source

BestCoder Round #45

 

题目类型:区间DP

算法分析:使用dp[i][j]表示[i,j]区间中逆序对的个数,状态转移方程为:dp[i][j] = dp[i][j-1] + tempsum,其中tempsum表示[i,j-1]区间中比a[j]大的数的个数。时间复杂度大约是O(n^3/k)

的,其中k是一个比较大的常数。由于测试数据是随机生成的,所以还是可以水过的,下面给出了一个使用前缀优化O(n^2)的算法

 

前缀优化:先简单说一下算法,使用dp[i][j]表示[i,j]区间中有多少个数比j大,然后一个优化是边读入边处理,从小区间到大区间计算dp数组并维护一个前缀性质,时间复杂度是O(n^2)的。状态转移方程为:

if (a[j] > a[i]) dp[j][i] = dp[j+1][i] + 1; else dp[j][i] = dp[j+1][i];

由于[i,j]区间内的逆序对个数=dp[i][i] + dp[i][i+1] + dp[i][i+2] + ……dp[i][j],所以再使用O(n^2)遍历一下数组维护一个前缀和即可,之后对于每一个查询,直接输出结果

 

树状数组(线段树)求解:这是一个比较常规的思路,在求解时使用DP递推,则可以将时间复杂度优化到O(n^2log(n))。注意这里元素的值比较大,在求解之前需要先进行离散化处理,这里将输入的数映射成一个连续的下标