题目链接:https://ac.nowcoder.com/acm/contest/881/C

题目大意

  给定 m 和 n 个整数 ai,$-m \leq a_i \leq m$,求$\sum\limits_{i = 1}^{n} (\frac{a_i}{m} - p_i)^2$在约束条件$\sum\limits_{i = 1}^{n} p_i = 1, p_i \geq 0$下的最小值。

分析

  首先,为了方便计算,可以把 p 坐标都扩大 m 倍,最后结果除个 m2 即可。
  如此一来只需要算$\sum\limits_{i = 1}^{n} (a_i - p_i)^2$即可。
  根据 AM-GM 不等式(算术—几何均值不等式)(这里先假设$p_i \geq a_i 或者 p_i \leq a_i$):

$$\begin{align*}
\frac{\sum\limits_{i = 1}^{n} (a_i - p_i)^2}{n} \geq \sqrt[n]{\prod\limits_{i = 1}^{n} (a_i - p_i)^2} \\
当且仅当 p_1 - a_1 = p_2 - a_2 = \dots = p_n - a_n 时取等号。
\end{align*}$$

  于是可以得到关于 pi 的式子:$n(p_i - a_i) = m - \sum\limits_{i = 1}^{n} a_i$。
  于是答案就显而易见了。
  但问题是,由于约束条件,pi 并不会都大于 0 且都大于 $a_i$ 或小于 $a_i$,为了解决这个问题,我们先把 ai 从大到小排个序,然后利用上面的 AM-GM 不等式。
  然后我们发现以某个数 k 为分界,$p_1 \dots p_k$ 都是大于等于 0 的,而 $p_{k + 1} \dots p_n$ 都是小于零的。
  于是我们可以让 $p_{k + 1} \dots p_n$ 全部取 0,然后在 $[1, k]$ 上递归运用 AM-GM 不等式,直到找到一个区间,没有 $p_i$ 小于 0,而这个时候一定有$p_i \geq a_i 或者 p_i \leq a_i$(迷)。(具体可用二分法实现)
  
  那有没有可能 $p_{k + 1} \dots p_n$ 中选几个取一个大于 0 的值,答案能更小呢?这个我不晓得,也不会证,不过代码AC了,说明是没可能的。
  我的理解是,与其给自己,不如均摊。
  

  然后这边有大佬的解释:https://blog.nowcoder.net/n/1539da6d6d6e47a6998b5c6f5bba2167?tdsourcetag=s_pcqq_aiomsg

  思想其实和我一样,大佬解释为推平,推到推不下去为止。

  

  正解用了拉格朗日乘子法,但是写的太飘,解释的又太少,有些符号又看不懂什么意思,思维太跳,反正我是看不懂。

代码如下

 #include <bits/stdc++.h>
