链接:https://ac.nowcoder.com/acm/contest/908/I

题意:

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

思路:

用一个二维数组记录原数组的前缀和数组modm的值。

如果sum[j]%m == sum[i]%m 则(sum[j]-sum[i])%m == 0说明j-(i-1)篮子里的蘑菇是m的倍数。(sum为前缀和数组)

找到%m的每个余数去最左和最右的两个位置。%m为0时只用取最右的。

代码:

#include <bits/stdc++.h>using namespace std;typedef long long LL;
const int MAXN = 2e6 + 10;
const int MOD = 1e9 + 7;
int n, m, k, t;LL fsum[MAXN];
int Po[MAXN][2];int main()
{memset(Po, -1, sizeof(Po));scanf("%d%d", &n ,&m);LL sum = 0;for (int i = 1;i <= n;i++){scanf("%lld", &fsum[i]);fsum[i] += fsum[i-1];}for (int i = 1;i <= n;i++){int lef = fsum[i] % m;if (lef == 0){Po[lef][0] = i;continue;}if (Po[lef][0] == -1)Po[lef][0] = i;elsePo[lef][1] = i;}LL res1 = 0;int res2 = 0;for (int i = 1;i <= m;i++){if (Po[i][1] == -1)continue;res1 = max(res1, fsum[Po[i][1]]-fsum[Po[i][0]]);res2 = max(res2, Po[i][1]-Po[i][0]);}if (Po[0][0] != -1){res1 = max(res1, fsum[Po[0][0]]);res2 = max(res2, Po[0][0]);}printf("%lld %d", res1, res2);return 0;
}

  

转载于:https://www.cnblogs.com/YDDDD/p/10960397.html

I.Algorithm Choosing Mushrooms相关推荐

  1. 湖南大学第十五届程序设计竞赛

    Problem A Oshino Shinobu loves Mr.Donut https://ac.nowcoder.com/acm/contest/908/A 题意: 题解: C++版本一 Pro ...

  2. 三种方法实现PCA降维

    主成分分析,即Principal Component Analysis(PCA),是多元统计中的重要内容,也广泛应用于机器学习和其它领域.它的主要作用是对高维数据进行降维.PCA把原先的n个特征用数目 ...

  3. History of pruning algorithm development and python implementation(finished)

    All the python-implementation for 7 post-pruning Algorithms are here. Table of Decision Trees: name ...

  4. CF372D. Choosing Subtree is Fun

    CF372D. Choosing Subtree is Fun Solution 想了一晚都不会,一觉醒来就悟了QwQQwQQwQ 之前一直想着如何用类似树形DPDPDP的方法求出每一个点的贡献再合并 ...

  5. Educational Codeforces Round 23 E. Choosing The Commander 字典树

    题目连接:E. Choosing The Commander 题意:士兵有个值,指挥官有两个p,l三种操作,第一种添加一个值为p的士兵,第二种删除一个值为p的士兵,(士兵pi只会尊敬pi^p<l ...

  6. FIP: A fast overlapping community-based influence maximization algorithm using probability coefficie

    FIP: A fast overlapping community-based influence maximization algorithm using probability coefficie ...

  7. In Search of an Understandable Consensus Algorithm(寻找可理解的共识算法)

    原文见 Raft Consensus Protocol 题目:In Search of an Understandable Consensus Algorithm 作者:Diego Ongaro an ...

  8. 【Algorithm】 Reservoir Sampling Shuffle an Array

    Reservoir Sampling Example Question: Randomly choosing k samples from a list of n items, where n is ...

  9. 「论文自译」MIT 6.824 In Search of an Understandable Consensus Algorithm (Extended Version)

    文章目录 Abstract 1 - Introduction 2 - Replicated state machines 3 - What's wrong with Paxos? 4 - Design ...

  10. 论文翻译:A Highly Accurate Prediction Algorithm for Unknown Web Service QoS Values

    A Highly Accurate Prediction Algorithm for Unknown Web Service QoS Values 一种用于未知Web服务QoS值的高精度预测算法 Ab ...

最新文章

  1. python librosa 或 ffmpeg 改变音频采样率
  2. Mac-OSX的Python3.5虚拟环境下安装Opencv
  3. shell脚本详解(二)——条件测试、if语句和case分支语句
  4. ant vue 设置中文_Ant Design Vue 添加区分中英文的长度校验功能
  5. MySQL命令行导出数据库
  6. 在WEB中实现打印分页
  7. C++ Primer 第五版 第6章 6.2——函数参数传递习题答案
  8. python3爬虫入门实例_10个python爬虫入门实例(小结)
  9. mpvue 初始化微信小程序
  10. Revit Family API 添加几何实体
  11. 【观点】Ruby优于Python之处(面向hackers)
  12. 使用密码字典暴力破解加密rar、zip压缩文件
  13. 亚马逊便携式小空调冷风机英国站UKCA认证测试标准
  14. javaGUI学习1:GUI框架,工具介绍
  15. MTK G-sensor 1
  16. javaSE基础大全--知识点总结
  17. 自动驾驶仿真测试的意义
  18. 加速媒体业务智能化升级 第四范式发布智能推荐系统先荐
  19. 2.JVM垃圾回收机制-什么时候回收内存
  20. 华为CE交换机堆叠配置

热门文章

  1. 抖音小程序开发教程之 01 构建开发环境(教程含源码)
  2. 啮齿类动物大尺度功能网络
  3. App Store Connect 如何删除应用App
  4. Summarization 文本摘要进展
  5. sql 树形 子节点获取最顶级的节点
  6. 市场细分与目标群体定位
  7. Android 加载进度条,加载成功,数据为空,加载失败,无网络等状态不同界面的切换
  8. Java笔记——08.面向对象(中级)
  9. 编程艺术——软件设计模式SOLID原则
  10. Activiti工作流的流转任务和结束任务