Codeforces 1141F2(贪心、预处理)
要点
- 一开始dp然后码力太辣鸡并且算法带假于是调了很久一交还WA在28……
- 吐槽完毕。后来想拿栈优化dp时发现其实完全不需要dp,贪心选取即可,当前的不兼容就干脆不要它了,结果不会变差。然后想要什么就预处理什么即可。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
using namespace std;
const int maxn = 1505;
int n, a[maxn], sum[maxn], ans, val, ID;
int d[maxn * maxn], cnt;
vector<int> v[maxn * maxn];
vector<int> vv[maxn];
unordered_map<int, int> mp, L[maxn];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]), sum[i] = sum[i - 1] + a[i];
for (int i = 1; i <= n; i++) {
for (int j = i - 1; ~j; j--) {
int t = sum[i] - sum[j];
d[++cnt] = t;
if (!L[i][t]) L[i][t] = j + 1, vv[i].push_back(t);
}
}
sort(d + 1, d + 1 + cnt);
cnt = unique(d + 1, d + 1 + cnt) - d - 1;
for (int i = 1; i <= cnt; i++) mp[d[i]] = i;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < vv[i].size(); j++) {
int t = vv[i][j], id = mp[t];
if (v[id].empty() || v[id].back() < L[i][t]) {
v[id].push_back(i);
}
if (ans < v[id].size()) {
ans = v[id].size();
val = t;
ID = id;
}
}
}
printf("%d\n", ans);
for (int i : v[ID]) {
printf("%d %d\n", L[i][val], i);
}
return 0;
}
Codeforces 1141F2(贪心、预处理)的更多相关文章
- CodeForces - 893D 贪心
http://codeforces.com/problemset/problem/893/D 题意 Recenlty Luba有一张信用卡可用,一开始金额为0,每天早上可以去充任意数量的钱.到了晚上, ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 828D) - 贪心
Arkady needs your help again! This time he decided to build his own high-speed Internet exchange poi ...
- Liebig's Barrels CodeForces - 985C (贪心)
链接 大意:给定$nk$块木板, 要制作$n$个$k$块板的桶, 要求任意两桶容积差不超过$l$, 每个桶的容积为最短木板长, 输出$n$个桶的最大容积和 假设最短板长$m$, 显然最后桶的体积都在$ ...
- CodeForces - 93B(贪心+vector<pair<int,double> >+double 的精度操作
题目链接:http://codeforces.com/problemset/problem/93/B B. End of Exams time limit per test 1 second memo ...
- 暴力贪心+预处理自动机——cf990E
枚举每种灯管,然后找到代价最小的那种灯管 贪心策略:灯管从0开始向右放置,如果末尾是不能放置灯管的结点,那么要往回找到最近一个可以放置灯管的结点,在那里放置灯管 所以先预处理每个不能放置灯管的结点对应 ...
- C - Ordering Pizza CodeForces - 867C 贪心 经典
C - Ordering Pizza CodeForces - 867C C - Ordering Pizza 这个是最难的,一个贪心,很经典,但是我不会,早训结束看了题解才知道怎么贪心的. 这个是先 ...
- Codeforces 570C 贪心
题目:http://codeforces.com/contest/570/problem/C 题意:给你一个字符串,由‘.’和小写字母组成.把两个相邻的‘.’替换成一个‘.’,算一次变换.现在给你一些 ...
- Codeforces 732e [贪心][stl乱搞]
/* 不要低头,不要放弃,不要气馁,不要慌张 题意: 给n个插座,m个电脑.每个插座都有一个电压,每个电脑都有需求电压. 每个插座可以接若干变压器,每个变压器可以使得电压变为x/2上取整. 有无限个变 ...
随机推荐
- Zookeeper- Error contacting service. It is probably not running解决方案和原理
搭建启动Zookeeper集群出现Error contacting service. It is probably not running解决方案和原理 1.关闭防火墙 [root@srv01 bi ...
- smack
XMPP/Smack/Openfire javax.net.ssl.SSLException: Received fatal alert: internal_error 解决: 1.在链接openfi ...
- 生成0-42之间的7个不重复的int值
public static void main(String[] args) { //set集合存储不重复无序的值 Set<Integer> set = new HashSet<In ...
- python 之gc(回收机制)--garbage collection(GC垃圾回收)
######################引用计数######################### 引用计数:python 当中一种用来解决垃圾回收的策略之一 char 1个字节(2**8) in ...
- 数据库关键字 (Oracle, SQL Server, DB2)
Oracle SQL Server DB2 ! @@IDENTITY DETERMINISTIC & ADD DISALLOW ( ALL DISCONNECT ) ...
- Day05:装饰器,三元表达式,函数的递归,匿名/内置函数,迭代器,模块,开发目录
上节课复习:1.函数的对象 函数可以被当作数据取处理2.函数嵌套 嵌套调用:在调用一个函数时,函数体代码又调用了其他函数 嵌套定义:在一个函数内部又定义了另一个函数 def foo( ...
- JVM中的Hello World运行机制:
栗子: package zagoo; public class HelloWorld { public static String HELLOWORLD="Hello World" ...
- The Tomcat server configuration at \Servers\Tomcat v8.0 Server at localhost-config is missing. Check the server for erro
解决方案 1.选择Eclipse工具栏中的Windows→Perferences 2.remove已经创建的server 3.选择Add重新添加,选择create anew local server ...
- python3中,pycharm中怎么连接数据库
因为python3现在还不能直接连接数据库,所有如果想连接,就只能通过以下方法: 在APP中的,__init__.py中,添加以下代码就可以: import pymysql pymysql.insta ...
- android apk 防止反编译技术第二篇-运行时修改字节码
上一篇我们讲了apk防止反编译技术中的加壳技术,如果有不明白的可以查看我的上一篇博客http://my.oschina.net/u/2323218/blog/393372.接下来我们将介绍另一种防止a ...