Chef's DreamProblem code: DREAM |
The Chef is sleeping now. He tries to cook new kind of meals in his dream.
These meals are arranged in a row and numbered from 1 to N consecutively. For each meal i (1<=i<=N) there is given one integer f(i) which denotes the time needed to cook it. Initially, all meals are uncooked. Each assistant of The Chef (there are infinite number of them) can help him with cooking.
The abilities of all assistants are same. There can be at most one assistant cooking at each moment. He must choose some continuous subsequence of meals with length K(any such subsequence can be chosen). And if there are uncooked meals in it, he will cook all uncooked meals which has the minimum cooking time among uncooked meals in the chosen subsequence. Nothing done to another meals.
The dream was so interesting that he tried to solve such a problem: What is the minimum number of assistants which can cook all the meals assuming that each of them will cook at most once? But since the bell rings and Chef's friends has come to visit him, he will wake up after 2 seconds. Your program should calculate the answer before The Chef will come to himself.
First line of input file contains two integers N (1<=N<=105) and K (1<=K<=N), followed by a line containing N integers. The ith integer denotes f(i)-the cooking time of meal number i (1<=f(i)<=109)
Print minimum number of assistans which can cook all the meals in one line.
Input: 5 3 40 30 40 30 40 Output: 3
Other schedules can also be possible.
题意: 输入n k n个数 区间长度k
然后每次在区间内把最小的数标记 问所有的都标记后要多少次
/*我一开始拿到这道题 手无足措不知道该如何下手 这种题千万注意 一定要多写几组数据 认真研究 如果多写几组数据就会发现如下规律*/ #include <iostream> #include <map> #include <cstdio> using namespace std; const int sz=200200; int B[sz],N,K,ans; map<int,bool> hm; int main() { int f; scanf("%d%d",&N,&K); for(int i=1;i<=N;i++) { scanf("%d",&f); if(!hm[f])//超级帅 把复杂度降低到了log(n),方法很巧妙 { hm[f]=true; ans++; B[i+K-1]=f; } hm[B[i]]=false; } for(i=1;i<=N;i++) printf("%d\n",B[i]); printf("%d\n",ans); return 0; }