CodeForces 429D Tricky Function
2016-06-30 17:40
239 查看
题目大意
给出一个长度为N的序列a。定义
f(i,j)=(i−j)2+g(i,j)2
g(i,j)=∑jk=iak
求最小的f(i,j)
Data Constraint
N≤105
题解
设Sum[i]表示1~i的和。那么f(i,j)=(i−j)2+(Sum[j]−Sum[i])2
问题变成:求形如(i,Sum[i])点对间的最小距离。
可以分治求解。
时间复杂度:O(NlogN^2)
SRC
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> using namespace std ; #define N 100000 + 10 typedef long long ll ; const ll Inf = 1e13 ; struct Note { ll x , y ; Note ( int X = 0 , int Y = 0 ) { x = X , y = Y ; } } Point , Q ; int n ; int a ; ll ans ; bool cmpx( Note a , Note b ) { return a.x < b.x || ( a.x == b.x && a.y < b.y ) ; } bool cmpy( Note a , Note b ) { return a.y < b.y ; } ll Sqr( ll x ) { return x * x ; } ll Dis2( Note a , Note b ) { return Sqr( a.x - b.x ) + Sqr( a.y - b.y ) ; } ll DIV( int l , int r ) { ll ret = Inf ; if ( l == r ) return ret ; if ( l + 1 == r ) return Dis2( Point[l] , Point[r] ) ; int mid = (l + r) / 2 ; ll d1 = DIV( l , mid ) ; ll d2 = DIV( mid + 1 , r ) ; ret = min( d1 , d2 ) ; double d = sqrt( (double)ret ) ; int k = 0 ; for (int i = mid ; i >= l ; i -- ) { if ( Point[mid].x - Point[i].x > (ll)d ) break ; Q[++k] = Point[i] ; } for (int i = mid + 1 ; i <= r ; i ++ ) { if ( Point[i].x - Point[mid].x > (ll)d ) break ; Q[++k] = Point[i] ; } sort( Q + 1 , Q + k + 1 , cmpy ) ; for (int i = 1 ; i < k ; i ++ ) { for (int j = i + 1 ; j <= k && Q[j].y - Q[i].y <= (ll)d ; j ++ ) { ret = min( ret , Dis2( Q[i] , Q[j] ) ) ; } } return ret ; } int main() { scanf( "%d" , &n ) ; ll sum = 0 ; for (int i = 1 ; i <= n ; i ++ ) { scanf( "%d" , &a[i] ) ; sum += a[i] ; Point[i].x = i ; Point[i].y = sum ; } sort( Point + 1 , Point + n + 1 , cmpx ) ; printf( "%lld\n" , DIV( 1 , n ) ) ; return 0 ; }
以上.
相关文章推荐
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- codeforces 618C. Constellation
- Codeforces Round #349 (Div. 2) - C
- Codeforces Round #265 (Div. 2)
- Codeforces #310 div2 C. Case of Matryoshkas
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- codeforces 16 Problem E fish
- Codeforces Round332 部分题解
- CodeForces 603A_Alternative Thinking (DP)
- CodeForces 602B_Approximating a Constant Range_DP
- Codeforces round #247 for Div. 2
- Codeforces Round #246 (Div. 2)
- Codeforces #264(div 2)D.Gargari and Permutations
- Codeforces Round #236 (Div. 2)------A,B
- codeforces 257 div2 B