106 simulation solution to a problem

A. The combined collections

Obviously interval dp.

Into a chain-off ring, a pre-set number of elements in each successive interval.

Then directly dp would be finished.

 

 

 

B. climb

I want some simple greed, and all the pseudo.

So consider how violence $ O (n ^ 2) $ to do this problem.

Enumeration final jump to the last step of pills, apparently in front of the pills can be sorted by $ a_i-b_i $.

Then consider how to optimize this process, the question is how to determine the circumstances of drowning.

In case of drowning occurs only $ a_i-b_i $ prefix and C_i less $ and $ prefix.

However, for adding to delete individual items, many elements of the subscript will be shifted, then it is difficult to engage. In fact not many, I mentally handicapped

One way is to divide and conquer tree line, first press the $ a_i-b_i $ descending order for all of the pills.

$ Solve (l, r) $ represents a jump to last step pills $ (l, r) within the range of $, other states have been considered.

The advantage of this approach is that, when $ (l, r) $ determined to get rid of the pills within this range for each rank is determined.

Maintenance of two tree line, at index ranking,

Wherein the maintenance of a minimum of $ a_i-b_i-c_i $ prefix and second trees and maintain maximum $ a_i-b_i $ prefix.

Recursive $ Solve before (mid + 1, r) $, the $ [l, mid] Statistics contribution between $ prefix and, after the interval plus $ l-1 $ prefix and a value obtained within the range of violence.

Similarly before recursive $ solve (l, mid) $, the $ [mid + 1, r] $ between statistical contribution, within the scope of the obtained prefix and violence, updating $ [r + 1, n] $ between value of the element.

When recursion to $ l = r $, can now for the first-half of a segment tree will not determine the boundaries of drowning, and then determine the earliest possible time to jump out of the tree line in the second half of the tree, update the final answer just fine.

Although the partition sets tree line, but the complexity of this approach is that $ O (nlogn) $ a.

Because apparently only $ solve $ function call $ n $ times for each call:

Determining the segment tree log interval is $ $ complexity, as set by nature of the segment tree, then the leaf node is recursively violence associated with the length of the linear.

For the final $ l = r $, are two half-line segment to achieve the trees, so complexity is correct.

Hit $ 4K $ codes on the test, again had to beat.

At first there are two results in complexity $ O (nlog ^ 2) $ place, the final step by step change to the $ O (nlogn) $, fight really cool.

