Training log 7 (7.25)

T1 match

  Water problem.

  KMP bare title.

  hash perfect AC.

  We WA 0.

  The heart is very excited.

  Significant KMP, but the board would not back, so yy:

  

while (j<=la+lb+1)
{
	if (!i) nx[j++]=i++;
	else 
	{
		while (sa[i+1]!=sa[j] and i) i=nx[i];
		nx[j++]=i++;
	        if (i>la) i=nx[i];
	}
}

 

   Dog feces.

  The only advantage is to let me go over it again Hash and KMP.

  Note the removal of carriage returns and spaces when processing the last b. (WA 9 root causes)

  Brother Fucai.

T2 home

  Water problem.

  But I'm not going WA 10.

  Have not played a double point that he yy an undirected graph contraction point, the last point, it shrunk to cook .

  This magic is super super super wrong solution could have 70, and I added the words of.

  And then I found not changed, the start line and positive solution.

  I learned a double point, a good harvest.

  Point double + Dfs, AC, remember to go to Dali array of open , start and end points should be removed.

  Brother Fucai.

  

  1 #include<cstdio>
  2 #include<vector>
  3 #include<cmath>
  4 #include<cstring>
  5 #include<iostream>
  6 #include<algorithm>
  7 #define mm(a) memset(a,0,sizeof(a))
  8 #define HZOI using namespace std
  9 HZOI;
 10 const int MAXN=2e6+3;
 11 struct node{
 12     int v,next;
 13 }edge[MAXN<<2];
 14 int T,n,m;
 15 int tt,et,efirst[MAXN],first[MAXN],vv[MAXN<<2],nx[MAXN<<2];
 16 int cnt,tot,tail,dfn[MAXN],low[MAXN],be[MAXN],vis[MAXN],stack[MAXN];
 17 int iscut[MAXN],idcut[MAXN],oldid[MAXN];
 18 int ans[MAXN],nu;
 19 vector<int > vec[MAXN];
 20 inline int read();
 21 inline void Add(int ,int );
 22 inline void NewAdd(int ,int );
 23 void Tarjan(int ,int );
 24 bool Dfs(int );
 25 inline void Clear();
 26 int main()
 27 {
 28     T=read();
 29     while (T--)
 30     {
 31         Clear();
 32         n=read(); m=read();
 33         for (int i=1,x,y; i<=m; ++i)
 34         {
 35             x=read(); y=read();
 36             Add(x,y); Add(y,x);
 37         }
 38         Tarjan(1,1);
 39         if (!dfn[n]) {printf("0\n\n");continue;}
 40         int num=cnt;
 41         for (int i=1; i<=n; ++i)
 42             if (iscut[i])
 43                 idcut[i]=++num,oldid[num]=i;
 44         for (int i=1; i<=cnt; ++i)
 45             for (int j=0; j<vec[i].size(); ++j)
 46             {
 47                 int x=vec[i][j];
 48                 if (iscut[x])
 49                 {
 50                     NewAdd(i,idcut[x]);
 51                     NewAdd(idcut[x],i);
 52                     be[x]=idcut[x];
 53                 }
 54                 else be[x]=i;
 55             }
 56         memset(vis,0,sizeof(vis));
 57         if (be[1]==be[n]) {printf("0\n\n");continue;}
 58         Dfs(be[1]);
 59         sort(ans+1,ans+nu+1);
 60         printf("%d\n",nu);
 61         for (int i=1; i<=nu; ++i)
 62             printf("%d ",ans[i]);
 63         puts("");
 64     }
 65     return 0;
 66 }
 67 bool Dfs(int k)
 68 {
 69     if (be[n]==k) return 1;
 70     if (iscut[oldid[k]] and oldid[k]!=1) ans[++nu]=oldid[k];
 71     vis[k]=1;
 72     for (int i=efirst[k]; i; i=edge[i].next)
 73     {
 74         if (vis[edge[i].v]) continue;
 75         if (Dfs(edge[i].v)) return 1;
 76     }
 77     if (iscut[oldid[k]] and oldid[k]!=1) --nu;
 78     return 0;
 79 }
 80 void Tarjan(int k,int root)
 81 {
 82     dfn[k]=low[k]=++tot;
 83     stack[++tail]=k;
 84     if (k==root and !first[k]) {vec[++cnt].push_back(k); return ;}
 85     int flag=0;
 86     for (int i=first[k]; i; i=nx[i])
 87     {
 88         int ver=vv[i];
 89         if (!dfn[ver])
 90         {
 91             Tarjan(ver,root);
 92             low[k]=min(low[k],low[ver]);
 93             if (low[ver]>=dfn[k])
 94             {
 95                 ++flag;
 96                 if (k!=root or flag>1) iscut[k]=1;
 97                 ++cnt;
 98                 int to;
 99                 do
100                 {
101                     to=stack[tail--];
102                     vec[cnt].push_back(to);
103                 }while (to!=ver);
104                 vec[cnt].push_back(k);
105             }
106         }
107         else low[k]=min(low[k],dfn[ver]);
108     }
109 }
110 inline void Add(int u,int v)
111 {
112     vv[++tt]=v; nx[tt]=first[u]; first[u]=tt;
113 }
114 inline void NewAdd(int u,int v)
115 {
116     edge[++et].v=v; edge[et].next=efirst[u]; efirst[u]=et;
117 }
118 inline int read()
119 {
120     int cc=0; char ch=getchar();
121     while (ch<'0' or ch>'9') ch=getchar();
122     while (ch>='0' and ch<='9') cc=(cc<<3)+(cc<<1)+(ch^48),ch=getchar();
123     return cc;
124 }
125 inline void Clear()
126 {
127     mm(efirst); mm(first);
128     mm(dfn); mm(be);
129     mm(iscut); mm(idcut); mm(oldid);
130     for (int i=1; i<=cnt; ++i) vec[i].clear();
131     cnt=tot=tail=tt=nu=et=0;
132     return ;
133 }
Come back home

 

 

 

 

 

