Magic Pen 6(hdu4648,贪心)

http://acm.hdu.edu.cn/showproblem.php?pid=4648

Magic Pen 6

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 1099    Accepted Submission(s): 387

Problem Description

In HIT, many people have a magic pen. Lilu0355 has a magic pen, darkgt has a magic pen, discover has a magic pen. Recently, Timer also got a magic pen from seniors.

At the end of this term, teacher gives Timer a job to deliver the list of N students who fail the course to dean's office. Most of these students are Timer's friends, and Timer doesn't want to see them fail the course. So, Timer decides to use his magic pen to scratch out consecutive names as much as possible. However, teacher has already calculated the sum of all students' scores module M. Then in order not to let the teacher find anything strange, Timer should keep the sum of the rest of students' scores module M the same.

Plans can never keep pace with changes, Timer is too busy to do this job. Therefore, he turns to you. He needs you to program to "save" these students as much as possible.

Input

There are multiple test cases.

The first line of each case contains two integer N and M, (0< N <= 100000, 0 < M < 10000),then followed by a line consists of N integers a1,a2,...an (-100000000 <= a1,a2,...an <= 100000000) denoting the score of each student.(Strange score? Yes, in great HIT, everything is possible)

Output

For each test case, output the largest number of students you can scratch out

Sample Input

2 3

1 6

3 3

2 3 6

2 5

1 3

 

Sample Output

1

2

0

Hint

The magic pen can be used only once to scratch out consecutive students.

 解析:

 题意:

 找出最长的一个连段段且且和为m的整数倍。

 即sum[j-i]%m==0

 等价于sum[j]%m-sum[i]%m==0;所以找出最大的j和最小的i,利用贪心的思想求解。

171MS 1012K 620 B

*/

//Accept


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include <iostream>
using namespace std;
const int maxn=100010;
int a[maxn],s[maxn];
int vis[maxn];
int main()
{int i,n,m,ans,j;
  while(scanf("%d%d",&n,&m)!=EOF)
  {
  	memset(s,0,sizeof(s));
  	ans=0;
  	for(i=1;i<=n;i++)
  	{
  	 scanf("%d",&a[i]);
  	 s[i]=(s[i-1]+a[i]%m);
  	 //if(s[i]==0&&ans<i)
  	 //ans=i;
}
for(i=0;i<=n;i++)
{
for(j=n;j>i;j--)
{
if((j-i)<=ans)//注意这里的剪枝
 break;
if((s[j]-s[i])%m==0)
{   if(j-i>ans)
     ans=j-i;
break;
}
}
  }
  printf("%d\n",ans);
}return 0;
}
//Wronganswer
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include <iostream>
using namespace std;
const int maxn=100010;
int a[maxn],s[maxn];
int vis[maxn];
int main()
{int i,n,m,ans,t;
  while(scanf("%d%d",&n,&m)!=EOF)
  {
      memset(s,0,sizeof(s));
      ans=0;
      for(i=1;i<=n;i++)
      {
          scanf("%d",&a[i]);
          s[i]=(s[i-1]+a[i]%m)%m;
          if(s[i]==0&&i>ans)
          ans=i;
    }
    for(i=0;i<=n;i++)
    {
        t=s[i];
        int r=n;
        int l=i,k=1;
        int mid;
        while(l<=r)
        {
            mid=(l+r)/2;
            if((s[mid]-t)%m==0)
            {
                k=mid;
                l=mid+1;
            }
            else
            r=mid-1;
        }
       if((k-i)>ans)
        ans=k-i;
    }
    printf("%d\n",ans);
  }
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值