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 ...
随机推荐
- 【Spring】Spring的数据库开发 - 1、Spring JDBC的配置和Spring JdbcTemplate的解析
Spring JDBC 文章目录 Spring JDBC Spring JdbcTemplate的解析 Spring JDBC的配置 简单记录-Java EE企业级应用开发教程(Spring+Spri ...
- 【Linux】rsync中sending incremental file list时间优化
每次使用rsync的时候,前面出现sending incremental file list 这句之后要等待很长时间 查了很多帖子和官方文档后,发现是-c这个选项的问题, -v, --verbose ...
- SDUST数据结构 - chap2 线性表
一.判断题: 二.选择题: 三.编程题: 7-1 jmu-ds-顺序表区间元素删除 : 输入样例: 10 5 1 9 10 67 12 8 33 6 2 3 10 输出样例: 1 67 12 33 2 ...
- Linux—curl命令讲解
命令:curl 在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具.它支持文件的上传和下载,是综合传输工具,但按传统,习惯称url为下载工具 ...
- pandas数据分析API常用操作
1.导入数据 df = pd.read_csv( # 该参数为数据在电脑中的路径,可以不填写 filepath_or_buffer='/Users/Weidu/Desktop/sz000002.csv ...
- 未使用绑定变量对share_pool的影响
oracle SGA中包含数据高速缓冲,重做日志缓冲,以及共享池(share_pool).共享池中包含库高速缓冲(所有的SQL,执行计划等)和数据字典缓冲(对象的定义,权限等). 所以,如果SQL中没 ...
- ABAP中SQL语句,指定索引(oracle)
①常用的两种方法: 1.指定使用全表扫描:%_HINTS ORACLE 'FULL(table_name)' 表示扫描整个表 2.指定索引:%_HINTS ORACLE 'INDEX("ta ...
- USB充电限流芯片,输出短路关闭,过压关闭
PW1503,PW1502是超低RDS(ON)开关,具有可编程的电流限制,以保护电源源于过电流和短路保护.它具有超温保护以及反向闭锁功能. PW1503,PW1502采用薄型(1毫米)5针薄型SOT2 ...
- 转 Fiddler3 使用技巧
Fiddler3 使用技巧 文章转自:https://www.cnblogs.com/zhengna/category/1466001.html 1.Fiddler抓不到包怎么解决 (1)先确定是H ...
- Vue基础之用插值表达式在视图区显示数据
Vue基础之用插值表达式在视图区显示数据 第一步:当然就是你要引入Vue.js这个脚本文件啦! <script src="https://cdn.jsdelivr.net/npm/vu ...