hdu 4649 Magic Pen 6 解题报告

hdu  4649  Magic Pen 6    解题报告

Magic Pen 6

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 595    Accepted Submission(s): 230


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 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.
 

Source
 

Recommend
zhuyuanchen520




多校的签到题,但我还是没做出来,不知道怎么优化时间效率,而且对取模也是有点迷糊,今天看了解题报告,说是有暴力+剪枝也能过的,挺棒的,反正我没做出来
正规的解法是DP,而且是DP中的水题;   枚举相同模,然后将相同模情况下最远的两个地址相减就行,这样的话,刨掉的就是0;
思路:“ 做法就是维护一个前序和的模,并把这个下标放到Hash表中,然后在Hash表中找一个下标距离最远的删掉即可
依然要注意的是0的初始化问题,POJ 3274是关于hash的类题的题"  引用http://www.cnblogs.com/lzj-0218/archive/2013/08/06/3241763.html


#include<iostream>
#include<cstdio>
#include<vector>

using namespace std;

int n,m;
int sum[100010];
vector<int> hash[20010];
///做法就是维护一个前序和的模,并把这个下标放到Hash表中,然后在Hash表中找一个下标距离最远的删掉即可
///
///依然要注意的是0的初始化问题,在POJ 3274里面有详解
int main()
{
    while(cin>>n>>m)
    {
        int temp;

        sum[0]=0;
        for(int i=0;i<10010;i++)
           hash[i].clear();///清空
        hash[0].push_back(0);

        for(int i=1;i<=n;i++)
        {
            scanf("%d",&temp);
            sum[i]=(sum[i-1]+temp%m+m)%m;///注意这里的初始化! 注意-3%5=?  不同的语言或者环境可能不一个样
            ///这里要求-3%5
            hash[sum[i]].push_back(i);
        }

        int ans=0;
        for(int i=0;i<m+m;i++)///遍历余数
            if(hash[i].size()>=2)
            {
                int t=hash[i][hash[i].size()-1]-hash[i][0];
                ///i表示余数是i的
                if(t>ans)
                    ans=t;
            }

        cout<<ans<<endl;
    }

    return 0;
}

下面又是一个很巧妙的做法:网址找不到了
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
#define LL __int64
#define INF 10000000
int dp1[10001];
int dp2[10001];
LL p[200001];
LL sum[200001];
int main()
{
    int n,m,i,ans;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(sum,0,sizeof(sum));
        for(i = 0;i < m;i ++)
        {
            dp1[i] = -INF;
            dp2[i] = INF;
        }
        ans = 0;
        for(i = 1;i <= n;i ++)
        {
            scanf("%I64d",&p[i]);
            sum[i] = sum[i-1] + p[i];
            sum[i]%= m;
            if(sum[i] < 0)
            sum[i] += m;
            if(sum[i] == 0) ans = max(ans,i);
            dp1[sum[i]] = max(dp1[sum[i]],i);
            dp2[sum[i]] = min(dp2[sum[i]],i);
        }
        for(i = 1;i < m;i ++)
        {
            if(dp2[i] != INF)
            ans = max(dp1[i]-dp2[i],ans);
        }
        printf("%d\n",ans);
    }
    return 0;
}

下面又是一份经典的代码: http://www.cnblogs.com/zyue/archive/2013/08/06/3241757.html

题意:给定一个序列,问你最多划掉多长的连续序列使得原序列和现序列%m的值相同

解题思路:解这个题有点曲折,c语言中 -3%5 = -3 , 但是题目要求应该是 2,所以在解题的时候出现了问题,这里是有 O(n)的算法的, 利用hash表记录 前i项和 第一次出现某余数的位置,如果这个余数出现过,则可以去除掉 (我们要去掉m的倍数),然后取最长的就可以了。


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <map>
#include <set>

using namespace std;

//#define DEBUG

const int maxn = 100000 + 5;

int dp[maxn];
int hs[maxn];


int main()
{

    int n, m;
    while (~scanf("%d%d", &n, &m))
    {
        memset(hs,-1,sizeof(hs));
        for (int i = 1; i <= n; i++)
            scanf("%d", &dp[i]);
        hs[0] = 0;
        int MAX = 0  ;
        for (int i = 1; i <= n; i++)
        {
            dp[i] += dp[i - 1];
            dp[i] %= m;
            if(dp[i] < 0 )
                dp[i] = m + dp[i];
            if(hs[dp[i]] != -1)
            {
               if(i - hs[dp[i]]  > MAX)
                MAX = i - hs[dp[i]];
            }
            else
            {
               hs[dp[i]] = i;
            }
        }
        printf("%d\n", MAX);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值