A:暴力从小到大枚举判断。
#include<bits/stdc++.h> using namespace std; #define ll long long #define inf 1000000010 char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } signed main() { #ifndef ONLINE_JUDGE freopen("a.in","r",stdin); freopen("a.out","w",stdout); #endif int n=read(); for (int i=n;i<=n+10000;i++) { int s=0,x=i; while (x) s+=x%10,x/=10; if (s%4==0) {cout<<i;return 0;} } return 0; //NOTICE LONG LONG!!!!! }
B:显然max-min<=2k时有解,最优解为min+k。
#include<bits/stdc++.h> using namespace std; #define ll long long #define inf 1000000010 #define N 110 char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int q,n,k,a[N]; signed main() { q=read(); while (q--) { n=read(),k=read(); for (int i=1;i<=n;i++) a[i]=read(); int mn=inf,mx=0; for (int i=1;i<=n;i++) mn=min(mn,a[i]),mx=max(mx,a[i]); if (mx-k<=mn+k) cout<<mn+k<<endl; else cout<<-1<<endl; } return 0; //NOTICE LONG LONG!!!!! }
C:一点脑子都不想动可以二分答案。
#include<bits/stdc++.h> using namespace std; #define ll long long #define inf 1000000010 #define N 110 #define int long long char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int q,k,n,a,b; signed main() { q=read(); while (q--) { k=read()-1,n=read(),a=read(),b=read(); if (b*n>k) printf("-1\n"); else { int l=0,r=n,ans=0; while (l<=r) { int mid=l+r>>1; if (a*mid+b*(n-mid)<=k) ans=mid,l=mid+1; else r=mid-1; } printf("%d\n",ans); } } return 0; //NOTICE LONG LONG!!!!! }
D、E:跳过。
F:当只要求选择两个数时,显然选择最大值及最大的不是其因子的数最优。因为假设次大值是最大值的因子,那么次大值<=最大值/2,如果不选择最大值,两个数的和一定不大于最大值。于是考虑枚举最大值,剩下的数按上述方法选择。可以使用set删去因子,复杂度O(nsqrt(n)log(n))但也能跑过。实际上sort后枚举最大值并暴力找去掉因子的最大数复杂度就是O(nsqrt(n))。
#include<bits/stdc++.h> using namespace std; #define ll long long #define inf 1000000010 #define N 200010 char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int q,n,a[N],b[N],t; set<int> f; void del(int x) { f.erase(x); b[++t]=x; } signed main() { q=read(); while (q--) { n=read(); for (int i=1;i<=n;i++) a[i]=read();f.clear(); sort(a+1,a+n+1);n=unique(a+1,a+n+1)-a-1; int ans=0; for (int i=1;i<=n;i++) f.insert(a[i]); for (int i=n;i>=1;i--) { t=0; int s=a[i];f.erase(a[i]); for (int j=1;j*j<=a[i];j++) if (a[i]%j==0) { if (f.find(j)!=f.end()) del(j); if (f.find(a[i]/j)!=f.end()) del(a[i]/j); } if (!f.empty()) { int x=*(--f.end()); for (int j=1;j*j<=x;j++) if (x%j==0) { if (f.find(j)!=f.end()) del(j); if (f.find(x/j)!=f.end()) del(x/j); } s+=x; } if (!f.empty()) s+=*(--f.end()); ans=max(ans,s); for (int j=1;j<=t;j++) f.insert(b[j]); } printf("%d\n",ans); } return 0; //NOTICE LONG LONG!!!!! }
#include<bits/stdc++.h> using namespace std; #define ll long long #define inf 1000000010 #define N 200010 char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int q,n,a[N]; signed main() { q=read(); while (q--) { n=read(); for (int i=1;i<=n;i++) a[i]=read(); sort(a+1,a+n+1);n=unique(a+1,a+n+1)-a-1; int ans=0; for (int i=n;i>=1;i--) { int s=a[i];int x=0; for (int j=i-1;j>=1;j--) if (a[i]%a[j]) {x=j,s+=a[j];break;} for (int j=x-1;j>=1;j--) if (a[i]%a[j]&&a[x]%a[j]) {s+=a[j];break;} ans=max(ans,s); } printf("%d\n",ans); } return 0; //NOTICE LONG LONG!!!!! }
G:求出每种糖果数量以及其中有多少个fi=1。然后从大到小考虑礼物中的糖果数量,将糖果数量足够的糖果种类加入set,取出Σfi最大的。
#include<bits/stdc++.h> using namespace std; #define ll long long #define inf 1000000010 #define N 200010 char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int q,n; struct data { int x,y; bool operator <(const data&a) const { return x<a.x||x==a.x&&y<a.y; } }a[N],c[N]; multiset<int> f; signed main() { q=read(); while (q--) { n=read(); for (int i=1;i<=n;i++) a[i].x=read(),a[i].y=read(); sort(a+1,a+n+1); int m=0; for (int i=1;i<=n;i++) { int t=i; while (t<n&&a[t+1].x==a[i].x) t++; c[++m].x=t-i+1;c[m].y=0; for (int j=i;j<=t;j++) if (a[j].y==1) c[m].y++; i=t; } sort(c+1,c+m+1);reverse(c+1,c+m+1); int cur=0,ans=0,ans2=0;f.clear(); for (int i=n;i>=1;i--) { while (cur<m&&c[cur+1].x>=i) { f.insert(c[++cur].y); } if (!f.empty()) { ans+=i; ans2+=min(i,*(--f.end())); f.erase(--f.end()); } } printf("%d %d\n",ans,ans2); } return 0; //NOTICE LONG LONG!!!!! }
H:建出序列自动机(对每个位置求出每个字符下一次出现位置),然后dp出长度为i的本质不同子序列数量即可。注意数量对k取min。
#include<bits/stdc++.h> using namespace std; #define ll long long #define inf 1000000010 #define N 110 #define int long long char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int n,f[N][N],nxt[N][26]; ll k,ans; char s[N]; signed main() { n=read();ll k;cin>>k; scanf("%s",s+1); ans=k*n; for (int i=0;i<26;i++) nxt[n+1][i]=n+1; for (int i=n;i>=1;i--) { for (int j=0;j<26;j++) nxt[i][j]=nxt[i+1][j]; nxt[i][s[i]-'a']=i; } for (int i=0;i<=n;i++) f[i][1]=1; for (int i=2;i<=n+1;i++) for (int j=n;j>=0;j--) { for (int k=0;k<26;k++) f[j][i]+=f[nxt[j+1][k]][i-1]; f[j][i]=min(f[j][i],k); } for (int i=n+1;i>=1;i--) { if (k>f[0][i]) ans-=(i-1)*f[0][i],k-=f[0][i]; else {ans-=(i-1)*k;k=0;break;} } if (k) cout<<-1;else cout<<ans; return 0; //NOTICE LONG LONG!!!!! }
小号5。result:rank 2 rating +322
来源:https://www.cnblogs.com/Gloid/p/11152048.html