"Training log 15" (8.10)

T1 Blue

  That is not simple, but there are still many huge guys out $ A $.

  For the farthest place each clam, should jump to it can reach, clams are the same, if a clam to jump the farthest, that there may be another clam will not jump over him so close to the location . At first assumed it would only clam jump closer position, the other clam will jump to more distant locations, this time two clam swapped places with each other, but rather the effect equivalent.

  Greedy greedy back from the front.

prove:

If the rightmost clams do not go the farthest stone jump, but chose a close of stones , so inevitably there will be a left of the clam clam crossed it jumped to its right side . Because each ability clams are the same, we can exchange route so that the greedy strategy does not deteriorate.

Followed by induction can prove to all the clams optimal strategy.

 

  Then on the simple, $ set $ simulations also can queue can be, that an element represents the queue is the location where the clams effective .

  Note that the analog set $ $ 0 is to be inserted and set the maximum L, to ensure the safety of the STL space (e.g., $ - s.begin () $).

  $ $ Lower_bound the STL is greater than or equal its elements, $ - $ after less than, greater than $ $ upper_bound, its elements, $ - $ is then less.

  Brother Fucai.

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<set>
 4 #define HZOI std;
 5 using namespace HZOI;
 6 int T,n,m,d,L,ans;
 7 set<int> s;
 8 inline int read();
 9 int main()
10 {
11 //    freopen("blue.in","r",stdin);
12 //    freopen("3.out","w",stdout);
13     T=read();
14 //    double t1=clock();
15     while (T--)
16     {
17         ans=0;
18         s.clear();
19         n=read(),m=read(),d=read(),L=read();
20         for (int i=1; i<=n; ++i)
21             s.insert(read());
22         s.insert(0),s.insert(L);
23         if (d==L)
24         {
25             puts("Excited");
26             continue;
27         }
28         if (n==L-1)
29         {
30             if (d>=m) puts("Excited");
31             else printf("%d\n",d);
32             continue;
33         }
34         for (int i=1; i<=m; ++i)
35         {
36             int pos=0,flag=1;
37             int to;
38 //            printf("i=%d=======\n",i);
39             do
40             {
41                 to=*--s.upper_bound(pos+d);
42 //                printf("pos=%d to=%d\n",pos,to);
43                 if (to<=pos or to-pos>d) {flag=0;break;}
44                 if (to>=L) {++ans;break;}
45                 else pos=to,s.erase(to);
46             }while (pos);
47             if (!flag) break;
48         }
49         if (ans==m) puts("Excited");
50         else printf("%d\n",ans);
51     }
52 //    double t2=clock();
53 //    printf("%.9lf\n",t2-t1);
54 }
55 inline int read()
56 {
57     int nn=0; char cc=getchar();
58     while (cc<'0' or cc>'9') cc=getchar();
59     while (cc>='0' and cc<='9') nn=(nn<<3)+(nn<<1)+(cc^48),cc=getchar();
60     return nn;
61 }
Blue(set)
 1 #include<cstdio>
 2 #define HZOI std;
 3 using namespace HZOI;
 4 const int N=1e6+3;
 5 int T,n,m,d,L,l,r;
 6 int a[N],q[N<<1];
 7 inline int read();
 8 int main()
 9 {
10     T=read();
11     while (T--)
12     {
13         n=read(),m=read(),d=read(),L=read();
14         for (int i=1; i<=n; ++i)
15             a[i]=read();
16         l=r=0;
17         for (int i=1; i<=m; ++i) q[++r]=0;
18         for (int i=1; i<=n; ++i)
19         {
20             while ((l^r) and q[l+1]+d<a[i]) ++l;
21             ++l,q[++r]=a[i];
22         }
23         while ((l^r) and q[l+1]+d<L) ++l;
24         if (r-l==m) puts("Excited");
25         else printf("%d\n",r-l);
26     }
27 }
28 inline int read()
29 {
30     int nn=0; char cc=getchar();
31     while (cc<'0' or cc>'9') cc=getchar();
32     while (cc>='0' and cc<='9') nn=(nn<<3)+(nn<<1)+(cc^48),cc=getchar();
33     return nn;
34 }
Blue (queue)

