2024.8.21 鲜花

Never Gonna Give You Up
We're no strangers to love
You know the rules and so do I
A full commitment's what I'm thinking of
You wouldn't get this from any other guy
I just wanna tell you how I'm feeling
Gotta make you understand
Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie and hurt you
We've known each other for so long
Your heart's been aching but you're too shy to say it
Inside we both know what's been going on
We know the game and we're gonna play it
And if you ask me how I'm feeling
Don't tell me you're too blind to see
Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie and hurt you
Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie and hurt you
(Ooh give you up)
(Ooh give you up)
(Ooh) never gonna give, never gonna give
(give you up)
(Ooh) never gonna give, never gonna give
(give you up)
We've known each other for so long
Your heart's been aching but you're too shy to say it
Inside we both know what's been going on
We know the game and we're gonna play it
I just wanna tell you how I'm feeling
Gotta make you understand
Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie and hurt you
Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie and hurt you
Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie and hurt you

模拟退火和本地 IO 交互。

模拟退火:

退火定义,有兴趣可以看看 this

当然这和算法无关。

考虑爬山缺陷,会陷入局部解,考虑让他跳出最优解。

定义退火函数 \(T\),每次决策后下降(一般为乘 \([0.995,0.996]\))每次决策点偏移量乘上 \(T\)

为了跳出局部解,要有一定概率接受不优的解。

\(\Delta\) 为这次解和上次的差,转移概率:

\[P(x,y)=\begin{cases} 1 ,\ \Delta < 0 \\ e ^ {- \frac{\Delta}{T}} \ , \ \Delta > 0\end{cases} \]

本地交互:

重点科技:管道

普通管道没有办法双向,考虑管道文件。

用 mkfifo 创建一个管道文件,然后就可以用这个交互了。

具体就是创建两个文件,分别用重定向读入写出,因为互相阻塞可以达到效果。

这里实现了一个简单的猜数交互

猜数程序(a.cpp)

#include<bits/stdc++.h>
using namespace std;
using llt=long long;
using llf=long double;
using ull=unsigned long long;
#ifdef LOCAL
	FILE *InFile=freopen("in_out/in.in","r",stdin),*OutFile=freopen("in_out/out.out","w",stdout);
#else
	FILE *InFile=stdin,*OutFile=stdout;
#endif

const llt RG=1e18;

int main(){
	ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
	llt l=1,r=RG;
	while(l<=r){
		llt mid=(l+r)>>1; bool fg; cout<<"? "<<mid<<endl,cin>>fg;
		if(fg) r=mid-1;
		else l=mid+1;
	}
	cout<<"! "<<l<<endl;
}

交互程序(b.cpp)

#include<bits/stdc++.h>
using namespace std;
using llt=long long;
using llf=long double;
using ull=unsigned long long;
#ifdef LOCAL
	FILE *InFile=freopen("in_out/in.in","r",stdin),*OutFile=freopen("in_out/out.out","w",stdout);
#else
	FILE *InFile=stdin,*OutFile=stdout;
#endif

const llt RG=1e18;
mt19937_64 rnd(ull(new char)*ull(new char));
 
int main(){
	ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
    llt a=rnd()%RG+1,tot=0;
	while(true){
		char k; llt x; cin>>k>>x;
		if(k=='?') cout<<(x>=a)<<endl,++tot;
		else{
			if(x!=a) return cerr<<"Sorry, you are wrong. Answer: "<<a<<' '<<x<<endl,-1;
			else return cerr<<"Good, you are right. Times: "<<tot<<endl,0;
		}
	}
}

Run 记得先用 mkfifo 建 inpipe 和 outpipe

#include<bits/stdc++.h>
using namespace std;

int main(){
	assert(!system("g++ a.cpp -o a -O2 -std=c++14"));
	assert(!system("g++ b.cpp -o b -O2 -std=c++14"));
    system("(./a>inpipe<outpipe)&(./b<inpipe>outpipe)");
}

注意要把先输出的放到前面

夙愿达成

posted @ 2024-08-21 20:50  xrlong  阅读(119)  评论(9编辑  收藏  举报