思路:随便猜一猜填的数字是不下降的,反证很好证明,然后就没了。。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define PLI pair<LL, int>
#define ull unsigned long long
using namespace std; const int N = 1e4 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ; int n, k, a[N];
struct Bit {
int a[];
void modify(int x, int val) {
for(int i = x; i <= ; i += i & -i)
a[i] += val;
}
int sum(int x) {
int ans = ;
for(int i = x; i; i -= i & -i)
ans += a[i];
return ans;
}
int query(int l, int r) {
if(l > r) return ;
return sum(r) - sum(l-);
}
} b1, b2; int main() {
scanf("%d%d", &n, &k);
for(int i = ; i <= n; i++)
scanf("%d", &a[i]);
for(int i = n; i >= ; i--)
if(~a[i]) b2.modify(a[i], ); LL ans = ;
for(int i = ; i <= n; i++) {
if(~a[i]) {
b2.modify(a[i], -);
b1.modify(a[i], );
ans += b1.query(a[i]+, );
} else {
int ret = inf;
for(int j = ; j <= ; j++) {
ret = min(ret, b1.query(j+, ) + b2.query(, j-));
}
ans += ret;
}
}
printf("%lld\n", ans);
return ;
} /*
*/

bzoj 1831的更多相关文章

  1. BZOJ 1831 逆序对

    Description 小可可和小卡卡想到Y岛上旅游,但是他们不知道Y岛有多远.好在,他们找到一本古老的书,上面是这样说的: 下面是N个正整数,每个都在\(1 \sim K\)之间.如果有两个数\(A ...

  2. BZOJ 1831 & 就是一个DP....

    题意: 比如说,4 2 1 3 3里面包含了5个逆序对:(4, 2), (4, 1), (4, 3), (4, 3), (2, 1). 可惜的是,由于年代久远,这些数字里有一部分已经模糊不清了,为了方 ...

  3. 「BZOJ 1831」「AHOI 2008」逆序对「贪心」

    题意 给定一个长度为\(n\),值域为\([1,k]\),某些位置不确定的数组,求最小的逆序对.\(n\leq 10^4, k \leq 100\) 题解 这题有人用前缀和优化\(dp\)过了,但是这 ...

  4. BZOJ 1831: [AHOI2008]逆序对

    题目大意: 给出一个序列,有几个位置上的数字任意.求最小的逆序对数. 题解: 自己决定放置的数一定是单调不降的.不然把任意两个交换一下就能证明一定会增加逆序对. 然后就可以DP了,f[i][j]表示第 ...

  5. 【BZOJ】1831: [AHOI2008]逆序对

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1831 考虑$-1$的位置上填写的数字一定是不降的. 令${f[i][j]}$表示$DP$到 ...

  6. BZOJ刷题指南(转)

    基础(65) 巨水无比(4):1214.3816:2B题:1000A+B:2462:输出10个1 模拟/枚举/暴力(15):4063傻子模拟:1968小学生暴力:1218前缀和暴力:3856读英文:4 ...

  7. BZOJ 2127: happiness [最小割]

    2127: happiness Time Limit: 51 Sec  Memory Limit: 259 MBSubmit: 1815  Solved: 878[Submit][Status][Di ...

  8. BZOJ 3275: Number

    3275: Number Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 874  Solved: 371[Submit][Status][Discus ...

  9. BZOJ 2879: [Noi2012]美食节

    2879: [Noi2012]美食节 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1834  Solved: 969[Submit][Status] ...

随机推荐

  1. POJ 3308 Paratroopers(最小点权覆盖)(对数乘转加)

    http://poj.org/problem?id=3308 r*c的地图 每一个大炮可以消灭一行一列的敌人 安装消灭第i行的大炮花费是ri 安装消灭第j行的大炮花费是ci 已知敌人坐标,同时消灭所有 ...

  2. Java压缩/解压.zip、.tar.gz、.tar.bz2(支持中文)

    本文介绍Java压缩/解压.zip..tar.gz..tar.bz2的方式. 对于zip文件:使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile,通过设置 ...

  3. jQuery中下拉框select的操作方法详解

    最近在写页面的时候常常遇到要动态增删改下拉框select的情况,由于我比较习惯用jquery框架来架构我的前端js,所以就顺便把各种jquery操作下拉框select的方法总结了一下,收藏起来以便下次 ...

  4. HDU 1259 ZJUTACM

    解题报告:就用了一个swap函数就行了. #include<cstdio> #include<iostream> int main() { int x,y,T,n; scanf ...

  5. HDU 3449 Consumer (背包问题之有依赖背包)

    题目链接 Problem Description FJ is going to do some shopping, and before that, he needs some boxes to ca ...

  6. python初步学习-python模块之 commands

    commands 通过 os.popen() 执行 shell 命令,返回两个对象,一个是 状态码(Int).另一个为命令输出(str) commands.getoutput(cmd) 返回命令执行输 ...

  7. D. Sum in the tree(树形+贪心)

    题目链接;http://codeforces.com/contest/1099/problem/D 题目大意:给出一棵树,每个节点到根节点的路径上经过的所有点的权值之和,其深度为偶数的节点的信息全部擦 ...

  8. JSON数据填充表格——(三)

    1.定义页面请求JSON的按钮与定义一个带表头的表格  请求数据的按钮 <button class="btn btn-primary search_bar_button floatR& ...

  9. ASM配置OGG

    两种方法:http://blog.sina.com.cn/s/blog_aa84cfe40101lsks.html 使用ACFS配置OGG:http://ylw6006.blog.51cto.com/ ...

  10. Next Permutation & Previous Permutation

    Next Permutation Given a list of integers, which denote a permutation. Find the next permutation in ...