T2 Weed

   Tree line, tree line maintenance test of understanding of the information.

  I was under the title of decoding seniors, seniors very well written, very clear.

  If this solution to a problem in the online search, there will be "This is not a board problem it" a mockery.

  In particular, I would like to cite seniors courseware:

• In fact, the operation of each addend and deleted can be seen as the stack and popped operations, then you can maintain the relationship between the use of multiple operating segment tree. Subscript segment tree is the operating time, since we want the entire sequence of the modified results after operation, thus maintaining the tree four information segments:
S: addend summation interval (considering the influence only the internal section);
Nd: current section to before deleting the number of digits;
Na: within the current range "net" increase in the number of elements.
sd: the sum of the current section is deleted right sibling. ※ This information is maintained only son left.
• We maintain more than four marked, you can get an answer (root-> s). Now consider how we maintain the information.

First, at the time of contribution or modified, the first three marks only need to maintain good leaf nodes can; when a recursive return, and then calculate the last one. If not enough left his son the right to delete his son, so very simple, direct use of these markers can be calculated, l-> sd = 0.
The only problem was the situation left son is deleted light. Let implement a function cal (x), is calculated using the sd deleting the marker elements and x in the current interval.

Consider implementing cal (x) of (the current node is O):
• 1. If r-> na> x, then return l-> sd + r-> cal ( x).
• 2. If r-> na <x, then the right son is not deleted, the return of L-> CAL (XR-> Na + R-> Nd)
• ※ very important here, must again delete the right to delete sons of
• 3. If r-> na == x, return directly l-> sd.
With this function, we can easily calculate left his son sd, maintain other marks, and not repeat them.

• strange because each node will call function, and a total of O (logn) layer node, the segment tree operation time complexity becomes O (log 2 n), the total time complexity than the conventional segment tree more than a log.
• The main difficulty lies in the use of additional functions maintenance information, and the right to discuss a variety of situations.
• The examination must make it clear, time complexity is a function of maintaining protection against the "chain" of the extension, if not carefully written down on both sides, it becomes O (n 2) a.

  Good move those slip.

  Let's analyze this problem solution.

  Mainly it is to achieve understanding and $ cal $ function information.

  $ Na $ represent "net" increase in demand out of the net increase in the current interval is affected by their own internal derived out of range, $ nd $ means "clean" reduce, find out the current range is the net affect their internal interval reduce, so, $ na $ role and may be after the interval, $ nd $ can act before the interval, which is their relationship, when thus merge information is not hard to incorporate, to a good range itself, interval relationship with the interval.

  $s$表示总数,$sd$表示左儿子受到右儿子影响后得到的总数,所以s只考虑内部贡献,而$sd$考虑的是一区间对另一区间的贡献。

  主要难点在于区间对区间的贡献影响是相对于区间而言的

  $cal$函数的具体实现方式,师兄的博客中也已经很清楚了,不再多说。

  $cal$函数的实现很巧妙,有效的作出了线段树本身没有的操作。

  具体实现代码中展示。

  小弟不才。

 1 #include<cstdio>
 2 #define HZOI std;
 3 using namespace HZOI;
 4 const int N=2e6+3;
 5 struct Tree{
 6     int l,r,na,nd,sm,smd;
 7 }tr[N<<2];
 8 int m,Q,t;
 9 void Build(int ,int ,int );
