Submission #456202

# Submission time Handle Problem Language Result Execution time Memory
456202 2021-08-06T08:57:33 Z Jasiekstrz Džumbus (COCI19_dzumbus) C++17
0 / 110
40 ms 2372 KB
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=1e3;
const long long INF=1e18L+7;
struct Dp
{
	int s;
	vector<long long> t[3]; // 0-off,1-on not counted,2-on counted
	vector<long long>& operator[](int x)
	{
		return t[x];
	}
};
vector<int> e[N+10];
bool vis[N+10];
int val[N+10];
Dp merge_disjoint(Dp a,Dp b)
{
	Dp c;
	c.s=a.s+b.s;
	for(int i=0;i<3;i++)
	{
		c[i].resize(c.s+1);
		fill(c[i].begin(),c[i].end(),INF);
	}

	for(int i=0;i<=b.s;i++)
		b[0][i]=min({b[0][i],b[1][i],b[2][i]});

	for(int i=0;i<=a.s;i++)
	{
		for(int j=0;j<=b.s;j++)
			c[0][i+j]=min(c[0][i+j],a[0][i]+b[0][j]);
	}

	for(int i=0;i<=c.s;i++)
		c[1][i]=c[2][i]=c[0][i];
	return c;
}
Dp merge(Dp a,Dp b,int x)
{
	Dp c;
	c.s=a.s+b.s;
	for(int i=0;i<3;i++)
	{
		c[i].resize(c.s+1);
		fill(c[i].begin(),c[i].end(),INF);
	}

	for(int i=0;i<=a.s;i++)
	{
		for(int j=0;j<=b.s;j++)
		{
			// 0
			c[0][i+j]=min(c[0][i+j],a[0][i]+b[0][j]);
			c[0][i+j]=min(c[0][i+j],a[0][i]+b[2][j]);
			// 2
			c[2][i+j]=min(c[2][i+j],a[2][i]+b[0][j]);
			if(i+j+1<=c.s)
				c[2][i+j+1]=min(c[2][i+j+1],a[2][i]+b[1][j]);
			c[2][i+j]=min(c[2][i+j],a[2][i]+b[2][j]);
			if(i+j+1<=c.s)
				c[2][i+j+1]=min(c[2][i+j+1],a[0][i]+val[x]+b[2][j]);
			if(i+j+2<=c.s)
				c[2][i+j+2]=min(c[2][i+j+2],a[0][i]+val[x]+b[1][j]);
		}
	}

	for(int i=0;i<=c.s;i++)
		c[1][i]=min(c[1][i],c[0][i]+val[x]);
	return c;
}
Dp dfs(int x)
{
	vis[x]=true;
	Dp ans;
	ans.s=1;
	ans[0]={0,INF};
	ans[1]={val[x],INF};
	ans[2]={INF,INF};
	for(auto v:e[x])
	{
		if(!vis[v])
			ans=merge(ans,dfs(v),x);
	}
	
	//cerr<<x<<":\n";
	//for(int j=0;j<3;j++)
	//{
	//	for(int i=0;i<=ans.s;i++)
	//		cerr<<ans[j][i]<<" \n"[i==ans.s];
	//}
	//cerr<<"\n";

	return ans;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		cin>>val[i];
	for(int i=1;i<=m;i++)
	{
		int a,b;
		cin>>a>>b;
		e[a].push_back(b);
		e[b].push_back(a);
	}
	Dp ans;
	ans.s=0;
	for(int i=0;i<3;i++)
		ans[i]={0};
	for(int i=1;i<=n;i++)
	{
		if(!vis[i])
			ans=merge_disjoint(ans,dfs(i));
	}

	//cerr<<"ans:\n";
	//for(int i=0;i<=n;i++)
	//	cerr<<ans[0][i]<<" \n"[i==n];

	int q;
	cin>>q;
	while(q--)
	{
		long long x;
		cin>>x;
		int bg=0,en=n;
		while(bg<en)
		{
			int mid=(bg+en+1)/2;
			if(ans[0][mid]<=x)
				bg=mid;
			else
				en=mid-1;
		}
		cout<<bg<<"\n";
	}
	return 0;
}

# Verdict Execution time Memory Grader output
1 Incorrect 9 ms 904 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 9 ms 904 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 40 ms 2372 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 9 ms 904 KB Output isn't correct
2 Halted 0 ms 0 KB -