everyday two problems / 3.1
题意:
给定由小写字母组成的长度为 n 的字符串
将其所有 n * (n + 1) / 2 个子串按字典序排序
输出第 k 个子串
k > (n * (n + 1) / 2) 就输出"No such line."
1 <= n, k <= 1e5
解法:
解法很多(我不会写自动机啊
我用的解法应该算是比较简单
一位一位地去计数来确定当前位的字母
比如一开始O(n)扫描出以a开头的子串有3个
以b开头的子串有4个,而我要输出排名第5的子串
所以这一位一定是'b',后面每一位字母的确定同理
当前位是空格的时候标志着结果输出结束
代码比较简单,可以参考
但是我们要说明一下效率
一开始脑残了误以为是O(26n)
但是其实每次while中运行次数与之前已经输出的子串在原字符串中出现的次数有关
那么我们可以考虑最坏情况,10w个'a'
这时候如果要求输出排名最靠后的子串的话效率已经O(n^2)
还好我们题目有限制 k <= 1e5
所以这种做法的最坏效率其实比较玄学(但是过了就好了233
#include <cstdio>
#include <vector>
#include <cstring> using namespace std; typedef long long ll; vector <int> p[], pp; char str[]; ll cnt[]; int n, m; int main() {
scanf("%s %d", str, &m);
n = strlen(str);
if(1ll * n * (n + ) / < m) {
puts("No such line.");
return ;
}
for(int j, i = ;i < n;i ++) {
j = str[i] - ;
cnt[j] += n - i;
p[j].push_back(i);
}
while() {
int pos;
for(int i = ;i <= ;i ++) {
if(cnt[i] >= m) {
pos = i;
if(!pos) return ;
putchar( + i);
break;
}
else m -= cnt[i];
}
for(int i = ;i <= ;i ++) {
cnt[i] = ;
if(i != pos) p[i].clear();
}
cnt[] = p[pos].size();
for(int k, j, i = ;i < p[pos].size();i ++) {
j = p[pos][i] + ;
if(j >= n) continue;
k = str[j] - ;
cnt[k] += n - j;
if(k == pos) pp.push_back(j);
else p[k].push_back(j);
}
p[pos].clear();
for(int i = ;i < pp.size();i ++)
p[pos].push_back(pp[i]);
pp.clear();
}
}
题意:
给定数组a[2000],-1e9 <= a[i] <= 1e9
最多允许修改 k(k <= n) 个元素
求 max(abs(a[i] - a[i + 1])) 的最小值
解法:
这题是看别人题解做的
猜测正解效率可能n^2
但并没有什么策略能直接由条件得到答案
满足单调,考虑二分,可能效率n^2logn,可以接受
然后这个judge函数就比较神了
考虑dp,f[i]表示先只考虑前 i 个且第 i 个不动的情况下
最少修改多少个能够让前 i 个的 max(abs(a[i] - a[i + 1])) <= mid
转移方程 f[i] = min(f[i], f[j] + i - j - 1)
转移条件是abs(f[i] - f[j]) <= (i - j) * mid
即,保证i j 都不变,(i, j)都改变
要能够使[i, j]任意相邻两个之差 <= mid才能转移
上面我们提到f[i]的意义,只考虑了前 i 个
但我们考虑最优解一定有最后连续 t 个都是要被修改的(0 <= t < n)
所以我们计算完所有 f[i] 之后
在从 1 到 n 扫一遍 f[i] + n - i
是一定能够包含最优解的
当然f[i] + n - i <= k就可以返回true了
#include <cstdio>
typedef long long ll;
int n, k, a[], f[];
int min(int x, int y) {
return x < y ? x : y;
}
int dis(int x, int y) {
if(x < y) return y - x;
return x - y;
}
bool judge(ll d) {
f[] = ;
for(int i = ;i <= n;i ++) {
f[i] = ;
for(int j = i- ;j;j --)
if(dis(a[i], a[j]) <= d * (i - j))
f[i] = min(f[i], f[j] + i - j - );
if(i <= k + ) f[i] = min(f[i], i - );
}
for(int i = ;i <= n;i ++)
if(f[i] + n - i <= k)
return ;
return ;
}
int main() {
scanf("%d %d", &n, &k);
for(int i = ;i <= n;i ++)
scanf("%d", &a[i]);
ll l = , r = 2e9, mid;
while(l <= r) {
mid = (l + r) >> ;
if(judge(mid)) r = mid - ;
else l = mid + ;
}
printf("%I64d", l);
return ;
}
everyday two problems / 3.1的更多相关文章
- everyday two problems / 3.11 - 3.17
7日共14题,因为3.14两题相同,所以实际总共13题 13道题分为难易两部分,没有按照时间顺序排列题目 A.易: 1.覆盖数字的数量 题意: 给出一段从A - B的区间S(A,B为整数) 这段区间内 ...
- (转) [it-ebooks]电子书列表
[it-ebooks]电子书列表 [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...
- Ehcache(2.9.x) - API Developer Guide, Blocking and Self Populating Caches
About Blocking and Self-Populating Caches The net.sf.ehcache.constructs package contains some applie ...
- 228. Summary Ranges (everyday promlems) broken problems
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- List of NP-complete problems
This is a list of some of the more commonly known problems that are NP-complete when expressed as de ...
- Unity性能优化(2)-官方教程Diagnosing performance problems using the Profiler window翻译
本文是Unity官方教程,性能优化系列的第二篇<Diagnosing performance problems using the Profiler window>的简单翻译. 相关文章: ...
- MS SQL错误:SQL Server failed with error code 0xc0000000 to spawn a thread to process a new login or connection. Check the SQL Server error log and the Windows event logs for information about possible related problems
早晨宁波那边的IT人员打电话告知数据库无法访问了.其实我在早晨也发现Ignite监控下的宁波的数据库服务器出现了异常,但是当时正在检查查看其它服务器发过来的各类邮件,还没等到我去确认具体情 ...
- Problems about trees
Problems (1) 给一棵带边权的树,求遍历这棵树(每个节点至少经过一次)再回到起点的最短路程. 答案是显然的:边权之和的两倍. (2)给一棵带边权的树,求遍历这棵树(每个节点至少经过一次)的最 ...
- Problems with MMM for mysql(译文)
Problems with mmm for mysql posted in MySQL by shlomi 原文:http://code.openark.org/blog/mysql/problems ...
随机推荐
- 重装mysql
重装mysql方法. 转自http://blog.sina.com.cn/s/blog_73000beb01012eh4.html 1.删除 mysql 1.1 sudo apt-get autore ...
- P3959 宝藏 状压dp
之前写了一份此题关于模拟退火的方法,现在来补充一下状压dp的方法. 其实直接在dfs中状压比较好想,而且实现也很简单,但是网上有人说这种方法是错的...并不知道哪错了,但是就不写了,找了一个正解. 正 ...
- glances内存分析工具使用
glances -b 以字节为单位显示网络流量 glances 是一个命令行工具包括如下命令选项:-b:显示网络连接速度 Byte/ 秒-B @IP|host :绑定服务器端 IP 地址或者主机名称- ...
- day02_12/12/2016_bean的实例化之定义多个配置方式
- (转)Vue 爬坑之路(三)—— 使用 vue-router 跳转页面
使用 Vue.js 做项目的时候,一个页面是由多个组件构成的,所以在跳转页面的时候,并不适合用传统的 href,于是 vue-router 应运而生. 官方文档: https://router.vue ...
- Echarts 出现不明竖线解决方案
Echarts出现了不明竖线,百思不得其解.去查相应的解决方案也没有找到. 后来自己点来点去,突然感觉像是上一个Echarts遗留的. 然后去Echarts官网看到了 clear()方法,这个方法可以 ...
- [原创]Toolbar setNavigationIcon无效
最近在做一个Toolbar,setNavigationIcon()这个方法一直无效,说什么的都有,什么getSupportActionBar().setNavigationIcon()的,说设置sty ...
- webSocket客服在线交谈
一>用户端 <%@ page language="java" pageEncoding="UTF-8" %><%@ taglib uri ...
- Rsync 传输不需要输入密码
1.背景 1) 一个作为服务器端:VM3(IP: 3.9.8.151) 2) 一个作为客户端:VM2(IP: 3.9.8.157) 3) 服务器端和客户端网络 ...
- 移动web——媒体查询
基本概念 响应式开发在没有媒体查询前,也可以通过js来实现,但是人们基本不会考虑,特别繁琐.在出现了媒体查询,才开始逐渐推广响应式.实际开发中,在时间与金钱充足的情况下还是别做响应式,影响性能,维护麻 ...