ZOJ Consecutive Blocks(左右指標移動)
題意很簡單就是讓你看看隔著k個不同的,最多還能構成多少個連續的啊。
10^5託託的不能o(n^2),顯然是左右指標瞎搞啊,但是這道題可以排序使得更簡單。
按照數字的大小,和地址的先後二級排序,然後分相同數字和不相同數字進行討論,找到滿足條件的最優的解。
There are N (1 ≤ N ≤ 105) colored blocks (numbered 1 to N from left to right) which are lined up in a row. And the i-th block's color is Ci (1 ≤ Ci ≤ 109). Now you can remove at most K (0 ≤ K ≤N) blocks, then rearrange the blocks by their index from left to right. Please figure out the length of the largest consecutive blocks with the same color in the new blocks created by doing this.
For example, one sequence is {1 1 1 2 2 3 2 2} and K=1. We can remove the 6-th block, then we will get sequence {1 1 1 2 2 2 2}. The length of the largest consecutive blocks with the same color is 4.
Input
Input will consist of multiple test cases and each case will consist of two lines. For each test case the program has to read the integers N and K, separated by a blank, from the first line. The color of the blocks will be given in the second line of the test case, separated by a blank. The i-th integer means Ci.
Output
Please output the corresponding length of the largest consecutive blocks, one line for one case.
Sample Input
8 1 1 1 1 2 2 3 2 2
Sample Output
4
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x7ffffff
#define PI 3.1415926535898
const int maxn = 101000;
using namespace std;
struct node
{
int x;
int point;
}f[maxn];
bool cmp(node a, node b)
{
if(a.x == b.x)
return a.point < b.point;
return a.x < b.x;
}
int main()
{
int n, k;
while(cin >>n>>k)
{
int x;
for(int i = 0; i < n; i++)
{
cin >>x;
f[i].x = x;
f[i].point = i;
}
sort(f, f+n, cmp);
int Max = -1;
int i, j;
int r = 1;
int dis = 1;
for(i = 0; i < n;)
{
dis = r-i;
for(j = r; j < n; j++)
{
if(f[i].x != f[j].x)
{
Max = max(Max, dis);
i = j;
r = j+1;
break;
}
if(f[j].point-f[i].point > j-i+k)
{
Max = max(Max, dis);
i++;
dis--;
r = j;
break;
}
dis++;
}
if(j == n)
{
Max = max(dis, Max);
break;
}
}
cout<<Max<<endl;
}
return 0;
}
相關文章
- python移動檔案指標seekPython指標
- fgets函式執行完成後,檔案指標如何移動?函式指標
- NULL 指標、零指標、野指標Null指標
- Win10系統下如何取消移動滑鼠出現指標軌跡Win10指標
- Windows10系統下設定滑鼠指標移動速度的方法Windows指標
- 野指標 空指標指標
- Nielsen:移動廣告績效指標CTR和SAR並不一致指標
- TalkingData:2015年4月移動遊戲Benchmark指標資料遊戲指標
- 指標常量和常量指標指標
- 誰動了我的指標? (轉)指標
- C語言指標(三):陣列指標和字串指標C語言指標陣列字串
- 陣列指標,指標陣列陣列指標
- ARC中強指標與弱指標指標
- 控制指標與統計指標指標
- 陣列指標 指標陣列陣列指標
- 關於指標傳遞和指標的指標指標
- 在Code::Blocks中使用C++11標準BloCC++
- 指標指標
- 指向指標的指標指標
- 點選左右箭頭可以移動選項例項程式碼
- 指標陣列與陣列指標指標陣列
- 詳解 常量指標和指標常量指標
- 陣列指標和指標陣列陣列指標
- 指標問題的一點體會(區別 [指向指標的指標] 與 [指標的指標] .) (轉)指標
- Js 跟手轉動的羅盤指標JS指標
- 廣東移動經營分析系統指標資料(摘自嚴飛雪報告)指標
- 通達信移動籌碼三合一主圖指標公式原始碼指標公式原始碼
- c語言野指標與結構體指標動態記憶體分配小解C語言指標結構體記憶體
- 第 10 節:複合型別-5. 指標 -- 指標與指標變數 -8. 多級指標型別指標變數
- 動態規劃-ZOJ 1 163 THE STAIRCASES動態規劃AI
- Blocks Programming Helper ↗ Blocks in System APIsBloCAPI
- C指標原理(15)-C指標基礎指標
- C指標原理(14)-C指標基礎指標
- vim實用指南:游標移動——行內、行間移動
- 函式指標之回撥函式和轉移表函式指標
- 移動端指紋識別(長按事件)事件
- pc滑鼠左右移動
- GO 指標Go指標