A.求两个时间比较一下。

#include<bits/stdc++.h>
using namespace std; int n,t,k,d; int main()
{
ios::sync_with_stdio(false);
cin >> n >> t >> k >> d;
int tt = n/k;
if(n%k) tt++;
int sum1 = tt*t;
int t1 = ,t2 = d,now = ;
while(now < tt)
{
if(t1 < t2) t1 += t;
else t2 += t;
now++;
}
int sum2 = max(t1,t2);
if(sum1 > sum2) cout << "YES" << endl;
else cout << "NO" << endl;
return ;
}

B.按价格排序,预记录每种颜色的位置,询问的时候保存前一个可行的位置,线性扫即可。

#include<bits/stdc++.h>
using namespace std; int n,m,pos[][],ok[] = {};
struct xx
{
int p,a,b;
friend bool operator <(xx a,xx b)
{
return a.p < b.p;
}
}a[]; int main()
{
ios::sync_with_stdio(false);
cin >> n;
for(int i = ;i <= n;i++) cin >> a[i].p;
for(int i = ;i <= n;i++) cin >> a[i].a;
for(int i = ;i <= n;i++) cin >> a[i].b;
sort(a+,a++n);
int cnt[] = {};
for(int i = ;i <= n;i++)
{
int x = a[i].a,y = a[i].b;
pos[x][++cnt[x]] = i;
if(x != y) pos[y][++cnt[y]] = i;
}
cin >> m;
int now[] = {,,,,};
while(m--)
{
int x;
cin >> x;
while(ok[pos[x][now[x]]]) now[x]++;
if(now[x] > cnt[x]) cout << - << " ";
else
{
ok[pos[x][now[x]]] = ;
cout << a[pos[x][now[x]]].p << " ";
}
}
return ;
}

C.分三种情况

1.两堆各取一个,直接遍历取每堆的符合要求的最大b值。

2.在A堆取2个。先按p小到大排序,然后从头到尾枚举一个c,二分符合要求的c-p值最后一个的位置,在这一段rmq找最大的b值,更新最大的和。

3.在B堆取2个,同上。

#include<bits/stdc++.h>
using namespace std; int n,c,d;
int maxdp1[][],maxdp2[][];
struct xx
{
int b,x;
xx(){};
xx(int bb,int xx):b(bb),x(xx){};
friend bool operator <(xx a,xx b)
{
return a.x < b.x;
}
}a[],b[]; void rmq_init1(int len)
{
for(int i = ;i <= len;i++) maxdp1[i][] = a[i].b;
for(int j = ;(<<j) <= len;j++)
{
for(int i = ;i+(<<j)- <= len;i++)
{
maxdp1[i][j] = max(maxdp1[i][j-],maxdp1[i+(<<(j-))][j-]);
}
}
} void rmq_init2(int len)
{
for(int i = ;i <= len;i++) maxdp2[i][] = b[i].b;
for(int j = ;(<<j) <= len;j++)
{
for(int i = ;i+(<<j)- <= len;i++)
{
maxdp2[i][j] = max(maxdp2[i][j-],maxdp2[i+(<<(j-))][j-]);
}
}
} int rmq_max1(int l,int r)
{
int k = (int)(log((double)(r-l+))/log(2.0));
return max(maxdp1[l][k],maxdp1[r-(<<k)+][k]);
} int rmq_max2(int l,int r)
{
int k = (int)(log((double)(r-l+))/log(2.0));
return max(maxdp2[l][k],maxdp2[r-(<<k)+][k]);
} int main()
{
ios::sync_with_stdio(false);
cin >> n >> c >> d;
int cnt1 = ,cnt2 = ;
for(int i = ;i <= n;i++)
{
int x,y;
string s;
cin >> x >> y >> s;
if(s == "C")
{
a[++cnt1].b = x;
a[cnt1].x = y;
}
else
{
b[++cnt2].b = x;
b[cnt2].x = y;
}
}
int maxx = ;
int pos1 = -,max1 = ;
for(int i = ;i <= cnt1;i++)
{
if(a[i].x <= c && a[i].b > max1)
{
pos1 = i;
max1 = a[i].b;
}
}
int pos2 = -,max2 = ;
for(int i = ;i <= cnt2;i++)
{
if(b[i].x <= d && b[i].b > max2)
{
pos2 = i;
max2 = b[i].b;
}
}
if(pos1 != - && pos2 != -) maxx = max1+max2;
sort(a+,a++cnt1);
sort(b+,b++cnt2);
rmq_init1(cnt1);
rmq_init2(cnt2);
if(cnt1 > && a[].x+a[].x <= c)
{
for(int i = ;i <= cnt1;i++)
{
if(a[i].x >= c) break;
int t = upper_bound(a+,a+cnt1+,xx(,c-a[i].x))-a-;
if(t <= i) break;
maxx = max(maxx,a[i].b+rmq_max1(i+,t));
}
}
if(cnt2 > && b[].x+b[].x <= d)
{
for(int i = ;i <= cnt2;i++)
{
if(b[i].x >= d) break;
int t = upper_bound(b+,b+cnt2+,xx(,d-b[i].x))-b-;
if(t <= i) break;
maxx = max(maxx,b[i].b+rmq_max2(i+,t));
}
}
cout << maxx << endl;
return ;
}

