湖南大学第十五届程序设计竞赛(重现赛)
I Algorithm Choosing Mushrooms
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
Baby bear and his good friends are very fond of mushrooms. On this day, they go to 402 mushroom field together. Kuangyeye, the owner of the mushroom field, is very happy to see the children. He is going to give the children some mushrooms, so he takes them to a warehouse.
In the warehouse, there are N baskets in a row, numbered from 1 to N, with some mushrooms in each basket. Baby bear and his friends can take several baskets of mushrooms, but Kuangyeye has several requirements:
·Kuangyeye likes to be neat and tidy. He asks the children to take away only the consecutively numbered baskets when they take the mushrooms. This means that if the children choose the 4th, 5th and 6th baskets of mushrooms, they can't choose the 9th basket unless they also choose the 7th and 8th baskets.
·Kuangyeye likes all of them, so he asks each child to get the same number of mushrooms. This means that the total number of mushrooms the children take away must be P = k * M, where k is an integer and M is the total number of children.
·Kuangyeye made a record of the number of mushrooms in each basket. He asks the children not to put some mushrooms in other baskets or throw them away. This means that when the children take a basket of mushrooms, they must take away all the mushrooms in the basket.
Now given the number of mushrooms in a row of N baskets, please answer:
1. The maximum number of mushrooms that baby bear and his friends can take away.
2. The maximum number of baskets that baby bear and his friends can take away.
Please note that the answers to questions 1 and 2 May not come from the same situation!!!
输入描述:
The input contains a single sample.
The first line of input has two integers N and M (1 <= N,M <= 200000) separated by space, representing the number of baskets and the number of children.
The second line of input has N integer A1,A2,……,AN (1 <= A1,A2,……,AN <= 1e9)separated by space, representing the number of mushrooms in the basket numbered from 1 to N.
输出描述:
Output two Numbers separated by space. The maximum number of mushrooms they could get and the maximum number of baskets they could take. If they can’t meet Kuangyeye's demands, please output “0 0”.
1 //江寒月熙丶 提交的代码https://ac.nowcoder.com/acm/contest/view-submission?submissionId=40729426 2 3 #include <bits/stdc++.h> 4 #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) 5 #define IT set<node>::iterator 6 #define mem(a) memset(a,0,sizeof(a)) 7 using namespace std; 8 typedef long long LL; 9 typedef long long ll; 10 const int maxn=1e6+5; 11 ll a[maxn]; 12 ll start[maxn]; 13 ll sumzz[maxn]; 14 unordered_map<int,int>mp; 15 int main() 16 { 17 #ifdef ONLINE_JUDGE 18 FAST_IO; 19 #endif // ONLINE_JUDGE 20 int n,k; 21 cin>>n>>k; 22 for(int i=1;i<=n;i++) 23 cin>>a[i],sumzz[i]=sumzz[i-1]+a[i]; 24 ll sum=0; 25 ll ans=0; 26 ll maxz=0; 27 for(int i=1;i<=n;i++) 28 { 29 sum=(sum+a[i])%k; 30 if(sum!=0&&start[sum]==0) 31 start[sum]=i; 32 else { 33 ans=max(ans,i-start[sum]); 34 maxz=max(maxz,sumzz[i]-sumzz[start[sum]]); 35 } 36 } 37 cout<<maxz<<' '<<ans<<endl; 38 }