Codeforces 193D Two Segments 解题报告
先是在蓝桥杯的网站上看到一道题:
给出1~n的一个排列,求出区间内所有数是连续自然数的区间的个数。n<=50000。
由于数据较弱,即使用O(N^2)的算法也能拿到满分。
于是在CF上发现了这一题:
给一个1~n的排列,在这个排列中选出两段区间,求使选出的元素排序后构成公差为1的等差数列的方案数。选出的两段区间中元素构成的集合相同时视为同一种方案。N<=300000。
可以发现CF的这题是上一题的强化版。
虽然蓝桥的题目是简化版,但正确的做法似乎没有CF这道题容易想到。
蓝桥的题首先想到的应该是枚举区间,判断区间最大值与最小值的差是否等于区间长度,这样时间复杂度是O(N^2)。
似乎不知道该如何优化。
因此先从CF这题考虑一个比较容易想到的做法:
考虑自然序列[l,r],假设已经知道[l,r]在读入序列中被分成了几段(假设f[l,r]=k),那么在加入数l-1时,只需判断l-1这个数是单独构成一段(f[l-1,r]=k+1),还是并入了其中一段(f[l-1,r]=k),或者连接了两段(f[l,r]=k-1).当k等于1或2时就可以累加答案.使用这样的方法同样只需要枚举区间左右端点就可以得到一个O(n^2)的算法.再进一步考虑自然序列[l,i],(l<=i<=n).假设t=l-1,在读入序列的左右分别是数x和y(假设x<y).那么对应的f[l-1,m] 中m对应的区间要进行相应的更新(+1,-1),这里可以用线段树来维护.和查询.如此只需要n到1循环l,可以在O(nlogn)的时间复杂度实现.
对于蓝桥杯的题,假设当前枚举的数是i令[l,r],为f[i,l~r]数值区间.线段树需要维护的东西有区间内最小值,区间内等于最小值的数的个数.CF题需要额外维护,等于区间最小值+1的数的个数.
蓝桥杯的代码:
#include <iostream>
#include <cstdio>
using namespace std;
#define lson x<<1
#define rson x<< 1 | 1
const int MAXN = ;
struct Segtree {
int l, r, val, add, tol;
} St[MAXN << ];
int p[MAXN], a[MAXN];
int n, ans;
void Build (int x, int l, int r) {
St[x].l = l, St[x].r = r;
St[x].tol = r - l + ;
if (l == r) return;
int mid = (l + r) >> ;
Build (lson, l, mid), Build (rson, mid + , r);
}
void push (int x) {
if (St[x].add == ) return;
St[lson].add +=St[x].add, St[rson].add += St[x].add;
St[lson].val += St[x].add, St[rson].val += St[x].add;
St[x].add = ;
}
void update (int x) {
St[x].val = min (St[lson].val, St[rson].val);
St[x].tol = St[lson].tol * (St[x].val == St[lson].val) + St[rson].tol * (St[x].val == St[rson].val);
}
void Modify (int x, int l, int r, int key) {
if (St[x].l >= l && St[x].r <= r) {
St[x].val += key, St[x].add += key;
return;
}
push (x);
int mid = (St[x].l + St[x].r) >> ;
if (mid >= l) Modify (lson, l, r, key);
if (mid < r) Modify (rson, l, r, key);
update (x);
}
void Query (int x, int l, int r) {
if (St[x].l >= l && St[x].r <= r) {
ans += St[x].tol * (St[x].val == );
return;
}
push (x);
int mid = (St[x].l + St[x].r) >> ;
if (mid >= l) Query (lson, l, r);
if (mid < r) Query (rson, l, r);
}
int main() {
scanf ("%d", &n);
for (int i = , x; i <= n; i++) {
scanf ("%d", &x);
p[x] = i;
}
Build (, , n);
for (int i = n; i; i--) {
a[p[i]] = i;
int x = a[p[i] - ], y = a[p[i] + ];
if (x > y) swap (x, y);
if (x && y) Modify (, i, x - , ), Modify (, y, n, -);
else if (y) Modify (, i, y - , );
else Modify (, i, n, );
Query (, i, n);
}
printf ("%d", ans);
}
CF的代码:
#include <iostream>
#include <cstdio>
using namespace std;
#define lson x<<1
#define rson x<< 1 | 1
const int MAXN = ;
struct Segtree {
int l, r, val, add, tol, tol2;
} St[MAXN << ];
int p[MAXN], a[MAXN];
int n;
long long ans;
void Build (int x, int l, int r) {
St[x].l = l, St[x].r = r;
St[x].tol = r - l + ;
if (l == r) return;
int mid = (l + r) >> ;
Build (lson, l, mid), Build (rson, mid + , r);
}
void push (int x) {
if (St[x].add == ) return;
St[lson].add +=St[x].add, St[rson].add += St[x].add;
St[lson].val += St[x].add, St[rson].val += St[x].add;
St[x].add = ;
}
void update (int x) {
St[x].val = min (St[lson].val, St[rson].val);
St[x].tol = St[lson].tol * (St[x].val == St[lson].val) + St[rson].tol * (St[x].val == St[rson].val);
St[x].tol2=St[lson].tol*(St[x].val+==St[lson].val)+St[rson].tol*(St[x].val+==St[rson].val);
St[x].tol2+=St[lson].tol2*(St[x].val==St[lson].val)+St[rson].tol2*(St[x].val==St[rson].val);
}
void Modify (int x, int l, int r, int key) {
if (St[x].l >= l && St[x].r <= r) {
St[x].val += key, St[x].add += key;
return;
}
push (x);
int mid = (St[x].l + St[x].r) >> ;
if (mid >= l) Modify (lson, l, r, key);
if (mid < r) Modify (rson, l, r, key);
update (x);
}
void Query (int x, int l, int r) {
if (St[x].l >= l && St[x].r <= r) {
ans += St[x].tol * (St[x].val <=)+St[x].tol2*(St[x].val==);
return;
}
push (x);
int mid = (St[x].l + St[x].r) >> ;
if (mid >= l) Query (lson, l, r);
if (mid < r) Query (rson, l, r);
}
int main() {
scanf ("%d", &n);
for (int i = , x; i <= n; i++) {
scanf ("%d", &x);
p[x] = i;
}
Build (, , n);
for (int i = n; i; i--) {
a[p[i]] = i;
int x = a[p[i] - ], y = a[p[i] + ];
if (x > y) swap (x, y);
if (x) Modify (, i, x - , ), Modify (, y, n, -);
else if (y) Modify (, i, y - , );
else Modify (, i, n, );
Query (, i, n);
}
printf ("%I64d", ans-n);
}
Codeforces 193D Two Segments 解题报告的更多相关文章
- Codeforces Round 665 赛后解题报告(暂A-D)
Codeforces Round 665 赛后解题报告 A. Distance and Axis 我们设 \(B\) 点 坐标为 \(x(x\leq n)\).由题意我们知道 \[\mid(n-x)- ...
- Codeforces Round 662 赛后解题报告(A-E2)
Codeforces Round 662 赛后解题报告 梦幻开局到1400+的悲惨故事 A. Rainbow Dash, Fluttershy and Chess Coloring 这个题很简单,我们 ...
- Codeforces Round #277.5 解题报告
又熬夜刷了cf,今天比正常多一题.比赛还没完但我知道F过不了了,一个半小时贡献给F还是没过--应该也没人Hack.写写解题报告吧= =. 解题报告例如以下: A题:选择排序直接搞,由于不要求最优交换次 ...
- codeforces B. Simple Molecules 解题报告
题目链接:http://codeforces.com/problemset/problem/344/B 题目意思:这句话是解题的关键: The number of bonds of an atom i ...
- codeforces 591A. Wizards' Duel 解题报告
题目链接:http://codeforces.com/problemset/problem/591/A 题目意思:其实看下面这幅图就知道题意了,就是Harry 和 He-Who-Must-Not-Be ...
- codeforces 582A. GCD Table 解题报告
题目链接:http://codeforces.com/problemset/problem/582/A 网上很多题解,就不说了,直接贴代码= = 官方题解: http://codeforces.com ...
- codeforces 581C. Developing Skills 解题报告
题目链接:http://codeforces.com/problemset/problem/581/C 题目意思:给出 n 个数:a1, a2, ..., an (0 ≤ ai ≤ 100).给出值 ...
- codeforces 577B. Modulo Sum 解题报告
题目链接:http://codeforces.com/problemset/problem/577/B 题目意思:就是给出 n 个数(a1, a2, ..., an) 和 m,问能不能从这 n 个数中 ...
- codeforces 540B.School Marks 解题报告
题目链接:http://codeforces.com/problemset/problem/540/B 题目意思:给出 k 个test的成绩,要凑剩下的 n-k个test的成绩,使得最终的n个test ...
随机推荐
- iOS开发:使用Tab Bar切换视图
iOS开发:使用Tab Bar切换视图 上一篇文章提到了多视图程序中各个视图之间的切换,用的Tool Bar,说白了还是根据触发事件使用代码改变Root View Controller中的Conten ...
- HDOJ/HDU 2561 第二小整数(水题~排序~)
Problem Description 求n个整数中倒数第二小的数. 每一个整数都独立看成一个数,比如,有三个数分别是1,1,3,那么,第二小的数就是1. Input 输入包含多组测试数据. 输入的第 ...
- 获得当前屏幕的CGRect
CGRect mainScreen = [[UIScreen mainScreen] bounds];
- 钥匙计数之一 - HDU 1438(状态压缩打表)
分析:首先想到每个钥匙的结尾有4种状态,不过题目还需要判断有三种不同的钥匙深度,所以每种深度结尾后有2^4种状态,0000->1111,不过题目还需需要有相邻的钥匙深度大于等于3,所以需要两种不 ...
- Distinct Substrings - spoj 694(不重复子串个数)
题目大意:RT 分析:练手题目....后缀数组确实很强大.....多理解height数组, 切勿使用模版,后缀数组本身就有很多细节,多犯错更有利理解这个算法. 代码如下: ========== ...
- Eclipse自动换行WordWrap插件
eclipse没有自动换行功能,需要安装插件wordwrap,方法请自行百度,可以参考下面的方法: http://jingyan.baidu.com/article/ce09321b7ba7042bf ...
- intellij安装 配置 创建项目
使用intellij创建项目的整个过程如下: 首先,点击intllij的.exe文件,如果是第一次安装,选择第二个选项即可 Intellij需要license key,可以使用注册机生成相应的name ...
- div页面居中(上下左右)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or ...
- IE8下提示'console'没有定义错误
在开发的过程中因为调试的原因,在代码中增加console.info("xxxx"),而未进行删除 在IE8下測试该代码所在的页面报错,例如以下: 须要注意的是,使用console对 ...
- iOS 8 设置导航栏的背景颜色和背景图片
假设是storyboard 直接embed一个导航栏.然后在新出现的导航栏 选属性 选一下颜色就能够了 代码实现背景颜色改动:self.navigationController.navigationB ...