还有一种方法,直接树状数组维护最大前缀max。

#include <bits/stdc++.h>
using namespace std; int n,c,d,C[],D[]; inline int lowbit(int x)
{
return x & (-x);
} void update(int tree[],int pos,int x)
{
while(pos <= )
{
tree[pos] = max(tree[pos],x);
pos += lowbit(pos);
}
} int getmax(int tree[],int pos)
{
int ans = INT_MIN;
while(pos > )
{
ans = max(ans,tree[pos]);
pos -= lowbit(pos);
}
return ans;
} int main()
{
ios::sync_with_stdio(false);
cin >> n >> c >> d;
for(int i = ;i <= ;i++)
{
C[i] = INT_MIN;
D[i] = INT_MIN;
}
int ans = ;
while(n--)
{
int b,p,t;
string s;
cin >> b >> p >> s;
if(s == "C")
{
if(p > c) continue;
t = max(getmax(C,c-p),getmax(D,d));
update(C,p,b);
}
else
{
if(p > d) continue;
t = max(getmax(C,c),getmax(D,d-p));
update(D,p,b);
}
ans = max(ans,t+b);
}
cout << ans << endl;
return ;
}

D.边长指数增长,我们需要的倍数很少,直接爆搜就可以了。

#include <bits/stdc++.h>
using namespace std; int a,b,w,h,n,A[],ans; void dfs(int x,int y,int now,int nowx,int nowy)
{
if(nowx >= x && nowy >= y)
{
ans = min(ans,now);
return;
}
if(now == n+) return;
if(A[now] == )
{
while(x > nowx)
{
nowx *= ;
now++;
}
while(y > nowy)
{
nowy *= ;
now++;
}
ans = min(ans,now);
return; }
if(x > nowx) dfs(x,y,now+,nowx*A[now+],nowy);
if(y > nowy) dfs(x,y,now+,nowx,nowy*A[now+]);
}
int main()
{
ios::sync_with_stdio(false);
cin >> a >> b >> w >> h >> n;
for(int i = ;i <= n;i++) cin >> A[i];
sort(A+,A++n);
reverse(A+,A++n);
ans = n+;
dfs((a-)/h+,(b-)/w+,,,);
dfs((a-)/w+,(b-)/h+,,,);
if(ans == n+) cout << - << endl;
else cout << ans << endl;
return ;
}

E.把原数组分为4类,①A、M都不喜欢v0。②A喜欢、M不喜欢。③A不喜欢、M喜欢。④A、M都喜欢。

我们枚举④中的数量,然后计算剩余②③中最少数量,剩下的数从①②③中小的挑出。

枚举④的数量从大到小,这样②③个数肯定是递增的,因此就能使用上一次保存②③的个数。

