CodeForces 939E Maximize!

题目链接

我的做法是离线进行分治。

假设选择第$i$个数字作为最大值,那么比它小的部分的数字肯定是某个前缀,也就是要去寻找选择哪个前缀使得平均值最小。

可以发现前缀的寻找具有决策单调性,也就是说,如果选择第$i$个数字作为最大值的时候,前缀是选择了$[1, p]$;那么选择第$i$个数字之后的数字作为最大值的时候,前缀是选择肯定大于等于$p$位置。因此分治就可以了。

#include <bits/stdc++.h>
using namespace std;

const int maxn = 5e5 + 10;
long long sum[maxn];
long long a[maxn];
double mean[maxn];
double ans[maxn];
int n;
queue<int> q;

void dp(int l, int r, int dl, int dr) {
  int mid = (l + r) >> 1, dm = dl;
  double g = 2e9;
  for(int i = dl; i <= dr && i < mid; i ++) {
    double tmp = 1.0 * (sum[i] + a[mid]) / (i + 1);
    if(tmp < g) g = tmp, dm = i;
  }
  ans[mid] = g;
  if(l < mid) dp(l, mid - 1, dl, dm);
  if(r > mid) dp(mid + 1, r, dm, dr);
}

int main() {
  int Q;
  scanf("%d", &Q);
  while(Q --) {
    int op;
    scanf("%d", &op);
    if(op == 1) {
      long long x;
      scanf("%lld", &x);
      n ++;
      a[n] = x;
      sum[n] = sum[n - 1] + a[n];
    } else {
      q.push(n);
    }
  }
  dp(1, n, 1, n);
  for(int i = 1; i <= n; i ++) {
    ans[i] = 1.0 * a[i] - ans[i];
  }
  ans[1] = 0.0;
  for(int i = 2; i <= n; i ++) {
    ans[i] = max(ans[i], ans[i - 1]);
  }
  while(!q.empty()) {
    printf("%.8f\n", ans[q.front()]);
    q.pop();
  }
  return 0;
}

原文地址:https://www.cnblogs.com/zufezzt/p/8455152.html

时间: 2024-08-05 03:43:00

CodeForces 939E Maximize!的相关文章

Codeforces 939E Maximize! 离散 三分

题目传送门↓http://codeforces.com/problemset/problem/939/E 题意:有一最初为空的不下降序列,有两种操作,一种是在序列里增加一个不小于序列尾的整数,另一种是查询当前序列中的某个子集,其最大值为max,平均值为mean,使得子集的max-mean在当前序列的所有子集中最大,输出这个max-mean. 思考: 1.序列最大值是否入选,假设不入选,则此时答案的子集中,最大值为max1,和为sum1,元素个数为k则答案可表示为max1 - sum1 / k:若

Codeforces 939E Maximize! (三分 || 尺取)

<题目链接> 题目大意:给定一段序列,每次进行两次操作,输入1 x代表插入x元素(x元素一定大于等于之前的所有元素),或者输入2,表示输出这个序列的任意子集$s$,使得$max(s)-mean(s)$表示这个集合的最大值与平均值的最大差值. 解题分析:首先,因为输入的$x$是非递减的,所以要使$max(s)-mean(s)$最大,肯定$max(s)$就是最后一个输入元素的大小.$x$已经确定了,现在就是尽可能的使$mean(s)$尽可能的小.如何使得平均值最小呢?肯定是从最前面的最小的元素开始

codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” consists of n servers and m two-way communication links. Two servers can communicate either through a direct link, or through a chain of links, by relayi

Codeforces Round #308 (Div. 2)

A. Vanya and Table Vanya has a table consisting of 100 rows, each row contains 100 cells. The rows are numbered by integers from 1 to 100 from bottom to top, the columns are numbered from 1 to 100 from left to right. In this table, Vanya chose n rect

Codeforces #180 div2 C Parity Game

// Codeforces #180 div2 C Parity Game // // 这道题的题目意思就不解释了 // // 题目有那么一点难(对于我而言),不多说啦 // // 解题思路: // // 首先如果a串和b串相等,不多说直接YES // 如果b串全是0,直接YES // 注意到a串有一个性质,1的个数不会超过本身的加1. // a有个1的上限设为x,b有个1的个数设为y,则如果x < y // 那么直接NO. // // 现在一般情况下,就是模拟啦,找到a的后缀和b的前缀一样的

B. Ohana Cleans Up(Codeforces Round #309 (Div. 2))

B. Ohana Cleans Up Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she swe

Codeforces Round #105 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/148 比较简单的一场,最长的一题也才写了30行多一点 A. Insomnia cure time limit per test:2 seconds memory limit per test:256 megabytes ?One dragon. Two dragon. Three dragon?, - the princess was counting. She had trouble falling asleep, and

Codeforces 148E Porcelain (预处理+多重背包)

E. Porcelain time limit per test:3 seconds memory limit per test:256 megabytes During her tantrums the princess usually smashes some collectable porcelain. Every furious shriek is accompanied with one item smashed. The collection of porcelain is arra

CodeForces 215B Olympic Medal(数学啊)

题目链接:http://codeforces.com/problemset/problem/215/B Description The World Programming Olympics Medal is a metal disk, consisting of two parts: the first part is a ring with outer radius of r1 cm, inner radius of r2 cm, (0?<?r2?<?r1) made of metal wi