【HDU- 4648】 Magic Pen 6 【思维 】
来源:互联网 发布:同花顺云计算怎么样 编辑:程序博客网 时间:2024/06/06 05:37
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 a 1,a 2,…a n (-100000000 <= a 1,a 2,…a n <= 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.
题意 : 一段n长度的序列,所有元素的和%m==k,现在想找到一段连续的子序列,将其去掉,剩下的部分的和%m==k。求最长的这样
序列的长度。
分析:
可以想到如果一段连续的子序列的和%m==0,则符合题意了。
即a[i]+a[i+2]+a[i+3]+…a[j] % m == 0
转化一下 (sum[j]-sum[i-1] ) %m ==0
sum[j]%m - sum[i-1]%m = 0 。
这个时候我们会发现, 我们只要找到距离最远的取模余数一样的端点 L 和 R 就行了。
代码
#include<vector>#include<cstdio>using namespace std;const int MAXN =10000+11;const int MAXM = 1e5+11;const int mod = 1e9+7;const int inf = 0x3f3f3f3f;int sum[MAXM];vector<int>Hash[MAXN+11];int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF){ for(int i=0;i<MAXN;i++) Hash[i].clear(); Hash[0].push_back(0); // 注意 :: 第一次出现取模为0的是第0个位置,即一个元素都不留的情况 int a; sum[0]=0; for(int i=1;i<=n;i++) { scanf("%d",&a); sum[i]=(sum[i-1]+a%m+m)%m; Hash[sum[i]].push_back(i); } int ans=0; for(int i=0;i<MAXN;i++){ if(Hash[i].size()>1) { int t=Hash[i][Hash[i].size()-1]-Hash[i][0]; if(ans<t) ans=t; } } printf("%d\n",ans); } return 0;}
分析二
暴力居然也能够过。
#include<bits/stdc++.h>using namespace std;typedef pair<int,int>pii;#define first fi#define second se#define LL long long#define fread() freopen("in.txt","r",stdin)#define fwrite() freopen("out.txt","w",stdout)#define CLOSE() ios_base::sync_with_stdio(false)const int MAXN = 1e6;const int MAXM = 1e6;const int mod = 1e9+7;const int inf = 0x3f3f3f3f;LL arr[MAXN];LL sum[MAXN];int main(){ CLOSE();// fread();// fwrite(); LL n,m; while(scanf("%lld%lld",&n,&m)!=EOF){ sum[0]=0; for(int i=1;i<=n;i++){ scanf("%lld",&arr[i]); sum[i]=sum[i-1]+arr[i]; } int ans=0; int flag=0; for(int i=n;i>=1;i--){ // 要倒序遍历,这样才是最优的 遍历区间长度i for(int j=1;j+i-1<=n;j++){// 区间为 j - j+i-1 if((sum[j+i-1]-sum[j-1])% m ==0) { //puts("dfsd "); flag=1; ans=i; break; } } if(flag) break; } printf("%d\n",ans); } return 0;}
- 【HDU- 4648】 Magic Pen 6 【思维 】
- hdu 4648 Magic Pen 6
- HDU 4648 Magic Pen 6
- hdu - 4648 - Magic Pen 6
- hdu 4648 Magic Pen 6
- HDU 4648 Magic Pen 6
- HDU 4648 Magic Pen 6
- hdu 4648Magic Pen 6
- HDU 4648 Magic Pen 6
- hdu 4648 Magic Pen
- HDU 4648 magic pen
- hdu 4648 - Magic Pen 6(“水”题)
- hdu 4648 Magic Pen 6(贪心)
- 2013 多校第五场 hdu 4648 Magic Pen 6
- hdu 4648 Magic Pen 6 多校的一个题目
- hdoj 4648 Magic Pen 6
- hdu 4649 Magic Pen 6 解题报告
- HDOJ 题目4648 Magic Pen 6(水题)
- 字符数组和字符串,char 和string定义的字符串的区别与遍历
- Python与机器学习之模型结构(生成学习算法)
- Net程序员Linux简单学习方法
- 6.18.3
- Socket send函数和recv函数详解
- 【HDU- 4648】 Magic Pen 6 【思维 】
- Oracle12c版本中未归档隐藏参数
- 信用卡评分模型(R语言)(转载)
- 1033. 旧键盘打字(20)
- 常见英文缩写及相应中文
- Android EditText 属性汇总
- 无锁数据结构一
- VC++ DLL 导出函数
- 46 个非常有用的 PHP 代码片段