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 ...
随机推荐
- Codeforces--400A--Inna and Choose Options(模拟水题)
Inna and Choose Options Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:26 ...
- 44. Ext信息提示对话框
转自:https://www.cnblogs.com/glsqh/p/5920500.html Ext.window.MessageBox是一个工具类,他继承自Ext.window.Windoe对象, ...
- mysql的启动和停止
1.检查数据库服务器是否开启:任务管理器-->后台进程-->查看mysqld是否存在.存在说明开启了,反之没开启 2.管理员运行cmd,输入重启指令:net start **(**数据库名 ...
- ACM_lowbit
lowbit Time Limit: 2000/1000ms (Java/Others) Problem Description: long long ans = 0; for(int i = 1; ...
- (转)Vuex简单入门
今天试了一下Vuex,感觉跟Redux的实现思想类似.再此,简单地总结一下. 什么是Vuex 在Vue中,多组件的开发给我们带来了很多的方便,但同时当项目规模变大的时候,多个组件间的数据通信和状态管理 ...
- mysql外键创建失败原因
引用:http://blog.csdn.net/wangpeng047/article/details/19624351 首先,如果和外键相关的几张表中已经插入了数据,可能导致外键插入的失败 在MyS ...
- android计算屏幕dp
首先我们来了解一些基本元素: px:像素,屏幕上的点. dpi:一英寸长的直线上的像素点的数量,即像素密度.标准值是160dp. /*** 正是因为dpi值其代表的特性,所以android项目的资源文 ...
- 编写高质量的js之恰当选用if和switch
switch结构中存在很多限制,存在这些限制的主要目的是提高多重分支结构的执行效率.因此,如果能够使用switch结构,就不要选择if结构. 无论是使用if结构,还是使用switch结构,应该确保下面 ...
- JS高级——歌曲管理
1.将歌曲管理的CURD方法放到原型中 2.在构造函数中,我们只有一个属性是songList,因为音乐库不是共有的,如果将songList放入原型中,任何一个人的一次修改songList,都将把son ...
- jQuery——stop
为什么要停止动画? 对同一个元素,如果拥有一个以上的动画对其加以作用,那么后面的动画会被放入一个动画队列中.动画队列的动画是在其上一个动画完成以后才会执行. 控制两个参数四种情况 1.第一个参数表示后 ...