test20190814 NOIP2019 simulation title

Binary Tree

[Problem Description]
There was once a binary tree, we use the following ways to represent binary tree.

  1. If a node has no son, we use "0" to represent him.
  2. If a node has a son, and we find it represented the beginning of a "1", followed by the representation of its sons.
  3. If a node has two sons, and we find it represented the beginning of the "2", he left behind to take his son said, followed by the right son of its representation.

KJDH very fun, these trees dyed, KJDH and is very clever, it stained and very rule: each node and its children can not have the same color, if a node has two children, then the two children can not have the same color. Because of this age-old tree, so we could not see the color of each node, but we only know KJDH stained with red, yellow or white colors. We want to know this tree the most and least how many nodes are white.

[Input format
into a file called tree.in. Input file only one line, a character string, only "0", "1", "2" form, that the tree structure.

[] Output format
output file name tree.out. The output file contains two numbers separated by a space, and represent the most minimal number of white nodes.

Sample 1 [O]
tree.in
200 is
tree.out
1 1

Sample 2 [O]
tree.in
1122002010
tree.out
. 5 2

[Agreed with the data size]
for 20% of the data, len <= 10.
For 50% of the data, len <= 2000
to 100% of the data, len <= 500000. Wherein len is the length of the string read.

answer

I would like for a moment how achievements. Stack analog sequences can bracket.

Then is the wisdom tree DP.

co int N=500000+10;
char str[N];
struct node{int id,val;};
int len,lc[N],rc[N],n;
int main(){
    freopen("tree.in","r",stdin),freopen("tree.out","w",stdout);
    scanf("%s",str+1),len=strlen(str+1);
    stack<node> st;
    for(int i=1;i<=len;++i){
        st.push((node){++n,str[i]-'0'});
        while(st.top().val==0){
            int x=st.top().id;st.pop();
            if(x==1) break;
            int fa=st.top().id;
//          cerr<<"link "<<fa<<" "<<x<<endl;
            lc[fa]?rc[fa]=x:lc[fa]=x;
            --st.top().val;
        }
    }
    dfs(1);
    printf("%d %d\n",max(f[1][0],max(f[1][1],f[1][2])),min(g[1][0],min(g[1][1],g[1][2])));
    return 0;
}

dancing

[Problem Description]
KJDH there are n sister, numbered from 1 to n, each sister will be dancing, dancing sister of the i-th Charisma is A i , one day KJDH holding a cup in the IOI arena, he's the n sister wanted to celebrate, to dance for him, to jump a total of n * (n + 1) / 2 dance, respectively, by numbered i ~ j sister dancing (1 <= i <= j <= n).

Each hop dance KJDH will be very pleased to increase the value of pleasure, numbered i ~ j sister dancing pleasure can increase the value of
(J-i + 1) Min (A i , A i + 1 , ..., A J ) max (A I , A I +. 1 , ..., A J )

Q KJDH after the dance through n * (n + 1) / 2 dance, how much pleasure can increase the value, the answer to the modulo 1,000,000,007.

[Input format
into a file called dance.in.
2 input common line.
Line 1 contains a positive integer n, denotes the n sister.
Line 2 contains n space-separated positive integers A . 1 , A 2 , ..., A n . Representing each sister dancing Charisma.

[] Output format
output file name dance.out.
Total output line, comprising an integer indicating KJDH increase pleasure value.

Sample 1 [O]
dance.in
. 4
2 1. 4. 4
dance.out
109

[O] Sample 1 shows
a total jumped 6 dance. With (i, j) is the number of i ~ j sister dancing pleasure value increases.
(1,1) = 4 (1,2) = 16 (1,3) = 12 (1,4) = 16
(2,2) = 16 (2,3) 8 = (2,4) = 12
( 3,3) = 1 (3,4) = 8
(4,4) = 16
all add up to 109.

[Agreed with the data size]
for 60% of data, n <= 2000;
to 100% of the data, n-<= 500000,1 <A = I <= 10 . 8 .

ChiTongZ solution to a problem of

The \ (j-i + 1 \ ) detached from the answer out
\ [\ sum_ {j = 1 } ^ n \ sum_ {i = 1} ^ j (j-i + 1) \ max (i, j) \ min (i, j) \\ = \ sum_ {j = 1} ^ nj \ sum_ {i = 1} ^ j \ max (i, j) \ min (i, j) - \ sum_ {i = 1} ^ n (i-1) \ sum_
{j = i} ^ n \ max (i, j) \ min (i, j) \] The second weight may be monotonously summation sign + stack segment tree maintenance.

Monotonous stack to cover the range that case, the maintenance interval overlay marks, section min, max, and, with intervals min * max and the segment tree. I can find to do.

Time complexity \ (O (n-\ log n-) \) , the title human constant card.

#include<bits/stdc++.h>
#define co const
#define il inline
template<class T> T read(){
    T x=0,w=1;char c=getchar();
    for(;!isdigit(c);c=getchar())if(c=='-') w=-w;
    for(;isdigit(c);c=getchar()) x=x*10+c-'0';
    return x*w;
}
template<class T> T read(T&x){
    return x=read<T>();
}
using namespace std;
typedef long long LL;

co int mod=1000000000+7;
il int add(int a,int b){
    return (a+=b)>=mod?a-mod:a;
}
il int mul(int a,int b){
    return (LL)a*b%mod;
}

co int N=500000+10;
namespace T{
    int l[N<<2],r[N<<2];
    int tag[N<<2][2],sum[N<<2][2];
    int pro[N<<2];
    #define lc (x<<1)
    #define rc (x<<1|1)
    void build(int x,int l,int r){
        T::l[x]=l,T::r[x]=r;
        tag[x][0]=tag[x][1]=sum[x][0]=sum[x][1]=0;
        pro[x]=0;
        if(l==r) return;
        int mid=(l+r)>>1;
        build(lc,l,mid),build(rc,mid+1,r);
    }
    void set(int x,int k,int v){
        tag[x][k]=v,sum[x][k]=mul(r[x]-l[x]+1,v);
        pro[x]=mul(sum[x][k^1],v);
    }
    void push_down(int x){
        for(int k=0;k<2;++k)if(tag[x][k]){
            set(lc,k,tag[x][k]),set(rc,k,tag[x][k]);
            tag[x][k]=0;
        }
    }
    void push_up(int x){
        for(int k=0;k<2;++k)
            sum[x][k]=add(sum[lc][k],sum[rc][k]);
        pro[x]=add(pro[lc],pro[rc]);
    }
    void change(int x,int ql,int qr,int k,int v){
        if(ql<=l[x]&&r[x]<=qr)
            return set(x,k,v);
        push_down(x);
        int mid=(l[x]+r[x])>>1;
        if(ql<=mid) change(lc,ql,qr,k,v);
        if(qr>mid) change(rc,ql,qr,k,v);
        push_up(x);
    }
    int query(int x,int ql,int qr){
        if(ql<=l[x]&&r[x]<=qr)
            return pro[x];
        push_down(x);
        int mid=(l[x]+r[x])>>1;
        if(qr<=mid) return query(lc,ql,qr);
        if(ql>mid) return query(rc,ql,qr);
        return add(query(lc,ql,qr),query(rc,ql,qr));
    }
}
int n,a[N];
int s1[N],t1,s2[N],t2; // max,min

int main(){
    freopen("dance.in","r",stdin),freopen("dance.out","w",stdout);
    read(n);
    for(int i=1;i<=n;++i) read(a[i]);
    int ans=0;
    T::build(1,1,n);
    for(int i=1;i<=n;++i){
        while(t1&&a[s1[t1]]<=a[i]) --t1;
        T::change(1,t1?s1[t1]+1:1,i,1,a[i]);
        s1[++t1]=i;
        while(t2&&a[s2[t2]]>=a[i]) --t2;
        T::change(1,t2?s2[t2]+1:1,i,0,a[i]);
        s2[++t2]=i;
        ans=add(ans,mul(i,T::query(1,1,i)));
    }
    T::build(1,1,n),t1=t2=0;
    for(int i=n;i>=1;--i){
        while(t1&&a[s1[t1]]<=a[i]) --t1;
        T::change(1,i,t1?s1[t1]-1:n,1,a[i]);
        s1[++t1]=i;
        while(t2&&a[s2[t2]]>=a[i]) --t2;
        T::change(1,i,t2?s2[t2]-1:n,0,a[i]);
        s2[++t2]=i;
        ans=add(ans,mod-mul(i-1,T::query(1,i,n)));
    }
    printf("%d\n",ans);
    return 0;
}

Progression

[Problem Description]
we define the number of columns n- is the number of columns having the following properties.

  1. The length of the number of columns is not less than 3, and the number of columns in each element is an integer between 1 to n.
  2. When the number as A . 1 , A 2 , ......, A m , then for any 3 <= k <= m, satisfy
    (A K -a K-2 ) (A K-. 1 -a K-2 ) <0

You are given n, the number of seeking the number of n- columns. The answer to the modulo 1,000,000,007.

[Input format
into a file called seq.in.
Input common line, is n.

[] Output format
output file name seq.out. Output line indicates the number of columns n-

Sample 1 [O]
seq.in
. 3
seq.out
2

Sample 1 [O] described
two sequences are n- (2,1,3) and (2,3,1)

Sample 2 [O]
seq.in
666
seq.out
805 846 404

[Data size and conventions]
for 10% of the data, n <= 10
to 30% of the data, n <= 200
for the 50% of the data, n <= 2000
data for 70%, n <= 10 18 is
for 100% data,. 3 <= n-<10 = 5000

The solution to a problem or ChiTongZ

Assuming noted \ (A_ B_i = {I}. 1 -a_i + \) , then \ (| B_i | \) is a monotonically increasing, and \ (B_i \) alternately positive and negative.

Consider \ (a_i - i \) images, connect it to a line chart, then we have to do is to determine the \ (a_i \) fluctuation range, i.e., \ (\ max \ {| b_i | \} = | b_m | \) , and then find the number of solutions to this fluctuation range, this range is multiplied by the number of placement programs.

Consider enumeration \ (| B_M | \) , assume \ (b_m \) is positive, multiplying the last few programs \ (2 \) can be.
\ [\ Frac 12 ans = \
sum_ {i = 1} ^ {n-1} 2 ^ {i-1} (n-1-i + 1) \] Since \ (B \) strict nature, so \ (1 \ sim i-1 \) any one of methods are unique to appear a scheme.

But there is no guarantee \ (| \ {A_N \} | \ GE. 3 \) , i.e. \ (| \ {B_n \} | \ GE 2 \) , so to subtract \ (| \ {b_n \} | = 1 \) situation.
\ [\ Frac 12 ans = \ sum_ {i = 1} ^ {n-1} 2 ^ {i-1} (n-1-i + 1) - \ sum_ {i = 1} ^ {n-1} (n-1-i + 1 ) \]

Is then calculated by a conventional routine, the answer
\ [ans = 2 ^ {n + 1} -n ^ 2-n-2 \]

Time complexity \ (O (\ n-LG) \) .

#include<bits/stdc++.h>
#define co const
#define il inline
template<class T> T read(){
    T x=0,w=1;char c=getchar();
    for(;!isdigit(c);c=getchar())if(c=='-') w=-w;
    for(;isdigit(c);c=getchar()) x=x*10+c-'0';
    return x*w;
}
template<class T> T read(T&x){
    return x=read<T>();
}
using namespace std;
typedef long long LL;

co int mod=1000000000+7;
il int fpow(int a,int b){
    int ans=1;
    for(;b;b>>=1,a=(LL)a*a%mod)
        if(b&1) ans=(LL)ans*a%mod;
    return ans;
}

int main(){
    freopen("seq.in","r",stdin),freopen("seq.out","w",stdout);
    int m=0,n=0;
    for(char c=getchar();isdigit(c);c=getchar()){
        m=((LL)m*10+c-'0')%(mod-1);
        n=((LL)n*10+c-'0')%mod;
    }
    int ans=fpow(2,m+1)+mod-((LL)n*n+n+2)%mod;
    printf("%d\n",ans%mod);
    return 0;
}

Guess you like

Origin www.cnblogs.com/autoint/p/test20190814.html