Bzoj1528: [poi2005] Sam-toy cars

Source: Internet
Author: User

1528: [poi2005] Sam-toy carstime limit: 5 sec memory limit: 64 MB
Submit: 282 solved: 129
[Submit] [Status] Descriptionjasio is a three-year-old boy who prefers to play with toys. He has n different toys, all of which are placed on a high shelf, so Jasio cannot get them. in order to make his room have enough space, there will be no more than K toys on the floor at any time. jasio plays with toys on the floor. jasio's mother is in the room with his son. when Jasio wants to play with other toys on the floor, he will take them by himself. If the toys he wants to play are on the shelf, his mother will help him get them, when she took the toys, she also put the toys on the floor on the shelf so that there was enough space on the floor. his mother knows her children well, so he can predict what toys Jasio wants to play. so she tried to minimize the number of times she had to take toys on the shelf. How should she arrange the order of toys? Three integers in the first line of input: N, K, P (1 <= k <= n <= 100.000, 1 <= P <= 500.000), indicating the total number of toys, respectively, the maximum number of toys on the floor and the number of sequences of the toys that Jasio wants to play with. Next, each row in line P describes a number of toys that Jasio wants to play. output A number indicates the minimum number of times that Jasio's mother needs to take toys. sample input3 2 7
1
2
3
1
3
1
2
Sample output4
Hint Source

Question:

It seems that this type of question has been done many times, but this time the data range is very large.

If n <= 500 m <= 500, fees can be lost ..

It is increased by 100 times...

Then, the greedy algorithm went on stage:

The next appearance of the current toy in the sequence should be pulled back as much as possible. Use heap for maintenance.

Good God's greed...

It makes sense, but the strict correctness is not t_t.

Pitfall

Code:

 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 100000000013 #define maxn 500000+10014 #define maxm 500+10015 #define eps 1e-1016 #define ll long long17 #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 100000000723 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         else56          {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

 

Bzoj1528: [poi2005] Sam-toy cars

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.