Yale-Yale 10.6 Morning examination of the situation T1
Two-point answer
There should be a counter-example, that is, the answer should not be monotonous.
But no other algorithms are written ...
T2
My TM ...
The second red frame circled out of the section should be
if(x1+x1!=s)
Write wrong, there is no (18\) points.
T3
wrote a \ (n^4\) of violence
Finally, it is found that the four vertices of a rectangle in the topic are not necessarily the given vertices.
That's GG.
The question and the examination room code T1
/* Two-point answer: * Complexity O (n+nlogn+m), feel very dangling * sorting should be optimized, but not very good. */#include <cstdio> #include <algorithm>inline int read () {int n=0,w=1;register char c=getchar (); while (c< ' 0 ' | | c> ' 9 ') {if (c== '-') W=-1;c=getchar ();} while (c>= ' 0 ' && c<= ' 9 ') n=n*10+c-' 0 ', C=getchar (); return n*w;} const int n=1e6+1;int n,m;struct seg{int b,k;} Seg[n];long Long s,emp[n];inline bool Judge (long long X) {for (int i=1;i<=n;++i) emp[i]=seg[i].k*x+seg[i].b; Std::sort (emp+1,emp+1+n);/* for (int i=1;i<=n;++i) printf ("%lld", Emp[i]); Puts (""); */long long res=0; for (int i=0;i<m;++i) {if (emp[n-i]<0) break; Res+=emp[n-i]; }//printf ("%lld\n", res); return res>=s;} int main () {freopen ("merchant.in", "R", stdin); Freopen ("Merchant.out", "w", stdout); N=read (), M=read (); scanf ("%lld", &s);//printf ("%d%d%lld\n", n,m,s); for (int i=1;i<=n;++i) {seg[i].k=read (), Seg[i].b=read ();//printf ("%d%d\n", seg[i].k,seg[i].b); } int l=0,r=1e9,mid; while (l<=r) {mid=l+r>>1; if (judge (mid)) r=mid-1; else l=mid+1; } printf ("%d", l); Fclose (stdin), fclose (stdout); return 0;}
T2
*/* Title describe what Ghost Ah ... Completely do not understand * * * * * * * emmmm * Gaussian elimination yuan ... */#include <cstdio> #include <algorithm>inline int read () {int n=0,w=1;register char c=getchar (); while (c< ' 0 ' | | C> ' 9 ') {if (c== '-') W=-1;c=getchar ();} while (c>= ' 0 ' &&c<= ' 9 ') n=n*10+c-' 0 ', C=getchar (); return n*w;} const int N=1e6+1;int n,q,fa[n],w[n];/*const double eps=1e-8;double a[n][n];int sign (double x) {//Determines whether a number is positive, negative, or 0 if (fabs (x) <=eps) return 0;//fabs is the absolute if (x>0) return of the floating-point number 1; return-1;} inline void GG (int x) {if (a[x][n+1]) puts ("none"); Else puts ("INF");} inline void Solve () {for (int i=1;i<=n;++i) a[i][n+1]=b[i];//gets the extended matrix for (int i=1;i<=n;++i) { int p=i; for (int j=i+1;j<=n;++j) if (Fabs (A[j][i]>fabs (a[p][i))) P=j; for (int j=1;j<=n+1;++j) swap (a[p][j],a[i][j]); if (sign (a[i][i)) ==0) GG (i); for (int j=i+1;j<=n;++j) {double ratio=a[j][i]/a[i][i];//calculation is how many times for (int k=1;k<=n+1;++k) a[j][k]=a[j][k]-ratio*a[i][k]; } for (int i=n;i>0;--i) {for (int j=i+1;j<=n;++j) a[i][n+1]=a[i][n+1]-x[j]*a[ I][J]; X[i]=a[i][n+1]/a[i][i]; }}int Main () {freopen ("equation.in", "R", stdin); Freopen ("Equation.out", "w", stdout); N=read (), Q=read (); for (int i=2;i<=n;++i) Fa[i]=read (), W[i]=read (); int type,u,v,s; while (q--) {type=read (), U=read (), V=read (); if (type==1) {s=read (); Solve (); } else {w[u]=v; A[u][fa[u]]=v; }} fclose (stdin); fclose (stdout); return 0;} Day.. Write Hung */int main () {freopen ("equation.in", "R", stdin); Freopen ("Equation.out", "w", stdout); N=read (), Q=read (); for (int i=2;i<=n;++i) Fa[i]=read (), W[i]=read (); int type,u,v,s; int x2,x1; if (n==2) {while (q--) {type=rEAD (), U=read (), V=read (); if (type==1) {s=read (); if (u==v && v==2) {x2=s/2; if (x2+x2!=s) puts ("none"); else printf ("%d\n", w[2]-x2); } else if (U==v && v==1) {x1=s/2; if (x2+x2!=s) puts ("none"); else printf ("%d\n", X1); } else {if (s==w[2]) puts ("INF"); Else puts ("none"); }} else w[u]=v; }} else {while (q--) {srand (19260817); while (q--) {int X=rand (); if (x%3==1) puts ("none"); else if (x%3==2) puts ("INF"); else printf ("%d\n", X); }}} fclose (stdin); fclose (stdout); return 0;}
T3
#include <cstdio> #include <map>const int n=2501,mod=1e9+7;std::map<int,bool> mp1,mp2;int n,t1,t2, Lx,ly,rx,ry;int ans,x[n],y[n];inline int Read () {int n=0,w=1;register char c=getchar (); while (c< ' 0 ' | | c> ' 9 ') {if (c== '-') W=-1;c=getchar ();} while (c>= ' 0 ' && c<= ' 9 ') n=n*10+c-' 0 ', C=getchar (); return n*w;} inline int min (int x,int y) {return x<y?x:y;} inline int max (int x,int y) {return x>y?x:y;} int main () {freopen ("rectangle.in", "R", stdin); Freopen ("Rectangle.out", "w", stdout); N=read (); for (int i=1;i<=n;++i) X[i]=read (), Y[i]=read (); for (int i=1;i<=n;++i) for (int j=1;j<=n;++j) for (int. q=1;q<=n;++q) for (int p=1;p& LT;=N;++P) {if (i==j&&j==q&&q==p) continue; Lx=min (min (x[i],x[j]), Min (x[q],x[p])); Ly=min (min (y[i],y[j]), Min (y[q],y[p])); Rx=max (Max (X[i],x[j]), Max (X[q],x[p])); Ry=max (Max (Y[i],y[j]), Max (Y[q],y[p])); T1= (LX*RY+LY*RX); T2= (Lx*rx+ly*ry); if (mp1[t1]==0&&mp2[t2]==0) {ans= (ans+ (RX-LX) * (ry-ly))%mod; Mp1[t1]=mp2[t2]=1; }} printf ("%d", ans); Fclose (stdin); fclose (stdout); return 0;}
Positive solutions and Code T1
Select any of the collections, and the resulting gain and all can be represented as a function of a single form.
We only care about the maximum value of one function at a time, and we can find that the maximum value must be first descending, monotonically increasing, or monotonically decreasing.
Therefore, we only need to \ (check\) the time (0\) whether the conditions are met, if not meet the two points. Note \ (check\) when we only need to find the largest \ (m\) , so you can \ (O (n) \) to do, specifically, the process of the fast row only one side, or directly with \ (stl\) \ (nth_element () \) to
#include <bits/stdc++.h>#define For(i, j, k) for (int i = j; i <= k; i++)using namespace std;const int N = 1e6 + 10;typedef long long LL;int n, m;LL S;int k[N], b[N];LL val[N];bool check(int x) { For(i, 1, n) val[i] = 1ll * k[i] * x + b[i]; nth_element(val + 1, val + m, val + n + 1, greater<LL>()); LL sum = 0; For(i, 1, m) if (val[i] > 0 && (sum += val[i]) >= S) return true; return sum >= S;}int main() { scanf("%d%d%lld", &n, &m, &S); For(i, 1, n) scanf("%d%d", &k[i], &b[i]); if (check(0)) { puts("0"); return 0; } int L = 1, R = 1e9; while (L < R) { int mid = (L + R) / 2; if (check(mid)) R = mid; else L = mid + 1; } printf("%d\n", L); return 0;}
T2
Each variable can be represented as a \ (x_i=k+x_1\) or \ (x_i=k?x_1\) form, which is expressed as a convenient way to answer the question.
For the inquiry \ (1\), simply add the expression \ (u\) and \ (v\) , there are two cases: you will get the form of \ (x_u+x_v=t\) , At this point only need to determine whether there is \ (s=t\), or you will get \ (x_u+x_v=t+2x_1\) or \ (x_u+x_v=t?2x_1\), at this time can solve the \ (x_1\) , be careful to determine if the solution is an integer.
For the modification operation, actually modifies the variable in a subtree \ (k\), here can be the depth of the odd and even points to consider separately, it is not difficult to find is the interval plus minus. Since only a single point of inquiry is required, maintenance with a tree array can be
\ (O ((n+q) \log n)? \)
#include <bits/stdc++.h> #define GetChar getchar_unlocked#define for (I, J, K) for (int i = j; I <= K; i++) using NA Mespace std;int Read () {char c = getchar (); int x = 0; int sig = 1; while (C < ' 0 ' | | c > ' 9 ') {if (c = = '-') sig =-1; c = GetChar ();} while (c >= ' 0 ' && C <= ' 9 ') x = x * ten + C-' 0 ', C = GetChar (); return x * SIG;} const int n = 1e6 + 10;int N, q;struct binary_indexed_tree {int c[n]; inline int lowbit (int x) {return x & (x); } void Add (int x, int w) {while (x <= N) {c[x] + = W; x + = Lowbit (x); }} int sum (int x) {int ret = 0; while (x) {ret + = c[x]; X-= Lowbit (x); } return ret; }}t;int Dfn[n], rdfn[n], Dep[n];int fa[n], w[n];vector<int> g[n];void dfs_init (int o) {static int clk = 0; Dfn[o] = ++CLK; for (int v:g[o]) dep[v] = Dep[o] ^ 1, dfs_init (v); Rdfn[o] = CLK;} int main () {n = read (), q = Read (); For (I, 2, n) fa[i] = Read (), w[i] = Read (), g[fa[i]].push_back (i); Dfs_init (1); For (I, 2, N) if (!dep[i]) w[i] =-w[i]; For (I, 2, N) t.add (Dfn[i], w[i]), T.add (Rdfn[i] + 1,-w[i]); while (q--) {int op = Read (); if (op = = 1) {int u = read (), V = Read (), s = Read (); int x = T.sum (Dfn[u]), y = T.sum (dfn[v]); if (Dep[u] && dep[v]) {long Long RT = 1LL * x + y-s; if (rt% 2) puts ("none"); else printf ("%lld\n", RT/2); } else if (!dep[u] &&!dep[v]) {long Long RT = 1LL * x + y + S; if (rt% 2) puts ("none"); else printf ("%lld\n", RT/2); } else {if (Dep[v]) swap (U, v), swap (x, y); if (x-y = = s) puts ("INF"); Else puts ("none"); }} else {int u = read (), NW = read (); if (!dep[u]) NW =-NW; T.add (Dfn[u], nw-w[u]), T.add (Rdfn[u] + 1, w[u]-NW); W[u] = NW; }} return 0;}
T3
#include <bits/stdc++.h> #define for (I, J, K) for (Int. i = j; I <= K; i++) #define Forr (I, J, K) for (int i = j; i >= K; i--) using namespace std;const int N = 2510;const int Mod = 1e9 + 7;int N, m = 2500;struct binary_indexed_tree {int c[n ]; void Init () {for (I, 1, m) c[i] = 0; } int lowbit (int x) {return x & (-X);} void Add (int x, int w) {for (; x <= m; x + = Lowbit (x)) c[x] + = W; } int sum (int x) {int ret = 0; for (; x; x-= Lowbit (x)) ret + = c[x]; return ret; }}t, S;int pos[n][n];int c[n];bool vis[n];void upd (int x) {if (vis[x]) return; Vis[x] = true; T.add (x, 1), S.add (x, x);} int main () {scanf ("%d", &n); For (i, 1, n) {int x, y; scanf ("%d%d", &x, &y); Pos[x][++c[x]] = y; } for (I, 1, m) sort (Pos[i] + 1, pos[i] + c[i] + 1), Pos[i][c[i] + 1] = m + 1; int ans = 0; For (I, 1, m) if (C[i]) {t.init (), S.init (); For (J, 1, m) vis[j] = FALse For (J, 1, C[i]) upd (Pos[i][j]); Forr (J, i-1, 1) if (C[j]) {int PA = 1, pb = 1, cur = max (pos[i][1], pos[j][1]); For (k, 1, C[j]) upd (pos[j][k]); while (Pos[i][pa + 1] <= cur) ++pa; while (POS[J][PB + 1] <= cur) ++pb; while (PA <= c[i] && PB <= c[j]) {int nxt = min (Pos[i][pa + 1], POS[J][PB + 1]), L = min (pos I [PA], POS[J][PB]); Ans = (ans + (1LL * (S.sum (nxt-1)-s.sum (cur-1)) * T.sum (L)-1ll * (T.sum (nxt-1)-T.S Um (cur-1)) * S.sum (L)) * (i-j))% Mod; cur = NXT; if (Pos[i][pa + 1] <= cur) ++pa; if (POS[J][PB + 1] <= cur) ++pb; }}} printf ("%d\n", ans); return 0;}
Afternoon Lectures: DP Selection Example 1
A length of \ (n\) sequence \ (a\), we call an element to be good when and only if it is strictly greater than the adjacent element. You can do it several times, reducing one element at a time \ (1\)
For each \ (k\in [1,\lceil\frac{n}{2}\rceil]\), at least how many operations to make at least \ (k\) a good element in the sequence
\ (N\le 5000,a_i\le 10^5\)
Solution:
A little observation shows that if a position in the final scheme is good, then we will not operate on it; if not, the final value is one in \ (a_{i-1}-1,a_i,a_{i+1}-1\) .
Make \ (dp[i][j][0]\) indicates that the former \ (i\) element has \ (j\) is good, and has been King \ (a_i\) is good, this
The minimum number of operations to be performed on a front \ (i\) element. Transfer to \ (i+1\) needs to be ensured after Operation \ (a_{i+1}\lt a_i\)
Similarly,\ (dp[i][j][1/2/3]\) indicates that \ (a_i\) is not a good three case
\ (O (n^2) \)
Example 2
A sequence of length \ (n\ ) \ ( a\), which defines an arrangement of 1 to n p is valid only if and only if \ (\forall i\in[1,n-1]. A_{p_i}\times a_{p_{i+1}}\) is not a full square number
Ask how many legal permutations, to \ (1e9+7\) modulo
\ (N\le 300,a_i\le 10^9\)
Solution:
For each element to get rid of its squared-mass factor, the problem is converted to how many permutations \ (p\) satisfy \ (\forall i\in[1,n-1],a_{p_i}\ne a_{p_{i+1}}\), that is, the neighboring elements are different
First count how many different elements, and the number of each element. Consider adding all elements of the same value each time to an arranged sequence
Set \ (dp[i][j]\) indicates that the former \ (i\) element has been added to the sequence, having \ (j\) the same number of scenarios as the adjacent position. When a transfer enumeration adds the i+1\ element, it will disassemble the original illegal adjacent position, and how many additional illegal adjacent positions will be added.
Total number of elements \ (o (n) \), so the complexity of the worst \ (o (n^3) \)
Example 3
There is a sequence \ (a\) and constant \ (l,p\)of length \ (n\ ), and you need to divide it into segments, the cost of each segment is \ (| ( \sum a_i)-l|^p\), to find the minimum cost of the Division scheme
\ (n\le 10^5,1\le p\le 10\)
Solution:
\[dp[j]=\min_{i=0}^{j-1}|sum_j-sum_i-l|^p+dp[i]\]
This equation has decision monotonicity, i.e. \ (\forall u\lt v\lt i\lt j\), if the decision in \ ( i\) is better than the decision (v\ ), then the \ (u\) (v\) better than decision-making \ (u\)
A stack is used to maintain the interval of each decision update, and when a new decision is added, it can be divided into two points.
\ (O (n\log n) \)
Prove:
The proof of the subject needs to discuss the absolute value of the symbol, first of all the values inside the case is positive, the rest is similar.
Definition \ (f_i (x) =dp[i]+ (sum_x-sum_i-l) ^p\), simply prove \ (g (x) =f_u (x)-f_v (x), u\lt v\lt x\) monotonically increasing
That is, with the increase in \ (x\) decision-making ( u\) compared to the decision-making \ (v\) more and more
\[g (x) = (Sum_x-sum_u) ^p-(sum_x-sum_v) ^p+dp[u]-dp[v]\g ' (x) =p (Sum_x-sum_u) ^{p-1}-p (sum_x-sum_v) ^{P-1}\g ' (x) \ge 0\]
Example 4
A \ (n\) point \ (m\) edge of the weighted graph, each edge of the length is \ (1\). The longest path is satisfied, the edge weights are strictly incremented, and the order of the top of the path is the same as the relative order of those edges in the input.
\ (N,m\le 10^5\)
Solution:
Consider each edge in the input order. Set \ (dp[i][j]\) indicates the longest length of the path to node \ (i\), the weight of the previous Edge \ (j\) .
It is clear that the useful state is \ (O (m) \) . Maintain one \ (set\) for each point to save these states and their \ (dp\) values, and ensure that the \ (dp\) value is incremented with \ (j\) . When this is added to an edge \ ((u,v) \) , only two points on the node \ (u\) \ (set\) can be transferred. You also need to maintain \ ( v\) \ (set\).
\ (O (n+m\log m) \)
Example 5
There is a tree \ (n\) point with the right binary tree (do not know the shape of the tree), give the binary tree in order to carry out the sequence of weights, to determine whether there is a binary tree matching, the tree on each pair of adjacent node weights \ (gcd\) greater than \ (1\)
\ (N\le 700,w_i\le 10^9\)
Solution
For a subtrees tree of a binary tree, the sequence traversal is a continuous interval. An idea (much like yesterday's topic) is set \ (dp[l][r][i]\) indicates whether the interval \ ( [l,r]\) can be built into a legitimate binary tree with \ (i\) , which shifts the root of the subtree on both sides of the enumeration. But this complexity is \ (O (n^5) \)
Notice that for the interval \ ([l,r]\) consists of a two-fork tree, unless \ (l=1,r=n\), otherwise it must be \ (r+1\) the left subtree, or \ (l-1\) the right subtree. So we only care if the root node and \ ( l-1\) and \ (r+1\) are \ ( gcd\) \ ( 1\), and do not need to know which node the root is.
So the state number changes to \ (o (n^2) \), the transfer still enumerates the root,\ (o (n^3) \)
Yale-Yale Study 10.6