Educational Codeforces Round 96 E. String Reversa 线段树模拟序列交换
传送门
文章目录
- 题意:
- 思路:
题意:
思路:
与上一篇题解大同小异,无非就是不需要枚举排列了。
// Problem: E. String Reversal
// Contest: Codeforces - Educational Codeforces Round 96 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1430/problem/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;string s,ps;
vector<int>v[28];
struct Node {int l,r;LL cnt,lazy;
}tr[N<<2];void pushdown(int u) {if(!tr[u].lazy) return;LL lazy=tr[u].lazy; tr[u].lazy=0;tr[L].lazy+=lazy; tr[R].lazy+=lazy;tr[L].cnt+=lazy; tr[R].cnt+=lazy;
}void build(int u,int l,int r) {tr[u]={l,r,1,0};if(l==r) {tr[u].cnt=l;return;}build(L,l,Mid); build(R,Mid+1,r);
} void change(int u,int l,int r,int c) {if(tr[u].l>=l&&tr[u].r<=r) {tr[u].cnt+=c;tr[u].lazy+=c;return;}pushdown(u);if(l<=Mid) change(L,l,r,c);if(r>Mid) change(R,l,r,c);
}int query(int u,int l,int r) {if(tr[u].l>=l&&tr[u].r<=r) return tr[u].cnt;int ans=0;pushdown(u);if(l<=Mid) ans+=query(L,l,r);if(r>Mid) ans+=query(R,l,r);return ans;
}LL check() {LL ans=0;build(1,1,ps.length());for(int i=0;i<s.length();i++) {int pos=pos=v[s[i]-'a'].back(); v[s[i]-'a'].pop_back();LL now=query(1,pos,pos);ans+=abs(now-(i+1));change(1,1,pos,1);}return ans;
}int main()
{ios::sync_with_stdio(false);cin.tie(0);int _=1;while(_--) {int n; cin>>n;cin>>ps; s=ps; reverse(ps.begin(),ps.end());for(int i=ps.length()-1;i>=0;i--) {v[ps[i]-'a'].pb(i+1);}printf("%lld\n",check());}return 0;
}
/**/
Educational Codeforces Round 96 E. String Reversa 线段树模拟序列交换相关推荐
- CodeForces - 1430E String Reversal(线段树+模拟)
题目链接:点击查看 题目大意:给出一个字符串 sss ,令其反转的串为 ttt ,每次操作可以将 ttt 中的两个相邻位置的字符交换,问最少需要进行多少次操作才能使得 ttt 变成 sss 题目分析: ...
- CodeForces - 504B Misha and Permutations Summation(线段树模拟康托展开与逆展开)
题目链接:点击查看 题目大意:给出两个排列 ppp 和 qqq,现在要求输出 Perm((Ord(p)+Ord(q))modn!)Perm((Ord(p)+Ord(q)) \bmod n!) Perm ...
- Educational Codeforces Round 112 E.Boring Segments-线段树+双指针
https://codeforces.com/contest/1555/problem/E 这场的e好像比较简单. 题目大意,给你n个线段,最大点是m,每一个线段有一个权值w,你能选择线段来覆盖1-m ...
- Educational Codeforces Round 96 (Rated for Div. 2)
今天先补了上一场的Codeforces Global Round 11三道题,做的心神恍惚,然后17点报名没敢提交,先写了4个题剩下的改天补一补 我是真的服信号,卷积卷si我了 A - Number ...
- C. Numbers on Whiteboard(模拟+贪心) Educational Codeforces Round 96 (Rated for Div. 2)
原题链接: https://codeforces.com/contest/1430/problem/C 测试样例 input 1 4 output 2 2 4 3 3 3 1 题意: 给定一个 1 1 ...
- Educational Codeforces Round 96 (Rated for Div. 2) C. Numbers on Whiteboard///思维
cf地址 题目大意:给一个数n,有1~n的数,每次现在两个数a,b,将这两个数去掉,然后添加一个(a+b)/2的数(向上取整),进行n-1次操作后,问你最后剩下的数最小是多少. 思路:最小的数必定为2 ...
- Educational Codeforces Round 96 (Rated for Div. 2) ABCD
很久不写题解了 因为最近一直在刷acwing和kuangbin的专题 acwing题解直接在acwing上传了 而kuangbin做了几个半个专题 完整了会写题解的 因为身体原因最近总是眼睛疼所以很少 ...
- Educational Codeforces Round 96 (Rated for Div. 2) C. Numbers on Whiteboard(构造)
C. Numbers on Whiteboard 题意: 给你一个排列1-n,每次可以选择两个数,( ⌈ a + b 2 ⌉ \lceil \frac{a+b}{2} \rceil ⌈2a+b⌉)进 ...
- Educational Codeforces Round 96 C. Numbers on Whiteboard
题意: 给你 1 . 2 . 3... n 1.2.3...n 1.2.3...n一共 n n n个数,每次操作可以选择两个数 a , b a,b a,b拿出,再放入 ( a + b ) / 2 (a ...
最新文章
- Ruby的Singleton method
- Dottext.Web.UI.Handlers.BlogExistingPageHandler
- [react] React组件的构造函数有什么作用?
- Linux学习笔记:Linux常用命令操作
- Dubbo(RPC原理、Dubbo架构负载均衡配置方式)(1)
- php替换局部大小写字母,php替换字符串中的一些字符(区分大小写)的函数str_replace()...
- hnu暑期实训之487-3279 字符串处理
- mysql数据备份数据解决方案_MYSQL数据备份解决方案
- win10 查看系统运行时间
- woocommerce产品选项描述修改_简历修改服务:中文修改、英文修改、中英互译、简历定制,名师一对一指导修改!...
- SpringCloud 微服务 (七) 服务通信 Feign
- USB接口ID卡读卡器oem软件(vb源代码)
- CSS3的clac 没有效果,警告提示“invalid property value”
- 计算机如何增加c盘容量,怎么给c盘增加空间 c盘增加空间步骤【图文】
- 旧苹果短信导入新苹果手机上,iphone短信迁移
- 服务器怎么建ip网站,云服务器搭建网站ip
- android 文件浏览器源码,android 文件管理器源码
- 理化计算程序必备:CoolProp+UnitsNet
- python随机选择一个幸运观众_从十名观众中随机选取8名幸运观众,不能重复选取同一个观众为幸运观众(CPrimerPlus第十六章第五题)...
- 运算放大器---功耗(Iq需求)
热门文章
- mysql 权重 取值_mysql如何按权重查询数据啊?
- 有生之年必看!原来历史还可以这样震撼,看完我惊呆了...
- 暴击!被初中生碾压智商!这份被国家数学集训队采用的初中奥数资料究竟有多厉害?...
- 这哥们到底是应聘的还是来收购公司的?| 今日趣图
- π!到底蕴藏了多少不为人知的秘密?|今日最佳
- 有人问我:AI这么火,要不要去追赶AI的热潮?
- ubuntu 新增mysql用户_Ubuntu中给mysql添加新用户并分配权限
- sql连接远程服务器索引超出了_手机怎么连接服务器远程桌面?RD client远程桌面使用教程...
- vc 控制台添加托盘显示_开源:ESP8266读DHT11温湿度,小程序实时显示
- 欧文分校的计算机科学博士,UCI的CS「加州大学欧文分校计算机科学系」