zoukankan      html  css  js  c++  java
  • luoguP3531 [POI2012]LIT-Letters

    (https://www.luogu.org/problem/P3531)

    注意编号

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<vector>
    #include<iostream>
    using namespace std;
    const int MAX = 1001001;
    
    int n;
    long long ans;
    int p[MAX], num[MAX], t[MAX];
    string a,b;
    vector <int> v[27];
    
    int add(int x, int k) {
    	while(x <= n) {
    		t[x] += k;
    		x += x&(-x);
    	}
    }
    
    int query(int x) {
    	int res = 0;
    	while(x) {
    		res += t[x];
    		x -= x&(-x);
    	}
    	return res;
    }
    
    int main() {
    	scanf("%d",&n);
    	cin>>a>>b;
    	for(int i = 0; i < n; i++) v[b[i]-'A'+1].push_back(i+1);//对b编号
    	for(int i = 0; i < n; i++) p[i+1] = v[a[i]-'A'+1][num[a[i]-'A'+1]++];//对a编号 
    //	for(int i = 1; i <= n; i++) printf("%d ", p[i]);
    //裸求逆序对(因为a中字母在b中的位置固定,对于相同的字母,一定是a中第一个位置的 放在 b中第一个位置, 第二个放第二个...)
    	for(int i = 1; i <= n; i++) {
    		add(p[i], 1);
    		ans += (long long)i - query(p[i]);
    	}
    	printf("%lld
    ",ans);
    }
    
  • 相关阅读:
    网线接线分类
    MongoDB修改用户密码
    win10计算器和商店英文改中文
    电脑微信双开
    ajax
    get和post的区别
    javascript中各种继承方式的优缺点
    原型
    高阶函数的封装
    深浅拷贝
  • 原文地址:https://www.cnblogs.com/tyner/p/11544039.html
Copyright © 2011-2022 走看看