类似墨墨的等式

设f[2][j][k]表示a[i].c是否和当前颜色相同,到当前枚举到的颜色为止,颜色数为j,对mnv取模为k的最小数

这是个无限循环背包,用spfa优化

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
typedef long long LL;
const int _=1e2;
const int maxn=+;
const int maxv=*1e5+_;
LL inf; LL f[][maxn][maxv];//a[i].c是否和当前颜色相同,到当前枚举到的颜色为止,颜色数为j,对mnv取模为k的最小数
struct SPFA
{
int op,j,k;
SPFA(){} SPFA(int OP,int J,int K){op=OP,j=J,k=K;}
}list[*maxn*maxv];int head,tail;bool v[][maxn][maxv];
void spfa(int cc,int mnv,int dv)
{
head=,tail=; list[tail++]=SPFA(,,);
for(int j=;j<=cc;j++)
for(int k=;k<mnv;k++)
{
if(f[][j][k]!=inf)list[tail++]=SPFA(,j,k),v[][j][k]=true;
if(f[][j][k]!=inf)list[tail++]=SPFA(,j,k),v[][j][k]=true;
}
while(head!=tail)
{
int op=list[head].op,j=list[head].j,k=list[head].k;
int tp=,tj=j+-op,tk=(k+dv)%mnv;
if(f[tp][tj][tk]>f[op][j][k]+dv)
{
f[tp][tj][tk]=f[op][j][k]+dv;
if(v[tp][tj][tk]==false)
{
v[tp][tj][tk]=true;
list[tail]=SPFA(tp,tj,tk);
tail++;if(tail==*maxn*maxv)tail=;
}
}
v[op][j][k]=false;
head++;if(head==*maxn*maxv)head=;
}
} struct node{int v,c;}a[maxn];
bool cmp(node n1,node n2){return n1.c<n2.c;}
int main()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
int n,mnv=(<<);
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d%d",&a[i].v,&a[i].c),mnv=min(mnv,a[i].v);
sort(a+,a+n+,cmp); int cc=;
memset(f,,sizeof(f));inf=f[][][];
f[][][]=;
for(int i=;i<n;i++)//开始放第i+1种
{
if(a[i+].c!=a[i].c)
{
for(int j=;j<=cc;j++)
for(int k=;k<mnv;k++)
f[][j][k]=min(f[][j][k],f[][j][k]);
}
spfa(cc,mnv,a[i+].v);
if(a[i+].c!=a[i].c)cc++;
}
for(int j=;j<=cc;j++)
for(int k=;k<mnv;k++)
f[][j][k]=min(f[][j][k],f[][j][k]); int Q,u;LL x;
scanf("%d",&Q);
while(Q--)
{
scanf("%lld",&x);u=x%mnv;
bool bk=false;
for(int i=n;i>=;i--)
if(f[][i][u]<=x){printf("%d\n",i);bk=true;break;}
if(!bk)puts("-1");
} return ;
}

CodeChef:Little Elephant and Colored Coins的更多相关文章

  1. CodeChef-LECOINS Little Elephant and Colored Coins 题解

    CodeChef-LECOINS Little Elephant and Colored Coins Little Elephant and Colored Coins The Little Elep ...

  2. codechef Little Elephant and Permutations题解

    The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numb ...

  3. CodeChef Little Elephant and Movies [DP 排列]

    https://www.codechef.com/FEB14/problems/LEMOVIE 题意: 对于一个序列,定义其“激动值”为序列中严格大于前面所有数的元素的个数.给定n个数p1;,p2.. ...

  4. CodeChef Little Elephant and Mouses [DP]

    https://www.codechef.com/problems/LEMOUSE 题意: 有一个n *m的网格.有一头大象,初始时在(1,1),要移动到(n,m),每次只能向右或者向下走.有些格子中 ...

  5. codechef Little Elephant and Bombs题解

    The Little Elephant from the Zoo of Lviv currently is on the military mission. There are N enemy bui ...

  6. CodeChef Little Elephant and Balance

    Given an array A1,A2...AN, you have to print the size of the largest contiguous subarray such that L ...

  7. codechef Chef and The Colored Grid

    难度 \(hard\) 题意 \(3\times n\)的方格,前两行已分别填入\(n-\)排列,要求求第三行填入\(n-\)排列,使得每行每列数不重复的方案数(数据保证前两行合法)\(n\le 10 ...

  8. 【翻译】A Next-Generation Smart Contract and Decentralized Application Platform

    原文链接:https://github.com/ethereum/wiki/wiki/White-Paper 当中本聪在2009年1月启动比特币区块链时,他同时向世界引入了两种未经测试的革命性的新概念 ...

  9. Ethereum White Paper

    https://github.com/ethereum/wiki/wiki/White-Paper White Paper EditNew Page James Ray edited this pag ...

随机推荐

  1. jmeter监控linux cpu 内存 网络 IO

    下载地址:http://jmeter-plugins.org/downloads/all/ PerfMon: 用来监控Server的CPU.I/O.Memory等情况 ServerAgent-2.2. ...

  2. excel打乱各行的顺序,实现无序随机排列

    由于公司做活动,经常会发些激活码过来,为了让激活码能够充分使用,经常要打乱激活码的顺序,百度了下,看了下网上的介绍,还不错,挺实用,记录下来. 具体方法如下: 1.将文本里的内容复制到Excel里的任 ...

  3. mysqldumpslow不是内部或外部命令,也不是可运行的程序

    mysqldumpslow不是内部或外部命令,也不是可运行的程序 window系统,mysql版本:5.7.14. 使用mysqldumpslow命令分析日志时,报错:mysqldumpslow'不是 ...

  4. 测试开发系列之Python开发mock接口(二)

    上一篇咱们已经把开发前的环境准备好了,还需要再做一点准备,你的账户信息是存在哪的呢,当然是存在数据库里的,咱们在去支付,扣钱的时候,肯定是从数据库里面操作的,去更新账户表里面的数据,所以咱们先要把数据 ...

  5. VM虚拟机

    VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转换模式)和host-only(主机模式).要想在网络管理和维护中合理应用它们,你就应该先了解一下这三种工作模式. 1 ...

  6. 顿悟:Linux是拿来用的,不是拿来折腾的

    Linux是拿来用的,而不是折腾其本身.相信这个道理不少聪明人(实用主义者)都明白,然而总是有那么一群人拿Linux去安装各种发行版.研究Linux命令.配置桌面.美化桌面.研究各种wm/DE.永无止 ...

  7. Jetty插件实现热部署(开发时修改文件自动重启Jetty)

    在pom.xml文件中配置Jetty插件的参数:scanIntervalSeconds <plugin> <groupId>org.mortbay.jetty</grou ...

  8. BroadcastReceiver详解(二)

    BroadCastReceiver 简介 (末尾有源码) BroadCastReceiver 源码位于: framework/base/core/java/android.content.Broadc ...

  9. IronPython 与C#交互

    http://www.cnblogs.com/nuaalfm/archive/2010/02/11/1667448.html 一.介绍 Python是一种面向对象.直译式计算机程序设计语言,也是一种功 ...

  10. 使用Python控制1602液晶屏实时显示时间(附PyCharm远程调试)

    前言 原创文章,转载引用务必注明链接.水平有限,如有疏漏,欢迎指正. 本文介绍一下UP板的GPIO资源使用,以及一个使用Python演示一个简单的demo. 本文使用Markdown写成,为获得更好的 ...