T3 Sushi

  Immortal title.

  The loop a sequence mess, end to end, so that he becomes a length 2N (conventional ring routine question).

 

 

  Then not miss, and I could see how this thing has monotonic ***? !

  As long as we think about a little, you will find a very obvious nature: there is a break in the sequence, make it the whole point of the left moves to the left, to the right of the whole move to the right.

  Then then there will be a more obvious nature: a breakpoint on the left must be equal to 0/1 0/1 right.

  O (n- 2 ): Enumeration violent break calculation.

  O (nlogn) (my limit): binary search breakpoint, then O (1) is calculated.

  O (n): This question has a magical said monotonic, we have a sequence as 0/1 string, so we toward 0 flanking sequences, as long as our mid (break) has been moved to the right, the phone number left 1 will be less and less, and 1 more and more to the right, which shows the number of steps 0 to the left to go less and less, so the break will continue to move to the right, which we will find this monotonicity, parallel linear O (n) complexity.

  Here are some of the solution to a problem of huge guys, keep as reference: mikufun , DeepInC .

  Brother Fucai.

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #define int long long
 5 #define mm(a) memset(a,0,sizeof(a))
 6 #define HZOI using namespace std
 7 HZOI;
 8 const int MAXLEN=3e6+3;
 9 int T;
10 int ch[MAXLEN];
11 int lb[MAXLEN],rb[MAXLEN],lr[MAXLEN],rr[MAXLEN],sl[MAXLEN],sr[MAXLEN];
12 int l,r,mid;
13 inline int min(int a,int b) {return a<b?a:b;}
14 inline int Cal(int ,int ,int );
15 signed main()
16 {
17     scanf("%lld",&T);
18     while (T--)
19     {
20         mm(lb); mm(rb); mm(lr); mm(rr); mm(sl); mm(sr);
21         int t=1;
22         ch[t]=getchar();
23         while (ch[t]!='B' and ch[t]!='R') ch[t]=getchar();
24         while (ch[t]=='B' or ch[t]=='R') ch[++t]=getchar();
25         --t;
26         for (int i=t+1; i<=t*2; ++i) ch[i]=ch[i-t];
27         t<<=1;
28         lb[0]=lr[0]=sl[0]=0;
29         for (int i=1; i<=t; ++i)
30         {
31             lb[i]=lb[i-1]; lr[i]=lr[i-1]; sl[i]=sl[i-1];
32             if (ch[i]=='R') ++lr[i];
33             else ++lb[i],sl[i]+=lr[i];
34         }
35         rb[t|1]=rr[t|1]=sr[t|1]=0;
36         for (int i=t; i>=1; --i)
37         {
38             rb[i]=rb[i+1]; rr[i]=rr[i+1]; sr[i]=sr[i+1];
39             if (ch[i]=='R') ++rr[i];
40             else ++rb[i],sr[i]+=rr[i];
41         }
42         t>>=1; mid=1;
43         int ans=0x7fffffffffffffff;
44         for (int i=1; i<=t; ++i)
45         {
46             l=i,r=i+t-1;
47             while(mid<i+t-1&&lb[mid]-lb[i-1]<=(lb[i+t-1]-lb[i-1])/2) mid++;
48 //            cout<<mid<<endl;
49             ans=min(ans,Cal(l,mid,r));
50 //            cout<<"ans="<<ans<<endl;
51         }
52         printf("%lld\n",ans);
53     }
54     return 0;
55 }
56 inline int Cal(int l,int mid,int r)
57 {
58     return sl[mid-1]-sl[l-1]-(lb[mid-1]-lb[l-1])*lr[l-1]+sr[mid]-sr[r+1]-(rb[mid]-rb[r+1])*rr[r+1];
59 }
sushi

 

 

  never give up.

 

Guess you like

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