using namespace std; #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // ?? x ?????? c
#define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
#define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper); #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T>
ostream &operator<<(ostream &out, vector<T> &v) {
Rep(i, v.size()) out << v[i] << " \n"[i == v.size()];
return out;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} template<class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
} inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
} //min <= aim <= max
template<typename T>
inline bool BETWEEN(const T aim, const T min, const T max) {
return min <= aim && aim <= max;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< int, PII > PIPII;
typedef pair< string, int > PSI;
typedef pair< int, PSI > PIPSI;
typedef set< int > SI;
typedef set< PII > SPII;
typedef vector< int > VI;
typedef vector< double > VD;
typedef vector< VI > VVI;
typedef vector< SI > VSI;
typedef vector< PII > VPII;
typedef map< int, int > MII;
typedef map< int, string > MIS;
typedef map< int, PII > MIPII;
typedef map< PII, int > MPIII;
typedef map< string, int > MSI;
typedef map< string, string > MSS;
typedef map< PII, string > MPIIS;
typedef map< PII, PII > MPIIPII;
typedef multimap< int, int > MMII;
typedef multimap< string, int > MMSI;
//typedef unordered_map< int, int > uMII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
typedef priority_queue< int > PQIMax;
typedef priority_queue< int, VI, greater< int > > PQIMin;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 1e4 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; LL n, m, a[maxN], preSum[maxN], ans; int main(){
//freopen("MyOutput.txt","w",stdout);
//freopen("input.txt","r",stdin);
//INIT();
while(~scanf("%lld %lld", &n, &m)) {
For(i, , n) scanf("%lld", &a[i]);
sort(a + , a + n + , greater< LL >()); For(i, , n) preSum[i] = a[i] + preSum[i - ]; int l = , r = n;
while(l < r) {
int mid = (l + r) >> ; if(m - preSum[mid + ] + (mid + ) * a[mid + ] >= ) l = mid + ;
else r = mid;
}
ans = (m - preSum[l]) * (m - preSum[l]) * l;
For(i, l + , n) ans += a[i] * a[i] * l * l; LL x = m * m * l * l;
LL d = __gcd(ans, x);
ans /= d;
x /= d;
if(x == ) printf("%lld\n", ans);
else printf("%lld/%lld\n", ans, x);
}
return ;
}
/*
7 16
4 9 -4 -6 -3 5 13
469/1024 8 16
4 9 -4 -6 -3 5 13 7
79/128
*/

2019 牛客多校第一场 C Euclidean Distance ?的更多相关文章

  1. 2019牛客多校第一场 I Points Division(动态规划+线段树)

    2019牛客多校第一场 I Points Division(动态规划+线段树) 传送门:https://ac.nowcoder.com/acm/contest/881/I 题意: 给你n个点,每个点有 ...

  2. 2019牛客多校第一场E ABBA(DP)题解

    链接:https://ac.nowcoder.com/acm/contest/881/E 来源:牛客网 ABBA 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语 ...

  3. 2019牛客多校第一场A-Equivalent Prefixes

    Equivalent Prefixes 传送门 解题思路 先用单调栈求出两个序列中每一个数左边第一个小于自己的数的下标, 存入a[], b[].然后按照1~n的顺序循环,比较 a[i]和b[i]是否相 ...

  4. 2019牛客多校第一场 A.Equivalent Prefixes

    题目描述 Two arrays u and v each with m distinct elements are called equivalent if and only if RMQ(u,l,r ...

  5. 2019 牛客多校第一场 D Parity of Tuples

    题目链接:https://ac.nowcoder.com/acm/contest/881/D 看此博客之前请先参阅吕凯飞的论文<集合幂级数的性质与应用及其快速算法>,论文中很多符号会被本文 ...

  6. 2019牛客多校第一场 E-ABBA(dp)

    ABBA 题目传送门 解题思路 用dp[i][j]来表示前i+j个字符中,有i个A和j个B的合法情况个数.我们可以让前n个A作为AB的A,因为如果我们用后面的A作为AB的A,我们一定也可以让前面的A对 ...

  7. 【2019牛客多校第一场】XOR

    题意: 给你一个集合A,里边有n个正整数,对于所有A的.满足集合内元素异或和为0的子集S,问你∑|S| n<=1e5,元素<=1e18 首先可以转化问题,不求∑|S|,而是求每个元素属于子 ...

  8. 2019 牛客多校第一场 B Integration

    题目链接:https://ac.nowcoder.com/acm/contest/881/B 题目大意 给定 n 个不同的正整数 ai,求$\frac{1}{\pi}\int_{0}^{\infty} ...

  9. 2019牛客多校第一场E ABBA 贪心 + DP

    题意:问有多少个有(n + m)个A和(n + m)个B的字符串可以凑出n个AB和m个BA. 思路:首先贪心的发现,如果从前往后扫,遇到了一个A,优先把它看成AB的A,B同理.这个贪心策略用邻项交换很 ...

随机推荐

  1. Linux服务器的16个监控命令

    想不想知道你的服务器到底在干什么?那么你要知道本文介绍的这些基本命令.一旦你熟悉掌握了这些命令,就为成为专业的 Linux系统管理员打下了基础. 你可以通过图形化用户界面(GUI)程序来获取这些外壳命 ...

  2. Chrome 调试跨域问题解决方案之插件篇

    跨域,就是A域名下的js,想请求B域名下的接口数据.跨域,只存在于浏览器端.App和小程序不存在跨域问题.跨域,分浏览器策略和服务器策略. 如果服务器配置了允许跨域,那就没有跨域问题 如果uni-ap ...

  3. WebBug靶场基础篇 — 02

    本篇以第一人称记录这个关卡的第 1-5 关. 由于我记录的过程有点偏向于思考,所以截图截的多 = =!所以文章有点长... 下午一觉醒来,已经 4 点多了,然后开电脑,在虚拟机里,铺了铺靶场,但是毕竟 ...

  4. C#简单游戏外挂制作(以Warcraft Ⅲ为例)

    网上有很多外挂制作的教程,大多是讲针对大型网络游戏的,主要包含一些抓包.反汇编.C++的知识综合.事实也如此,常见的外挂都是使用VC++写的,从来没有过C#或者其他.NET语言编写的外挂. 作为微软. ...

  5. SQLServer如何手动设置id值(主键)的自动增长

    近期做东西,用到了对SQLServer数据库的操作.好吧,确实好久没看了,对这个数据库陌生到了极点,连最简单的如何设置一个id主键,让它随着插入数据的增多,自动增长id值的设置都忘记了,网上查了一下, ...

  6. CSS:CSS 盒子模型

    ylbtech-CSS:CSS 盒子模型 1.返回顶部 1. CSS 盒子模型 CSS 盒子模型(Box Model) 所有HTML元素可以看作盒子,在CSS中,"box model&quo ...

  7. 导入安全证书到jdk

    一:.导入证书 1.打开doc窗口,打开cmd,执行命令: keytool -import -file f:\ca.crt -keystore "%JAVA_HOME%\jre\lib\se ...

  8. 【开发者笔记】Linq 多表关联排序操作

    c# 一直是一门好用的语言,但是像linq这种骚操作实在是记不住.特此记下以备后用. var ls = from c in db.T_ProductReturnEntity join s in db. ...

  9. 剑指offer第二版面试题10:斐波那契数列(JAVA版)

    题目:写一个函数,输入n,求斐波那契数列的第n项.斐波那契数列的定义如下: 1.效率很低效的解法,挑剔的面试官不会喜欢 使用递归实现: public class Fibonacci { public ...

  10. 设置非阻塞的套接字Socket

    当使用socket()函数和WSASocket()函数创建套接字时,默认都是阻塞的.在创建套接字之后,通过调用ioctlsocket()函数,将该套接字设置为非阻塞模式.函数的第一个参数是套接字,第二 ...