Codeforces Round #618 E
题意:
给你一个n的数组,你可以进行无数次,选择区间使得区间内的值相加,然后区间的所有的值变成平均值。
使得最后数组的字典序最小
思路:
最后的数组一定是单调递增的,只要它比之前的平均值数大,就不会操作,如果比他小,需要进行操作到符合它的范围
用单调递减栈进行操作
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define il inline
#define it register int
#define inf 0x3f3f3f3f
#define lowbit(x) (x)&(-x)
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
const int maxn=1e6+;
int n,m,t;
double a[maxn],ans[maxn];
int l[maxn],len[maxn];
int main(){
scanf("%d",&n);
for(it i=;i<=n;i++){
scanf("%lf",&a[i]);len[i]=;
}
it top=;l[]=;
for(it i=;i<=n;i++){
double now=a[i],sum=a[i];
while(top>= && a[l[top]]>now){
len[i]+=len[l[top]];
sum+=a[l[top]]*len[l[top]];
now=sum/len[i];
top--;
}
a[i]=sum/len[i];
l[++top]=i;
}
it l1=;
while(top>=){
for(it i=;i<=len[l[top]];i++){
ans[++l1]=a[l[top]];
}top--;
}
for(it i=l1;i;i--){
printf("%.10lf\n",ans[i]);
}
return ;
}
补一道,单调栈
题意:
求数组n,连续的k,从头到n-k,其中每一段中最大的数字是什么
样例
5 3
1 5 3 4 2
输出
5
5
4
思路
从前到后,存进去比sta.top大的值,和已经存进去并且在区间内的值
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define il inline
#define it register int
#define inf 0x3f3f3f3f
#define lowbit(x) (x)&(-x)
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 998244353
const int maxn=2e6+;
int n,k;
int a[maxn],sta[maxn],ans[maxn],pos[maxn];
int main() {
scanf("%d%d",&n,&k);
for(it i=;i<n;i++){
scanf("%d",&a[i]);
}
int top=,t=;sta[top]=a[];pos[top]=;
for(it i=;i<k-;i++){
while(top>= && (a[i]>sta[top] || i-k>=pos[top])){
top--;
}
sta[++top]=a[i];pos[top]=i;
}
for(it i=k-;i<n;i++){
while(top>= && (a[i]>sta[top] || i-k>=pos[top])){
top--;
}
sta[++top]=a[i];pos[top]=i;
it c=;while(i-k>=pos[c]){c++;}
printf("%d\n",sta[c]);
}
return ;
}
Codeforces Round #618 E的更多相关文章
- Codeforces Round #618 (Div. 2)
		
题库链接 https://codeforces.ml/contest/1300 A. Non-zero 一个数组,每次操作可以给某个数加1,让这个数组的积和和不为0的最小操作数 显然如果有0的话,必须 ...
 - Codeforces Round #618 (Div. 1)C(贪心)
		
把所有数看作N块,后面的块比前面的块小的话就合并,这个过程可能会有很多次,因为后面合并后会把前面的块均摊地更小,可能会影响更前面地块,像是多米诺骨牌效应,从后向前推 #define HAVE_STRU ...
 - Codeforces Round #618 (Div. 1)B(几何,观察规律)
		
观察猜测这个图形是中心对称图形是则YES,否则NO #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace ...
 - Codeforces Round #618 (Div. 1)A(观察规律)
		
实际上函数值为x&(-y) 答案仅和第一个数字放谁有关 #define HAVE_STRUCT_TIMESPEC #include <bits/stdc++.h> using na ...
 - Codeforces Round #618 (Div. 2)A. Non-zero
		
Guy-Manuel and Thomas have an array aa of nn integers [a1,a2,…,an ]. In one step they can add 11 to ...
 - Codeforces Round #618 (Div. 2)C. Anu Has a Function
		
Anu has created her own function ff : f(x,y)=(x|y)−y where || denotes the bitwise OR operation. For ...
 - Codeforces Round #618 (Div. 2) 小号上紫之路
		
这一场涨了不少,题也比较偏思维,正好适合我 A. Non-zero 我们记录这些数字的总和sum,并且记录0的个数zero,显然答案应该是这些0的个数,注意如果sum+zero==0的话答案要额外加一 ...
 - [CF百场计划]#2 Codeforces Round #618 (Div. 2)
		
A. Non-zero Description: Guy-Manuel and Thomas have an array \(a\) of \(n\) integers [\(a_1, a_2, \d ...
 - Codeforces Round #618 (Div. 2)-B. Assigning to Classes
		
Reminder: the median of the array [a1,a2,-,a2k+1] of odd number of elements is defined as follows: l ...
 
随机推荐
- java注册界面及mysql连接
			
题目要求 完成注册界面及添加功能 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母.数字组成.(1 ...
 - C++——一维数组
			
6.数组 指针与字符串 6.1 数组 数组是具有一定顺序关系的若干相同类型变量的集合体,组成数组的变量成为数组的元素.数组属于构造类型. 一维数组的声明: 类型说明符 数组名[常量表达式],若int ...
 - Java中的IO、NIO、File、BIO、AIO详解
			
java中有几种类型的流?JDK为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类? Java中的流分为两种,一种是字节流,另一种是字符流,分别由四个抽象类来表示(每种流包 ...
 - Ubuntu, 更新Sourses.list
			
1.备份原文件 sudo cp /etc/apt/sources.list /etc/apt/sources_list.bak 2.加载文件 vim:vim sourses.list ubuntu d ...
 - codeforces 1269E K Integers (二分+树状数组)
			
链接:https://codeforces.com/contest/1269/problem/E 题意:给一个序列P1,P2,P3,P4....Pi,每次可以交换两个相邻的元素,执行最小次数的交换移动 ...
 - [lua]紫猫lua教程-命令宝典-L1-03-01. 闭包
			
L1[闭包]01. 函数的传递赋值 没什么说的 1.函数作为变量来看 可以轻松的声明 相互赋值 2.函数变量本质是 一个内存指针 所以函数变量的相互赋值不是传递的函数本身 而是指向这个函数的内存地址 ...
 - 求int型正整数在内存中存储时1的个数  &&  int型的数到底最大是多少?
			
输入一个int型的正整数(十位数之内!嘞!),计算出该int型数据在内存中存储时1的个数. #include<bits/stdc++.h> using namespace std; int ...
 - css之变形(transform)
			
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
 - Bugku-CTF分析篇-flag被盗(flag被盗,赶紧溯源!)
			
flag被盗 flag被盗,赶紧溯源!
 - 深度学习之tensorflow框架(中)
			
会话 开启会话 tf.Session用于完整的程序中 tf.InteractiveSession用于交互式上下文中的tensorflow 查看张量的值 都必须在会话里面 c_new_value=new ...