二分$T$​​​​​​​​和$S$​​​​​​​​第一个不同的位置,即需要对于$s$​​​​​​​​,判定是否存在$T[1,s]=S[1,s]$​​​​​​​​且满足条件的$T$​​​​

(注:这里的条件不包含$T$的字典序最小)

显然$T[1,s]$​​已经确定,记其中第$i$​​种字母出现次数为$a_{i}$​​

而对于$T(s,n]$​,只有字典序(尽量大)和字符数量的限制,因此若交换两个字符能增大字典序显然不劣,重复交换这样的字符,不难得到最终字符从前往后是不上升的

设其中第$i$​​种字符有$b_{i}$​​个,即希望依次最大化$b_{26},b_{25},...,b_{1}$​​​​,不妨依次暴力枚举(共$\sigma(k)$​​​种取值),并对后面的部分背包+bitset判定是否可行,即可得到该序列并进而比较与$S$的字典序

这一部分的复杂度为$o(\alpha\sigma(k)\frac{n}{\omega}\log n)$​​(其中$\alpha=26$​​,$\omega=64$​​)

进一步的,由于下一个位置一定不同,那么再之后的部分也仅有字典序(尽量小)和字符数量的限制,可以类似地求出,而下一个位置直接暴力枚举即可

这一部分的复杂度为$o(\alpha^{2}\sigma(k)\frac{n}{\omega})$

综上,(单组数据)复杂度为$o(\alpha\sigma(k)\frac{n}{\omega}\log n+\alpha^{2}\sigma(k)\frac{n}{\omega})$,可以通过​

 1 #include<bits/stdc++.h>
2 using namespace std;
3 #define N 100005
4 #define A 26
5 vector<int>v;
6 bitset<N>bt[A+1];
7 int t,n,m,a[A],b[A];
8 char s[N];
9 bool get_max(int n){
10 memset(b,0,sizeof(b));
11 for(int i=A-1;i>=0;i--){
12 bt[i].reset();
13 for(int j=0;(j<v.size())&&(a[i]<=v[j]);j++){
14 int x=v[j]-a[i];
15 if (x<=n)bt[i]|=(bt[i+1]<<x);
16 }
17 }
18 if (!bt[0][n])return 0;
19 for(int i=0;i<A;i++)
20 for(int j=0;(j<v.size())&&(a[i]<=v[j]);j++){
21 b[i]=v[j]-a[i];
22 if ((b[i]<=n)&&(bt[i+1][n-b[i]])){
23 n-=b[i];
24 break;
25 }
26 }
27 return 1;
28 }
29 bool check(int k){
30 memset(a,0,sizeof(a));
31 for(int i=1;i<=k;i++)a['z'-s[i]]++;
32 if (!get_max(n-k))return 0;
33 for(int i=0;i<A;i++)
34 for(int j=0;j<b[i];j++)
35 if (s[++k]!='z'-i)return s[k]<'z'-i;
36 return 1;
37 }
38 int main(){
39 bt[A][0]=1;
40 scanf("%d",&t);
41 while (t--){
42 scanf("%d%d%s",&n,&m,s+1);
43 v.clear();
44 for(int i=m;i;i--)
45 if (m%i==0)v.push_back(i);
46 v.push_back(0);
47 memset(a,0,sizeof(a));
48 if (!check(0)){
49 printf("-1\n");
50 continue;
51 }
52 int l=0,r=n;
53 while (l<r){
54 int mid=(l+r+1>>1);
55 if (check(mid))l=mid;
56 else r=mid-1;
57 }
58 if (l==n){
59 printf("%s\n",s+1);
60 continue;
61 }
62 memset(a,0,sizeof(a));
63 for(int i=1;i<=l;i++)a[s[i]-'a']++;
64 for(int i=s[l+1]-'a'+1;i<A;i++){
65 a[i]++;
66 if (get_max(n-l-1)){
67 for(int j=1;j<=l;j++)printf("%c",s[j]);
68 printf("%c",i+'a');
69 for(int j=0;j<A;j++)
70 while (b[j]--)printf("%c",j+'a');
71 printf("\n");
72 break;
73 }
74 a[i]--;
75 }
76 }
77 return 0;
78 }

