社区讨论
60RE求助
P3878[TJOI2010] 分金币参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @lo8so3rn
- 此快照首次捕获于
- 2023/10/27 23:55 2 年前
- 此快照最后确认于
- 2023/10/27 23:55 2 年前
floating point exception
CPP#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ut unsigned int
#define y second
#define x first
#define vi vector<int>
#define vl vector<ll>
#define lb lower_bound
#define up upper_bound
//#define getchar() gc()
#define yes cout<<"YES"<<endl
#define of(i,m,n) for(register ll i=m;i>=n;i--)
#define fo(i,m,n) for(register ll i=m;i<=n;i++)
#define no cout<<"NO"<<endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template<class T,int nn>using seq=T[nn];
const int N=100030,M=100030,mod=1e9+7;
inline ll read(){
ll s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
ll a[40000],n,ans;
double t=1e7,dt=0.999;
ll calc(){
ll n_2=0,n_=0;
fo(i,1,n/2)n_2+=a[i];
fo(i,n/2+1,n)n_+=a[i];
ans=min(ans,abs(n_-n_2));
return abs(n_-n_2);
}
ll suffix_array(){
srand(time(0));
t=1e14;ans=1e18;
ll c=calc();
while(t>2){
int i=rand()%(n/2)+1,j=rand()%(n-n/2)+n/2+1;
swap(a[i],a[j]);
ll cc=calc();
if(t<1)return ans;
if(cc<c||(c-cc)/t>log(rand()/(double)(RAND_MAX)))c=cc;
else swap(a[i],a[j]);
t*=dt;
}
return ans;
}
int main(){
ll t=read();
while(t--){
n=read();
fo(i,1,n)a[i]=read();
cout<<suffix_array()<<endl;
}
return 0;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...