QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#347031#7199. BombrageOfThunderTL 162ms144780kbC++142.2kb2024-03-09 10:12:582024-03-09 10:12:59

Judging History

你现在查看的是最新测评结果

  • [2024-03-09 10:12:59]
  • 评测
  • 测评结果:TL
  • 用时:162ms
  • 内存:144780kb
  • [2024-03-09 10:12:58]
  • 提交

answer

#include<bits/stdc++.h>

#define ll long long
#define mk make_pair
#define fi first
#define se second

using namespace std;

inline int read(){
	int x=0,f=1;char c=getchar();
	for(;(c<'0'||c>'9');c=getchar()){if(c=='-')f=-1;}
	for(;(c>='0'&&c<='9');c=getchar())x=x*10+(c&15);
	return x*f;
}

const int mod=998244353;
int ksm(int x,ll y,int p=mod){
	int ans=1;y%=(p-1);
	for(int i=y;i;i>>=1,x=1ll*x*x%p)if(i&1)ans=1ll*ans*x%p;
	return ans%p;
}
int inv(int x,int p=mod){return ksm(x,p-2,p)%p;}
mt19937 rnd(time(0));
int randint(int l,int r){return rnd()%(r-l+1)+l;}
void add(int &x,int v){x+=v;if(x>=mod)x-=mod;}
void Mod(int &x){if(x>=mod)x-=mod;}
int cmod(int x){if(x>=mod)x-=mod;return x;}

template<typename T>void cmax(T &x,T v){x=max(x,v);}
template<typename T>void cmin(T &x,T v){x=min(x,v);}

const int N=3005;
int n,pos[N];
ll f[2][N][N];

const ll INF=1e15;

void clr(){
	for(int i=0;i<=n;i++)pos[i]=0;
	for(int c=0;c<=1;c++)for(int i=0;i<=n;i++)for(int j=0;j<=n;j++)f[c][i][j]=INF;
}

void solve(){
	if(n==1)return puts("0"),void();
	pos[0]=-1e6,pos[n+1]=2e6;
	
	vector<vector<pair<int,int> > >ws(n+1);
	for(int i=1;i<=n;i++){
		vector<pair<int,int> >vals;
		for(int j=1;j<=n;j++)vals.emplace_back(mk(abs(pos[i]-pos[j]),j));
		sort(vals.begin(),vals.end());
		int l=i,r=i;
		for(auto [dis,id]:vals){
			cmin(l,id),cmax(r,id);
			ws[i].emplace_back(mk(l,r));
		}
	}
	
	auto sq=[&](int x){return 1ll*x*x;};
	
	int cur=0;
	f[cur][1][2]=sq(pos[2]-pos[1]);
	for(int i=2;i<=n;i++){
		memset(f[cur^1],63,sizeof(f[cur^1]));
//		cout<<"ws "<<i<<" = ";for(auto [l,r]:ws[i])cout<<"["<<l<<","<<r<<"] ";puts("");
		for(int j=1;j<=i-1;j++)for(int k=i;k<=n;k++)if(f[cur][j][k]<=INF){
//			cout<<"f "<<i-1<<" "<<j<<" "<<k<<" = "<<f[cur][j][k]<<endl;
			for(auto [l,r]:ws[i]){
				if(l==i&&r==i)continue;
				int jj=(l<=j?i:j);
				cmin(f[cur^1][jj][max(k,r)],f[cur][j][k]+sq(max(pos[r]-pos[i],pos[i]-pos[l])));
			}
		}
		cur^=1;
	}
	cout<<f[cur][n][n]<<endl;
	
	clr();
}

signed main(void){

#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif

	memset(f,63,sizeof(f));
	while(cin>>n){
		for(int i=1;i<=n;i++)cin>>pos[i];
		solve();
	}

	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 35ms
memory: 144780kb

input:

5
1 4 5 6 10
3
1 2 6

output:

51
33

result:

ok 2 tokens

Test #2:

score: 0
Accepted
time: 162ms
memory: 144724kb

input:

18
1 2 3 6 11 23 47 106 235 551 1301 3159 7741 19320 48629 123867 317955 823065
5
1 5 6 7 11
2
1 1000000
3
1 12345 1000000
4
1 2 3 1000000

output:

554621353432
59
1999996000002
1951077172386
1999988000020

result:

ok 5 tokens

Test #3:

score: -100
Time Limit Exceeded

input:

5
1 2 5 6 7
5
1 4 5 7 10
5
1 2 5 7 10
5
1 4 5 7 10
5
1 2 5 6 7
5
1 4 5 7 10
5
1 2 5 6 7
5
1 2 5 6 7
5
2 5 6 7 9
5
2 5 6 7 9
5
1 4 5 7 10
5
2 3 5 6 9
5
2 5 6 7 9
5
1 2 5 6 7
5
2 3 6 8 9
5
1 2 5 7 10
5
2 3 5 6 9
5
1 4 5 7 10
5
1 2 5 6 7
5
1 2 5 7 10
5
2 5 6 7 9
5
1 2 5 7 10
5
1 4 5 7 10
5
2 3 6 8 9
5
...

output:

21
37
37
37
21
37
21
21
27
27
37
24
27
21
24
37
24
37
21
37
27
37
37
24
27
27
27
27
24
24
37
27
24
24
27
21
21
37
37
21
24
27
24
27
21
27
37
37
37
27
37
21
24
37
37
24
24
21
27
24
37
27
21
37
37
24
27
21
27
37
21
37
37
24
37
24
37
37
21
37
21
27
21
24
24
27
27
24
37
24
24

result: