【CodeForce 1299C】Water Balance

Water Balance :

题意:

抽象版本:给出一段序列n,有一种操作:使得[L,R]内区间的所有数,变为区间的平均数。这个操作可以使用无数次,问最后得到最小字典序的序列是多少?

三组样例自行体会:

4
7 5 5 7
5.666666667
5.666666667
5.666666667
7.000000000


5
7 8 8 10 12
7.000000000
8.000000000
8.000000000
10.000000000
12.000000000


10
3 9 5 5 1 7 5 3 8 7
3.000000000
5.000000000
5.000000000
5.000000000
5.000000000
5.000000000
5.000000000
5.000000000
7.500000000
7.500000000

题目思路:

思维好题,首先证明一个结论。

如果区间S1=[x1,y1]的平均值为z1,区间S2=[x2,y2]的平均值为z2,如果z2<z1,那么如果将S2合并到S1中,S1的平均值会变小。

证:

Supposed\ the\ length\ of\ s1\ is\ N,\ and\ the\ length\ of\ s2\ is\ P\\ z1 = \frac{sum1}{N}\\ z2=\frac{sum2}{P}\\ z3=\frac{z1*N+z2*P}{P+N}\\

显然 :

1.z2 == z1 那么平均值不变

2.z2>z1 就会使得平均值变大

3.z2<z1合并后会使得平均值变小

所以根据这个性质,就可把这个题写掉了(2100分感觉2000分都在这个小学数学上)

遍历N个数字,因为要让前面的最小所以,判断新来数字是否可以将前面的变小,如果则融入到前面的序列中,继续判断新序列能否将再前面的平均值变小,依次进行——也就是单调栈思想了。

Code:

/*** keep hungry and calm CoolGuang!***/
#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pp;
const ll INF=1e18;
const int maxn=1e6+6;
const int mod=1e9+7;
const double eps=1e-9;
inline bool read(ll &num)
{char in;bool IsN=false;
in=getchar();if(in==EOF) return false;while(in!='-'&&(in<'0'||in>'9')) in=getchar();if(in=='-'){ IsN=true;num=0;}else num=in-'0';while(in=getchar(),in>='0'&&in<='9'){num*=10,num+=in-'0';}if(IsN) num=-num;return true;}
ll n,m;
struct node{
    int len;
    double w;
    double sum;
}st[maxn];
ll num[maxn];
int main()
{
    read(n);
    for(int i=1;i<=n;i++) read(num[i]);
    int s=0;
    for(int i=1;i<=n;i++){
        st[++s].w=num[i]*1.0;
        st[s].len=1;
        st[s].sum=num[i]*1.0;
        while(s>1&&st[s].w<st[s-1].w){
            st[s-1].sum+=st[s].sum;
            st[s-1].len+=st[s].len;
            st[s-1].w=st[s-1].sum/st[s-1].len*1.0;
            s--;
        }
    }
    for(int i=1;i<=s;i++){
        for(int k=1;k<=st[i].len;k++)
            printf("%.9f\n",st[i].w);
    }
    return 0;
}
/**
10 5
1 7
2 6
2 10
3 7
4 8
5 9
0 0
**/

这个题的思路确实强,以后多注意运用数学

还有 ,数论真的该好好补补了 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章