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 ...
随机推荐
- bzoj3771
http://www.lydsy.com/JudgeOnline/problem.php?id=3771 生成函数... 其实就是多项式乘法...lrj书上有一个通俗的解释... 然后就是这个样子,我 ...
- hadoop-Combiner作用用法
文章来源http://blog.csdn.net/ipolaris/article/details/8723782 reduce的输入每个key所对应的value将是一大串1,但处理的文本很多时,这一 ...
- Linux学习大纲
- ecshop的一些东西,来看看
\includes\lib_commom.php =>公用函数库 \includes\lib_main.php =>前台公用函数库 \includes\lib_init.php => ...
- python常见的加密方式
1.前言 我们所说的加密方式都是对二进制编码的格式进行加密,对应到python中,则是我妈们的bytes. 所以当我们在Python中进行加密操作的时候,要确保我们的操作是bytes,否则就会报错. ...
- 题解报告:hdu 1285 确定比赛名次
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 Problem Description 有N个比赛队(1<=N<=500),编号依次 ...
- 加密解密Url字符串,C#对Url进行处理,传递Url
string _QueryStringKey = "abcdefgh"; //URL传输参数加密Key /// 加密URL传输的字符串 public string E ...
- 搭建本地wordpress
1.首先,下载xampp,安装按默认勾选即可. 2.安装完成后,启动Apache和MySQL这两个服务. 启动后变成绿色,表示启动成功. 3.点击MySQL项的Admin进入数据库后台. 4.点击用户 ...
- Android使用的webcview中带有音乐播放控件,在关闭或分享时处于界面不可见状态下,声音仍在播放的问题解决
一. 问题出现原因 我们在做APP分享时,分享webview加载带有音乐播放控件的网页.当弹出分享界面,webview的网页处于后台状态或关闭该网页时,音乐声仍在播放.出现该类现象使我 ...
- 使用adb命令提示端口被占用
图是我的65535端口被占用了,一般adb默认使用的是5037端口##方式一 5037为adb默认端口,若5037端口被占用,查看占用端口的进程(使用命令netstat -aon|findstr ...