2019年黑龙江省赛A题
Problem A
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 262144/262144K (Java/Other)
Total Submission(s) : 183 Accepted Submission(s) : 37
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
WNJXYK and VineAsh are friends. They often play puzzle games together.
One day, WNJXYK found a very interesting puzzle game. At the beginning of this game, he had infinite pattern puzzles, all of which are exactly the same and each pattern puzzle could be considered as a number sequence P1,P2,…Pn. Meanwhile, he had another number sequence T1,T2,…,Tm which is his target. During this game, he could pick up a pattern puzzle and delete some numbers in it (Or don’t delete any number at all) in order to build a processed puzzle. His goal is to build some processed puzzles and concatenate them together to construct his target.
This game is so easy. To show off in front of VineAsh, WNJXYK wanted to do something harder. He decided to use minimum number of pattern puzzles to construct the target. Soon after, he realized that even though he found a solution he could not prove that the solution uses minimum number of pattern puzzles.
Therefore, WNJXYK wants you to tell him the minimum number of pattern puzzles he needs to use in order to construct the target.
Input
There are multiple test causes.
The first line of input contains an integer T, representing the number of test cases.
For each test case, the first line contains two integers n, m, indicating the length of pattern puzzle and the length of target. The second line contains a sequence with n integers P1,P2,…,Pn which is the pattern puzzle. The third line contains a sequence with m integers T1,T2,…,Tn which is the target.
Output
For each test case, output the answer which is the minimum number of pattern puzzles WNJXKY needs to use in a line.
I11 addition, if WNJXKY could not construct the target with pattern puzzles, please output −1.
Sample Input
3
3 5
11 22 33
11 22 33 11 22
3 6
1 2 3
3 2 1 3 2 1
5 5
1 2 3 4 5
1 2 3 4 6
Sample Output
2
5
-1
Hint
1 ≤ T ≤ 10
1 ≤ n, m ≤ 1e5
1 ≤ Pi, Ti ≤ 1e9
For the second sample, “3”+“2”+“1 3”+“2”+“1”=“3 2 1 3 2 1”
题目大意:回头补上
思路:回头补上
AC代码:
#include<bits/stdc++.h>
#define INF 0x3F3F3F3F
#define endl '\n'
#define css(n) cout<<setiosflags(ios::fixed)<<setprecision(n);
using namespace std;
typedef long long ll;
const int maxn=1e5+50;
int n,m;
int t;
double a,b;
map<int,int> mp;
vector<int> vec[maxn];
int ans[maxn];
int main()
{
// std::ios::sync_with_stdio(false);
scanf("%d",&t);
while(t--)
{
int k=1;
mp.clear();
// cin>>n>>m;
scanf("%d%d",&n,&m);
/* for(int i=0;i<maxn;i++)
vec[i].clear();*/
// memset(ans,0,sizeof(ans));
for(int i=1;i<=n;i++)
{
int a;
cin>>a;
if(!mp[a])
{
mp[a]=k++;
vec[mp[a]].clear();
}
vec[mp[a]].push_back(i);
}
int fin=0;
int pos=0;
int flag=1;
for(int i=1;i<=m;i++)
{
int a;
// cin>>a;
scanf("%d",&a);
if(!mp[a]) flag=0;
else ans[i]=mp[a];
}
if(flag==0)
{
// cout<<"-1"<<endl;
printf("-1\n");
continue;
}
else
{
int i=1;
fin=0;
while(i<=m)
{
fin++;
int pos=vec[ans[i]][0];
i++;
while(i<=m)
{
vector<int>::iterator it;
/* int it=upper_bound(vec[ans[i]].begin(),vec[ans[i]].end(),pos)-vec[ans[i]].begin();
if(it>=vec[ans[i]].size())
{
break;
}
pos=vec[ans[i]][it];*/
it=upper_bound(vec[ans[i]].begin(),vec[ans[i]].end(),pos);
if(it==vec[ans[i]].end())
{
break;
}
pos=*it;
i++;
}
}
}
// cout<<fin<<endl;
printf("%d\n",fin);
}
return 0;
}