#include<bits/stdc++.h>
using namespace std; int n,m,k,a[];
int v0[],v1[],v2[],v3[];
long long sum0[] = {},sum1[] = {},sum2[] = {},sum3[] = {};
bool has1[] = {},has2[] = {}; int main()
{
ios::sync_with_stdio(false);
cin >> n >> m >> k;
for(int i = ;i <= n;i++) cin >> a[i];
int x;
cin >> x;
while(x--)
{
int xx;
cin >> xx;
has1[xx] = ;
}
cin >> x;
while(x--)
{
int xx;
cin >> xx;
has2[xx] = ;
}
int cnt0 = ,cnt1 = ,cnt2 = ,cnt3 = ;
for(int i = ;i <= n;i++)
{
if(has1[i] && has2[i]) v3[++cnt3] = a[i];
else if(has1[i]) v1[++cnt1] = a[i];
else if(has2[i]) v2[++cnt2] = a[i];
else v0[++cnt0] = a[i];
}
sort(v0+,v0++cnt0);
sort(v1+,v1++cnt1);
sort(v2+,v2++cnt2);
sort(v3+,v3++cnt3);
for(int i = ;i <= cnt0;i++) sum0[i] = sum0[i-]+v0[i];
for(int i = ;i <= cnt1;i++) sum1[i] = sum1[i-]+v1[i];
for(int i = ;i <= cnt2;i++) sum2[i] = sum2[i-]+v2[i];
for(int i = ;i <= cnt3;i++) sum3[i] = sum3[i-]+v3[i];
long long ans = 1e18;
int t1 = ,t2 = ;
for(int i = min(cnt3,m);i >= ;i--)
{
t1 = max(t1,k-i),t2 = max(t2,k-i);
if(t1+t2+i > m || t1 > cnt1 || t2 > cnt2) break;
if(cnt0+cnt1+cnt2+i < m) break;
int t0 = ;
while(t0+t1+t2+i < m)
{
int t = m-t1-t2-i;
if(cnt0 >= t && (t1 == cnt1 || v0[t] <= v1[t1+]) && (t2 == cnt2 || v0[t] <= v2[t2+])) {t0 = t;break;}
else if(t1 < cnt1 && (t2 == cnt2 || v1[t1+] <= v2[t2+])) t1++;
else t2++;
}
ans = min(ans,sum0[t0]+sum1[t1]+sum2[t2]+sum3[i]);
}
if(ans == 1e18) cout << - << endl;
else cout << ans << endl;
return ;
}

Codeforces_799的更多相关文章

随机推荐

  1. 1025 反转链表 (25 分)C语言

    题目描述 给定一个常数K以及一个单链表L,请编写程序将L中每K个结点反转.例如:给定L为1→2→3→4→5→6,K为3,则输出应该为 3→2→1→6→5→4:如果K为4,则输出应该为4→3→2→1→5 ...

  2. spring之为什么要使用AOP(面向切片编程)?

    需求1-日志:在程序执行期间追踪正在发生的活动: 需求2-验证:希望计算器只处理正数的运算: 一.普通方法实现 Calculator.java package com.gong.spring.aop. ...

  3. notepad++中cmd运行中文乱码?

    notepad++中有中文内容时,cmd运行时中文显示乱码,如何处理? 设置-->首选项-->新建-->选择ANSI编码(注意现在的文件不会被转换,要重新把代码拷入修建的文件中才可以 ...

  4. 2020面试还搞不懂MyBatis?快看看这27道面试题!(含答案和思维导图)

    前言 MyBatis是一个优秀的持久层ORM框架,它对jdbc的操作数据库的过程进行封装,使开发者只需要关注SQL 本身,而不需要花费精力去处理例如注册驱动.创建connection.创建statem ...

  5. kmp算法初步理解

    123456789 abbdaxnds Next   01212 第三位看第二位b,第二位和第三位相同,都是b,所以第三位的next是第二位的next加1,即1+1=2 第四位看第三位b,第四位d与第 ...

  6. docker命令总结(二)

    上次只是给大家把命令的作用以及简单使用列出来了(大家可以查看:docker命令总结(一)),那这篇文章会详细介绍每条命令的参数,命令比较多建议大家使用搜索,进行查看 search docker sea ...

  7. Vim的环境设定与记录

    vim 会主动将曾经做过的行为记录下来,记录在文件   ~/.viminfo,好方便下次作业. 更改  /etc/vimrc配置操作环境 vim的环境设定参数 :set nu :set  nonu 设 ...

  8. Essential C++学习笔记

    1.当我们调用一个函数时,会在内存中建立起一块特殊区域,称为“程序栈”,这块特殊区域提供了每个函数参数的存储空间,它也提供函数所定义的每个对象的内存空间--我们将这些对象称为局部对象.一旦函数完成,这 ...

  9. 关于爬虫的日常复习(18)——scrapy系列3

  10. Activiti 规则任务(businessRuleTask)

    Activiti 规则任务(businessRuleTask) 作者:Jesai 目前国内研究Activiti规则任务businessRuleTask)的文章在网上应该不超出3篇 小觑夜漫酒作伴,破晓 ...