noip 2 summarizes the simulation game

# (Fill summary)

# Pit training day8

Today was the day the exam ah ~

noip senior simulation game in question 4 3 hours

I was, as always, this garbage is beating and hanging ~

Well do not say waste (zhuang), then (ruo)

 

 

 

 

 

 

T1 math class

 

 

Easy to see that this problem is to choose the smallest result, i.e., to obtain all A I  * A J minimum + 1

Since (A I * A J +. 1) A * K + A. 1 = I * A J * A K + A K +. 1, and therefore for any three A I , A J , A K , selecting the largest of two multiplied by the number of prioritizing the final result is the smallest, empathy, due to the newly acquired number will still be added to the queue, so every one should select a maximum of two numbers multiplied

The following is the competition program

. 1 #include <the iostream>
 2 #include <cstdio>
 . 3 #include <Queue>
 . 4  
. 5  #define the MOD 1000000007
 . 6  
. 7  the using  namespace STD;
 . 8  
. 9  int n-, A;
 10  Long  Long X1, X2, RES;
 . 11 The priority_queue < Long  Long > q; // initially seek the maximum value as the brain pumping, staged a
 12                      // priorty_queue <int, the Vector <int>, Greater <int>>
 13  int main () // In fact, it seems can be plugged directly into the team ...... End
 14  {
 15  //   freopen("a.in", "r", stdin);
16  //   freopen("a.out", "w", stdout);
17     scanf("%d", &n);
18     for (int i = 1; i <= n; i++)
19     {
20         scanf("%d", &a);
21         q.push(a);
22     }
23     while(q.size ()> . 1 )
 24      {
 25          X1 = q.top ();
 26 is          q.pop ();
 27          X2 = q.top ();
 28          q.pop ();
 29          RES = (X1 X2 * + . 1 )% the MOD; // avoid direct multiplication process burst open like a long long ......
 30          q.push (RES);
 31 is      }
 32      the printf ( " % LLD " , q.top ());
 33 is      return  0 ;
 34 }

However Notes Note!

This program is wrong!

The largest number, but if the priority queue is inserted in the modulo without modulo two numbers must be multiplied + 1 newly obtained , and can not guarantee a direct selection of the number , that it is possible to obtain results not the smallest (but data too much water actually passed)

Thus the queue should be the maximum number of the number obtained by multiplying the original direct

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <queue>
 4 
 5 #define MOD 1000000007
 6 
 7 using namespace std;
 8 
 9 int n, a;
10 long long x1, x2, res;
11 priority_queue<long long> q;
12 
13 int main()
14 {
15 //    freopen("a.in", "r", stdin);
16 //    freopen("a.out", "w", stdout);
17     scanf("%d", &n);
18     for (int i = 1; i <= n; i++)
19     {
20         scanf("%d", &a);
21         q.push(a);
22     }
23     res = q.top();
24     q.pop();
25     while (q.size())
26     {
27         x1 = q.top();
28         q.pop();
29         res = (x1 * res + 1) % MOD;
30     }
31     printf("%lld", res);
32     return 0;
33 }

So my question really should sign off wa ......

 

