CF1141F Same Sum Blocks(easy/hard)
传送门easy
传送门hard
切水题的感觉真好
看到数据范围这么小,所以暴力枚举所有的可能,然后用map+vector存下每种值的区间,然后贪心去选
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<map>
using namespace std;
void read(int &x) {
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=1.5e3+10;
map<int,vector<pair<int,int> > >mp;
int n,a[maxn],s[maxn],w[maxn*maxn],tot,ans,l[maxn],r[maxn],ll[maxn],rr[maxn];
bool solve(int k)
{
int sum=0,las=0;sort(mp[k].begin(),mp[k].end());int now=mp[k].size();
for(rg int i=0;i<now;i++)if(las<mp[k][i].second)las=mp[k][i].first,ll[++sum]=mp[k][i].second,rr[sum]=las;
if(sum>ans)memcpy(l,ll,sizeof l),memcpy(r,rr,sizeof r),ans=sum;
return 0;
}
int main()
{
read(n);
for(rg int i=1;i<=n;i++)read(a[i]),s[i]=s[i-1]+a[i];
for(rg int i=1;i<=n;i++)
for(rg int j=i;j<=n;j++)
w[++tot]=s[j]-s[i-1],mp[w[tot]].push_back(make_pair(j,i));
sort(w+1,w+tot+1);tot=unique(w+1,w+tot+1)-w-1;
for(rg int i=1;i<=tot;i++)if(solve(w[i]))break;
printf("%d\n",ans);
for(rg int i=1;i<=ans;i++)printf("%d %d\n",l[i],r[i]);
}
CF1141F Same Sum Blocks(easy/hard)的更多相关文章
- Codeforces Round #547 (Div. 3) F1/2. Same Sum Blocks (Easy/Hard) (贪心,模拟)
题意:有一长度为\(n\)的数组,求最多的区间和相同的不相交的区间的个数. 题解:我们可以先求一个前缀和,然后第一层循环遍历区间的右端点,第二层循环枚举左端点,用前缀和来\(O(1)\)求出区间和,\ ...
- 1. Two Sum【easy】
1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up ...
- Into Blocks (easy version)
G1 - Into Blocks (easy version) 参考:Codeforces Round #584 - Dasha Code Championship - Elimination Rou ...
- F2. Same Sum Blocks (Hard) 解析(思維、前綴和、貪心)
Codeforce 1141 F2. Same Sum Blocks (Hard) 解析(思維.前綴和.貪心) 今天我們來看看CF1141F2(Hard) 題目連結 題目 給你一個數列\(a\),要你 ...
- 【CF1141F2】Same Sum Blocks
题解:发现可以通过枚举区间将区间和相同的元组记录在一个表中,对于答案来说,在同一个表中的元组的选择才会对答案产生贡献.发现每一个表中都是一个个区间,问题转化成了对于每一个表来说,选择若干个不相交的区间 ...
- 【CF1141F1】Same Sum Blocks
题目大意:给定一个 N 个值组成的序列,求序列中区间和相同的不相交区间段数量的最大值. 题解:设 \(dp[i][j]\) 表示到区间 [i,j] 时,与区间 [i,j] 的区间和相同的不相交区间数量 ...
- [LeetCode] 437. Path Sum III_ Easy tag: DFS
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- leetcode 之 two sum (easy)c++
1.数组的长度 length() .容器vector长度 size() .容器vector vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库. ...
- 1. Two Sum [Array] [Easy]
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
随机推荐
- STM32 Flash Download failed
笔者使用Keil开发环境对STM32L Discovery进行开发,更新一次firmware后就不能连接目标板了,报错Internal command error Flash download fai ...
- linux 下 监控系统运行状况 命令 dstat
推荐读文:https://linux.cn/article-3215-1.html
- 使用C语言解析URL
1. [代码]容易写成自己输入URL,这里测试一个例子 #include <stdio.h>#include <stdlib.h>#include <string ...
- Anomaly Detection for Time Series Data with Deep Learning——本质分类正常和异常的行为,对于检测异常行为,采用预测正常行为方式来做
A sample network anomaly detection project Suppose we wanted to detect network anomalies with the un ...
- python基础-发邮件smtp
先来想下发送邮件需要填写什么,还需要有什么条件1.与邮件服务器建立连接,用户名和密码2.发邮件:发件人,收件人,主题,内容,附件3.发送 使用第三方邮箱发送邮件 #! /usr/bin/env pyt ...
- python基础-正则2
正则函数 Python提供re模块,包含所有正则表达式的功能 由于python的字符串本身也有\转义,所以需要注意: s = "ABC\\-001" 对应的正则表达式应为:'ABC ...
- listen 76
Flavors Fluctuate With Temperature Does an ice-cold drink actually taste better than the same bevera ...
- jQuery 下拉框输入匹配提示选项
做页面输入时,为方便输入和提高用户体验,常用下拉框,当下拉选项数据很多时,也不易找到想要的选项,这时,提供一种下拉框输入匹配提示选项,如下: 图示
- 【Matlab】调试基础
1.matlab 调试子程序 在主程序进入子程序前一句加断点,然后step in,可以进入子程序. 但是直接在子程序里设置断点,运行主程序是不能进入子程序的.
- zk 04之 Zookeeper Api(java)与应用
如何使用 Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储 ...