9.3 solution to a problem

T1

Everyone on the test have thought of violent search, and violent search may be over, but why there are still more than half of people $ WA0 $ of it, because it questions the details of the majority, on the test I pulled into the fast 9:00, just after a large sample the results still can not escape the fate of $ WA0 $, I too hard

In fact, no idea on what you enumerate the length of the loop section, the rear position on the digital back, see if you can legally on the line, I remember mad debugging

  1 #include<algorithm>
  2 #include<iostream>
  3 #include<cstring>
  4 #include<cstdio>
  5 #include<vector>
  6 #define inf 2139062143
  7 #define maxm 10010
  8 #define maxp 100100
  9 using namespace std;
 10 struct node{
 11     int p,x;
 12 }a[maxm];
 13 int t,c,m,js,tail,len,pd;
 14 int b[maxp],lon[5],ma[5],st[5],mi[5],stt[5];
 15 vector <int> chan;
 16 bool cmp(const node &a,const node &b)
 17 {
 18     return a.p<b.p;
 19 }
 20 int main()
 21 {
 22 //    freopen("1.in","r",stdin);
 23 //    freopen("W.out","w",stdout);
 24     scanf("%d%d",&t,&c);
 25     while(t--)
 26     {
 27         scanf("%d",&m);  js=0;  len=c;  pd=0;
 28         memset(b,-1,sizeof(b));  memset(lon,0,sizeof(lon));
 29         memset(ma,-1,sizeof(ma));  memset(st,-1,sizeof(st));
 30         memset(mi,0x7f,sizeof(mi));  memset(stt,0x7f,sizeof(stt));
 31         for(int i=1;i<=m;++i)
 32         {
 33             int pp,xx;  scanf("%d%d",&pp,&xx);
 34             if(b[pp-1]==-1)  {b[pp-1]=xx;  a[++js].p=pp-1;  a[js].x=xx;}
 35         }
 36         if(c==1)  {printf("1\n");  continue;}
 37         sort(a+1,a+js+1,cmp);  tail=a[js].p+c;
 38         for(int i=1;i<=js;++i)
 39             if(a[i].x<a[i-1].x)  {tail=a[i].p;  break;}
 40         for(int i=0;i<len;++i)
 41             if(b[i]!=-1)  {st[b[i]]=i;  stt[b[i]]=min(stt[b[i]],i);}
 42         while(len<=tail)
 43         {
 44             if(b[len-1]!=-1)
 45                 {st[b[len-1]]=len-1;  stt[b[len-1]]=min(stt[b[len-1]],len-1);}
 46             for(int i=0;i<c;++i)  {mi[i]=stt[i];  ma[i]=st[i];}
 47             mi[0]=0;  mi[c-1]=min(len-1,mi[c-1]);  ma[c-1]=len-1;  chan.clear();
 48             int flag=0;
 49             for(int i=1;i<=js;++i)
 50             {
 51                 int ls=a[i].p%len;
 52                 if(b[ls]!=a[i].x)
 53                 {
 54                     if(b[ls]==-1)
 55                     {
 56                         if(a[i].x!=0&&ls<=ma[a[i].x-1]&&ma[a[i].x-1]!=-1)
 57                             {flag=1;  break;}
 58                         if(ls>mi[a[i].x+1])  {flag=1;  break;}
 59                         b[ls]=a[i].x;
 60                         chan.push_back(ls);
 61                         ma[a[i].x]=max(ma[a[i].x],ls);
 62                         mi[a[i].x]=min(mi[a[i].x],ls);
 63                     }
 64                     else  {flag=1;  break;}
 65                 }
 66             }
 67             if(flag)
 68             {
 69                 for(int i=0;i<chan.size();++i)  b[chan[i]]=-1;
 70                 len++;  continue;
 71             }
 72             else
 73             {
 74                 for(int i=0;i<c;++i)
 75                 {
 76                     if(ma[i]==-1)
 77                     {
 78                         if(i==0)  {ma[i]=0;  mi[i]=0;}
 79                         else
 80                         {
 81                             if(ma[i-1]+1!=mi[i+1])  {ma[i]=ma[i-1]+1;  mi[i]=ma[i];}
 82                             else  {flag=1;  break;}
 83                         }
 84                     }
 85                     if(mi[i]-ma[i-1]<=0&&i!=0)  {flag=1;  break;}
 86                     if(mi[i+1]-ma[i]==0&&i!=c-1)  {flag=1;  break;}
 87                 }
 88                 if(flag)
 89                 {
 90                     for(int i=0;i<chan.size();++i)  b[chan[i]]=-1;
 91                     len++;  continue;
 92                 }
 93                 lon[0]=ma[0]+1;
 94                 for(int i=1;i<c;++i)  lon[i]=ma[i]-ma[i-1];
 95                 pd=1;  break;
 96             }
 97         }
 98         if(!pd)  {printf("NO\n");  continue;}
 99         for(int i=0;i<c;++i)  printf("%d ",lon[i]);
100         puts("");
101     }
102     return 0;
103 }
Debugging hack out the hard-won countless AC code Code

