uva 11300 Spreading the Wealth
2013-09-11 00:01
295 查看
解题思路:给定数轴上的 n 个点,中位数离所有点的距离之和最小
/////////////////////////////////////////////////////////////////////////// //problem_id: uva 11300 //user_id: SCNU20102200088 /////////////////////////////////////////////////////////////////////////// #include <algorithm> #include <iostream> #include <iterator> #include <iomanip> #include <cstring> #include <cstdlib> #include <string> #include <vector> #include <cstdio> #include <cctype> #include <cmath> #include <queue> #include <stack> #include <list> #include <set> #include <map> using namespace std; /////////////////////////////////////////////////////////////////////////// #pragma comment(linker,"/STACK:1024000000,1024000000") #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// const double EPS=1e-8; const double PI=acos(-1.0); const int x4[]={-1,0,1,0}; const int y4[]={0,1,0,-1}; const int x8[]={-1,-1,0,1,1,1,0,-1}; const int y8[]={0,1,1,1,0,-1,-1,-1}; /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// typedef long long LL; typedef int T; T max(T a,T b){ return a>b? a:b; } T min(T a,T b){ return a<b? a:b; } T gcd(T a,T b){ return b==0? a:gcd(b,a%b); } T lcm(T a,T b){ return a/gcd(a,b)*b; } /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// //Add Code: LL a[1000005],b[1000005]; /////////////////////////////////////////////////////////////////////////// int main(){ /////////////////////////////////////////////////////////////////////// //Add Code: int n,i; while(scanf("%d",&n)!=EOF){ LL sum=0; for(i=1;i<=n;i++){ scanf("%lld",&a[i]); sum+=a[i]; } LL avg=sum/n; b[0]=0; for(i=1;i<n;i++) b[i]=b[i-1]+a[i]-avg; sort(b,b+n); LL mid=b[n/2],ans=0; for(i=0;i<n;i++) ans+=abs(mid-b[i]); printf("%lld\n",ans); } /////////////////////////////////////////////////////////////////////// return 0; } /////////////////////////////////////////////////////////////////////////// /* Testcase: Input: 3 100 100 100 4 1 2 5 4 Output: 0 4 */ ///////////////////////////////////////////////////////////////////////////
相关文章推荐
- UVa-11300 - Spreading the Wealth(中位数)
- UVa11300 Spreading the Wealth 分金币
- UVA11300 Spreading the Wealth
- Uva - 11300 - Spreading the Wealth
- UVA 11300 Spreading the Wealth (思路题目)
- uva - 11300 - Spreading the Wealth(数学推导,中位数)
- Uva 11300 Spreading the Wealth(数学,中位数)
- uva 11300 - Spreading the Wealth(思维)
- Uva-11300-Spreading the Wealth
- Math - Uva 11300 Spreading the Wealth
- Uva11300 Spreading the Wealth
- [UVa 11300]Spreading the Wealth
- UVA 11300 Spreading the Wealth
- uva 11300 - Spreading the Wealth(数论)
- UVA 11300 - Spreading the Wealth(代数分析+距离和最小)
- uva 11300 - Spreading the Wealth(思维)
- [训练指南] 第一章 例题3 分金币 (Spreading the wealth,Uva 11300)
- UVA11300 - Spreading the Wealth
- uva 11300 - Spreading the Wealth
- UVa 11300:Spreading the Wealth(杂题)