CF79D 【Password】

Original title description
D. Password
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Finally Fox Ciel arrived in front of her castle!

She have to type a password to enter her castle. An input device attached to her castle is a bit unusual.

The input device is a 1 × n rectangle divided into n square panels. They are numbered 1 to n from left to right. Each panel has a state either ON or OFF. Initially all panels are in the OFF state. She can enter her castle if and only if x1-th, x2-th, ..., xk-th panels are in the ON state and other panels are in the OFF state.

She is given an array a1, ..., al. In each move, she can perform the following operation: choose an index i (1 ≤ i ≤ l), choose consecutive ai panels, and flip the states of those panels (i.e. ON → OFF, OFF → ON).

Unfortunately she forgets how to type the password with only above operations. Determine the minimal number of operations required to enter her castle.

Input

The first line contains three integers nk and l (1 ≤ n ≤ 10000, 1 ≤ k ≤ 10, 1 ≤ l ≤ 100), separated by single spaces.

The second line contains k integers x1, ..., xk (1 ≤ x1 < x2 < ... < xk ≤ n), separated by single spaces.

The third line contains l integers a1, ..., al (1 ≤ ai ≤ n), separated by single spaces. It is possible that some elements of the array ai are equal value.

Output

Print the minimal number of moves required to type the password. If it's impossible, print -1.

Examples
input
10 8 2
1 2 3 5 6 7 8 9
3 5
output
2
input
3 2 1
1 2
3
output
-1
Note

One possible way to type the password in the first example is following: In the first move, choose 1st, 2nd, 3rd panels and flip those panels. In the second move, choose 5th, 6th, 7th, 8th, 9th panels and flip those panels.

 Translated title face:

Question D: lock (password)

Title Description

hzwer has a lock, of N switch. In the beginning, all switches are turned on. If and only if the switch x1, x2, x3, ... xk is open, the other switch is off, the lock will open.

He may operate M thereof, each operation has a size [i], he said that if he chose the i-th operation, he can select consecutive size [i] lattice arbitrary, they all negated. (Note that since gold is very great God of God, so the number of operations can be infinitely> _ <)

Originally this is a trivial issue, but, God, Gold accidentally lost his money went in, there is no money where he can avoid being chenzeyu97 NTR fate? > _ <Then, he burst CZY for abuse, but also to more defoaming sister, decided to open the lock. But he was so God did not bother this "water problem." So he found you.

Your mission is very simple, in order to obtain a minimum number of steps required to open the lock, or if no solution, please output -1.

Entry

Line 1, three positive integer N, K, M, as the subject.

Line 2, K a positive integer indicating switch x1, x2, x3..xk must be open, ensure x pairwise disjoint.

Third row, M a positive integer representing the size [i], size [] may have a repeating element.

Export

Output answer, no solution output -1.

Sample input [2]

3 2 1

1 2

3

Sample output [2]

-1

[Data] scale

For 50% of the data, 1≤N≤20,1≤k≤5,1≤m≤3;

For the other 20% of data, 1≤N≤10000,1≤k≤5,1≤m≤30;

To 100% of the data, 1≤N≤10000,1≤k≤10,1≤m≤100.

Sample input  Copy

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

Sample output  Copy

2 

Solution:

Tags: + bfs + dp-shaped pressure

Although the title says the range is modified, but we can consider its differential sequence, so that the number of possible positions to be modified into two.

We should be very easy to find, this problem can use reverse thinking, as long as the calculated minimum modified so that the original sequence becomes the all-0 on it.

so, the difference of the original sequence, then each modification is to add 1 [] at the position you number i and i + size 2 sense die. Easy to see: the difference in the sequence, the value is the number 1, will not exceed the 2k, i.e., not more than 20.

Consider each of the process i and i + x changes, if the original sequence, the position number i and i + x is the number 0 position, then there is no need to make changes. Therefore any time, the value 1 is the number of positions is not increased. so, we can put each 1 as a pebble, then we can put each stone to move in a certain direction size [] step, if there is a position after moving stones, then it cancel each other out.

At this point, it is easy to see the relationship between the stones is definitely a matching relationship, otherwise there is no offset this. Let's all be over bfs point 1, to obtain Dist [i] [j] said gravel stones j i went to the location, at least how many steps need to be moved, the complexity of this part is O (2kmn).

Now the problem into a size of no more than 20 completely, we would like to request its minimum weight maximum matching.

It can be used like pressure DP, provided F [S] represents the minimum cost of set S is divided, each smallest element selected as one of the matching element, another element can be enumerated.

The Code

#include<bits/stdc++.h>
using namespace std;
const int M=1e4+5,INF=1<<29;
int n,k,m,cnt;
int a[M],num[M],Dist[25][25],size[M];
int visited[M],Distance[M],f[1<<25];
queue<int> q;
void bfs(int x,int id) 
{
    memset(visited,0,sizeof(visited));
    memset(Distance,0x3f3f3f3f,sizeof Distance);
    q.push(x);
	visited[x]=1; Distance[x]=0;
    while(!q.empty()) 
	{
        x=q.front();q.pop();
        for(int i=1; i<=m; i++)
		{
			int tmp=size[i];
            if((x+tmp)<=n && !visited[x+tmp]) 
			{
                visited[x+tmp]=1;
				Distance[x+tmp]=Distance[x]+1;
				q.push(x+tmp);
            }
            if((x-tmp)>=1 && !visited[x-tmp]) 
			{
                visited[x-tmp]=1;
				Distance[x-tmp]=Distance[x]+1;
				q.push(x-tmp);
            }
        }
    }
    for(int i=1; i<=cnt; i++)
	{
        if(visited[num[i]]) Dist[id][i]=Distance[num[i]];
        else Dist[id][i]=INF;
    } 
}
void dp()
{
	for(int i=1; i<(1<<cnt); i++) f[i]=INF;
    for(int i=0,j; i<(1<<cnt); i++)
	{
        for(int k=1; k<=cnt; k++)
		    if((1<<k-1)&i) 
			{
			    j=k;
				break;
			}
        for(int k=1; k<=cnt;a ++)
		    if((1<<k-1)&i) 
                f[i]=min(f[i],f[i^(1<<j-1)^(1<<k-1)]+Dist[j][k]);
    }
}
int main() 
{
    scanf("%d%d%d",&n,&k,&m);
    for(int i=1; i<=k; i++)  
    {
    	int x; scanf("%d",&x);
    	a[x]=1;
	}
    for(int i=1; i<=m; i++) scanf("%d",&size[i]);
    for(int i=++n; i>=1; i--) a[i]^=a[i-1];
    for(int i=1; i<=n; i++) 
	    if(a[i]) 
		{
			a[i]=++cnt;
			num[cnt]=i;
		}
    for(int i=1; i<=n; i++) 
	    if(a[i]) bfs(i,a[i]);
	dp(); 
    if(f[(1<<cnt)-1]==INF) puts("-1");
    else printf("%d\n",f[(1<<cnt)-1]);
    return 0;
}

  2020-02-1415:38:47

Guess you like

Origin www.cnblogs.com/crh1272336175/p/12307648.html