博客
关于我
Codeforces round #649 1364A-XXXX(双指针)
阅读量:319 次
发布时间:2019-03-01

本文共 553 字,大约阅读时间需要 1 分钟。

题目大意:给定一个数x,和长度为n的数组,找出最短的子数组,使得子数组的和不能整除x,不存在这样的子数组输出-1。

题解:先考虑两种极端情况,第一种每个数都可以整除x那输出-1,第二种,原数列的和本就来就不能整除x那输出n。现在考虑第二种情况的一般情况,如果原数列和能整除x,我们必定要从左右两端删除一个数,而且要满足题意,这个数必须是不能整除x的,因为原数列和是可以整除x的,所以要满足数组长度最长,只删一个就行(同时删两个不能整除x的反而会有问题),以此类推,详见代码。

AC代码:

#include 
#include
#include
using namespace std;const int maxn=1e6+5;int a[maxn];int main(){ int t; cin>>t; while(t--){ int n,x; cin>>n>>x; int sum1=0,sum2=0,flag=0; for(int i=1;i<=n;i++){ cin>>a[i]; sum1+=a[i]; if(a[i]%x!=0)flag=1; } sum2=sum1; if(flag==0){ cout<<-1<

 

转载地址:http://cfnt.baihongyu.com/

你可能感兴趣的文章
nmap使用
查看>>
nmap使用实战(附nmap安装包)
查看>>
Nmap哪些想不到的姿势
查看>>
Nmap扫描教程之Nmap基础知识
查看>>
nmap指纹识别要点以及又快又准之方法
查看>>
Nmap渗透测试指南之指纹识别与探测、伺机而动
查看>>
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>