洛谷 P3131 [USACO16JAN]子共七Subsequences Summing to Sevens
题目描述
Farmer John's NN cows are standing in a row, as they have a tendency to do from time to time. Each cow is labeled with a distinct integer ID number so FJ can tell them apart. FJ would like to take a photo of a contiguous group of cows but, due to a traumatic childhood incident involving the numbers 1 \ldots 61…6, he only wants to take a picture of a group of cows if their IDs add up to a multiple of 7.
Please help FJ determine the size of the largest group he can photograph.
给你n个数,求一个最长的区间,使得区间和能被7整除
输入输出格式
输入格式:
The first line of input contains NN (1 \leq N \leq 50,0001≤N≤50,000). The next NN
lines each contain the NN integer IDs of the cows (all are in the range
0 \ldots 1,000,0000…1,000,000).
输出格式:
Please output the number of cows in the largest consecutive group whose IDs sum
to a multiple of 7. If no such group exists, output 0.
输入输出样例
说明
In this example, 5+1+6+2+14 = 28.
思路:前缀和+二分答案。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,l,r,mid;
int num[],sum[];
bool judge(){
for(int i=;i<=n-mid;i++)
if((sum[i+mid]-sum[i])%==) return true;
return false;
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&num[i]),sum[i]=sum[i-]+num[i];
l=;r=n;
while(l<=r){
mid=(l+r)/;
if(judge()) l=mid+;
else r=mid-;
}
cout<<l-;
}
80
思路:求出前缀和mod7,然后遍历,如果拥有相同的余数,说明这个区间是可以被7整除的记录。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 50010
using namespace std;
int n;
int pri[],v[];
int a[MAXN],sum[MAXN];
int main(){
scanf("%d",&n);
sum[]=;
for(int i=;i<=n;i++)
scanf("%lld",&a[i]),sum[i]=(sum[i-]+a[i])%;
for(int i=;i<=n;i++){
if(!v[sum[i]])
v[sum[i]]=i,pri[sum[i]]=i;
else pri[sum[i]]=i;
}
int ans=-;
for(int i=;i<;i++){
if(!v[i]) continue;
ans=max(ans,pri[i]-v[i]);
}
printf("%d\n",ans);
}
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 50010
using namespace std;
int n;
int pri[],v[];
int a[MAXN],sum[MAXN];
int main(){
scanf("%d",&n);
sum[]=;
for(int i=;i<=n;i++)
scanf("%lld",&a[i]),sum[i]=(sum[i-]+a[i])%;
for(int i=;i<=n;i++){
if(!v[sum[i]])
v[sum[i]]=i,pri[sum[i]]=i;
else pri[sum[i]]=i;
}
int ans=-;
for(int i=;i<;i++){
if(!v[i]) continue;
ans=max(ans,pri[i]-v[i]);
}
printf("%d\n",ans);
}
洛谷 P3131 [USACO16JAN]子共七Subsequences Summing to Sevens的更多相关文章
- [USACO16JAN]子共七Subsequences Summing to Sevens
[USACO16JAN]子共七Subsequences Summing to Sevensa[i]表示前缀和如果a[i]%7==t&&a[j]%7==t那么a[j]-a[i-1]一定是 ...
- 【洛谷P3131】 【USACO16JAN】子共七
P3131 [USACO16JAN]子共七Subsequences Summing to Sevens 题目描述 Farmer John's cows are standing in a row, a ...
- Subsequences Summing to Sevens
Subsequences Summing to Sevens 题目描述 Farmer John's N cows are standing in a row, as they have a tende ...
- 洛谷 P3131 子共七
看到这一题第一印象就是暴力好打,$O(n^2)$,预计得分$70$分 这明显满足不了啊,我们要用到前缀和. $sum[i]$记录到i的前缀和,区间$[a,b]$的和就是$sum[b]-sum[a-1] ...
- BZOJ 4511 洛谷3131 USACO 16.Jan 七子共
用sum[i]表示前缀和模7的值,若存在i≤j,满足sum[i]==sum[j],则区间(i,j]的和为7的倍数. O(N)扫出sum[0]~sum[6]第一次出现的位置first和最后一次出现的次数 ...
- [洛谷 P3788] 幽幽子吃西瓜
妖梦费了好大的劲为幽幽子准备了一个大西瓜,甚至和兔子铃仙打了一架.现在妖梦闲来无事,就蹲在一旁看幽幽子吃西瓜.西瓜可以看作一个标准的球体,瓜皮是绿色的,瓜瓤是红色的,瓜皮的厚度可视为0.妖梦恰好以正视 ...
- 洛谷 P3133 [USACO16JAN]无线电联系Radio Contact
P3133 [USACO16JAN]无线电联系Radio Contact 题目描述 Farmer John has lost his favorite cow bell, and Bessie the ...
- [Luogu] 子共七
https://www.luogu.org/problemnew/show/P3131 A表示前缀和数组 A[r] - A[l - 1] = 0 (mod 7) 得 A[r] = A[l - 1] ( ...
- 2018.08.17 洛谷P3135 [USACO16JAN]堡哞(前缀和处理)
传送门 有趣的前缀和. 数据范围中的n≤200" role="presentation" style="position: relative;"> ...
随机推荐
- MVC权限验证过滤器
Action属性,权限设定属性 [AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)] ...
- Swift学习笔记(7)--控制流
1.For循环 //1.条件递增 for var index = 0; index < 3; ++index { println("index is \(index)") } ...
- CSUOJ 1541 There is No Alternative
There is No Alternative Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on Aiz ...
- 可靠的UDP连接 & MTU MSS
这个网页里面写了: http://blog.csdn.net/plusboy/article/details/1523308 其可靠性必须由上层应用实现.一般都会采用消息重传来实现其可靠性,采用消息重 ...
- 值得学习的CSS知识
这里零度给大家推荐几个值得学习的CSS技巧,能让你编写网页事半功倍!一.清除默认值 通常 padding 的默认值为 0,background-color 的默认值是 transparent.但是在不 ...
- Impala SQL
不多说,直接上干货! 其实,跟hive差不多,大家可以去参考我写的hive学习概念系列. Impala SQL VS HiveQL 下面是Impala对基础数据类型和扩展数据类型的支持 • 此外,Im ...
- js对象拷贝的方法
对象拷贝的方法是一个难点,尤其是深拷贝.建议把代码都运行下,帮助理解拷贝. 一. json方法 1. 适合情况: JSON对象的深度克隆.方法是先JSON.stringify() 转为json字符 ...
- nice---进程优先级
在当前程序运行优先级基础之上调整指定值得到新的程序运行优先级,用新的程序运行优先级运行命令行"command [arguments...]".优先级的范围为-20 - 19 等40 ...
- pat(A) 2-06. 数列求和(模拟摆竖式相加)
1.链接:http://www.patest.cn/contests/ds/2-06 2.思路:模拟摆竖式相加,因为同样位置上的数字同样,那么同一位上的加法就能够用乘法来表示 3.代码: #inclu ...
- 【Android Studio探索之路系列】之六:Android Studio加入依赖
作者:郭孝星 微博:郭孝星的新浪微博 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells github:https://github.co ...