您的位置:首页 > 其它

P1063 数字串

2016-03-22 22:52 274 查看
本以为要动规什么的,结果居然是一边录,一边算

也许,也许…….

当然,因为没有

if (answer!=n)
{
cout << answer << endl;

}
else
{
cout << "NO" << endl;
}


出现了wa

哼哼,这次我可是开足了空间

代码如下

// 13_P1063 数字串.cpp : 定义控制台应用程序的入口点。
//
/*
P1063 数字串
时间: 1000ms / 空间: 131072KiB / Java类名: Main
描述

给你一个长度为n的数字串,数字串里会包含1-m这些数字。如果连续的一段数字子串包含了1-m这些数字,则称这个数字字串为NUM串。
你的任务是求出长度最短的NUM串是什么,只需要输出这个长度即可。
1<=n,m<=200000
输入格式

第一行给定n和m。
第二行n个数,表示数字串,数字间用空格隔开。
输出格式

如果存在NUM串则输出最短NUM串长度,否则输出“NO”。
测试样例1

输入

5 3
1 2 2 3 1
输出

3
*/
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
int n, m;
int a[2000000], b[2000000];
void print(int *c)
{
for (int i = 0; i < n; i++)
{
cout << setw(5) << c[i];
if (i%5==4)
{
cout << endl;
}
}
cout << endl;
}
int main()
{
int begin, end;
begin = 0;
end = 0;
int num = 0;
cin >> n >> m;
int answer = n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
if (b[ a[i]-1 ]==0)
{
num++;
}
b[a[i]
4000
-1]++;
end++;
while(b[ a[begin]-1 ]>1)
{
//cout <<"b a"<< b[a[begin] - 1] << " " << a[begin] - 1 << endl;
b[a[begin]-1]--;
begin++;
//cout << b[a[begin] - 1] << endl;
//print(b);
}
if (num == m)
{
answer = min(answer, end - begin);
//cout << end << " " << begin << " " << end - begin << endl;
}
}if (answer!=n) { cout << answer << endl; } else { cout << "NO" << endl; }
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: