有两辆车,容量都为K,有n(10w)个人被划分成m(2k)组,依次上车,每个人上车花一秒。每一组的人都要上同一辆车,一辆车的等待时间是其停留时间*其载的人数,问最小的两辆车的总等待时间。

是f(i,j)表示前i组,j个人是否可行。w(i)表示第i组的人数。

if f(i,j)==1 then f(i+1,j+w(i+1))=1。

这是个bitset可以做的事情,每次左移以后或上f(i-1)的bitset即可。其实可以滚动数组。

然后每更新一次bitset,求一下其最左侧的1的位置,就是对于第一辆车要载的总人数,然后可以O(1)算出第二辆车的等待时间(因为第二辆车必然要接走最后一个人,由于两辆车其实等价,所以可以默认第二辆车等到最后),然后尝试更新答案。

队友代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <cstring>
#include <vector>
#include <ctime>
#include <bitset>
using namespace std; bitset<100001> f; struct nod{
int num;
long long last;
}t[2100]; int n,m,k,a,i,j;
long long ans; bool cmp(nod a,nod b)
{
return a.last<b.last;
} int main()
{
// freopen("ac.in","r",stdin);
// freopen("ac.out","w",stdout);
scanf("%d%d%d",&n,&m,&k);
for (i=1;i<=n;i++)
{
scanf("%d",&a);
t[a].num++;
t[a].last=i;
}
ans=1000000000000;
sort(t+1,t+m+1,cmp);
f[0]=1;
for (i=1;i<=m;i++)
{
f=(f|(f<<t[i].num));
for (j=k;j>=0;j--)
if (f[j]==1 && n-j<=k)
{
ans=min(ans,j*t[i].last+(n-j)*t[m].last);
break;
}
}
if (ans==1000000000000) printf("-1\n"); else printf("%lld\n",ans);
}

【动态规划】【滚动数组】【bitset】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem J. Terminal的更多相关文章

  1. 【二分】【字符串哈希】【二分图最大匹配】【最大流】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem I. Minimum Prefix

    给你n个字符串,问你最小的长度的前缀,使得每个字符串任意循环滑动之后,这些前缀都两两不同. 二分答案mid之后,将每个字符串长度为mid的循环子串都哈希出来,相当于对每个字符串,找一个与其他字符串所选 ...

  2. 【枚举】【最小表示法】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem F. Matrix Game

    给你一个n*m的字符矩阵,将横向(或纵向)全部裂开,然后以任意顺序首尾相接,然后再从中间任意位置切开,问你能构成的字典序最大的字符串. 以横向切开为例,纵向类似. 将所有横排从大到小排序,枚举最后切开 ...

  3. XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem A. Arithmetic Derivative

    题目:Problem A. Arithmetic DerivativeInput file: standard inputOutput file: standard inputTime limit: ...

  4. XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem D. Clones and Treasures

    题目:Problem D. Clones and TreasuresInput file: standard inputOutput file: standard outputTime limit: ...

  5. 【二分图】【并查集】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem L. Canonical duel

    给你一个网格(n<=2000,m<=2000),有一些炸弹,你可以选择一个空的位置,再放一个炸弹并将其引爆,一个炸弹爆炸后,其所在行和列的所有炸弹都会爆炸,连锁反应. 问你所能引爆的最多炸 ...

  6. 【推导】【构造】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem E. Space Tourists

    给你n,K,问你要选出最少几个长度为2的K进制数,才能让所有的n位K进制数删除n-2个元素后,所剩余的长度为2的子序列至少有一个是你所选定的. 如果n>K,那么根据抽屉原理,对于所有n位K进制数 ...

  7. 【推导】【贪心】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem D. Clones and Treasures

    给你一行房间,有的是隐身药水,有的是守卫,有的是金币. 你可以任选起点,向右走,每经过一个药水或金币就拿走,每经过一个守卫必须消耗1个药水,问你最多得几个金币. 药水看成左括号,守卫看成右括号, 就从 ...

  8. 【找规律】【DFS】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem A. Arithmetic Derivative

    假设一个数有n个质因子a1,a2,..,an,那么n'=Σ(a1*a2*...*an)/ai. 打个表出来,发现一个数x,如果x'=Kx,那么x一定由K个“基础因子”组成. 这些基础因子是2^2,3^ ...

  9. XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem F. Matrix Game

    题目: Problem F. Matrix GameInput file: standard inputOutput file: standard inputTime limit: 1 secondM ...

随机推荐

  1. word-wrap word-break 区别

    word-wrap word-break 区别 word-break * word-break:break-all;//直接把单词截断 * word-break:break-word;//虽然单词截断 ...

  2. 分布式队列Celery

    Celery是什么? Celery 是一个由 Python 编写的简单.灵活.可靠的用来处理大量信息的分布式系统,它同时提供操作和维护分布式系统所需的工具. Celery 专注于实时任务处理,支持任务 ...

  3. python基础===输入必须为数字的检验的另一种方法

    print("[+]welcome to python3") while True: num = input("please input a num:") if ...

  4. mybatis开启字段自动映射为java驼峰命名规则

    <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> & ...

  5. PHP-5.3.27源码安装及nginx-fastcgi配置

    源码安装php cat /etc/redhat-release uname -rm wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.c ...

  6. Median_of_Two_Sorted_Arrays(理论支持和算法总结)

    可以将这个题目推广到更naive的情况,找两个排序数组中的第K个最大值(第K个最小值). 1.直接 merge 两个数组,然后求中位数(第K个最大值或者第K个最小值),能过,不过复杂度是 O(n + ...

  7. io流中比较特殊的流-java

    1.序列流(SequenceInputStream)整合个多个文件 A SequenceInputStream表示其他输入流的逻辑级联. 它从一个有序的输入流集合开始,从第一个读取到文件的结尾,然后从 ...

  8. Centos7 环境准备

    Centos7 环境准备 #关闭防火墙 systemctl stop firewalld systemctl disable firewalld #关闭selinux sed -i 's/SELINU ...

  9. P1474 货币系统 Money Systems(完全背包求填充方案数)

    题目链接:https://www.luogu.org/problemnew/show/1474 题目大意:有V种货币,求用V种货币凑出面值N有多少种方案. 解题思路:就是完全背包问题,只是将求最大价值 ...

  10. [转]nginx启动期都做了哪些事

    nginx是个多进程web容器,不同的配置下它的启动方式也是不同的,这里我只说说最典型的启动方式. 它有1个master进程,和多个worker进程(最优配置的数量与CPU核数相关).那么,首先我们要 ...