偶然间最糟糕的再会
Description
一天Murphyc從睡夢中醒來,發現自己居然穿越到了彈丸論破的世界。作為原作全成就通關的超高校級Gamer,Murphyc很清楚接下來會發生什么。為了中止Chiaki即將面臨的“學級裁判”,Murphyc溜進了未來機關內部,但是只有持有特定編號的識別卡才能進入機關控制室。
好在,Murphyc其實還有另一重身份----法師學徒!雖然Murphyc手中只有門口警衛的識別卡,但機智的他發現該警衛的識別卡編號只要經過若干次Magic操作便可變為原作CG中某重要角色Chisa的的識別碼。
Magic操作:對于i與j位置的兩個數字你可以消耗|i-j|的魔力值以交換兩個數字的位置。
例如
現在已知警衛的識別碼為一個長度為n的整數序列a,ai<=n,?Chisa的識別碼為長度為n的整數序列b,bi<=n,并且,a、b序列均為[1,n]的全排列之一.為了節約魔力,請問Murphyc至少要消耗多少魔力才能獲得Chisa的識別碼?
?
Input
第一行一個數字T代表測試的組數(T<=20)
對于每組測試,第一行一個數字n代表序列的長度(n<=2e5)
接下來一行有n個數字a1,a2,a3….an(1<=ai<=n)
接下來一行有n個數字b1,b2,b3…bn(1<=bi<=n)
Output
對于每行輸出一個整數
Sample Input
1 4 2 3 4 1 1 3 4 2Sample Output
3C++版本一
題解:
本題改編自Codeforces Round #324 (Div. 2)
http://codeforces.com/contest/584/problem/E
#include<bits/stdc++.h> using namespace std; typedef long long ll; int a[200005],b[200005]; int conv[200005]; int main() {freopen("stdin.txt","r",stdin);freopen("stdout.txt","w",stdout);int zu;scanf("%d",&zu);while(zu--){int n;scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%d",&a[i]),conv[a[i]]=i;for(int i=1;i<=n;i++) scanf("%d",&b[i]);ll ans=0;for(int i=1;i<=n;i++) ans+=abs(i-conv[b[i]]);printf("%lld\n",ans/2);} }C++版本二
題解:
類似題解一
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <algorithm> #include <string.h> #include <cmath> #include <queue> using namespace std; typedef long long ll; const int N=2e5+100; int t,n,m; int a[N],b[N],c[N],w[N]; int main() {scanf("%d",&t);while(t--){scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",&a[i]);w[a[i]]=i;}for(int i=1;i<=n;i++){scanf("%d",&b[i]);c[i]=w[b[i]];}int sum=0;for(int i=1;i<=n;i++){if(i>c[i])sum+=i-c[i];}cout << sum << endl;}//cout << "Hello world!" << endl;return 0; }?
總結
- 上一篇: Bone Collector II
- 下一篇: 你面对以希望为名的绝望微笑