Time Limit: 1 second

Memory Limit: 128 MB

【问题描述】

生活中,大多数事物都是有序的,因为顺序的美是最令人陶醉的。所以现在RCDH看了不顺的东西就头痛。所以他想让世界变成有序,

可是他只是一个无名小辈,所以只好对数字序列下手。据他所知序列的混乱程度是由“逆序对”的个数决定,公式是Q=2^n,其中

Q是指混乱程度,n是指这个序列“逆序对”的个数。逆序对是这样定义的:假设序列中第I个数是ai,若存在Iaj,则

就为一个逆序对。你的任务是给定一个序列,计算其混乱程度Q。这个数可能会比较大,你只需输出Q mod 1991 的结果。

【输入格式】

第一行,整数n,表示序列中有n个数。

第二行,有n个数。

【输出格式】

仅一行,Q mod 1991 的值。

样例注释:样例中共有2个逆序对,故Q=2^2=4。所以,Q mod 1991=4。

【数据规模】

对于30%的数据 2= 对于100%的数据 2= 数列中的每个数不超过10000000的正整数。

Sample Input

4

1 3 4 2

Sample Output

4

【题目链接】:http://noi.qz5z.com/viewtask.asp?id=t059

【题意】

【题解】



线段树求逆序对的方法:http://blog.csdn.net/harlow_cheng/article/details/52453361

这里有会有重复数字,求逆序对的时候注意先递增相同大小的数字的答案,再更新线段树



【完整代码】

//n最大5万
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x) typedef pair<int, int> pii;
typedef pair<LL, LL> pll; const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 5e4+100;
const LL mod = 1991; struct abc
{
int x, id;
}; int n;
LL sum[N << 2];
LL ni = 0,temp = 1;
abc a[N]; bool cmp1(abc a, abc b)
{
return a.x > b.x;
} void in()
{
rei(n);
rep1(i, 1, n)
rei(a[i].x), a[i].id = i;
} void up_data(int pos,int l, int r, int rt)
{
if (l == r)
{
sum[rt]++;
return;
}
int m = (l + r) >> 1;
if (pos <= m)
up_data(pos, lson);
else
up_data(pos, rson);
sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
} LL query(int L, int R, int l, int r, int rt)
{
if (L > R)
return 0;
if (L <= l && r <= R)
return sum[rt];
int m = (l + r) >> 1;
LL temp1 = 0, temp2 = 0;
if (L <= m)
temp1 += query(L, R, lson);
if (m < R)
temp2 += query(L, R, rson);
return temp1 + temp2;
} void get_ans()
{
rep1(i, 1, n)
{
int l = i,r = i;
while (r + 1 <= n && a[r + 1].x == a[l].x) r++;
rep1(j, l, r)
ni += query(1, a[j].id - 1, 1, n, 1);
rep1(j,l,r)
up_data(a[j].id, 1, n, 1);
i = r;
}
} void ksm(LL x)
{
if (!x) return;
ksm(x >> 1);
temp = (temp*temp) % mod;
if (x & 1)
temp = (temp * 2) % mod;
} void o()
{
printf("%I64d\n", temp);
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
in();
sort(a + 1, a + 1 + n, cmp1);
get_ans();
ksm(ni);
o();
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}

