LeetCode: Reverse Nodes in k-Group [024]

题目链接: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): 1857    Accepted Submission(s): 637

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.

Source

2013 Multi-University Training Contest 5

Recommend

zhuyuanchen520   |   We have carefully selected several similar problems for you:  4822 4821 4820 4819 4818

简单题,len从大到小,(sum[j+len]-sum[j])%M==0 len即为所求。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAX=100010;
int sum[MAX];
int main()
{
    int N,M;
    while(cin>>N>>M)
    {
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=N;i++)
        {
            int x;
            scanf("%d",&x);
            sum[i]=sum[i-1]+x;
        }
        bool flag=true;
        for(int i=N;i>=0&&flag;i--)
        {
            for(int j=0;j+i<=N;j++)
            {
                if((sum[j+i]-sum[j])%M==0)
                {
                    cout<<i<<endl;
                    flag=false;
                    break;
                }
            }
        }
    }
    return 0;
}

LeetCode: Reverse Nodes in k-Group [024]

时间: 2024-11-03 22:30:56

LeetCode: Reverse Nodes in k-Group [024]的相关文章

Leetcode Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod

[LeetCode] Reverse Nodes in k-Group 每k个一组翻转链表

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod

LeetCode Reverse Nodes in k-Group 每k个节点为一组,反置链表

题意:给一个单链表,每k个节点就将这k个节点反置,若节点数不是k的倍数,则后面不够k个的这一小段链表不必反置. 思路:递归法.每次递归就将k个节点反置,将k个之后的链表头递归下去解决.利用原来的函数接口即可,不用重新定义. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NUL

[leetcode]Reverse Nodes in k-Group @ Python

原题地址:https://oj.leetcode.com/problems/reverse-nodes-in-k-group/ 题意: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should

LeetCode——Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod

LeetCode: Reverse Nodes in k-Group 解题报告

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod

(Java) LeetCode 25. Reverse Nodes in k-Group —— k个一组翻转链表

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in

[LeetCode] 25. Reverse Nodes in k-Group 每k个一组翻转链表

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in

leetCode 25.Reverse Nodes in k-Group (以k个节点为一组反转链表) 解题思路和方法

Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the valu