目录
例1 逆序对2 - 题目 - Daimayuan Online Judge
例3 [CF 1385D] a-Good String - 题目 - Daimayuan Online Judge
例4 平面最近点对 - 题目 - Daimayuan Online Judge
核心思想:
- 自上而下通过递归将大问题拆分成多个子问题,直至被拆分出来的子问题可以通过简单的方法解决
- 自下而上的用子问题的解求大问题的解
- 最终得到初始问题的解
应用:快速排序,归并排序
例1 逆序对2 - 题目 - Daimayuan Online Judge
样例输入
4
4 2 3 1
样例输出
5
样例解释
5 个逆序对分别为 (4,2),(4,3),(4,1),(2,1),(3,1)。
数据范围
对于 100% 的数据,保证 2≤n≤100000,1≤ai≤n并且每个数字都只会出现一次。
代码:
#include <iostream>
using namespace std;
int n, a[100005], c[100005];
long long solve(int l, int r) {//归并排序并计算逆序对
if (l == r)return 0;
int m = (l + r) / 2;
long long res = solve(l, m) + solve(m + 1, r);
int p1 = l, p2 = m + 1, cnt = 0;
while (p1 <= m && p2 <= r) {
if (a[p1] < a[p2])
c[++cnt] = a[p1++], res += p2 - m - 1;
else
c[++cnt] = a[p2++];
}while (p1 <= m)
c[++cnt] = a[p1++], res += p2 - m - 1;
while (p2 <= r)
c[++cnt] = a[p2++];
for (int i = l; i <= r; i++)
a[i] = c[i - l + 1];
return res;
}
int main(){
scanf("%d", &n);
for (int i = 1; i <= n;i++)
scanf("%d", &a[i]);
printf("%lld\n", solve(1, n));
return 0;
}
例2 C - BrainmanC - Brainman
Background
Raymond Babbitt drives his brother Charlie mad. Recently Raymond counted 246 toothpicks spilled all over the floor in an instant just by glancing at them. And he can even count Poker cards. Charlie would love to be able to do cool things like that, too. He wants to beat his brother in a similar task.
Problem
Here's what Charlie thinks of. Imagine you get a sequence of N numbers. The goal is to move the numbers around so that at the end the sequence is ordered. The only operation allowed is to swap two adjacent numbers. Let us try an example:
Start with: 2 8 0 3
swap (2 8) 8 2 0 3
swap (2 0) 8 0 2 3
swap (2 3) 8 0 3 2
swap (8 0) 0 8 3 2
swap (8 3) 0 3 8 2
swap (8 2) 0 3 2 8
swap (3 2) 0 2 3 8
swap (3 8) 0 2 8 3
swap (8 3) 0 2 3 8
So the sequence (2 8 0 3) can be sorted with nine swaps of adjacent numbers. However, it is even possible to sort it with three such swaps:
Start with: 2 8 0 3
swap (8 0) 2 0 8 3
swap (2 0) 0 2 8 3
swap (8 3) 0 2 3 8
The question is: What is the minimum number of swaps of adjacent numbers to sort a given sequence?Since Charlie does not have Raymond's mental capabilities, he decides to cheat. Here is where you come into play. He asks you to write a computer program for him that answers the question. Rest assured he will pay a very good prize for it.
Input
The first line contains the number of scenarios.
For every scenario, you are given a line containing first the length N (1 <= N <= 1000) of the sequence,followed by the N elements of the sequence (each element is an integer in [-1000000, 1000000]). All numbers in this line are separated by single blanks.
Output
Start the output for every scenario with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the minimal number of swaps of adjacent numbers that are necessary to sort the given sequence. Terminate the output for the scenario with a blank line.
Sample
Inputcopy | Outputcopy |
---|---|
4 4 2 8 0 3 10 0 1 2 3 4 5 6 7 8 9 6 -42 23 6 28 -100 65537 5 0 0 0 0 0 | Scenario #1: 3 Scenario #2: 0 Scenario #3: 5 Scenario #4: 0 |
代码:
#include <iostream>
using namespace std;
int t, n, a[100005], c[100005], ani = 1;
long long solve(int l, int r) {//¹é²¢ÅÅÐò²¢¼ÆËãÄæÐò¶Ô
if (l == r)return 0;
int m = (l + r) / 2;
long long res = solve(l, m) + solve(m + 1, r);
int p1 = l, p2 = m + 1, cnt = 0;
while (p1 <= m && p2 <= r) {
if (a[p1] <= a[p2])
c[++cnt] = a[p1++];
else
c[++cnt] = a[p2++], res += m - p1 + 1;
}while (p1 <= m)
c[++cnt] = a[p1++];
while (p2 <= r)
c[++cnt] = a[p2++];
for (int i = l; i <= r; i++)
a[i] = c[i - l + 1];
return res;
}
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
printf("Scenario #%d:\n%lld\n\n", ani++, solve(1, n));
}
return 0;
}
例3 [CF 1385D] a-Good String - 题目 - Daimayuan Online Judge
样例输入
5
8
bbdcaaaa
8
asdfghjk
8
ceaaaabb
8
bbaaddcc
1
z
样例输出
0
7
4
5
1
数据规模
对于 100%的数据,保证 1≤T≤5,1≤n≤131072,保证 n=2k,k 为大于等于零的整数。
代码:
#include<bits/stdc++.h>
using namespace std;
int t, n;
char s[200001];
int solve(int l, int r, int x) {
if (l == r)
if (x == s[l])return 0;
else return 1;
int m = (l + r) / 2;
int y1 = 0, y2 = 0;
for (int i = l; i <= m; i++)
if (s[i] != x)y1++;
for (int i = m + 1; i <= r; i++)
if (s[i] != x)y2++;
return min(y1 + solve(m + 1, r, x + 1), y2 + solve(l, m, x + 1));
}
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%s", &n, s + 1);
printf("%d\n", solve(1, n, 'a'));
}
return 0;
}
例4 平面最近点对 - 题目 - Daimayuan Online Judge
样例输入
3
1.0 2.0
1.0 3.0
1.0 5.0
样例输出
1.00000000
数据规模
对于 100%的数据,保证 1≤n≤2×10^5, 0≤xi,yi≤10^9。
代码:
#include <bits/stdc++.h>
using namespace std;
struct Node {
double x, y;
bool operator < (const Node& A) const {
return x < A.x;
}
}a[200001], c[200001];
int n;
double solve(int l, int r) {
if (l == r)
return 1e10;
int m = (l + r) / 2;
double d = min(solve(l, m), solve(m + 1, r));
int cnt = 0;
for (int i = l; i <= r; i++)
if (abs(a[i].x - a[m].x) < d)
c[++cnt].x = a[i].y, c[cnt].y = a[i].x;
sort(c + 1, c + cnt + 1);
for (int i = 1; i <= cnt; i++)
for (int j = i + 1; j <= cnt && c[j].x - c[i].x < d; j++)
d = min(d, sqrt((c[i].x - c[j].x) * (c[i].x - c[j].x) + (c[i].y - c[j].y) * (c[i].y - c[j].y)));
return d;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lf%lf", &a[i].x,&a[i].y);
}
sort(a + 1, a + n + 1);
printf("%.10f\n", solve(1, n));
return 0;
}