• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

CF1393CPinkiePieEatsPatty-cakes

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

思路:

二分+贪心。

实现:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N = 100001;
 4 int cnt[N], a[N], n;
 5 bool check(int x)
 6 {
 7     set<pair<int, int>, greater<pair<int, int>>> st; 
 8     for (int i = 1; i <= n; i++) cnt[i] = 0;
 9     for (int i = 0; i < n; i++) cnt[a[i]]++;
10     for (int i = 1; i <= n; i++)
11     {
12         if (cnt[i]) st.insert(make_pair(cnt[i], i));
13     } 
14     vector<int> v;
15     for (int i = 0; i < n; i++)
16     {
17         if (i >= x + 1)
18         {
19             int j = v[i - x - 1];
20             if (cnt[j] > 0) st.insert(make_pair(cnt[j], j));
21         }
22         if (st.empty()) return false;
23         auto it = st.begin();
24         v.push_back(it->second);
25         assert(cnt[it->second] > 0);
26         cnt[it->second]--;
27         st.erase(it);
28     }
29     return true;
30 }
31 int main()
32 {
33     int T; cin >> T;
34     while (T--)
35     {
36         cin >> n;
37         for (int i = 0; i < n; i++) cin >> a[i];
38         int l = 0, r = n - 2, res = 0;
39         while (l <= r)
40         {
41             int m = l + r >> 1;
42             if (check(m))
43             {
44                 res = m;
45                 l = m + 1;
46             }
47             else r = m - 1;
48         }
49         cout << res << endl;
50     }
51     return 0;
52 }

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# linq to entity内一行代码实现多元判断发布时间:2022-07-13
下一篇:
C#Attribute实现简单的AOP处理的例子(转)发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap