巨佬的讲解
P1337 [JSOI2004]平衡点 / 吊打XXX
题意:
桌子上有
n
n
n 个孔,每个孔都有一个一根绳穿过,下边吊着一个重物,重物坐标
x
,
y
x,y
x,y,这些绳打结在一起,绳子是完全弹性的,孔是足够小的,保证绳结最多是卡在某个洞口处。求最后绳结的位置
保留三位小数。
思路:
模拟退火
code:
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define eps 1e-15
using namespace std;
const int maxn = 2e5 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double down = 0.996;//徐徐降温
const double MAX_TIME = 0.9;
ll n, m;
struct node{
double x, y, w;
}a[maxn];
double ansx, ansy, answ;
double energy(double x, double y)//根据物理学知识,能量总和越小越稳定
{
double ans = 0, dx, dy;
for(int i = 1; i <= n; ++i)
{
dx = x - a[i].x;
dy = y - a[i].y;
ans += sqrt(dx * dx + dy * dy) * a[i].w;
}
return ans;
}
void sa()
{
double t = 3000;//温度要足够高
while(t > eps)
{
double ex = ansx + (rand() * 2 - RAND_MAX) * t;// 每次随机的数跟t建立联系
double ey = ansy + (rand() * 2 - RAND_MAX) * t;
double ew = energy(ex, ey);
double diff = ew - answ;
if(diff < 0) ansx = ex, ansy = ey, answ = ew;// 更新答案
else if(exp(-diff / t) * RAND_MAX > rand())// 根据多项式概率更新答案
ansx = ex, ansy = ey;// 实测不进行这个更新也能AC
t *= down;
}
}
void work()
{
cin >> n;
for(int i = 1; i <= n; ++i){
cin >> a[i].x >> a[i].y >> a[i].w;
ansx += a[i].x; ansy += a[i].y;
}
ansx /= n; ansy /= n;//以平均数作为初始答案
answ = energy(ansx, ansy);
while ((double)clock() / CLOCKS_PER_SEC * 1.0 < MAX_TIME)// MAX_TIME是执行时间,根据需要进行调整
sa();
cout << fixed << setprecision(3) << ansx << " " << ansy;
}
int main()
{
// srand(time(0));
ios::sync_with_stdio(0);
// int TT;cin>>TT;while(TT--)
work();
return 0;
}
P2216 [HAOI2007]理想的正方形
题意:
在
a
×
b
a \times b
a×b 的矩阵中找到一个
n
×
n
n \times n
n×n 的矩阵,使得矩阵内最大值与最小值差值最大
思路:
巨佬的讲解
这里只谈模拟退火,这种设计整数随机化搜索的不是很容易
a
c
ac
ac ,毕竟是假做法
按照这位巨佬的搜索,能拿
60
−
80
60-80
60−80 分左右,开
O
2
O^2
O2 更容易拿高分
正解是单调队列
code:
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define eps 1e-8
using namespace std;
const int maxn = 1e3 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double down = 0.996;
const double MAX_TIME = 0.9;
ll n, m, a, b;
int ansx, ansy, ans = inf;
int w[maxn][maxn];
int cal(int x, int y)
{
int Max = -inf, Min = inf;
for(int i = x - n + 1; i <= x; ++i)
for(int j = y - n + 1; j <= y; ++j)
Max = max(Max, w[i][j]),
Min = min(Min, w[i][j]);
return Max - Min;
}
void sa()
{
double t = max(a, b);
int x = ansx, y = ansy;
while(t > 0.1)
{
int dx = ((x + (rand() * 2 - RAND_MAX)) % (int)round(t * 10)) % a + 1;
int dy = ((y + (rand() * 2 - RAND_MAX)) % (int)round(t * 10)) % b + 1;
t *= down;
if(dx < n || dy < n || dx > a || dy > b) continue;
double now = cal(dx, dy);
double diff = now - ans;
if(diff < 0) x = dx, y = dy, ansx = dx, ansy = dy, ans = now;
else if(exp(-diff / t) * RAND_MAX > rand())
x = dx, y = dy;
}
}
void work()
{
srand(time(NULL));
cin >> a >> b >> n;
for(int i = 1; i <= a; ++i)
for(int j = 1; j <= b; ++j)
cin >> w[i][j];
ansx = ansy = n;
while ((double)clock() / CLOCKS_PER_SEC * 1.0 < MAX_TIME) sa();
cout << ans << endl;
}
int main()
{
ios::sync_with_stdio(0);
// cout << rand() << " " << RAND_MAX << endl;
// int TT;cin>>TT;while(TT--)
work();
return 0;
}
222

被折叠的 条评论
为什么被折叠?



