【noip模拟】德充符
时间限制:2s
内存限制:512MB
【题目描述】
申徒嘉和郑子产都是伯昏无人的学生,子产因为申徒嘉是残疾人,非常看不起他,于是
想要***难他。
子产给了申徒嘉 n个数 a1,a2...an。
现在他要求申徒嘉重新排列这些数,使得 H=||...|b1-b2|-b3|-b4|-...|-bn|最大(b 是
a 重新排列后的序列,|x|表示取 x的绝对值)
申徒嘉对于吹逼很擅长,但是数学就不怎么样了,于是他请你来帮帮他。
【输入格式】
第一行一个数 n,接下来一行n个数,第 i 个数表示a[i]
n<=300
1<=a[i]<=300
对于30%的数据,n<=10
【输出格式】
输出一行一个整数表示答案
【样例输入】
4
3 6 7 8
【样例输出】
6
【样例解释】
对于第一组样例:
|||6-8|-3|-7| = 6
【题目分析】
乱搞做法:
因为答案不超过300,而且似乎要控制只有少数特定的排列使得答案最大是不太容易的,因此不断随机打乱更新答案,就很容易得到最大值。
【code】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<cmath> #include<ctime> #include<vector> using namespace std; const int N = 305; int n, a[N]; inline int read(){ int i = 0, f = 1; char ch = getchar (); for (; (ch < '0' || ch > '9' ) && ch != '-' ; ch = getchar ()); if (ch == '-' ) f = -1, ch = getchar (); for (; ch >= '0' && ch <= '9' ; ch = getchar ()) i = (i << 3) + (i << 1) + (ch - '0' ); return i * f; } inline void wr( int x){ if (x < 0) putchar ( '-' ), x = -x; if (x > 9) wr(x / 10); putchar (x % 10 + '0' ); } int main(){ freopen ( "dcf.in" , "r" , stdin); freopen ( "dcf.out" , "w" , stdout); n = read(); srand ( time (0)); for ( int i = 1; i <= n; i++) a[i] = read(); int T = 200000, ans = -1; while (T--){ random_shuffle(a + 1, a + n + 1); int sum = a[1]; for ( int i = 2; i <= n; i++) sum = abs (sum - a[i]); if (sum > ans) ans = sum; } wr(ans); return 0; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步