洛谷 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;"> ...
随机推荐
- oracle基础入门(一)
一:Oracle的安装 我所知的Oracle的数据库有 10g,11g, 12g, 我们常用的是10g ,因为10g有scott的用户,而11和12现在还没有(哪位大神看到有问题可以指出哦), ...
- CSUOJ 1555 Inversion Sequence
1555: Inversion Sequence Time Limit: 2 Sec Memory Limit: 256 MBSubmit: 107 Solved: 34 Description ...
- jsapi微信支付v3版
请看清楚你的微信支付是v2还是v3.在这里整理的是v3的,v2的同学请忽略! 前期准备须要用的是商户证书,用的是p12的.设置api密钥(在微信商户端中设置),还须要在微信公众号中设置jsapi授权文 ...
- java.lang.IllegalArgumentException: The observer is null.终于解决方式
java.lang.IllegalArgumentException: The observer is null.终于解决方式 在使用数据适配的时候的问题: java.lang.IllegalArgu ...
- 谈谈 .NET Reflector
著名的 .NET Reflector 如今要收费了,价格还不低: .NET Reflector Standard: $95 .NET Reflector VS: $195 .NET Reflector ...
- UI_搭建MVC
新建RootViewController 继承于 UIViewController 新建RootView 继承于 UIView AppDelegate.m 中引入 #import "Root ...
- uva103 - Stacking Boxes(DAG)
题目:uva103 - Stacking Boxes(DAG) 题目大意:给出N个boxes, 而且给出这些箱子的维度.要求找一个最长的序列.可以使得以下的箱子一定可以有个维度序列大于上面的那个箱子的 ...
- SQL查询表中的用那些索引
方法1. 使用系统表 -- 查询一个表中的索引及索引列 USE AdventureWorks2008 GO SELECT indexname = a.name , tablename = c. n ...
- css3--简单的加载动画
.load-container { width: 30%; height: auto; position: relative; margin: 1rem auto; } .load { width: ...
- 今日SGU 5.7
SGU 169 题意:求k位数里面有多少个是完美数,完美数的定义就是n是好数,n+1也是好数,那么n就是完美数,好数就是n%p(n)==0&&p(n)!=0,p(n)=a1*...*a ...