NOIP simulation tests 14

Finished 19 of 14 write, I am also enough of goo.

14 good questions, but also fully expose my problem.

T1 is an analysis of the nature of the conclusions title push

For the interval [L, R], may wish to set up a [L]! = A [R], then the two endpoints of the answer does not contribute, that is, [L + 1, R], [L, R-1] are in line with interval meaning of the questions.

Two endpoints that is optimal intervals must be equal

Then the position and size of each number as end points of the interval, the statistics can answer.

 1 #include<bits/stdc++.h>
 2 #define F(n) for(int i=1;i<=n;i++)
 3 int a[500005],sum[500005],n,ans;
 4 std::vector< std::pair<int,int> >k[500005<<1];
 5 int main(){
 6     scanf("%d",&n);
 7     F(n)scanf("%d",&a[i]),sum[i]=sum[i-1]+(a[i]==i);ans=sum[n];
 8     F(n)k[a[i]+i].push_back(std::make_pair(std::min(i,a[i]),std::max(i,a[i])));
 9     F((n<<1))sort(k[i].begin(),k[i].end());
10     F((n<<1))
11         for(int t=k[i].size(),j=t-1,last=0;j>=0;j--,last++)
12             ans=std::max(ans,last+1+sum[k[i][j].first-1]+sum[n]-sum[k[i][j].second]);
13     std::cout<<ans;
14 }
View Code

T2 go plaid

The key is how to abstract topic to graph theory, rather than just blindly search.

This question is not too many restrictions, only moved to a different distance of each grid.

Therefore, direct construction side, it can run dijstra

 1 #include<bits/stdc++.h>
 2 #define MAXN 505
 3 using namespace std;
 4 const int h[5]={0,1,-1,0,0},l[5]={0,0,0,1,-1};
 5 int mp[MAXN][MAXN],endx,endy,n,m,tot=0;
 6 priority_queue<pair<int,int> >Q;
 7 vector<int>zh[MAXN],zl[MAXN];
 8 bool vst[MAXN*MAXN];
 9 int ans=0x7f7f7f7f,stax,stay;
10 int p[MAXN][MAXN],d[MAXN*MAXN],nxt[MAXN*MAXN*100],val[MAXN*MAXN*100],to[MAXN*MAXN*100],cnt,head[MAXN*MAXN];
11 void add(int u,int v,int w)
12 {
13     to[++cnt]=v;nxt[cnt]=head[u];head[u]=cnt;val[cnt]=w;
14     return ;
15 }
16 int dijstra()
17 {
18     memset(d,0x3f,sizeof(d));d[p[stax][stay]]=0;
19     Q.push(make_pair(0,p[stax][stay]));
20     while(Q.size())
21     {
22         int k=Q.top().second;Q.pop();
23         if(k==p[endx][endy])return d[k];
24         if(vst[k])continue;
25         vst[k]=1;
26         for(int i=head[k];i;i=nxt[i])
27         {
28             int y=to[i];
29             if(d[y]>d[k]+val[i])
30             {
31                 d[y]=d[k]+val[i];
32                 Q.push(make_pair(-d[y],y));
33             }
34         }
35     }
36     return 0x7f7f7f7f;
37 }
38 int main()
39 {
40     scanf("%d%d",&n,&m);
41     for(int i=1;i<=n;i++)
42     {
43         for(int j=1;j<=m;j++)
44         {
45             p[i][j]=++tot;
46             char c=getchar();
47             while(c!='C'&&c!='F'&&c!='.'&&c!='#')c=getchar();
48             if(c=='C')stax=i,stay=j,mp[i][j]=1;
49             else if(c=='F')endx=i,endy=j,mp[i][j]=1;
50             else if(c=='.')mp[i][j]=1;
51             else mp[i][j]=0;
52         }
53     }
54     for(int i=1;i<=n;i++)
55         for(int j=1;j<=m;j++)
56             if(!mp[i][j])zh[i].push_back(j);
57     for(int j=1;j<=m;j++)
58         for(int i=1;i<=n;i++)
59             if(!mp[i][j])zl[j].push_back(i);
60     for(int i=1;i<=n;i++)
61         for(int j=1;j<=m;j++)
62         {
63             if(!mp[i][j])continue;
64             if(i^1&&mp[i-1][j])add(p[i][j],p[i-1][j],1);
65             if(i^n&&mp[i+1][j])add(p[i][j],p[i+1][j],1);
66             if(j^1&&mp[i][j-1])add(p[i][j],p[i][j-1],1);
67             if(j^m&&mp[i][j+1])add(p[i][j],p[i][j+1],1);
68             int up=*(--lower_bound(zl[j].begin(),zl[j].end(),i));
69             int down=*(lower_bound(zl[j].begin(),zl[j].end(),i));
70             int lef=*(--lower_bound(zh[i].begin(),zh[i].end(),j));
71             int rig=*(lower_bound(zh[i].begin(),zh[i].end(),j));
72             int dis=min(i-up,min(down-i,min(j-lef,rig-j)));
73             add(p[i][j],p[up+1][j],dis);
74             add(p[i][j],p[down-1][j],dis);
75             add(p[i][j],p[i][lef+1],dis);
76             add(p[i][j],p[i][rig-1],dis);
77         }
78     ans=dijstra();
79     if(ans==0x7f7f7f7f)cout<<"no"<<endl;
80     else cout<<ans<<endl;
81     return 0;
82 }
View Code