10 void Change(int ,int ,int ,int );
11 int Cal(int ,int );
12 inline int read();
13 int main()
14 {
15     m=read(),Q=read();
16     Build (1,1,m);
17     for (int i=1,x,y; i<=m; ++i)
18         x=read(),y=read(),Change(1,i,x,y);
19     for (int i=1,x,y,z; i<=Q; ++i)
20     {
21         x=read(),y=read(),z=read();
22         Change(1,x,y,z);
23         printf("%d\n",tr[1].sm);
24     }
25     return 0;
26 }
27 int Cal(int k,int x)
28 {
29     if (tr[k].l==tr[k].r) return x>=tr[k].na?0:tr[k].sm;
30     if (tr[k<<1|1].na==x) return tr[k<<1].smd;
31     else if (tr[k<<1|1].na<x) return Cal(k<<1,x-tr[k<<1|1].na+tr[k<<1|1].nd);
32     else if (tr[k<<1|1].na>x) return Cal(k<<1|1,x)+tr[k<<1].smd;
33 }
34 void Change(int k,int pos,int opt,int weight)
35 {
36     if (tr[k].l==tr[k].r)
37     {
38         if (opt) tr[k].nd=weight,tr[k].sm=tr[k].na=0;
39         else tr[k].na=1,tr[k].sm=weight,tr[k].nd=0;
40         return ;
41     }
42     int mid=tr[k].l+tr[k].r>>1;
43     if (pos<=mid) Change(k<<1,pos,opt,weight);
44     else Change(k<<1|1,pos,opt,weight);
45     tr[k].nd=tr[k<<1].nd+((tr[k<<1|1].nd-tr[k<<1].na)>0?(tr[k<<1|1].nd-tr[k<<1].na):0);
46     tr[k].na=tr[k<<1|1].na+((tr[k<<1].na-tr[k<<1|1].nd)>0?(tr[k<<1].na-tr[k<<1|1].nd):0);
47     if (tr[k<<1].l==tr[k<<1].r)
48     {
49         if (tr[k<<1|1].nd>=1) tr[k<<1].smd=0;
50         else tr[k<<1].smd=tr[k<<1].sm;
51     }
52     else
53     {
54         if (tr[k<<1|1].nd>=tr[k<<1].na) tr[k<<1].smd=0;
55         else tr[k<<1].smd=Cal(k<<1,tr[k<<1|1].nd);
56     }
57     tr[k].sm=tr[k<<1].smd+tr[k<<1|1].sm;
58 }
59 void Build(int k,int l,int r)
60 {
61     tr[k].l=l,tr[k].r=r;
62     if (l==r) return ;
63     int mid=l+r>>1;
64     Build(k<<1,l,mid);
65     Build (k<<1|1,mid+1,r);
66 }
67 inline int read()
68 {
69     int nn=0; char cc=getchar();
70     while (cc<'0' or cc>'9') cc=getchar();
71     while (cc>='0' and cc<='9') nn=(nn<<3)+(nn<<1)+(cc^48),cc=getchar();
72     return nn;
73 }
Weed

 

T3 Drink

  暴力可过,不明原因,循环展开。

  小弟不才。

 

 1 #include<cstdio>
 2 #define HZOI std;
 3 using namespace HZOI;
 4 int n,m,Q,g,h;
 5 char a[2003][2003],cc1,cc2,cc3,cc4;
 6 int main()
 7 {
 8     scanf("%d%d%d",&n,&m,&Q);
 9     for (int i=1; i<=n; ++i)
10         for (int j=1; j<=m; ++j)
11         {
12             a[i][j]=getchar();
13             while (a[i][j]<'0' or a[i][j]>'9') a[i][j]=getchar();
14         }
15     for (int q=1,x,y,z; q<=Q; ++q)
16     {
17         scanf("%d%d%d",&x,&y,&z);
18         for (int k=z; k>0; k-=2)
19         {
20             for (int i=0; i<k-1; ++i)
21             {
22                 int tx1=x,ty1=y+i;
23                 int tx2=x+i,ty2=y+k-1;
24                 int tx3=x+k-1,ty3=y+k-1-i;
25                 int tx4=x+k-1-i,ty4=y;
26                 cc1=a[tx1][ty1];
27                 cc2=a[tx2][ty2];
28                 cc3=a[tx3][ty3];
29                 cc4=a[tx4][ty4];
30                 a[tx4][ty4]=cc3;
31                 a[tx3][ty3]=cc2;
32                 a[tx2][ty2]=cc1;
33                 a[tx1][ty1]=cc4;
34             }
35             ++x,++y;
36         }
37     }
38     for (int i=1; i<=n; ++i)
39     {
40         for (int j=1; j<=m; ++j)
41             putchar(a[i][j]),printf(" ");
42         puts("");
43     }
44     return 0;
45 }
View Code

 

 

 

 

Guess you like

Origin www.cnblogs.com/LH-Xuanluo/p/11333780.html