Codeforces 788C The Great Mixing (Bfs/dp+bitset optimized)

Source: Internet
Author: User
Test Instructions:N (1e3), K (1e6), A1~ak (1E3). Select the minimum number of AI (can be repeated) T, so that sum (AI)/t/1000==n/1000 set up. The following:
K (1e6), A1~ak (1E3)--and the number is only 1e3.
SUM (AI)/t/1000==n/1000--sum (AI) ==t*n---sum (ai-n) ==0
t<=2000: In the case of a solution, there is at least one solution: Select Ai,aj,ai<n,aj>n,t=abs (AI) +abs (AJ)

Solution One: BFS
If there is a set of solutions b1+b2+...+bt==0, then we can change the position of the array elements so that all prefixes and ranges are [ -1000,1000].
Then we can use 0 as the starting point for BFS, until we return to 0.

Solution Two: Dp+bitset
Consider the naïve DP notation, DP[I][J], which selects whether I elements can reach the state J. The pseudo code is as follows:
for (int i=1;i<=2000;++i) {//enumerator, up to 2000 times for
	(int j=0;j<=2000;++j) {//State unified plus 1000 becomes positive for
		(int x=1;x<= M;++X) {
			if (0<=j-a[x]&&j-a[x]<=2000) d[i][j] |= d[i-1][j-a[x];}}}

The complexity of doing this is N3, considering that there are only 12 of DP states, we can optimize with bitset. The second layer of circulation is pressed into bitset, and the complexity becomes N3/64.
Bitset Internal implementation:
The internal maintenance of a long array, initially only a long, so bitset the smallest size is 64, when the storage of elements more and more, the bitset inside will dynamically expand, the final internal is by n a long to store, these operations are transparent.

#include <cstdio> #include <map> #include <cstdlib> #include <queue> using namespace std;
#define MP Make_pair #define FI first #define SE second const int n=2020;
Map<int,bool> Vis,vi;

int a[n];
	int main () {int x,m;
		while (~SCANF ("%d%d", &x,&m)) {///init vis.clear ();

		Vi.clear ();
		Read int n=0;
			for (int i=1;i<=m;++i) {int t;scanf ("%d", &t);
			T-=x;
			if (!vis[t]) a[++n]=t;
		Vis[t]=1;
			}//if (x==0) {if (Vis[0]) puts ("1");
			Else puts ("-1");
		Continue
		}//int cnt0=0,cnt1=0;
			for (int i=1;i<=n;++i) {if (a[i]>0) ++cnt0;
		if (a[i]<0) ++cnt1; } if (cnt0==n| |
			Cnt1==n) {puts ("-1");
		Continue
		}///solve queue<pair<int,int> > Q;
		Q.push (MP (0,0));
		BOOL Flag=1;
		int ans; for (; flag;)
			{pair<int,int> now=q.front (); Q.pop ();
					for (int i=1;i<=n;++i) {if (now.fi+a[i]==0) {flag=0;
					ans=now.se+1;
				Break
				} int nxt=now.fi+a[i]; If(ABS (NXT) &LT;=1000&AMP;&AMP;!VI[NXT])
					{Q.push (MP (nxt,now.se+1));
				Vi[nxt]=1;

	}}}///print printf ("%d\n", ans);
} return 0; }

#include <cstdio>
#include <map>
#include <bitset>
using namespace std;

const int n=1007;
Map<int,bool> Vis;
Bitset<n*2> dp[2];
int a[n];

int main () {
	int x,m;
	while (~SCANF ("%d%d", &x,&m)) {
		///init
		vis.clear ();

		Read
		int n=0;
		for (int i=1;i<=m;++i) {
			int t;scanf ("%d", &t);
			if (!vis[t]) a[++n]=t;
			vis[t]=1;
		}

		Solve
		int now=0,ans=-1;
		Dp[now].reset ();
		Dp[now][1000]=1;

		for (int i=1;i<=2000&&ans==-1;++i) {

			now=1-now;
			Dp[now].reset ();

			for (int j=1;j<=n;++j) {
				Dp[now] |= (dp[1-now]<<a[j]) >>x;
				if (dp[now][1000]) {
					ans=i;
					Break

		;

		}}} Print
		printf ("%d\n", ans);
	}
	return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.