T3 Histogram  Good question mark

This question is examined thirds.

Look for a clear position, the relationship between the height and size of the final answer is a single valley.

(When you get an optimal answer when, if the General Assembly change the remaining pillars rise again based on the original one and vice versa)

Thus was born the practice of 60 minutes:

Enumeration position, one-third height, statistical answers to O (n ^ 2logn)

 1 #include<bits/stdc++.h>
 2 #define MAXN 100005
 3 #define int long long
 4 #define F(x) for(int i=1;i<=n;i++)
 5 #define re register
 6 #define min(a,b) (a)<(b)?(a):(b)
 7 using namespace std;
 8 int a[MAXN],now,n,maxn;
 9 inline int Get(int h)
10 {
11     int ans=abs(h-a[now]);
12     F(n)
13     {
14         if(i==now)continue;
15         if(h-abs(i-now)<=0)return 0x7f7f7f7f7f7f7ff;
16         ans+=abs(a[i]-(h-abs(i-now)));
17     }
18     return ans;
19 }
20 inline int San_fen()
21 {
22     int l=now,r=maxn*2;
23     while(l+2<r)
24     {
25         int lmid=l+(r-l)/3;
26         int rmid=r-(r-l)/3;
27         if(Get(lmid)<Get(rmid))r=rmid;
28         else l=lmid;
29     }
30     return min(Get(l),min(Get(l+1),Get(l+2)));
31 }
32 signed main()
33 {
34     re int ans=0x7f7f7f7f7f7f7ff;
35     scanf("%lld",&n);
36     F(n)scanf("%lld",&a[i]),maxn=max(maxn,a[i]);
37     for(now=1;now<=n;now++)
38         ans=min(ans,San_fen());
39     cout<<ans<<endl;
40     return 0;
41 }
View Code

Consider optimizing for statistical answers, we talk about Points

(A discussion wool) Details are here

Mainly talk about skyh garbage annealing (fog)

Simulated annealing is a particular god (kan) odd (lian) algorithm.

Is such that:

According to the physics of common sense, we know that within smaller objects can be more stable.

The so-called simulated annealing, is to simulate the process.

1. Set the initial temperature T 0

2. Find the next target position

3. Try to update with new answers to old answers

4. Cooling

Is not look fooled?

In detail:

 

. 1  the while (T> EPS)                                                           // termination boundary 
2  {
 . 3      tmp = now + (RAND () *-2LL and RAND_MAX) * T * 0.000001 ;                          // higher the temperature, the greater the change 
. 4      IF (T < 1.0 ) tmp = max (tmp, 1LL), min = tmp (tmp, n-);                          // when the temperature is less than a certain extent, so that the next change in position as small as possible 
. 5      the else tmp = (tmp + n-n-%) n-% + . 1 ;                                            // let the next position within the required range 
. 6      tmpans Calc = (tmp); D = -fabs (tmpans-nowans);                           // find new solutions 
. 7      IF(tmpans <nowans || exp (D / T) * and RAND_MAX> RAND ()) = tmpans nowans, now = tmp; // The probability is determined whether the answer is changed 
. 8      IF (tmpans <ANS) ANS = tmpans;                                          // updates the last answer 
. 9      T * = 0.975 ;                                                          // cooling 
10 }

 

According to previous experience, the updated probability exp (D / T) (D is the opposite of the absolute value of the amount of change, T is the current temperature)