But I actually practice and there is no need, because the neighbor term changes will only change the value on an ordinary tree line can be achieved.

  1 #include<algorithm>
  2 #include<iostream>
  3 #include<cstdio>
  4 #define ll long long
  5 #define cri const register 
  6 #define lch p<<1
  7 #define rch p<<1|1
  8 using namespace std;
  9 const int N=1e5+7;
 10 const int inf=0x3f3f3f3f;
 11 struct YaoWan{
 12     int a,b;
 13 }med[N];
 14 intn-, ANS = INF;
 15  Long  Long Lim, pre [N], W [N];
 16  struct Node {
 . 17      LL Val [ 2 ], LZY [ 2 ]; // 0 represents the minimum value after reduction pre 
18 is } S [N << 2 ]; // . 1 represents the maximum value of the pre diminished 
. 19 inline void Down (CRI int P) {
 20 is      IF (S [P] .lzy [ 0 ]) {
 21 is          S [LCH] .val [ 0 ] = S + [P] .lzy [ 0 ];
 22 is          S [Rch] .val [ 0 ] = S + [P] .lzy [ 0 ];
 23         s[lch].lzy[0]+=s[p].lzy[0];
 24         s[rch].lzy[0]+=s[p].lzy[0];
 25         s[p].lzy[0]=0;
 26     }
 27     if(s[p].lzy[1]){
 28         s[lch].val[1]+=s[p].lzy[1];
 29         s[rch].val[1]+=s[p].lzy[1];
 30         s[lch].lzy[1]+=s[p].lzy[1];
 31         s[rch].lzy[1]+=s[p].lzy[1];
 32         s[p].lzy[1]=0;
 33     }
 34 }
 35 inline void up(cri int p){
 36     s[p].val[0]=min(s[lch].val[0],s[rch].val[0]);
 37     s[p].val[1]=max(s[lch].val[1],s[rch].val[1]);
 38 }
 39 void build(cri int p,cri int l,cri int r){
 40     if(l==r) return s[p].val[0]=-pre[l],void();
 41     int mid=l+r>>1;
 42     build(lch,l,mid);
 43     build(rch,mid+1,r);
 44     up(p);
 45 }
 46 void toadd(cri int p,cri int fl,cri int l,cri int r,cri int L,cri int R,cri int f){
 47     if(L==R) return s[p].val[0]+=w[L-l+fl]*f,s[p].val[1]+=w[L-l+fl]*f,void();
 48     down(p);
 49     int mid=L+R>>1;
 50     if(l<=mid) toadd(lch,fl,l,r,L,mid,f);
 51     if(r>mid) toadd(rch,fl,l,r,mid+1,R,f);
 52     up(p);
 53 }
 54 void modify(cri int p,cri int l,cri int r,cri ll val,cri int L,cri int R){
 55     if(L>=l&&R<=r) return s[p].lzy[0]+=val,s[p].lzy[1]+=val,s[p].val[0]+=val,s[p].val[1]+=val,void();
 56     down(p);
 57     int mid=L+R>>1;
 58     if(l<=mid) modify(lch,l,r,val,L,mid);
 59     if(r>mid) modify(rch,l,r,val,mid+1,R);
 60     up(p);
 61 }
 62 ll query(cri int p,cri int l,cri int r,cri int L,cri int R){
 63     if(L>=l&&R<=r) return s[p].val[1];
 64     down(p);
 65     int mid=L+R>>1;
 66     if(r<=mid) return query(lch,l,r,L,mid);
 67     if(l>mid) return query(rch,l,r,mid+1,R);
 68     return max(query(lch,l,r,L,mid),query(rch,l,r,mid+1,R));
 69 }
 70 int ask(cri int p,cri ll add,cri int L,cri int R){
 71     if(L==R) return L;
 72     down(p);
 73     int mid=L+R>>1;
 74     if(s[lch].val[1]+add>=lim) return ask(lch,add,L,mid);
 75     else return ask(rch,add,mid+1,R);
 76 }
 77 int find(cri int p,cri int L,cri int R){
 78     if(L==R) return L;
 79     down(p);
 80     int mid=L+R>>1;
 81     if(s[lch].val[0]<=0) return find(lch,L,mid);
 82     else return find(rch,mid+1,R);
 83 }
 84 void solve(cri int l,cri int r){
 85     ll val;
 86     if(l==r){
 87         if(l!=1&&l!=n){
 88             val=query(1,l-1,l-1,1,n-1);
 89             modify(1,l,n-1,val,1,n-1);
 90         }
 91         register int fr=n-1;
 92         if(s[1].val[0]<=0) fr=find(1,1,n-1)-1;
 93         if(fr&&query(1,1,fr,1,n-1)+med[l].a>=lim) ans=min(ans,ask(1,med[l].a,1,n-1)+1);
 94         if(l!=1&&l!=n) modify(1,l,n-1,-val,1,n-1);
 95         return ;
 96     }
 97     cri int mid=l+r>>1;
 98     w[mid]=0;
 99     for(register int i=mid+1;i<=r;++i) w[i]=w[i-1]+med[i].a-med[i].b;
100     toadd(1,mid+1,mid,r-1,1,n-1,1);
101     if(r!=n) modify(1,r,n-1,w[r],1,n-1);
102     solve(l,mid);
103     w[mid]=0;
104     for(register int i=mid+1;i<=r;++i) w[i]=w[i-1]+med[i].a-med[i].b;
105     toadd(1,mid+1,mid,r-1,1,n-1,-1);
106     if(r!=n) modify(1,r,n-1,-w[r],1,n-1);
107     
108     w[l-1]=0;
109     for(register int i=l;i<=mid;++i) w[i]=w[i-1]+med[i].a-med[i].b;
110     toadd(1,l,l,mid,1,n-1,1);
111     if(l!=1){
112         val=query(1,l-1,l-1,1,n-1);
113         modify(1,l,mid,val,1,n-1);
114     }
115     solve(mid+1,r);
116     w[l-1]=0;
117     for(register int i=l;i<=mid;++i) w[i]=w[i-1]+med[i].a-med[i].b;
118     toadd(1,l,l,mid,1,n-1,-1);
119     if(l!=1) modify(1,l,mid,-val,1,n-1);
120 }
121 inline bool cmp(const YaoWan &x,const YaoWan &y){
122     return x.a-x.b>y.a-y.b;
123 }
124 inline int read(register int x=0,register char ch=getchar()){
125     for(;!isdigit(ch);ch=getchar()) ;
126     for(; isdigit(ch);ch=getchar()) x=(x<<1)+(x<<3)+(ch^48);
127     return x;
128 }
129 int main(){
130     freopen("climb.in","r",stdin);
131     freopen("climb.out","w",stdout);
132     n=read(); scanf("%lld",&lim);
133     for(int i=1;i<=n;++i){
134         med[i].a=read(); med[i].b=read();
135         if(med[i].a>=lim) return printf("%d\n",1),0;
136     }
137     for(int i=1;i<=n;++i) pre[i]=pre[i-1]+read();
138     if(n==1) return puts("-1"),0;
139     sort(med+1,med+n+1,cmp);
140     build(1,1,n-1);
141     solve(1,n);
142     printf("%d\n",ans==inf?-1:ans);
143     return 0;
144 }
T2

 

 

 

C. coin

When there is a program, so that both can work together to achieve positive all the coins up, the answer is clearly no less than 2.

Otherwise, the answer is only related to parity + number of columns and rows.

So, the first sentence whether the situation can be transformed to face up all the coins:

For each coin,

If it is positive, then the corresponding column row flip flip, flip the corresponding column line does not flip.

If it is negative, then the corresponding column is not inverted row inversion, row inversion without inverting the corresponding column.

We found that this thing can be used to expand the domain disjoint-set simple maintenance,

If the final state there is a contradiction, so no solution, or at least a set of solutions exist.

After considering whether the upper hand to win.

For each set, if the set corresponds to an even number of flip, which corresponds to an even number of opposition collections flip, then you can obviously ignore this collection.

The remaining two cases:

The set corresponds to an odd number of inverted, which corresponds to the opposite set of an even number of inversion.

The set corresponds to an odd number of inverted, which corresponds to the opposite set of flip odd number.

Two cases are considered so long as, set $ dp (i, j) $ denotes the former state in which a $ I $, $ J latter state has a $.

Obviously $ dp (0,0) = 0 $, corresponding to the upper hand will be lost.

There Transfer

$ Dp (i, j) |! = Dp (i, j-1) $, is represented simply a collection of odd sides, which was converted to the even set useless.

$ Dp (i, j) |! = Dp (i-1, j) $, represents a plane select odd parity of the collection surface, which was converted to the even set useless.

$ Dp (i, j) |! = Dp (i-1, j + 1) $, even surface of a parity indicating the selection of the surface set, a set of so many odd.

Guess you like

Origin www.cnblogs.com/skyh/p/11824295.html
Recommended