gym 102082G BZOJ4240 贪心+树状数组
4240: 有趣的家庭菜园
Time Limit: 10 Sec Memory Limit: 256 MB
Submit: 756 Solved: 349
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
2
8
4
5
3
6
Sample Output
HINT

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<time.h>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 300005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
#define mclr(x,a) memset((x),a,sizeof(x))
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii; inline int rd() {
int x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ ll ans;
int n, l, r;
struct node {
int v, p;
}t[maxn];
int tr[maxn];
bool cmp(node a, node b) {
return a.v > b.v;
}
void add(int x) {
while (x <= n) {
tr[x]++; x += x & -x;
} } int query(int x) {
int rs = 0;
while (x > 0) {
rs += tr[x]; x -= x & -x;
}
return rs;
} int main()
{
// ios::sync_with_stdio(0);
n = rd();
for (int i = 1; i <= n; i++)t[i].v = rd(), t[i].p = i;
sort(t + 1, t + 1 + n, cmp);
for (int i = 1; i <= n; i++) {
int tp = i; while (t[tp].v == t[tp + 1].v&&tp < n)tp++;
for (int j = i; j <= tp; j++)ans += 1ll * min(query(t[j].p), i - 1 - query(t[j].p));
for (int j = i; j <= tp; j++)add(t[j].p);
i = tp;
}
cout << (ll)ans << endl;
return 0;
}
gym 102082G BZOJ4240 贪心+树状数组的更多相关文章
- 【bzoj4240】有趣的家庭菜园 贪心+树状数组
题目描述 对家庭菜园有兴趣的JOI君每年在自家的田地中种植一种叫做IOI草的植物.JOI君的田地沿东西方向被划分为N个区域,由西到东标号为1~N.IOI草一共有N株,每个区域种植着一株.在第i个区域种 ...
- [BZOJ4240]有趣的家庭菜园(贪心+树状数组)
最后数列一定是单峰的,问题就是最小化最后的位置序列的逆序对数. 从大到小加数,每次贪心看放左边和右边哪个产生的逆序对数更少,树状数组即可. 由于大数放哪对小数不产生影响,所以正确性显然. 注意相同数之 ...
- 贪心+树状数组维护一下 Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D
http://codeforces.com/contest/724/problem/D 题目大意:给你一个串,从串中挑选字符,挑选是有条件的,按照这个条件所挑选出来的字符集合sort一定是最后选择当中 ...
- D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆]
题目描述 Koishi喜欢线段. 她的条线段都能表示成数轴上的某个闭区间.Koishi喜欢在把所有线段都放在数轴上,然后数出某些点被多少线段覆盖了. Flandre看她和线段玩得很起开心,就抛给她一个 ...
- Gym 101908C - Pizza Cutter - [树状数组]
题目链接:https://codeforces.com/gym/101908/problem/C 题意: 一块正方形披萨,有 $H$ 刀是横切的,$V$ 刀是竖切的,不存在大于等于三条直线交于一点.求 ...
- [P4064][JXOI2017]加法(贪心+树状数组+堆)
题目描述 可怜有一个长度为 n 的正整数序列 A,但是她觉得 A 中的数字太小了,这让她很不开心. 于是她选择了 m 个区间 [li, ri] 和两个正整数 a, k.她打算从这 m 个区间里选出恰好 ...
- [luoguP2672] 推销员(贪心 + 树状数组 + 优先队列)
传送门 贪心...蒟蒻证明不会... 每一次找最大的即可,找出一次最大的,数列会分为左右两边,左边用stl优先队列维护,右边用树状数组维护.. (线段树超时了....) 代码 #include < ...
- codeforces 1249 D2 Too Many Segments (hard version) 贪心+树状数组
题意 给定n个线段,线段可以相交,第\(i\)个线段覆盖的区间为\([l_i,r_i]\),问最少删除多少个线段让覆盖每个点的线段数量小于等于k. 分析 从左往右扫每个点\(x\),若覆盖点\(x\) ...
- [JZO6401]:Time(贪心+树状数组)
题目描述 小$A$现在有一个长度为$n$的序列$\{x_i\}$,但是小$A$认为这个序列不够优美. 小$A$认为一个序列是优美的,当且仅当存在$k\in [1,n]$,满足:$$x_1\leqsla ...
随机推荐
- 外网访问vmvare
使用端口映射即可,以tplink wr886n为例, 方法如下: 1.打开浏览器,输入默认ip192.168.0.1回车登录: 2.登录对话框,输入 密码点击确定: 3.点击转发规则--虚拟服务器,点 ...
- Tornado 接口的实现
- Project Browser & Inspector
[Project Browser] You can drag items from the project structure list to the Favourites and also save ...
- Mac设置Root密码
[Mac设置Root密码] Mac系统重新设置root用户密码 如果不知道root用户密码,需要重设. 命令[sudo passwd root ] 然后提示你输入当前登录用户密码,通过以后, ...
- CSS 内容生成
原文地址:http://www.zhangxinxu.com/wordpress/?p=739 一.哗啦哗啦的简介 zxx://这里“哗啦哗啦”的作用是为了渲染一种氛围.content属性早在CSS2 ...
- selenium2 用selenium安装、加载、启用插件(一)
一:下载 下载地址是:http://docs.seleniumhq.org/download/
- 598. Range Addition II 矩阵的范围叠加
[抄题]: Given an m * n matrix M initialized with all 0's and several update operations. Operations are ...
- Part5核心初始化_lesson4---关闭mmu
1.ARM存储体系 2.cache 3.虚拟地址 那么谁来完成把虚拟地址转换成物理地址呢? 4.这个工作就由MMU来转换!! 5.关闭MMU和cache 他们都是通过cp15协处理器来控制的!应该在A ...
- Android不间断上报位置信息-应用进程防杀指南
没用的 除非加入白名单 或者用户自己设置锁屏后不被杀死 不然的话 锁屏5分钟以内app会被杀死,包 括所有的service. 说白了就是定位不要纯依赖gps,很多硬件为了省电,会对熄屏下的模块功能和运 ...
- ASP.NET 5 Middleware, Or Where Has My HttpModule Gone?
31 March 2015 13:22 ASP.NET 5 has been largely rewritten from the ground up, and incorporates some r ...