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上取整. 有无限个变 ...
随机推荐
- ASM磁盘组mount一例
环境信息: oracle10gRAC + RHEL5.8 问题现象: db1服务器crs服务正常,ASM的data磁盘组处于dismount状态.db2集群服务正常. SQL> select ...
- 【译】在ES6中如何优雅的使用Arguments和Parameters
原文地址:how-to-use-arguments-and-parameters-in-ecmascript-6 ES6是最新版本的ECMAScript标准,而且显著的改善了JS里的参数处理.我们现在 ...
- ASCII UNICODE UTF "口水文"
最近接了一个单是需要把非 UTF-8 (No BOM)编码的文件转换成 UTF-8 (No BOM),若此文件是 UTF-8 但带有 BOM ,需要转换成不带 BOM 的.于是开启了一天的阅读.首先花 ...
- codeforces 705C C. Thor(模拟)
题目链接: C. Thor time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- linux命令学习笔记-kill和killall命令详解
*杀死进程最安全的方法是单纯使用kill命令,不加修饰符,不带标志. 首先使用ps -ef命令确定要杀死进程的PID,然后输入以下命令: # kill -pid 注释:标准的kill命令通常都能达到目 ...
- JQuery添加删除标签
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <script src= ...
- android自定义控件(三) 增加内容 自定义属性 format详解
转自 http://www.gisall.com/html/35/160435-5369.html 1. reference:参考某一资源ID. (1)属性定义: <declare-stylea ...
- HDU5875Function(单调队列)
The shorter, the simpler. With this problem, you should be convinced of this truth. You are giv ...
- 【Python】File IO
1.numpy.genfromtxt() numpy.genfromtxt() CSV文件很容易被numpy类库的genfromtxt方法解析 2.
- 【Lintcode】096.Partition List
题目: Given a linked list and a value x, partition it such that all nodes less than x come before node ...