• 【CF】CF1430_E String Reversal_傻题/逆序对


    链接

    codeforces 1430E

    题面

    原文

    You are given a string s

    . You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string.

    Your task is to calculate the minimum number of swaps you have to perform to reverse the given string.

    大意

    每次可以交换相邻的两个字母,问最少多少步可以把该字符串翻转。

    题解

    • 第i个原串中的字母alphab最终会到达第i个翻转串中的字母alphab。
    • 利用这个算一下逆序对数,逆序对数就是答案。

    代码

    #include<bits/stdc++.h>
    #define LL long long
    #define MAXN 1000000
    using namespace std;
    template<typename T> void Read(T &cn)
    {
    	char c; int sig = 1;
    	while(!isdigit(c = getchar())) if(c == '-') sig = 0;
    	if(sig) {cn = c-48; while(isdigit(c = getchar())) cn = cn*10+c-48; }
    	else    {cn = 48-c; while(isdigit(c = getchar())) cn = cn*10+48-c; }
    }
    template<typename T> void Write(T cn)
    {
    	int wei = 0; T cm = 0; int cx = cn%10; cn/=10;
    	if(cn < 0 || cx < 0) {putchar('-'); cn = 0-cn; cx = 0-cx; }
    	while(cn)cm = cm*10+cn%10,cn/=10,wei++;
    	while(wei--)putchar(cm%10+48),cm/=10;
    	putchar(cx+48);
    }
    template<typename T> void WriteL(T cn) {Write(cn); puts(""); }
    template<typename T> void WriteS(T cn) {Write(cn); putchar(' '); }
    template<typename T> void Max(T &cn, T cm) {cn = cn < cm ? cm : cn; }
    template<typename T> void Min(T &cn, T cm) {cn = cn < cm ? cn : cm; }
    struct Szsz{
    	int a[MAXN+1], n;
    	void build(int cn) {n = cn; memset(a,0,sizeof(a)); }
    	void jia(int cn) {for(;cn<=n;cn+=cn&-cn) a[cn]++; }
    	int qiu(int cn) {int guo = 0; for(;cn;cn-=cn&-cn) guo+=a[cn]; return guo; }
    }T;
    char c[MAXN+1];
    int a[MAXN+1];
    int n;
    LL ans;
    int zhan[MAXN+1], zlen;
    void getit(char c[], int &clen)
    {
    	while(!isalpha(c[1] = getchar())); clen = 1;
    	while(isalpha(c[++clen] = getchar())); clen--;
    }
    int main()
    {
    //	freopen(".in","r",stdin);
    //	freopen(".out","w",stdout);
    	Read(n); getit(c, n); ans = 0;
    	for(int i = 'a';i<='z';i++)
    	{
    		zlen = 0;
    		for(int j = 1;j<=n;j++) if(c[j] == i) zhan[++zlen] = j;
    		for(int j = 1;j<=zlen;j++) a[zhan[j]] = n-zhan[zlen-j+1]+1;
    	}
    	T.build(n); ans = 0;
    	for(int i = n;i>=1;i--) ans = ans+T.qiu(a[i]-1), T.jia(a[i]);
    	WriteL(ans);
    	return 0;
    }
    
  • 相关阅读:
    caffe for python (官方翻译)
    实验三、页式地址重定位模拟
    实验二、银行家算法
    实验一:进程调度实验
    植物大战僵尸作弊器源代码(MFC版)
    植物大战僵尸作弊器源代码(控制台版)
    CE寻找游戏基址
    植物大战僵尸内存地址(转)
    Detour的简单使用
    C/S模型之命名管道
  • 原文地址:https://www.cnblogs.com/czyarl/p/13832377.html
Copyright © 2020-2023  润新知