Bumb

Time Limit: 20 Sec  Memory Limit: 512 MB

Description

  

Input

  

Output

  

Sample Input

  4
  1 5 1 4

Sample Output

  5

HINT

  

Solution

  首先,我们对于一个已知的k,可以O(n)得到Ans,这样就有60%了。

  那么怎么做90%呢?老老实实写O(nlogn)是不可能的!模拟退火美滋滋!

  传授一点人生的经验吧:写个对拍用于调参,由于这种题不能很单峰,显然温度变化设大一点比较容易正确,无限running + 卡时即可。

  是的,没有错!BearChild就这样拿到了90%!

  我们来考虑100%怎么做,显然复杂度O(n)。我们把( a[i - 1], a[i] ]看作若干个区间挂在数轴上。

  然后考虑k:1->m对于答案的变化。显然可以记录cnt表示k包含的区间个数,以及sum和,这样就可以做完啦!

Code

 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<ctime>
using namespace std;
typedef long long s64; const int ONE = ;
const s64 INF = 1e18; int n, m;
int a[ONE];
int Now, A;
s64 Ans = INF; int get()
{
int res=,Q=;char c;
while( (c=getchar())< || c> )
if(c=='-')Q=-;
res=c-;
while( (c=getchar())>= && c<= )
res=res*+c-;
return res*Q;
} void TimeE()
{
if((double)clock() / CLOCKS_PER_SEC > 0.97)
{
printf("%lld", Ans);
exit();
}
} s64 Get(int x, int y)
{
TimeE();
if(x <= y) return y - x;
return y + m - x;
} s64 Judge(int k)
{
s64 res = ;
for(int i = ; i <= n; i++)
res += min(Get(a[i - ], a[i]), + Get(k, a[i]));
Ans = min(Ans, res);
return res;
} double Random() {return rand() / (double)RAND_MAX;}
void SA(double T)
{
Now = m / ;
while(T >= )
{
A = Now + (int)(T * (Random() * - ));
if(A < || A > m) A = T * Random();
s64 dE = Judge(A) - Judge(Now);
if(dE < ) Now = A;
T *= 0.92;
}
} int main()
{
n = get(); m = get();
for(int i = ; i <= n; i++)
a[i] = get();
if(n > ) {for(;;)SA(m); return ;} for(int i = ; i <= m; i++)
Ans = min(Ans, Judge(i)); printf("%lld", Ans);
}

模拟退火 90%

 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
using namespace std;
typedef long long s64; const int ONE = ;
const s64 INF = 1e18; int n, m;
int a[ONE];
int L[ONE];
vector <int> R[ONE];
s64 Ans; int get()
{
int res=,Q=;char c;
while( (c=getchar())< || c> )
if(c=='-')Q=-;
res=c-;
while( (c=getchar())>= && c<= )
res=res*+c-;
return res*Q;
} int Get(int x, int y)
{
if(x <= y) return y - x;
return y + m - x;
} int main()
{
n = get(); m = get();
for(int i = ; i <= n; i++)
a[i] = get(); for(int i = ; i <= n; i++)
{
Ans += Get(a[i - ], a[i]);
L[a[i - ]]++, R[a[i]].push_back(a[i - ]);
} s64 sum = Ans, cnt = ; for(int i = ; i <= n; i++)
if(a[i - ] > a[i])
sum -= m - a[i - ], cnt++; for(int k = ; k <= m; k++)
{
int len = R[k - ].size();
cnt = cnt + L[k - ] - len;
sum -= cnt;
for(int i = ; i < len; i++)
sum += Get(R[k - ][i], k - ) - ;
Ans = min(Ans, sum);
} printf("%lld", Ans);
}

正解 100%

