# BZOJ1584: [Usaco2009 Mar]Cleaning Up 打扫卫生

n<=40000个<=m<=n的数，一段数不和谐（河蟹？？？）度为该段中不同数的个数的平方，求把n个数划成若干段后的最小不和谐度。

 1 #include<stdio.h>
2 #include<string.h>
3 #include<algorithm>
4 #include<cstdlib>
5 #include<math.h>
6 //#include<iostream>
7 using namespace std;
8
9 int n,m;
10 #define maxn 40011
11 int cnt[205][maxn],a[maxn],pos[maxn],f[maxn];
12 int main()
13 {
14     scanf("%d%d",&n,&m);m=(int)sqrt(n)+1;
15     for (int i=1;i<=n;i++) scanf("%d",&a[i]);
16     for (int i=1;i<=m;i++) pos[i]=-i*i;
17     f[0]=0;
18     memset(cnt,0,sizeof(cnt));
19     for (int i=1;i<=n;i++)
20     {
21         for (int j=1;j<=m;j++)
22         {
23             if (pos[j]>0)
24             {
25                 cnt[j][a[pos[j]]]--;
26                 if (cnt[j][a[pos[j]]]==0) cnt[j][0]--;
27             }
28             pos[j]++;
29             cnt[j][a[i]]++;
30             if (cnt[j][a[i]]==1) cnt[j][0]++;
31         }
32         bool ok=0;
33         for (int j=1;j<=m;j++)
34             if (pos[j]>0 && cnt[j][0]<=j) ok=1;
35         f[i]=f[i-1]+(!ok);
36     }
37     printf("%d\n",f[n]);
38     return 0;
39 }
View Code

10 5
2 3 2 2 4 4 4 2 1 1

 1 #include<stdio.h>
2 #include<string.h>
3 #include<algorithm>
4 #include<cstdlib>
5 #include<math.h>
6 //#include<iostream>
7 using namespace std;
8
9 int n,m;
10 #define maxn 40011
11 int pos[maxn],last[maxn],cnt[maxn],a[maxn],f[maxn];
12 int main()
13 {
14     scanf("%d%d",&n,&m);m=(int)sqrt(n)+1;
15     for (int i=1;i<=n;i++) scanf("%d",&a[i]);
16     for (int i=1;i<=m;i++) pos[i]=1;
17     memset(last,0,sizeof(last));
18     memset(cnt,0,sizeof(cnt));
19     for (int i=1;i<=n;i++)
20     {
21         for (int j=1;j<=m;j++) if (last[a[i]]<pos[j]) cnt[j]++;
22         last[a[i]]=i;
23         for (int j=1;j<=m;j++) if (cnt[j]>j)
24         {
25             while (last[a[pos[j]]]!=pos[j]) pos[j]++;
26             pos[j]++;
27             cnt[j]--;
28         }
29         f[i]=n;
30         for (int j=1;j<=m;j++) f[i]=min(f[i],f[pos[j]-1]+j*j);
31     }
32     printf("%d\n",f[n]);
33     return 0;
34 }
View Code

posted @ 2017-07-21 18:26  Blue233333  阅读(104)  评论(0编辑  收藏  举报