Description
n个人买票,第i个人要买编号为a[i]的票,有m张票,第i张编号为b[i],每次买票只能给连续的一段区间的人买连续的区间票且最多买k张,问至少要买多少张票
Input
第一行输入三个整数n,m,k分别表示人数,票数和每次买票数量上限,之后输入n个整数a[i]表示第i个人要买的票的编号,然后输入m个整数b[i]表示第i张票的编号(1<=n,m,k<=1e5,1<=a[i],b[i]<=1e9),保证有解
Output
输出最少买票次数cnt,然后输出cnt行,第i行表示第i次买票的数量和对应的编号
Sample Input
4 6 2
1 4 5 6
1 2 4 5 6 8
Sample Output
3
1 1
2 2 3
1 4
Solution
贪心,如果票满足要求且还能买就一直买
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define maxn 111111
int n,m,k,a[maxn],b[maxn],res;
vector<int>ans[maxn];
int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
res=0;
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=m;i++)scanf("%d",&b[i]);
int i=1,j=1;
while(i<=n)
{
res++;
while(b[j]!=a[i])j++;
int cnt=0;
ans[res].clear();
while(i<=n&&cnt<k&&a[i]==b[j])ans[res].push_back(i),cnt++,i++,j++;
}
printf("%d\n",res);
for(int i=1;i<=res;i++)
{
printf("%d",ans[i].size());
for(int j=0;j<ans[i].size();j++)printf(" %d",ans[i][j]);
printf("\n");
}
}
return 0;
}