Finally, attach the complete code

 1 #include<bits/stdc++.h>
 2 #define MAXN 100005
 3 #define int long long 
 4 using namespace std;
 5 int yx[MAXN],n,now;
 6 struct Bit{
 7     int tr[2][MAXN];
 8     void insert(int x,int val,int opt)
 9     {
10         for(int i=x;i<=n;i+=i&-i)tr[0][i]+=val,tr[1][i]+=opt;
11         return ;
12     }
13     pair<int,int> query(int x)
14     {
15         int num=0,ans=0;
16         for(int i=x;i;i-=i&-i)num+=tr[1][i],ans+=tr[0][i];
17         return make_pair(ans,num);
18     }
19 }T[2];
20 int sa[MAXN],sb[MAXN],ap[MAXN],bp[MAXN],ans=0x7f7f7f7f7f7f7ff,maxa,a[MAXN],b[MAXN];
21 inline int check(int x)
22 {
23     if(x<now||x<(n-now+1))return 0x7f7f7f7f7f7f7ff;
24     int ans=abs(x-yx[now]);
25     int num=upper_bound(a+1,a+n+1,x-now)-a-1;
26     pair<int,int> w=T[0].query(num);
27     ans+=abs(w.first-w.second*(x-now))+abs(sa[now-1]-w.first-(now-1-w.second)*(x-now));
28     num=upper_bound(b+1,b+n+1,x+now)-b-1;
29     w=T[1].query(num);
30     ans+=abs(w.first-(now+x)*w.second)+abs(sb[n]-sb[now]-w.first-(n-now-w.second)*(now+x));
31     return ans;
32 }
33 inline int san_fen()
34 {
35     int l=1,r=maxa*2;
36     while(l+2<r)
37     {
38         int lmid=l+(r-l)/3,
39             rmid=r-(r-l)/3;
40         if(check(lmid)<check(rmid))r=rmid;
41         else l=lmid;
42     }
43     return min(check(l+1),min(check(l+2),check(l)));
44 }
45 signed main()
46 {
47     cin>>n;
48     for(int i=1;i<=n;i++)
49     {
50         scanf("%lld",&yx[i]);
51         ap[i]=a[i]=yx[i]-i;
52         bp[i]=b[i]=yx[i]+i;
53         sa[i]=sa[i-1]+a[i];
54         sb[i]=sb[i-1]+b[i];
55         maxa=max(maxa,yx[i]);
56     }
57     sort(a+1,a+n+1);sort(b+1,b+n+1);
58     for(int i=1;i<=n;i++)
59     {
60         ap[i]=lower_bound(a+1,a+n+1,ap[i])-a;
61         bp[i]=lower_bound(b+1,b+n+1,bp[i])-b;
62     }
63     for(int i=1;i<=n;i++)T[1].insert(bp[i],yx[i]+i,1);
64     for(now=1;now<=n;now++)
65     {
66         T[1].insert(bp[now],-(yx[now]+now),-1);
67         ans=min(san_fen(),ans);
68         T[0].insert(ap[now],yx[now]-now,1);
69     }
70     cout<<ans<<endl;
71     return 0;
72 }
正解AC
 1 #include<bits/stdc++.h>
 2 #define MAXN 100005
 3 #define int long long
 4 #define re register
 5 using namespace std;
 6 double eps=1e-5,T=100,D;
 7 int mid,n,a[MAXN],b[MAXN];
 8 inline int calc(int x)
 9 {
10     for(int i=1;i<=n;i++)b[i]=a[i]+abs(i-x);
11     nth_element(b+1,b+mid,b+n+1);
12     re int now=max(b[mid],max(x,n-x+1)),ans=0;
13     for(int i=1;i<=n;i++)
14     {
15         if(now-abs(i-x)<=0)return 0x7f7f7f7f7f;
16         ans+=abs(now-b[i]);
17     }
18     return ans;
19 }
20 signed main()
21 {
22     srand((unsigned)time(0));
23     cin>>n;
24     mid=(1+n)>>1;
25     for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
26     int ans=0x7f7f7f7f7f7f;
27     int now=(1+n>>1);
28     int nowans=calc(now),tmp,tmpans;
29     while(T>eps)
30     {
31         tmp=now+(rand()*2ll-RAND_MAX)*T*0.000001;
32         if(T<1.0)tmp=max(tmp,1ll),tmp=min(tmp,n);
33         else tmp=(tmp%n+n)%n+1;
34         tmpans=calc(tmp);D=-fabs(tmpans-nowans);
35         if(tmpans<nowans||exp(D/T)*RAND_MAX>rand())nowans=tmpans,now=tmp;
36         if(tmpans<ans)ans=tmpans;
37         T*=0.975;
38     }
39     cout<<ans<<endl;
40     return 0;
41 }
退火AC

模拟退火是一个特别优秀的算法,请不要针对算法,(尽管你可以针对脸某)。

Guess you like

Origin www.cnblogs.com/hzoi-kx/p/11343580.html