[hdu7044]Fall with Fake Problem的更多相关文章

  1. IOS UITableView分组与索引分区实例

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  2. Problem of saving images in WPF (RenderTargetBitmap)zz

      To save a visual to an image file need to use RenderTargetBitmap, detail is reference to Save and ...

  3. fake gucci outlet perform a couple associated with things in great trust

    Based on my a lot of years of encounter within Taobao, purchase bags must go to the high reputation ...

  4. Google Code Jam 2010 Round 1A Problem A. Rotate

    https://code.google.com/codejam/contest/544101/dashboard#s=p0     Problem In the exciting game of Jo ...

  5. Bag Problem

    Bag Problem Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/131072 K (Java/Others) Total ...

  6. Codeforces Round #310 (Div. 2) B. Case of Fake Numbers 水题

    B. Case of Fake Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  7. HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

    Mr. Frog's Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  8. C#学习日志 day10 -------------- problem statement

    Revision History Date Issue Description Author 15/May/2015 1.0 Finish most of the designed function. ...

  9. 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem D. Grumpy Cat 交互题

    Problem D. Grumpy Cat 题目连接: http://www.codeforces.com/gym/100253 Description This problem is a littl ...

随机推荐

  1. vite首次启动加载慢

    背景 随着vue3的到来,vite开始被各大vue3组件库使用,公司开始一个新项目,准备尝试用vite试一波. 问题发现 当把公司新项目移植到vite后,启动非常快,但发现页渲染时间慢了很多 可以看到 ...

  2. ORA-4031诊断分析

    1.Environment11.2.0.4 RAC 2.Symptomsrac的一节点alert日志一直刷ORA-4031报错,提示shared pool不足,二节点并没有此报错 Sat Oct 09 ...

  3. netty 处理客户端连接

    Netty如何处理连接事件 上文讲了Netty如何绑定端口,现在我们来阅读下netty如何处理connect事件.上文我们说了NioEventLoop启动后不断去调用select的事件,当客户端连接时 ...

  4. 洛谷4630APIO2018铁人两项(圆方树+dp)

    QWQ神仙题啊(据说是今年第一次出现圆方树的地方) 首先根据题目,我们就是求对于每一个路径\((s,t)\)他的贡献就是两个点之间的点数,但是图上问题我并没有办法很好的解决... 这时候考虑圆方树,我 ...

  5. 如何在另一台设备上搭建python接口自动化项目所需要的第三方库

    1.如何将当前项目引用的第三方库导出,在新建项目时,选择New environment using>Virtualenv 2.然后右键选择open in terminal:输入命令:pip fr ...

  6. perl打开读取文件(open)

    在Perl中可以用open或者sysopen函数来打开文件进行操作,这两个函数都需要通过一个文件句柄(即文件指针)来对文件进行读写定位等操作.下面以open函数为例:1:读:open(文件句柄,&qu ...

  7. 一站式交付体验:云效+Kubernetes

    背景 云效依托于阿里巴巴研发效能多年规模化持续交付,赋能云上开发者专为云端用户提供的一站式研发协作平台.Kubernetes,由Google开源的容器集群管理平台,面向运维侧提供自动化的集群和应用管理 ...

  8. VS Code C/C++开发环境配置

    VS Code C/C++开发环境配置 一.安装 ​ 1.前往官网下载安装即可 https://code.visualstudio.com/ ​ 2.进入VS Code安装如下插件 二.C/C++开发 ...

  9. 腾讯bugly产生bug定位行数不准解决方案

    定位不准是因为做了混淆导致行数与实际代码行数不对.解决方案是要上传符号表.下载地址https://bugly.qq.com/v2/downloads 下载好因为我打开文件的说明文件是空文件(可能工作人 ...

  10. 晶振在电路设计时关于负载电容CL大小取值特别需要注意什么?

    在无源晶体的设计中,经常遇到负载电容CL的大小取值.晶振设计与精度的提高.KHz无源晶振的停止.音叉晶体谐振器的精度漂移以及精度和无源晶振在高温下的精度是否等于低温的精度烦忧的问题等. 无源晶体振荡器 ...