题解:CF217E Alien DNA
题目链接:洛谷 Codeforces
更差的阅读体验
分享一个比较不需要脑子的做法。
思路
因为只需要输出前
这样就得到了每个操作最终的位置。取前
这个问题就是块状链表的板子了。不过没必要真的去写一个又臭又长的块状链表,可以用
如果你不会
::::info[操作]
-
引用、声明
#include <ext/rope> #include<bits/extc++.h>//可以不用上面那个,直接把 std 换成 ext using namespace __gnu_cxx;//引用命名空间 rope<char> c1;//定义一个 char 类型的 rope crope c2;//这个和上面是等价的 -
string 转 rope
string s="114514"; crope c=s.c_str(); -
插入子串
crope c="abbba"; string s="cac"; c.insert(3,s.c_str());//从 3 位置插入 s cout<<c;//输出为 "abbcacba" -
截取子串
crope c1="1919810"; crope c2=c1.substr(2,3);//从 c1 的下标 2 开始截取 3 个字符 cout<<c2;//输出为 "198"
| 然后还有迭代器遍历的操作,和其他容器是类似的,这里就不列举了。 |
|---|
最后分析一下复杂度:
代码
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_cxx;
#define pii pair<int,int>
#define ll long long
const int M=5e3+5;
int n,k,m,sz[M];
pii q[M];
vector<pii > a;
string s;
signed main()
{
cin>>s;
n=s.size();
scanf("%d%d",&k,&m);
a.push_back({n,0});
for(int i=1;i<=m;i++)
{
scanf("%d%d",&q[i].first,&q[i].second);
int d=q[i].second-q[i].first+1;
int res=q[i].second;
for(auto it=a.begin();it!=a.end();it++)
{
if(it->first>=res)
{
pii v=*it;
it->first=res;
v.first-=res;
it++;
it=a.insert(it,{d,i});
if(v.first)
a.insert(next(it),v);
break;
}
res-=it->first;
}
}
int res=k;
for(auto it=a.begin();it!=a.end();it++)
{
sz[it->second]+=min(res,it->first);
if(it->first>=res)
break;
res-=it->first;
}
crope c=s.c_str();
crope cc=c.substr(0);
for(int i=1;i<=m;i++)
if(sz[i])
{
int d=q[i].second-q[i].first+1;
int d1=min(sz[i],d/2);
int d2=sz[i]-d1;
string add;
crope nw=c.substr(q[i].first-1,max(d1,d2)*2);
auto p=nw.begin();
for(auto it=next(p);d1--;it+=2)
add+=*it;
for(auto it=p;d2--;it+=2)
add+=*it;
c.insert(q[i].second,add.c_str());
}
cout<<c.substr(0,k);
return 0;
}