Luogu P2662 Cattle Farm Fence

Source: Internet
Author: User
Tags gcd

Transmission Door

Since a plank can be cut off up to \ (m\), it is possible to pre-preprocess which lengths of planks are available, open a bucket, then mark the \ ([l-m,l]\) , and then remove the marked number

Assuming the available length \ (a_1,a_2,,, a_n\) from small to large order, we can first without \ (a_1\), only with the length of the back to spell, and then with \ (a_1\) together, so set \ (di_i\) for the shortest length (\ (di_0=0\)) that can be mod\ with the back and \ (a_1\ ) for \ (i\) , then the \ (a_2,,, a_n\) can be taken from the location i\ \ ( (i+a_j) mod\ a_1\), Edge ( a_j\) Edge, run a shortest way to find all \ (di\)

The final answer is \ (\max_{i=0}^{a_1-1}di_i-a_1\), because these lengths \ (d_i\) do not use \ (a_1\) to spell out, this inside any one length minus \ (a_1\) is the length that cannot be spelled out

Pay attention to the sentence without solution

#include <bits/stdc++.h> #define LL long long#define il inline#define re registerusing namespace Std;const int n=3000    +10,m=n*n;il LL Rd () {re LL x=0,w=1;re char ch; while (ch< ' 0 ' | |    Ch> ' 9 ') {if (ch== '-') W=-1;ch=getchar ();}    while (ch>= ' 0 ' &&ch<= ' 9 ') {x= (x<<3) + (x<<1) + (ch^48); Ch=getchar ();} return x*w;} int to[m],nt[m],w[m],hd[n],tot=1;il void Add (int x,int y,int z) {++tot,to[tot]=y,nt[tot]=hd[x],w[tot]=z,hd[x]=tot;}  struct node{int x,d; BOOL operator < (const node &b) Const {return d>b.d;}}; int n,m,a[n],di[n];short Vv[n];il int gcd (int a,int b) {return B?GCD (b,a%b): A;}  int main () {n=rd (), M=rd ();      while (n--) {int x=rd ();    ++vv[max (1,x-m)],--vv[x+1];  } n=0;      for (int i=1;i<=n-10;i++) {vv[i]+=vv[i-1];    if (vv[i]>0) a[++n]=i;  } int gg=a[1];  for (int i=2;i<=n;i++) GG=GCD (Gg,a[i]); if (gg>1| |  A[1]==1) {puts ("-1"); return 0;} No solution for (int i=0;i<a[1];i++) for (int j=2;j<=n;j++) Add (i, (i+A[J])%a[1],a[j]);  memset (di,63,sizeof (DI));  di[0]=0;  Priority_queue<node> Q;  Q.push (node) {0,0});      while (!q.empty ()) {int x=q.top (). X,d=q.top (). D;      Q.pop ();      if (d>di[x]) continue;          for (int i=hd[x];i;i=nt[i]) {int y=to[i];              if (Di[y]>di[x]+w[i]) {di[y]=di[x]+w[i];            Q.push (node) {y,di[y]});  }}} int ans=a[1];  for (int i=0;i<a[1];i++) Ans=max (Ans,di[i]);  printf ("%d\n", ans-a[1]); return 0;}

Luogu P2662 Cattle field fence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.