【Foreign】Bumb [模拟退火]的更多相关文章

  1. 【Foreign】咏叹 [模拟退火]

    咏叹 Time Limit: 100 Sec  Memory Limit: 256 MB Description 有n根木棍,第i根长度为ai.你要贴着墙围出一个矩形区域,木棍围成的矩形边缘必须平行或 ...

  2. bzoj3680模拟退火

    看题意就是一道数学物理题,带权费马点   --这怎么是数学了,这也是物理的 所以要用物理方法,比如FFF 国际著名oi选手miaom曾说 模拟退火初温可以低,但是最好烧个几千次 国际著名物理课代表+1 ...

  3. Constraint6:更新外键约束(Foreign Key Constraint)的引用列

    在SQL Server中,表之间存在引用关系,引用关系通过创建外键约束(Foreign Key Constraint)实现.如果一个Table中的column被其他Table引用,那么该表是参考表,或 ...

  4. MySQL主从复制中断,报“Error on master: message (format)='Cannot delete or update a parent row: a foreign key constraint fails' error code=1217” 错误

    前几天,发现从库挂了,具体报错信息如下: 分析思路 1. 因为我采用的是选择性复制,只针对以下几个库进行复制: card,upay,deal,monitor,collect.所以,不太可能出现对于sa ...

  5. ORA-02266: unique/primary keys in table referenced by enabled foreign keys

    在数据库里面使用TRUNCATE命令截断一个表的数据时,遇到如下错误 SQL >TRUNCATE TABLE ESCMOWNER.SUBX_ITEM ORA-02266: unique/prim ...

  6. SQL Server 2008 R2——TRUNCATE TABLE 无法截断表 该表正由 FOREIGN KEY 约束引用

    =================================版权声明================================= 版权声明:原创文章 禁止转载  请通过右侧公告中的“联系邮 ...

  7. 【MySQL】Create table 以及 foreign key 删表顺序考究。

    1.以下是直接从数据库导出的建表语句. 1 -- ---------------------------- 2 -- Table structure for files 3 -- ---------- ...

  8. MYSQL外键(Foreign Key)的使用

    在MySQL 3.23.44版本后,InnoDB引擎类型的表支持了外键约束.外键的使用条件:1.两个表必须是InnoDB表,MyISAM表暂时不支持外键(据说以后的版本有可能支持,但至少目前不支持): ...

  9. 无题的题 & 模拟退火...

    题意: 给你不超过8条一端在圆心的半径,求他们组成的凸包的最大面积. SOL: 正解怎么搞啊不会啊...然后昨天毛爷爷刚讲过模拟退火...那么就打一个吧... 然后就T了,不过三角形的部分分妥妥的.. ...

随机推荐

  1. 软件工程课堂作业(二)续——升级完整版随机产生四则运算题目(C++)

    一.设计思想: 1.根据题目新设要求,我将它们分为两类:一类是用户输入数目,根据这个数目改变一系列后续问题:另一类是用户输入0或1,分情况解决问题. 2.针对这两类要求,具体设计思路已在上篇博文中写出 ...

  2. Ubuntu 配置 Android 开发 环境

    . 果断换Ubuntu了, Ubuntu的截图效果不好, 不能设置阴影 ... 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article ...

  3. 2019寒假训练营第三次作业part1-网络空间安全概论第五章

    第五章 网络攻防技术 5.1 网路信息收集技术--网络踩点 黑客入侵系统之前,需要了解目标系统可能存在的: 管理上的安全缺陷和漏洞 网络协议安全缺陷与漏洞 系统安全缺陷与漏洞 黑客实施入侵过程中,需要 ...

  4. 深入理解Java对象序列化(转载)

    原文地址:http://developer.51cto.com/art/201202/317181.htm 1. 什么是Java对象序列化 Java平台允许我们在内存中创建可复用的Java对象,但一般 ...

  5. MFC修改视图CView的背景颜色

    (1) 在CYournameView(就是你的视图类,以下以CDrawLineView为例)添加了一个背景颜色变量 COLORREF m_bgcolor; (2)修改这个函数: BOOL CDrawL ...

  6. 如何高效的使用Google

    文章再转自知乎:http://www.zhihu.com/question/20161362

  7. Maven jeetsite项目 搭建

    , 一直没有系统的总结一下Maven的知识,今天,想从网上找一个Maven的项目,练练手,顺便学习一下maven的原理 和布局. 官网:http://www.jeesite.com/ 没想到,上来就给 ...

  8. openstack之horizon部署

    登录官网 www.openstack.org 查看安装文档 https://docs.openstack.org/newton/install-guide-rdo/horizon.html 第一步yu ...

  9. BZOJ4710 JSOI2011分特产(容斥原理+组合数学)

    显然可以容斥去掉每人都不为空的限制.每种物品分配方式独立,各自算一个可重组合乘起来即可. #include<iostream> #include<cstdio> #includ ...

  10. 【BZOJ4520】K远点对(KD-Tree)

    [BZOJ4520]K远点对(KD-Tree) 题面 BZOJ 洛谷 题解 考虑暴力. 维护一个大小为\(K\)的小根堆,然后每次把两个点之间的距离插进去,然后弹出堆顶 这样子可以用\(KD-Tree ...