Badboy学长:
1 #include<iostream> 2 #include<algorithm> 3 #include<cmath> 4 #include<cstring> 5 #include<string> 6 #include<map> 7 #include<cstdio> 8 using namespace std; 9 10 int main(){ 11 int t,m,n,result; 12 int a[105]; 13 cin>>t; 14 while(t--){ 15 cin>>n>>m; 16 memset(a,0,sizeof(a)); 17 int x; 18 for(int i=1;i<=n;i++){ 19 cin>>a[i]; 20 } 21 a[n+1]=101; 22 if(m>=n) {//如果补提交卡数不小于漏提交天数,就可以将所有漏掉的全部提交; 23 cout<<100<<endl; 24 } 25 else { 26 sort(a+1,a+1+n);//将漏掉的日期从小到大排序; 27 result=a[m+1]-1;//初始最长连续天数设置为补了日期最少的m天; 28 for(int i=m+1;i<=n;i++){ 29 result=max(result,a[i+1]-a[i-m]-1); 30 //将补提交卡用来弥补连续的m天,每次求最大连续更新即可; 31 } 32 cout<<result<<endl; 33 } 34 } 35 36 return 0; 37 }
Yimi学长:
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[105], pre[105]; 4 int main() 5 { 6 int T; 7 cin >> T; 8 while(T--) 9 { 10 int n, m; 11 cin >> n >> m; 12 memset(a, 0, sizeof a); 13 for(int i = 0; i < n; i++) 14 { 15 int x; 16 cin >> x; 17 a[x] = 1; 18 } 19 for(int i = 1; i <= 100; i++) pre[i] = pre[i - 1] + a[i]; 20 int ans = 0; 21 for(int i = 1; i <= 100; i++) 22 { 23 int p = upper_bound(pre, pre + 101, pre[i - 1] + m) - pre; 24 ans = max(ans, p - i); 25 } 26 cout << ans << endl; 27 } 28 return 0; 29 }
提交地址:http://hihocoder.com/problemset/problem/1051