nowcoder 牛客小白月赛12
题目链接https://ac.nowcoder.com/acm/contest/392#question
A-华华听月月唱歌
区间覆盖问题(在点上的覆盖)
1、先对start从小到大排序
2、设s为当前已经覆盖的最右端,对所有包含s点的区域,取最右端的点+1赋予s
ac代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<algorithm> using namespace std; const int maxn = 1e5 + 10; struct node{ int start, end; }a[maxn]; int n, m; bool cmp(node a, node b){ return a.start < b.start; } int main() { cin >> n >> m; for (int i = 0; i < m; i++) cin >> a[i].start >> a[i].end; sort(a,a+m,cmp); int res = 0; int pos = 0,s=1; int tot = 0; while (1){ int t = 0; while (a[pos].start<=s && pos < m) t = max(t, a[pos++].end); s = t + 1; res++; if (s >= n) break; if (pos == m||a[pos].start>s){ cout << "-1" << endl; return 0; } } cout << res << endl; return 0; }
B-华华教月月做数学
python快速幂
ac代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
t = int(input()) for _ in range(t): a, b, c = map(int, input().split()) print(pow(a, b, c))
C++ 卡long long的快速幂 (网上找的,原博客地址https://blog.csdn.net/qq_42217376/article/details/88378436#Java_69)
用 快速幂+快速乘模板
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<cmath> #include<iostream> #include<cstring> #include<algorithm> using namespace std; const int Max_n=100005; typedef long long LL; LL q_mul(LL a,LL b,LL mod){//快速乘 LL ans=0,res=a; while(b){ if(b&1) ans=(ans+res)%mod; res=(res+res)%mod; b>>=1; } return ans; } LL q_pow(LL a,LL b,LL mod){//快速幂 LL ans=1,res=a; while(b){ if(b&1) ans=q_mul(ans,res,mod); res=q_mul(res,res,mod); b>>=1; } return ans; } int main(){ int t; scanf("%d",&t); while(t--){ LL a,b,mod; scanf("%lld%lld%lld",&a,&b,&mod); LL ans=q_pow(a,b,mod); printf("%lld\n",ans); } return 0; }
E-华华给月月准备礼物
二分木棍的长度,可以说是暴力了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> using namespace std; typedef long long ll; const int maxn = 2e5 + 10; int n, k,ans; int a[maxn]; bool check(int mid){ int res = 0; for (int i = 0; i < n; i++) res += a[i] / mid; if (res >= k) return true; return false; } int main() { ios::sync_with_stdio(false); cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; int l = 1, r = 1e9,mid; while (l <= r){ mid = (l + r) >> 1; if (check(mid)) ans = mid, l = mid + 1; else r = mid - 1; } cout << ans << endl; return 0; }
G-华华对月月的忠诚
最大公约数,注意要用long long
ac代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<string> using namespace std; long long gcd(long long x,long long y){ if(y==0) return x; return gcd(y,x%y); } int main() { long long a,b; string n; cin>>a>>b>>n; cout<<gcd(a,b)<<endl; return 0; }
J-月月查华华手机
字符串匹配(可以不连续),序列自动机
ac代码:
#include<bits/stdc++.h> typedef long long ll; typedef unsigned long long ull; using namespace std; const int MAXN=1E6+10; char s[MAXN]; int n,q,lst[26]; int son[MAXN][26]; int main() { scanf("%s",s+1); n=strlen(s+1); for(int i=0;i<26;i++) son[n+1][i]=lst[i]=n+1; for(int i=n;~i;i--) { for(int j=0;j<26;j++) son[i][j]=lst[j]; lst[s[i]-'a']=i; } scanf("%d",&q); while(q--) { scanf("%s",s+1); int x=0; for(int i=1;s[i];i++) x=son[x][s[i]-'a']; if(x==n+1) puts("NO"); else puts("YES"); } }