A. Array Factory
The subscript is prefixed and sorted, and then the double pointer maintains the maximum right boundary.
#include <cstdio> #include <algorithm>using namespace std;typedef long long ll;const int N=200010;int n,i,j, ANSLEN,ANSL,ANSR,MR,Q[N];LL a[n],lim;inline BOOL cmp (int x,int y) {return a[x]<a[y];} int main () { freopen ("arrayfactory.in", "R", stdin); Freopen ("Arrayfactory.out", "w", stdout); scanf ("%d%lld", &n,&lim); for (i=1;i<=n;i++) scanf ("%lld", &a[i]), a[i]+=a[i-1]; for (i=0;i<=n;i++) q[i]=i; Sort (q,q+n+1,cmp); for (i=j=0;i<=n;i++) { while (J<=n&&a[q[j]]-a[q[i]]<=lim) Mr=max (mr,q[j++]); if (Mr>q[i]) { int len=mr-q[i]; if (Len>anslen) { anslen=len; Ansl=q[i]; ANSR=N-MR; } else if (len==anslen&&q[i]<ansl) { ansl=q[i]; ANSR=N-MR ; }}} if (!anslen) return puts ("-1"), 0; printf ("%d\n%d%d", ANSLEN,ANSL,ANSR);} /*a[r]-a[l]<=limask Max ra[r]<=a[l]+lim*/
B. Purchases and bonuses
$f [i][j]$ means the purchase of the former $i$ items, the current number of $j$ points to save the maximum amount of money, transfer is to either buy directly, or put all the points out.
#include <cstdio> #include <algorithm>using namespace std;typedef long Long ll;typedef pair<ll,ll>pi; const int Maxn=102,inf=1e9;int n;int a[maxn];int pre[102][100020];int pw[102][100020];int rep[102];int dp[2][100020]; int main () {freopen ("bonuses.in", "R", stdin); Freopen ("Bonuses.out", "w", stdout); while (scanf ("%d", &n)!=eof) {for (int i=1;i<=n;i++) scanf ("%d", a+i); int cs=0; for (int i=0;i<=n*1000;i++) Dp[cs][i]=inf; dp[cs][0]=0; for (int i=1;i<=n;i++,cs^=1) {for (int j=0;j<=n*1000;j++) Dp[cs^1][j]=inf; for (int j=0;j<=i*1000;j++) {if (dp[cs][j]==inf) continue; if (Dp[cs^1][j+a[i]/100]>dp[cs][j]+a[i]) {dp[cs^1][j+a[i]/100]=dp[cs][j]+a[i]; Pre[i][j+a[i]/100]=j; pw[i][j+a[i]/100]=0; } int Tmp=min (A[I],J); int tmpv=dp[cs][j]+a[i]-tmp; if (DP[CS^1][J-TMP]>TMPV) {DP[CS^1][J-TMP]=TMPV; Pre[i][j-tmp]=j; pw[i][j-tmp]=tmp; }}} int Ans=inf,anscs; for (int i=0;i<=n*1000;i++) {if (Ans>dp[cs][i]) {ans=dp[cs][i];anscs=i; }} for (int i=n; i>=1;i--) {Rep[i]=pw[i][anscs]; ANSCS=PRE[I][ANSCS]; } printf ("%d\n", ans); for (int i=1;i<=n;i++) printf ("%d +%d\n", a[i]-rep[i],rep[i]); }}/*a[r]-a[l]<=limask Max ra[r]<=a[l]+lim*/
C. Number of Solutions
Leave the pit.
D. Cutting potatoes
Brute force enumeration can be.
#include <cstdio> #include <algorithm>using namespace std;typedef long Long ll;typedef pair<ll,ll>pi; int n,k;struct node{ll x, y; Node () {}node (LL x,ll y): x (x), Y (y) {}bool operator < (const node&a) Const{return x*a.y<=y*a.x;} Node operator/(const NODE&A) const{return node (x*a.y,y*a.x);}; int cmp (Node A,node b) {if (a.x*b.y==a.y*b.x) return 0;return a.x*b.y>a.y*b.x?1:-1;} int A[102];int Rep[102],tmprep[102];int Main () {freopen ("cut-potatoes.in", "R", stdin); Freopen ("Cut-potatoes.out", "w", stdout); while (scanf ("%d%d", &n,&k)!=eof) {for (int i=1;i<=n;i++) scanf ("%d", a+i); Node Ans=node (10000000,1); for (int i=1;i<=n;i++) {for (int j=1;j<=k;j++) {Node minx=node (a[i],j); Node Maxx=node (1,10000000); BOOL Flag=1; for (int k=1;k<=n;k++) {int x=a[k]*minx.y/minx.x; if (!x) {flag=0;break;} X=min (X,K); Tmprep[k]=x; Maxx=max (Node (A[k],x), Maxx); } if (!flag) {continue;} Node Tans=maxx/minx; if (Tans<ans) {Ans=tans; for (int k=1;k<=n;k++) rep[k]=tMPREP[K]; }}} for (int i=1;i<=n;i++) printf ("%d%c", rep[i],i==n? ' \ n ': '); }}/*a[r]-a[l]<=limask Max ra[r]<=a[l]+lim*/
E. Divide and Conquer
DP to calculate the number of points for $n$, and then enumerate the decision to find the $k$ pee.
#include <bits/stdc++.h>using namespace Std;typedef long Long ll;typedef vector<int>vi;struct p{int x, y; P () {}p (int x,int y): x (x), Y (y) {}bool operator< (const p&a) const{if (x!=a.x) return X<a.x;return Y<A.Y;} P operator-(const p&a) Const{return P (X-A.X,Y-A.Y);} int operator * (const p&a) Const{return x*a.y-y*a.x;}} A[33];int N; LL mp[44];int cal (P t1,p t2,p t3) {int tmp= (T2-T1) * (T3-T1); return tmp>0;} BOOL CMP (int x,int y) {return a[x]<a[y];} LL dfs (int n) {if (mp[n]>=0) return mp[n];if (!n) {return mp[n]=1;} LL t=0;for (int i=0;i<n;i++) {for (int j=i+1;j<n;j++) {int l=j-i-1,r=n-2-l;if ((l&1)) continue; LL Res1=dfs (L); LL Res2=dfs (R); t+=res1*res2;}} return mp[n]=t;} Vector<p>rep;void Pt (vi cur,ll ned) {if (Cur.empty ()) return;bool flag=1;for (int i=0;i<cur.size () && flag;i++) {for (int j=i+1;j<cur.size () &&flag;j++) {P t1=a[cur[i]],t2=a[cur[j]];vi vl,vr;for (int k=0;k< Cur.size (); k++) {if (k==i| | K==J) Continue;if (Cal (T1,t2,a[cur[k])) Vl.push_back (cur[K]); else Vr.push_back (Cur[k]);} if (Vl.size () &1) Continue;sort (Vl.begin (), Vl.end (), CMP); sort (Vr.begin (), Vr.end (), CMP); LL res1=mp[vl.size ()]; LL res2=mp[vr.size ()];if (ned>=res1*res2) ned-=res1*res2;else{rep.push_back (t1); Rep.push_back (T2);p T (vl,ned/ Res2);p T (vr,ned%res2); flag=0;break;}}} int main () {freopen ("dnc.in", "R", stdin); Freopen ("Dnc.out", "w", stdout); Cal (P (0,0), P (2,0), P (2,1)); while (scanf ("%d", &n)!=eof) {for (int i=0;i<n;i++) {int x, y; scanf ("%d%d", &x,&y); A[i]=p (x, y); } vi Ori (n,0); for (int i=0;i<n;i++) ori[i]=i; Sort (Ori.begin (), Ori.end (), CMP); memset (mp,-1,sizeof Mp); DFS (n); for (int i=1;i<=n;i++) printf ("%lld", Mp[i]);p UTS (""); Rep.clear (); LL k;scanf ("%lld", &k); PT (ORI,K); for (int i=0;i<rep.size (); i++) printf ("%d%d\n", rep[i].x,rep[i].y); }}/*a[r]-a[l]<=limask Max ra[r]<=a[l]+lim*/
F. Doubling
Large-scale direct $/2$ structure, small-range DP solution.
#include <cstdio> #include <algorithm> #include <string> #include <iostream>using namespace std ; typedef long long ll;const int n=200010;const int inf=100000000;typedef pair<int,string>p;int n,i,j; P f[111];string cal (int n) { if (n<=100) return f[n].second; if (n&1) return cal (n-1) + "1"; Return "[" +cal (N/2) + "]";} int main () { freopen ("doubling.in", "R", stdin); Freopen ("Doubling.out", "w", stdout); scanf ("%d", &n); F[0]=p (0, ""); F[1]=p (1, "1"); F[2]=p (2, "one"); for (i=3;i<=100;i++) { f[i]=p (INF, ""); for (j=1;j<i;j++) { P x=f[j]; X.first+=f[i-j].first; X.second+=f[i-j].second; F[i]=min (f[i],x); } if (i%2==0) { P x (f[i/2].first+2, "[" +f[i/2].second+ "]"); F[i]=min (f[i],x); } } Cout<<cal (n) <<endl; return 0;} /*a[r]-a[l]<=limask Max ra[r]<=a[l]+lim*/
G. New Collection
Randomly 100 rounds calculate the desired set size, and then find the nearest gap.
#include <bits/stdc++.h>using namespace std; typedef long long LL; typedef pair < int, int > PII; #define Get ID (l, r) L + r | (l! = r) Set < string> s;d ouble magic[7]={10.0,100.0,999.96,6325.61,9517.19,9950.67,9995.01};void Solve () {string c; for (int i = 1; I <= 10000; + + i) {cout << "+" << Endl; fflush (stdout); Cin >> C; s.insert (c);} int x = (int) s.size ();d ouble ret=1e9;int ans;for (int i=0;i<7;i++) { double now=fabs (1.0*x-magic[i]); if (Now<ret) ret=now,ans=i; } int fin=1; for (int i=0;i<=ans;i++) fin*=10; printf ("=%d\n", fin); Fflush (stdout);} int main () {solve (); return 0;}
H. Path or coloring
Greedy dyeing, if you can $k$ dyeing, then good, otherwise there must be a simple path of length $k$.
#include <cstdio> #include <algorithm> #include <string> #include <iostream> #include < Cstdlib>using namespace Std;typedef long long ll;const int n=1010;const int m=20010;const int Inf=100000000;int Case,n, m,k,i,j,x,y,g[n],v[m],nxt[m],ed,vis[n],col[n],pos;inline void Add (int x,int y) {v[++ed]=y;nxt[ed]=g[x];g[x]=ed;} void Dfs (int x) {pos++; for (int i=g[x];i;i=nxt[i]) if (Col[v[i]]) Vis[col[v[i]]]=pos; for (int i=1;; i++) if (vis[i]<pos) {col[x]=i; Break } for (int i=g[x];i;i=nxt[i]) if (!col[v[i]]) DFS (v[i]);} void Solve () {scanf ("%d%d%d", &n,&m,&k); for (ed=0,i=1;i<=n;i++) g[i]=col[i]=0; while (m--) scanf ("%d%d", &x,&y), add (x, y), add (y,x); for (i=1;i<=n;i++) if (!col[i]) DFS (i); BOOL flag=0; for (i=1;i<=n;i++) if (col[i]>k) {flag=1;break;} if (!flag) {printf ("coloring"); for (i=1;i<=n;i++) printf ("%d", col[i]); Return } for (i=1;i<=n;i++) if (col[i]==k+1) {x=i;break;} printf ("path"); for (i=1;i<=k+1;i++) {printf ("%d", x); for (J=g[x];j;j=nxt[j]) if (col[v[j]]==col[x]-1) {x=v[j]; Break }}}int Main () {freopen ("pathorcoloring.in", "R", stdin); Freopen ("Pathorcoloring.out", "w", stdout); scanf ("%d", &case); while (case--) {solve (); Puts (""); }}
I. Double Shuffle
Leave the pit.
J. Timer
Press test instructions to simulate.
#include <bits/stdc++.h>using namespace std; typedef long long LL; typedef pair < int, int > PII; #define CLR (A, X) memset (A, x, sizeof a) #define GETID (L, R) L + r | (l! = r) Const int MAXN = 100005; char s[12][65]; char A[12][MAXN]; char digit[10][8][9] = {{". XXXXX: "," XX. XXX. "," XX. xxxx. "," xxxx. XX. "," XXX. XX. "," XXX. XX. ",". XXXXX ... "," ... ","},{"... XX ... ",". XXX ... ",". XXXX ... "," ... XX ... "," ... XX ... "," ... XX ... ",". XXXXXX. "," ... ","},{". XXXXX ... "," XX ... XX. "," ..... XX. "," ... XXX: ",". xxx ..... "," XX ... "," XXXXXXX. "," ... ","},{". XXXXX ... "," XX ... XX. "," ..... XX. ",". XXXX: "," ..... xx. "," xx ... XX. ",". XXXXX ... "," ... ","},{"... XXX: ",". XXXX: ",". Xx. XX: "," xx.. XX: "," XXXXXXX. "," .... XX: "," ... XXXX. "," ... ","},{"," XXXXXXX "," XX ... "," XXXXXX ... "," ... ". XX. "," ..... xx. "," xx ... XX. ",". XXXXX ... "," ... ","},{". XXXXX ... "," XX ... xx. "," xx ... "," XXXXXX ... "," xx ... xx. "," xx ... XX. ",". XXXXX ... "," ... ",},{" XXXXXXX. "," XX ... XX. "," X ..... XX. "," ..... XX: "," ... XX ... ",". XX ..... ",". XX ..... ","........",},{". XXXXX ... "," XX ... xx. "," xx ... XX. ",". XXXXX ... "," XX ... xx. "," xx ... XX. ",". XXXXX ... "," ... ","},{". XXXXX ... "," XX ... xx. "," xx ... XX. ",". XXXXXX. "," ..... xx. "," xx ... XX. ",". XXXXX: "," ... ","}, "; void paint (int x, int v) {for (int i = 3; i <; + + i) {for (int j = x; j < x + 8; + + j) {A[i][j] = Digit[v][i-3][j-x];}}} void mark (int x) {a[0][x + 3] = a[0][x + 4] = a[1][x + 3] = a[1][x + 4] = ' x ';} int check (int x) {for (int i = 0; i <; + + i) {for (int j = 0; J <; + + j) {if (s[i][j] = = '-') con tinue; if (s[i][j]! = a[i][x + j]) return 0;}} return 1;} void Solve () {for (int i = 0; i <; + + i) {scanf ("%s", S[i]);} int ans = 3470; for (int i = 0;; + + i) {if (check (i)) {printf ("%02d:%02d\n", ans/60, ans); return;} Ans-= 5; if (ans < 0) ans + = 3600;}} void Show (int l, int r) {for (int i = 0; i <; + + i) {for (int j = l; J < R; + + j) {printf ("%c", a[ I][J]);} Puts ("");}} int mAin () {clr (A, '. '); int x = 0; for (int i = 0; i < 50000; i + = + +) {if (x% 5 = = 0) {if (= = = =) {if "X <" Paint (i, x); else {paint (i + 4, x%);p aint (i-4, X/10);}} Mark (i); x = (x-1 + 60)% 60;} Show (0, solve);/*for (int i = 0; I < + + i) {scanf ("%d", &n);p rintf ("{\ n"); for (int j = 0; J < 8; + + j) {scanf ("%s", tmp);p rintf ("\"%s\ "\ n", tmp);} printf ("},\n");} */return 0;}
K. Ultraprime Numbers
The answer is at most only $9$.
#include <cstdio> #include <algorithm> #include <string> #include <iostream>using namespace std ; typedef long long ll;const int n=2010;const int inf=100000000;int n,i,j,p[n/10],tot;bool is[n],v[n];int Q[1111],ans; inline bool Check (int x) {if (!is[x]) return 0; static int f[100]; int n=0; while (x) f[++n]=x%10,x/=10; for (int i=n;i;i--) {int t=0; for (int j=i;j;j--) {t=t*10+f[j]; if (!is[t]) return 0; }} return 1;} int main () {freopen ("ultraprime.in", "R", stdin); Freopen ("Ultraprime.out", "w", stdout); scanf ("%d", &n); n=2000; for (i=2;i<=n;i++) {if (!v[i]) is[i]=1,p[tot++]=i; for (j=0;j<tot;j++) {if (i*p[j]>n) break; V[i*p[j]]=1; if (i%p[j]==0) break; }} for (i=2;i<=n;i++) if (check (i)) {q[++ans]=i; printf ("%d%d\n", ans,i); if (ans==1000) break; } scanf ("%d", &n); if (ans<n) puts ("1"), Else printf ("%d", q[n]); return 0;} /*a[r]-a[l]<=limask Max ra[r]<=a[l]+lim*/
XVII Open Cup named after e.v. Pankratiev. GP of SPb