hihocoder 1388 &&2016 ACM/ICPC Asia Regional Beijing Online Periodic Signal
#1388 : Periodic Signal
描述
Profess X is an expert in signal processing. He has a device which can send a particular 1 second signal repeatedly. The signal is A0 ... An-1 under n Hz sampling.
One day, the device fell on the ground accidentally. Profess X wanted to check whether the device can still work properly. So he ran another n Hz sampling to the fallen device and got B0 ... Bn-1.
To compare two periodic signals, Profess X define the DIFFERENCE of signal A and B as follow:
You may assume that two signals are the same if their DIFFERENCE is small enough.
Profess X is too busy to calculate this value. So the calculation is on you.
输入
The first line contains a single integer T, indicating the number of test cases.
In each test case, the first line contains an integer n. The second line contains n integers, A0 ... An-1. The third line contains n integers, B0 ... Bn-1.
T≤40 including several small test cases and no more than 4 large test cases.
For small test cases, 0<n≤6⋅103.
For large test cases, 0<n≤6⋅104.
For all test cases, 0≤Ai,Bi<220.
输出
For each test case, print the answer in a single line.
- 样例输入
-
2 9 3 0 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 5 1 2 3 4 5 2 3 4 5 1
- 样例输出
-
80 0
这题啊,我觉得暴力可做,刚开始超时了,又做了一点优化还是不行啊。
然后我觉得这个k的取值,和排完序的前m项有很大的关系,然后取m在不超时和wa的范围之间。。。这个看运气,竟然AC了
我的思路是排序a, b数组,按住其中一个数组不动,在前m个数内,用a1的下标减去b的下标,取一个得到k的最大值就好
正规做法竟然是fft。。不会啊
我的方法是歪门邪道。。看看就好,不要采纳1 #include <iostream> 2 #include <sstream> 3 #include <fstream> 4 #include <string> 5 #include <vector> 6 #include <deque> 7 #include <queue> 8 #include <stack> 9 #include <set> 10 #include <map> 11 #include <algorithm> 12 #include <functional> 13 #include <utility> 14 #include <bitset> 15 #include <cmath> 16 #include <cstdlib> 17 #include <ctime> 18 #include <cstdio> 19 #include <cstring> 20 #define FOR(i, a, b) for(int i = (a); i <= (b); i++) 21 #define RE(i, n) FOR(i, 1, n) 22 #define FORP(i, a, b) for(int i = (a); i >= (b); i--) 23 #define REP(i, n) for(int i = 0; i <(n); ++i) 24 #define SZ(x) ((int)(x).size ) 25 #define ALL(x) (x).begin(), (x.end()) 26 #define MSET(a, x) memset(a, x, sizeof(a)) 27 using namespace std; 28 29 30 typedef long long int ll; 31 typedef pair<int, int> P; 32 ll read() { 33 ll x=0,f=1; 34 char ch=getchar(); 35 while(ch<'0'||ch>'9') { 36 if(ch=='-')f=-1; 37 ch=getchar(); 38 } 39 while(ch>='0'&&ch<='9') { 40 x=x*10+ch-'0'; 41 ch=getchar(); 42 } 43 return x*f; 44 } 45 const double pi=3.14159265358979323846264338327950288L; 46 const double eps=1e-6; 47 const int mod = 1e9 + 7; 48 const int INF = 0x3f3f3f3f; 49 const int MAXN = 1005; 50 const int xi[] = {0, 0, 1, -1}; 51 const int yi[] = {1, -1, 0, 0}; 52 53 int N, T; 54 ll a[120002], b[120002]; 55 ll c[60002], d[60002]; 56 struct asort { 57 int num; 58 ll date; 59 } sa[60002], sb[60002]; 60 bool cmp(asort a, asort b) { 61 return a.date > b.date; 62 } 63 int main() { 64 //freopen("in.txt", "r", stdin); 65 int t, n, k; 66 scanf("%d", &t); 67 68 while(t--) { 69 ll sum = 0; 70 scanf("%d", &n); 71 72 for(int i = 0; i < n; i++) a[i] = read(); 73 for(int i = 0; i < n; i++) b[i] = read(); 74 for(int i = n; i < 2*n ; i++) { 75 a[i] = a[i-n]; 76 b[i] = b[i-n]; 77 } 78 for(int i = 0; i < n; i++) { 79 sum += a[i]*a[i]; 80 sum += b[i]*b[i]; 81 sa[i].num = i, sa[i].date = a[i]; 82 sb[i].num = i, sb[i].date = b[i]; 83 } 84 sort(sa, sa+n, cmp); 85 sort(sb, sb+n, cmp); 86 int m = min(n, 10000); 87 ll res = 0; 88 for(int ai = 0; ai < m; ai++) { 89 ll ans = 0; 90 int i = (sb[0].num - sa[ai].num + n)%n; 91 for(int j = i; j < n+i; j++) { 92 ans += (a[j-i]*b[j]) <<1; 93 } 94 if(ans > res) { 95 res = ans; 96 k = i; 97 } 98 } 99 printf("%lld\n", sum - res); 100 // printf("%d\n", k); 101 } 102 return 0; 103 }