[hdu5216]排序

题意:给定两个长度为M的数组a,b,对于一个1-M的排列,不妨设为P,如果对任意0<=i<M,都有a[i] <= b[P[i]],那么称为一种合法情况,对于一种合法情况,对所有0<=i<M,在n个长度为1的线段上的区间[a[i],b[p[i]]]涂上颜色,计X=没有涂颜色的最大连续长度,求x在所有合法情况中的期望。

思路:这个题想到了就是大水题了,可惜比赛的时候题目都没看。由于全排列P的存在,使得a数组可以对应b数组的任意一种“比较方式”,于是存在合法情况等价于存在一种b数组的全排列使得a[i]<=b[i]恒成立,由于全排列的任意性,不妨将a数组,b数组分别排序,如果对任意i,a[i]<=b[i]恒成立,那么合法情况存在。然后合法情况存在的基础上,考虑重新排列一下b数组,以得到其它的合法情况。在重排列过程中注意到,无论怎么重排,只要是合法情况,最后线段的涂色情况是一样的!于是对每一种合法情况,概率一样,X一样,所以期望等于任意一种合法情况的X。由于根据所有的i,把区间[a[i], b[i]]的线段涂上颜色,没涂颜色的连续段只可能出现在[b[i]+1,a[i+1]-1](至于为什么,画个图就清楚了,相当于左右边界分别递增的线段去覆盖),用这个去更新答案。

  1 #pragma comment(linker, "/STACK:102400000,102400000")
  2 
  3 #include <iostream>
  4 #include <cstdio>
  5 #include <algorithm>
  6 #include <cstdlib>
  7 #include <cstring>
  8 #include <map>
  9 #include <queue>
 10 #include <deque>
 11 #include <cmath>
 12 #include <ctime>
 13 #include <cctype>
 14 #include <set>
 15 #include <bitset>
 16 #include <functional>
 17 #include <numeric>
 18 #include <stdexcept>
 19 #include <utility>
 20 #include <vector>
 21 
 22 using namespace std;
 23 
 24 #define mem0(a) memset(a, 0, sizeof(a))
 25 #define mem_1(a) memset(a, -1, sizeof(a))
 26 #define lson l, m, rt << 1
 27 #define rson m + 1, r, rt << 1 | 1
 28 #define define_m int m = (l + r) >> 1
 29 #define rep_up0(a, b) for (int a = 0; a < (b); a++)
 30 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
 31 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
 32 #define rep_down1(a, b) for (int a = b; a > 0; a--)
 33 #define all(a) (a).begin(), (a).end()
 34 #define lowbit(x) ((x) & (-(x)))
 35 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
 36 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
 37 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
 38 #define pchr(a) putchar(a)
 39 #define pstr(a) printf("%s", a)
 40 #define sstr(a) scanf("%s", a)
 41 #define sint(a) scanf("%d", &a)
 42 #define sint2(a, b) scanf("%d%d", &a, &b)
 43 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
 44 #define pint(a) printf("%d
", a)
 45 #define test_print1(a) cout << "var1 = " << a << endl
 46 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
 47 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
 48 
 49 typedef long long LL;
 50 typedef pair<int, int> pii;
 51 typedef vector<int> vi;
 52 
 53 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
 54 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };
 55 const int maxn = 1e5 + 7;
 56 const int md = 10007;
 57 const int inf = 1e9 + 7;
 58 const LL inf_L = 1e18 + 7;
 59 const double pi = acos(-1.0);
 60 const double eps = 1e-6;
 61 
 62 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
 63 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
 64 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
 65 template<class T>T condition(bool f, T a, T b){return f?a:b;}
 66 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
 67 int make_id(int x, int y, int n) { return x * n + y; }
 68 
 69 int a[60], b[60];
 70 
 71 int main() {
 72     //freopen("in.txt", "r", stdin);
 73     int T, n, m;
 74     cin >> T;
 75     while (T --) {
 76         cin >> n >> m;
 77         rep_up0(i, m) {
 78             sint(a[i]);
 79         }
 80         rep_up0(i, m) {
 81             sint(b[i]);
 82         }
 83         sort(a, a + m);
 84         sort(b, b + m);
 85         bool ok = true;
 86         rep_up0(i, m) {
 87             if (a[i] > b[i]) {
 88                 ok = false;
 89                 break;
 90             }
 91         }
 92         if (!ok) {
 93             puts("Stupid BrotherK!");
 94             continue;
 95         }
 96         int ans = a[0] - 1;
 97         rep_up0(i, m - 1) {
 98             max_update(ans, a[i + 1] - b[i] - 1);
 99         }
100         max_update(ans, n - b[m - 1]);
101         printf("%d.000000
", ans);
102     }
103     return 0;
104 }
View Code
原文地址:https://www.cnblogs.com/jklongint/p/4486586.html