BZOJ1528: [POI2005]sam-Toy Cars

1528: [POI2005]sam-Toy Cars

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 282  Solved: 129
[Submit][Status]

Description

Jasio 是一个三岁的小男孩,他最喜欢玩玩具了,他有n 个不同的玩具,它们都被放在了很高的架子上所以Jasio 拿不到它们. 为了让他的房间有足够的空间,在任何时刻地板上都不会有超过k 个玩具. Jasio 在地板上玩玩具. Jasio'的妈妈则在房间里陪他的儿子. 当Jasio 想玩地板上的其他玩具时,他会自己去拿,如果他想玩的玩具在架子上,他的妈妈则会帮他去拿,当她拿玩具的时候,顺便也会将一个地板上的玩具放上架子使得地板上有足够的空间. 他的妈妈很清楚自己的孩子所以他能够预料到Jasio 想玩些什么玩具. 所以她想尽量的使自己去架子上拿玩具的次数尽量的少,应该怎么安排放玩具的顺序呢?

Input

第一行三个整数: n, k, p (1 <= k <= n <= 100.000, 1 <= p <= 500.000), 分别表示玩具的总数,地板上玩具的最多个数以及Jasio 他想玩玩具的序列的个数,接下来p行每行描述一个玩具编号表示Jasio 想玩的玩具.

Output

一个数表示Jasio 的妈妈最少要拿多少次玩具.

Sample Input

3 2 7
1
2
3
1
3
1
2

Sample Output

4

HINT

 

Source

题解:

貌似做过这种类型的题好多次了,可是这次数据范围奇大啊。

如果n<=500 m<=500 大概可以费用流搞掉。。

扩大了100倍。。。

然后贪心大法上场了:

当前要取出的那个玩具在序列中下一次出现的位置要尽量靠后。用堆维护。

好神的贪心。。。

有道理但是严格的正确性还不会T_T

挖坑

代码:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<iostream>
 7 #include<vector>
 8 #include<map>
 9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 500000+100
14 #define maxm 500+100
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 #define for0(i,n) for(int i=0;i<=(n);i++)
19 #define for1(i,n) for(int i=1;i<=(n);i++)
20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
22 #define mod 1000000007
23 using namespace std;
24 inline int read()
25 {
26     int x=0,f=1;char ch=getchar();
27     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
28     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
29     return x*f;
30 }
31 int n,m,k,ans,cnt,a[maxn],last[maxn],next[maxn];
32 bool v[maxn];
33 priority_queue<pa,vector<pa>,less<pa> >q;
34 int main()
35 {
36     freopen("input.txt","r",stdin);
37     freopen("output.txt","w",stdout);
38     n=read();k=read();m=read();
39     for1(i,m)
40     {
41         a[i]=read();
42         next[last[a[i]]]=i;
43         last[a[i]]=i;
44     }
45     for1(i,n)next[last[i]]=m+1;
46     for(int i=1;i<=m;i++)
47     {
48         if(v[a[i]]){q.push(pa(next[i],a[i]));continue;}
49         if(cnt<k)
50          {
51              cnt++;ans++;
52              q.push(pa(next[i],a[i]));
53              v[a[i]]=1;
54          }
55         else
56          {
57              v[q.top().second]=0;
58              q.pop();
59              v[a[i]]=1;ans++;
60              q.push(pa(next[i],a[i]));
61          }
62     }
63     printf("%d\n",ans);
64     return 0;
65 }
View Code

 

posted @ 2014-10-12 11:51  ZYF-ZYF  Views(252)  Comments(0Edit  收藏  举报