Keke quickly look at the second question (cover up embarrassment

Sequence segments T2

Directly attached to the address bar https://www.luogu.org/problem/P1182

It is a more conventional binary answer

When k is divided into sections (1 <= k <= m), the maximum length of each segment L, the maximum length of> = l answer, certainly feasible, consistent decision monotonicity

That write directly to check on okay ......

 1 bool check(int k)
 2 {
 3     int i, j, tot = 0;
 4     for (i = 1, j = 1; i <= n && j <= m; i++)
 5     {
 6         tot += a[i];
 7         if (tot > k)
 8         {
 9             tot = a[i];
10             if (a[i] > k)
11                 return 0;
12             j++;
13         }
14     }
15     if (i == n + 1 && j <= m)
16         return 1;
17     return 0;
18 }

 

T3 patrol

Paste the address https://www.luogu.org/problem/P3629

The problem is a APIO / Khan

In my opinion this is a problem fw hardest game ......

Initially I thought it was a directed acyclic graph ...... posted almost ready to begin when suddenly see a sample of n-1 line input (eyes can not be donated to people in need)

Therefore, after careful thought (fa) test (dai) decided to start their own journey cheat points

First Category talk:

1.k = 1 Shi

Then we found that prior to adding the edge, since the rings in a tree does not exist, each side are traversed twice (back and forth between a parent node and child node), an edge is added, can make a fi point subtree with a node, a path between two points need to go through and only once , and because the front side was not added, to an arbitrary node as a parent node results are the same, it is only necessary to find the tree the longest chain to

1 if (k == 1)
2 {
3     dfs(1, 0);
4     printf("%d", 2 * (n - 1) - maxlen + 1);
5 }

dfs implementation:

1  // label is a step k = 2 k = 2 for the case of service 
2  int the maxlen, MAX1, MAX2;
 . 3  int F1 [MAXN], F2 [MAXN];
 . 4  int N1 [MAXN], N2 [MAXN];         // K = 2 path recording 
. 5  void DFS ( int now, int Last)
 . 6  {
 . 7      Hi [now] Hi = [Last] + . 1 ;
 . 8      FAT [now] = Last;         // K 2 = parent recording 
. 9      N1 [now] = N2 [now] = now;
 10      for ( int I = head [now]; I; I = Edge [I] .next)
 . 11     {
12         int v = edge[i].v;
13         if (v == last)
14             continue;
15         dfs(v, now);
16         edf[v] = i;            
17         if (f1[v] + 1 > f1[now])
18         {
19             f2[now] = f1[now];
20             n2[now] = n1[now];    // k = 2
21             f1[now] = f1[v] + 1;
22             n1[now] = n1[v];     // k = 2
23         }
24         else if (f1[v] + 1 > f2[now])
25         {
26             f2[now] = f1[v] + 1;
27             n2[now] = n1[v];
28         }
29     }
30     if (f1[now] + f2[now] > maxlen)
31     {
32         maxlen = f1[now] + f2[now];
33         max1 = n1[now];            // k = 2
34         max2 = n2[now];            // k = 2
35     }
36 }

2.k = 2 when

APIO the problem rather make it so easy to fall over 2333

Therefore highlights the force is beginning to rip hhhh

First, the longest chain are certain to be selected, it would need to make a decision in the other chain

It can be seen in the finished selecting the longest chain after, there are three sides may run a few

So if this time we choose times of long-chain (2-7)

As can be seen, in this case the shortest path is 1-2-8-5-7-2-1-3-4-3-5-6-5-3-1

That is the original chain 2-8 may not necessarily be repeated after three green edge , 2-7 chain without repeatedly after three yellow edge

But now we have repeated after which a total of 2, i.e. when the second dfs, the two sides of the contribution provided -1

Then we can re-record the value and the value of the right to seek the tree according to the longest chain

 

 

Paste the code

. 1      the else {
 2          DFS ( . 1 , 0 );
 . 3          int now1 = MAX1, MAX2 = now2, LEN1 = 0 , LEN2 = 0 ;
 . 4          the while (Hi [now1] <Hi [now2])         // longest chain pairs each edge is labeled 
. 5          {
 . 6              edge [EDF [now2]] W = -. . 1 ;         // EDF indicates the number of each node and a parent node side 
. 7              now2 FAT = [now2];             // FAT represents each node parent node 
. 8          }
 . 9          the while (Hi [now1]> Hi [now2])
 10         {
11             edge[edf[now1]].w = -1;
12             now1 = fat[now1];
13         }
14         while (now1 != now2)
15         {
16             edge[edf[now2]].w = -1;
17             now2 = fat[now2];
18             edge[edf[now1]].w = -1;
19             now1 = fat[now1];
20         }
21         dfs1(1, 0);
22         printf("%d", 2 * (n - 1) - maxlen + 1 - seclen + 1);
23     }
. 1  int seclen;
 2  int DFS1 ( int now, int Last)         // Common Target dfs longest chain of weights were determined 
. 3  {
 . 4      int S1 = 0 , S2 = 0 ;
 . 5      for ( int I = head [ now]; I; I = Edge [I] .next)
 . 6      {
 . 7          int V = Edge [I] .v;
 . 8          IF (V == Last)
 . 9              Continue ;
 10          int RES = DFS1 (V, now);
 . 11          if (res + edge[i].w > s1)
12         {
13             s2 = s1;
14             s1 = res + edge[i].w;
15         }
16         else if (res + edge[i].w > s2)
17             s2 = res + edge[i].w;
18     }
19     seclen = max(seclen, s1 + s2);
20     return s1;
21 }

Keke but above analysis I said, k = 2, when the whole game did not think

If the game when the direct path is the longest edge in the direct exclusion, vis [i] = 1

This approach may prove falsity of ...... (but I was so stupid I did not expect positive solution)

T4 toy named

Paste the address https://www.luogu.org/problem/P4290

Meaning of the questions simply, is a string of characters which can be made by transform

As a love Fudge (Luantong) people must be prepared to fight directly O (the n- the n- ) violence (slip

Then looked len <= 200 gave up like pressure

However, both methods are based on the wing upward to expand

See original string <= length 200, it is conceivable compressed downwardly on the basis of the string should be better

Wherein each of two selected characters compressed, and then compressing the string has been obtained

But this search will still deal with a lot of duplication, more difficult to record the status of each search

So we can think of, is recorded in the deformation of the original string by dp / memory search

I.e., with f [i] [j] [k] bit string records ij able to use the k-th character represents (1 <= i, j <= len, 1 <= k <= 4), with D [ i] [j] [k] denotes the k-th character can become "ij" (1 <= i, j, k <= 4)

You will be able to launch recurrence equation:

f[i][j][c] = f[i][k][a]&f[k+1][j][b]&d[a][b][c] (i<=k<j, 1<=a,b,c<=4)

So directly on the bar code

 1     for (int l = 2; l <= len; l++)                // l枚举区间长度 
 2     {
 3         for (int i = 1; i + l - 1 <= len; i++)
 4         {
 5             int j = i + l - 1;
 6             for (int k = i; k < j; k++)
 7                 for (int c1 = 1; c1 <= 4; c1++)
 8                     for (int c2 = 1; c2 <= 4; c2++)
 9                         if (f[i][k][c1] && f[k + 1][j][c2])
10                             for (int c3 = 1; c3 <= 4; c3++)
11                                 if (d[c1][c2][c3])
12                                     f[i][j][c3] = 1;
13         }
14     }

 

Guess you like

Origin www.cnblogs.com/Conless/p/11327240.html