T2

Is a very zz interval dp, I did not want to come out in the examination room, the storm also tune the search for a long time

If dp transfer, we set $ dp [i] [j] $ represents the $ i $ to $ j $ cake the size of the interval up to get. We consider how a $ dp [i] [j] $ spread to other states

First into the ring for this problem, it is a classic approach is to be doubled on the sequence becomes a problem for the transfer, we divided into two cases

1. Takuji Jun cake to take, that is, $ i-1 $ and $ j + 1 $ can be transferred to, can not $ 2N $ sequence

2. Rain Misaki (Xiao four tones) to get sauce cake, to ensure $ i-1 $ and $ j + 1 $ the lawful, selected from a large transfer it

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #define int long long
 5 #define maxn 3010
 6 using namespace std;
 7 int n,ans;
 8 int a[maxn*2];
 9 int dp[maxn*2][maxn*2];
10 main()
11 {
12     scanf("%lld",&n);
13     for(int i=1;i<=n;++i)  scanf("%lld",&a[i]);
14     a[0]=a[n];
15     for(int i=1;i<=n;++i)  a[i+n]=a[i];
16     for(int i=1;i<=2*n;++i)  dp[1][i]=a[i];
17     for(int i=1;i<n;++i)//上一个区间长度
18     {
19         for(int j=1;j<=2*n;++j)// a point on the left end section 
20 is          {
 21 is              int K = J-I + . 1 ; // a point on the right end section 
22 is              int L = J, R & lt = K;
 23 is              IF (R & lt> 2 * n-)   Continue ;
 24              int LS1 L-= . 1 , = R & lt LS2 + . 1 ;
 25              IF (% I 2 ) // one interval is an odd number, not to calculate the contribution of candidate 
26 is              {
 27                  IF (LS1 < 0 || LS2> 2 * n-)   Continue ;
 28                  int = POS 0;
 29                  IF (A [LS1]> A [LS2]) POS = LS1;
 30                  the else   POS = LS2;
 31 is                  IF (POS> = L && POS <= R & lt)   Continue ;
 32                  IF (POS < 0 || POS> 2 * n- )   Continue ;
 33 is                  int LS = min (POS, L);
 34 is                  DP [I + . 1 ] [LS] = max (DP [I + . 1 ] [LS], DP [I] [L]);
 35              }
 36              the else // a is an even interval, calculate the contribution candidate 
37 [              {
 38 is                  IF (LS1 <L || LS1> R & lt)
39                 {
40                     if(ls1<0||ls1>2*n)  continue;
41                     int ls=min(ls1,l);
42                     dp[i+1][ls]=max(dp[i+1][ls],dp[i][l]+a[ls1]);
43                 }
44                 if(ls2<l||ls2>r)
45                 {
46                     if(ls2<0||ls2>2*n)  continue;
47                     int ls=min(ls2,l);
48                     dp[i+1][ls]=max(dp[i+1][ls],dp[i][l]+a[ls2]);
49                 }
50             }
51         }
52     }
53     for(int i=1;i<=2*n;++i)  ans=max(ans,dp[n][i]);
54     printf("%lld\n",ans);
55     return 0;
56 }
View Code

T3

Did not understand the question honestly say that when opened, to be honest my last copy of the solution to a problem, that problem can also be explained, it posted a solution to a problem, put the code ran away , I hurry to the water problem

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdio>
  4 #include<cmath>
  5 #include<queue>
  6 #define int long long
  7 #define bh(i,j)  ((i-1)*y+j)
  8 #define maxn 6001000
  9 #define maxm 6001000
 10 using namespace std;
 11 struct node{
 12     int pos,len;
 13     bool operator <(const node &a)const
 14     {
 15         return len>a.len;
 16     }
 17 };
 18 struct NODE{
 19     int h,z;
 20 };
 21 int x,y,a,b,c,n,ans,js,tot;
 22 int xx[maxn],yy[maxn];
 23 int head[maxn],to[maxm],xia[maxm],w[maxm];
 24 int pd[maxn],dis[maxn];
 25 priority_queue <node> s;
 26 queue <NODE> dl;
 27 void add(int x,int y,int z)
 28 {
 29     to[++js]=y;  xia[js]=head[x];  w[js]=z;  head[x]=js;
 30 }
 31 main()
 32 {
 33     scanf("%lld%lld%lld%lld%lld%lld",&x,&y,&a,&b,&c,&n);  x++;  y++;  tot=x*y;
 34     for(int i=1;i<=n;++i)  {scanf("%lld%lld",&xx[i],&yy[i]);  xx[i]++;  yy[i]++;}
 35     memset(dis,0x7f,sizeof(dis));
 36     for(int i=1;i<=n;++i)
 37     {
 38         int ls=bh(xx[i],yy[i]);  dis[ls]=0;
 39         dl.push((NODE){xx[i],yy[i]});
 40     }
 41     while(dl.size())
 42     {
 43         NODE ls=dl.front();  dl.pop();
 44         int ls1=ls.h,ls2=ls.z;
 45         if(ls2-1>=1&&dis[bh(ls1,ls2-1)]>dis[bh(ls1,ls2)]+1)
 46         {
 47             dis[bh(ls1,ls2-1)]=dis[bh(ls1,ls2)]+1;
 48             dl.push((NODE){ls1,ls2-1});
 49         }
 50         if(ls2+1<=y&&dis[bh(ls1,ls2+1)]>dis[bh(ls1,ls2)]+1)
 51         {
 52             dis[bh(ls1,ls2+1)]=dis[bh(ls1,ls2)]+1;
 53             dl.push((NODE){ls1,ls2+1});
 54         }
 55         if(ls1-1>=1&&dis[bh(ls1-1,ls2)]>dis[bh(ls1,ls2)]+1)
 56         {
 57              Dis [was (ls1- 1 , ls2)] = Dis [was (ls1, ls2)] + 1 ;
58              dl.push ((NODE) {ls1- 1 , ls2});
59          }
 60          IF (ls1 + 1 <= x && Dis [was (ls1 + 1 , ls2)]> Dis [was (ls1, ls2)] + 1 )
 61          {
 62              Dis [was (ls1 + 1 , ls2)] = Dis [was ( ls1, ls2)] + 1 ;
63              dl.push ((NODE) {ls1 + 1 , ls2});
64          }
 65      }
 66      for (int i=1;i<=x;++i)
 67     {
 68         for(int j=1;j<=y;++j)
 69         {
 70             int ls1=bh(i,j),ls2=bh(i,j)+tot,ls3=bh(i,j)+2*tot;
 71             if(i-1>=1)  add(ls1,bh(i-1,j),a);
 72             if(i+1<=x)  add(ls1,bh(i+1,j),a);
 73             if(j-1>=1)  add(ls2,bh(i,j-1)+tot,a);
 74             if(j+1<=y)  add(ls2,bh(i,j+1)+tot,a);
 75             add(ls3,ls1,b);  add(ls3,ls2,b);
 76             add(ls1,ls3,dis[ls1]*c);  add(ls2,ls3,dis[ls1]*c);
 77             if(i-1>=1)  add(ls3,bh(i-1,j)+2*tot,c);
 78             if(i+1<=x)  add(ls3,bh(i+1,j)+2*tot,c);
 79             if(j-1>=1)  add(ls3,bh(i,j-1)+2*tot,c);
 80             if(j+1<=y)  add(ls3,bh(i,j+1)+2*tot,c);
 81         }
 82     }
 83     memset(dis,0x7f,sizeof(dis));
 84     int qd=bh(xx[1],yy[1])+2*tot;
 85     dis[qd]=0;  s.push((node){qd,dis[qd]});
 86     while(s.size())
 87     {
 88         int qd=s.top().pos;  s.pop();
 89         if(pd[qd])  continue;
 90         pd[qd]=1;
 91         for(int j=head[qd];j;j=xia[j])
 92         {
 93             int ls=to[j];
 94             if(dis[ls]>dis[qd]+w[j]&&(!pd[ls]))
 95             {
 96                 dis[ls]=dis[qd]+w[j];
 97                 s.push((node){ls,dis[ls]});
 98             }
 99         }
100     }
101     int= was źd (xx [n], xyy [n]); military = min (min (Dis [źd], Dis [źd tot +]), Dis [źd + 2 * tot]);
102      printf ( " % LLD \ n " , military);
103      return  0 ;
104 }
A slight edge to build a little bit of nausea

This question thanks, take me out of the wrong stack optimized dj lira board

Guess you like

Origin www.cnblogs.com/hzjuruo/p/11518799.html