【t059】序列的更多相关文章

  1. 【夯实PHP基础】UML序列图总结

    原文地址 序列图主要用于展示对象之间交互的顺序. 序列图将交互关系表示为一个二维图.纵向是时间轴,时间沿竖线向下延伸.横向轴代表了在协作中各独立对象的类元角色.类元角色用生命线表示.当对象存在时,角色 ...

  2. Windows10-UWP中设备序列显示不同XAML的三种方式[3]

    阅读目录: 概述 DeviceFamily-Type文件夹 DeviceFamily-Type扩展 InitializeComponent重载 结论 概述 Windows10-UWP(Universa ...

  3. 软件工程里的UML序列图的概念和总结

    俗话说,自己写的代码,6个月后也是别人的代码……复习!复习!复习! 软件工程的一般开发过程:愿景分析.业务建模,需求分析,健壮性设计,关键设计,最终设计,实现…… 时序图也叫序列图(交互图),属于软件 ...

  4. python序列,字典备忘

    初识python备忘: 序列:列表,字符串,元组len(d),d[id],del d[id],data in d函数:cmp(x,y),len(seq),list(seq)根据字符串创建列表,max( ...

  5. BZOJ 1251: 序列终结者 [splay]

    1251: 序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 3778  Solved: 1583[Submit][Status][Discu ...

  6. 最长不下降序列nlogn算法

    显然n方算法在比赛中是没有什么用的(不会这么容易就过的),所以nlogn的算法尤为重要. 分析: 开2个数组,一个a记原数,f[k]表示长度为f的不下降子序列末尾元素的最小值,tot表示当前已知的最长 ...

  7. [LeetCode] Sequence Reconstruction 序列重建

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...

  8. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  9. [LeetCode] Repeated DNA Sequences 求重复的DNA序列

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

随机推荐

  1. the steps that may be taken to solve a feature selection problem:特征选择的步骤

    參考:JMLR的paper<an introduction to variable and feature selection> we summarize the steps that m ...

  2. 什么是MVC,什么是WCF

    在C#中总会遇到这几个概念,网上搜了一下,做一下总结和比较,东拼西凑,如有雷同,纯属直接拷贝,人懒,但无意侵权. 1.什么是MVC MVC是三个单词的首字母缩写,它们是Model(模型).View(视 ...

  3. 互信息 & 卡方 - 文本挖掘

    在做文本挖掘,特别是有监督的学习时,常常需要从文本中提取特征,提取出对学习有价值的分类,而不是把所有的词都用上,因此一些词对分类的作用不大,比如“的.是.在.了”等停用词.这里介绍两种常用的特征选择方 ...

  4. (6)uboot具体解释——关闭缓存和mmu

    uboot具体解释--关闭缓存和mmu 当设置完时钟分频以后,uboot就会运行cpu_init_crit汇编函数,这个函数的主要作用就是关闭缓存和mmu.然后调用lowlevel_init函数进行系 ...

  5. P2P网贷第三方托管模式存在5大缺陷,托管机构才是最大赢家

    1.注册开户需要2次,用户体验很差劲儿.   理财人和借款人,首先在平台注册,然后还要在第三方托管账户注册.   很多类似的地方,用户体验非常差劲.   比如,密码4个.   平台:登录密码.交易密码 ...

  6. 小米笔记本(13.3 I7) ubuntu14.04下网卡驱动安装

    ubuntu 内核升级到4.6.4(更高版本可能造成系统无法启动) kernel debian包下载地址 http://kernel.ubuntu.com/~kernel-ppa/mainline/v ...

  7. php框架排名(Laravel一直第一)

    php框架排名(Laravel一直第一) 一.总结 1.Laravel,后面就用这个框架(要用好这个框架,英语得6啊) 2.YII框架和tp框架一样,也是一个国产框架 二.2017世界PHP框架排名T ...

  8. 使用ng-content进行组件内容投射

    原文 https://www.jianshu.com/p/c0a39b1776c0 大纲 1.认识内容投射 2.一个简单组件 3.简单投射 4.针对性投射 5.ngProjectAs 6.代码资源 认 ...

  9. 9个完整android开源app项目

    一.photoup 介绍: photoup 是一款开源的相册类app,主要功能是将本地图片提交到facebook上去,虽然他的功能和facebook的远程服务相关,但是本身是可以被当作一款 相册应用的 ...

  10. css强制不换行 多出的字省略号

    width: 100%;//需要指定宽度 overflow: hidden;//溢出隐藏 text-overflow: ellipsis; white-space: nowrap;//强制不换行 te ...