锕鲤2020-3-30 bst

n个养鸡场,

  • 初始a[i]只鸡。
  • 每天增k只
  • 每天结束都会在数量最多的养鸡场里卖掉一半的小鸡,
    • 有x只鸡,则卖出后只剩下x/2(向下取整)只鸡。
  • m天后小强的n个养鸡场一共多少只小鸡?

  • 第一行输入三个int类型n,m,k(1 <= n,m,k <= 10^6)
  • 第二行输入n个正整数,表示n个养鸡场初始鸡的个数
  • 出 输出一个整数表示鸡的总数

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
intmain(){
    int n,m,k,t;
    ll base(0),sum(0);
    scanf("%d%d%d",&n,&m,&k);
    priority_queue<int> heap;//默认大根堆
    for(int i = 0; i < n; i++){
        scanf("%d", &t);
        heap.emplace(t);
        sum += t;
    }
    for(int i = 0;i < m; i++){
        base += k;
        t = heap.top() + base;
        int d = (t + 1) / 2;
        heap.pop();
        heap.emplace(t - d - base);
        sum -= d;
    }

    printf("%lld\n",base * n + sum);

}

长度为n的序列,

  • 随机选择一个连续子序列,并求这个序列的最大值,
    • 最大值的期望
  • 输入 第一行n表示序列长度
  • n个数描述这个序列,
  • n大于等于1小于等于1000000,数字保证是正整数且不超过100000
  • 保留6位小数

在这里插入图片描述

  • 总的序列数= n+(n-1)+…+2+1 = n*(n+1)/2 个,概率相同
  • x[i]为结尾的子序列,这些子序列中有两种情况
    • 一种是最大值为x[i],
    • 一种是最大值不为x[i];
    • 最大值不为x[i]的相当于x[i]没有加入,可以借助之前的状态求解;最大值为x[i]的情况只需记录有多少个。
  • 用单调栈的思路,从大到小存放出现的元素,并记录值对应的index值。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int N = 1000006;
double dp[N];//dp[i]表示前i个数中的最大值期望
intmain(){
    int n,t;
    scanf("%d",&n);
    ll c = (ll)n * (n + 1) / 2;
    dp[0] = 0;//无意义
    stack<PII> m;
    double res = 0;
    for(int i = 0;i < n; i++){
        scanf("%d",&t);
        while(!m.empty() && m.top().first <= t){
            m.pop();
        }
        int d = m.empty()?i + 1 : i-m.top().second;
        dp[i + 1] = 1.0 * t *d / c + dp[i + 1 - d];
        res += dp[i + 1];
        m.emplace(t,i);
    }
    printf("%.6f\n",res);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章