[USACO] Gold组刷题记录

题目取自BZOJ,为了查找方便,Gold组将收录进同一篇博客

1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 

 二分答案(长度)+ Hash 记录出现次数

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #define ULL unsigned long long
 5 #define maxn 1000007
 6 using namespace std;
 7 
 8 const ULL base = 21239;
 9 int n,k,ans;
10 ULL h[maxn],P[maxn];
11 
12 struct edge{ int from,ans; ULL val; }e[maxn];
13 int tot,first[maxn];
14 void Insert(int L,int R){
15     ULL code = h[R]-h[L-1]*P[R-L+1];
16     int w = code%maxn;
17     for(int i = first[w];i;i = e[i].from){
18         if(e[i].val == code){ ans = max(ans,++e[i].ans); return; }
19     }e[++tot].from = first[w], first[w] = tot, e[tot].val = code;
20     e[tot].ans = 1; ans = max(ans,1);
21 }
22 
23 bool Judge(int len){
24     ans = 0,tot = 0;
25     memset(first,0,sizeof(first));
26     for(int i = 1;i <= n-len+1;i++){
27         Insert(i,i+len-1);
28     }return ans >= k;
29 }
30 
31 int read(){
32     int ret = 0; char ctr = getchar();
33     while(ctr < '0' || ctr > '9') ctr = getchar();
34     while(ctr >= '0' && ctr <= '9') ret = ret*10+ctr-'0',ctr = getchar();
35     return ret;
36 }
37 
38 int main(){
39     n = read(),k = read();
40     
41     if(!k) return printf("%d",n),0;
42     
43     P[0] = 1;
44     for(int i = 1;i <= n;i++) 
45         h[i] = h[i-1]*base+read(),
46         P[i] = P[i-1]*base;
47     
48     int L = 0,R = n,mid;
49     while(L < R){
50         mid = (L+R+1)/2;
51         if(Judge(mid)) L = mid;
52         else R = mid-1;
53     }printf("%d",L);
54     
55     return 0;
56 }
据说我用的hash叫做秦九韶?

 1724: [Usaco2006 Nov]Fence Repair 切割木板

合并果子qwq

首先我们可以研究怎么让每一次切都非常接近木板中点,我们可以来一次逆向的归并排序

使得木板的切点尽量在中间

然后orzccz跟我说这是合并果子

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<queue>
 5 using namespace std;
 6 
 7 int n,cnt;long long tot;
 8 
 9 int read(){
10     int ret = 0; char ctr = getchar();
11     while(ctr < '0' || ctr > '9') ctr = getchar();
12     while(ctr >= '0' && ctr <= '9') ret = ret*10+ctr-'0',ctr = getchar();
13     return ret;
14 }
15 
16 priority_queue<int,vector<int>,greater<int> > Q;
17 
18 int main(){
19     n = read();
20     
21     for(int i = 1;i <= n;i++){
22         cnt = read();
23         Q.push(cnt);
24     }
25     
26     for(int i = 1;i < n;i++){
27         int a = Q.top(); Q.pop();
28         int b = Q.top(); Q.pop();
29         tot += a+b;
30         Q.push(a+b);
31     }
32     printf("%lld",tot);
33     return 0;
34 }
优先队列默认大根堆

 

转载于:https://www.cnblogs.com/Chorolop/p/7673873.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值