Codeforces Round #649 (Div. 2) A. XXXXX (贪心)
题意:有一个长度为\(n\)的数组,找一段最长子数组,使得其元素和为\(x\),如果存在,输出子数组的长度,否则输出\(-1\).
题解:这题我们要从元素和\(sum\)来考虑,首先,如果原数组的所有元素都被\(x\)整除,那么条件不成立.
假如原数组的\(sum\)不被\(x\)整除,那么长度就为\(n\),如果被\(x\)整除,那么我们贪心来想,从前和从后来找第一个\(a[i]\)%\(x\ne0\)的位置,然后比较求个最长即可.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL; int t;
int n,x;
int a[N]; int main() {
ios::sync_with_stdio(false);cin.tie(0);
cin>>t;
while(t--){
int sum=0;
bool ok=0;
cin>>n>>x;
for(int i=1;i<=n;++i){
cin>>a[i];
sum+=a[i];
if(a[i]%x!=0) ok=1;
}
if(!ok){
cout<<-1<<endl;
continue;
}
if(sum%x!=0){
cout<<n<<endl;
continue;
}
int f=n;
int b=0;
for(int i=1;i<=n;++i){
if(a[i]%x!=0){
f=i;
break;
}
}
for(int i=n;i>=1;--i){
if(a[i]%x!=0){
b=i;
break;
}
}
cout<<max(n-f,b-1)<<endl;
} return 0;
}
Codeforces Round #649 (Div. 2) A. XXXXX (贪心)的更多相关文章
- Codeforces Round #649 (Div. 2) A. XXXXX
题目链接:https://codeforces.com/contest/1364/problem/A 题意 找出大小为 $n$ 的数组 $a$ 的最长连续子数组,其元素和不被 $x$ 整除. 题解 如 ...
- Codeforces Round #649 (Div. 2)
Codeforces Round #649 (Div. 2) -- WKL \(\mathcal{A}\)题: \(\mathrm{XXXXX}\) Greedy implementation *12 ...
- Codeforces Round #649 (Div. 2) C. Ehab and Prefix MEXs (构造,贪心)
题意:有长度为\(n\)的数组\(a\),要求构造一个相同长度的数组\(b\),使得\({b_{1},b_{2},....b_{i}}\)集合中没有出现过的最小的数是\(a_{i}\). 题解:完全可 ...
- Codeforces Round #202 (Div. 1) A. Mafia 贪心
A. Mafia Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/348/problem/A D ...
- Codeforces Round #382 (Div. 2)B. Urbanization 贪心
B. Urbanization 题目链接 http://codeforces.com/contest/735/problem/B 题面 Local authorities have heard a l ...
- Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp
题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...
- Codeforces Round #180 (Div. 2) B. Sail 贪心
B. Sail 题目连接: http://www.codeforces.com/contest/298/problem/B Description The polar bears are going ...
- Codeforces Round #192 (Div. 1) A. Purification 贪心
A. Purification Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/329/probl ...
- Codeforces Round #274 (Div. 1) A. Exams 贪心
A. Exams Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/A Des ...
随机推荐
- Go中由WaitGroup引发对内存对齐思考
转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com 本文使用的go的源码时14.4 WaitGroup使用大家都会,但是其中是怎么实现的我们 ...
- 【Linux】系统打开文件最大数量限制(进程打开的最大文件句柄数设置)
利用ulimit命令可以对资源的可用性进行控制. -H选项和-S选项分别表示对给定资源的硬限制(hard limit)和软限制(soft limit)进行设置. 硬限制(hard limit)一旦被设 ...
- Windows Server 2012 R2远程桌面默认端口修改
修改3389默认端口可使服务器安全性进一步提升,可以避免阻断大部分的恶意暴力密码爆破. 在开始--运行菜单里,输入regedit 或者: 远程登陆服务器选择系统桌面中的"Windows Po ...
- ctfhub技能树—文件上传—.htaccess
首先介绍一下.htaccess(来自百度百科) .htaccess文件(或者"分布式配置文件"),全称是Hypertext Access(超文本入口).提供了针对目录改变配置的方法 ...
- cobalt strike出现连接超时情况解决办法
服务器安装好teamserver服务后,进行连接,此时出现了连接超时的情况 检查方法: 一.检查端口是否正常开启 netstat -an | grep <设置的端口号>centos7可以用 ...
- JVM 判断对象已死,实践验证GC回收
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 提升自身价值有多重要? 经过了风风雨雨,看过了男男女女.时间经过的岁月就没有永恒不变 ...
- px转rem的填坑之路
这是要为一个vue项目做自适应,设计稿是1920*1080的,要适应各种手机.ipad.3840*2160的超大屏,所以就选择了rem,包用的是 postcss-pxtorem 在适配的时候遇到了很多 ...
- 用SAP浏览网页
在SAP里,通过两个类就可以做一个简单的,嵌入sap里的网页.这两个类就是 1. cl_gui_custom_container 这个类是自定义屏幕里用得,也就是画一个container,在这个容器中 ...
- 入门OJ:八中生成树2
题目描述 八中里面有N个建设物,M条边.对于这种要建最小生成树的问题,你应该很熟练了.现在老大决定降低某条边的费用,然后这条边必须要被选中,因为这条路他每天都要走,自然......问选了这条边后是否可 ...
- 06. struts2中指定struts2处理的请求后缀
概述 默认情况下我们都是使用.action后缀访问Action. 其实默认后缀是可以通过常量"struts.action.extension"进行修改的. 我们可以配置Struts ...