类似墨墨的等式

设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. Java面试题集(四)

    二. Java Web基础部分 在js中如何创建一个对象? var p1={name:”tom”,”age”:12}; function Person(name,age){ this.name=nam ...

  2. msp430入门学习44

    msp430的其他十二 msp430入门学习

  3. Lucene 6.5.0 入门Demo

    Lucene 6.5.0 要求jdk 1.8 1.目录结构: 2.数据库环境: private int id; private String name; private float price; pr ...

  4. SecureCRT 配置文件中 找密码

    打开本地电脑如下路径 C:\Users\XXX\AppData\Roaming\VanDyke\Config\Sessions 找到配置文件. 运行命令:python SecureCRTDecrypt ...

  5. grafana 安装 和 Nginx 、EL 联调

    https://blog.csdn.net/u010735147/article/details/80943593

  6. codeforces 1051 F

    多源点最短路. 但是有限制,m - n <= 20,边数 - 点数 <= 20, 所以这个图非常的稀疏. 任意提取出一个生成树出来,LCA处理任意两点最短路. 然后再去遍历那些多余出来的点 ...

  7. 洛谷P2058 仪仗队

    P2058 仪仗队 24通过 34提交 题目提供者shengmingkexue 标签数论(数学相关) 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 暂时没有讨论 题目描述 作为体育委员,C君 ...

  8. 解决filter拦截request中body内容后,字符流关闭,无法传到controller的问题

    解决filter拦截request中body内容后,字符流关闭,无法传到controller的问题 2.问题: 在一般的请求中,content-type为:application/x-www-form ...

  9. Java8 时区DateTime API

    原文:http://www.yiibai.com/java8/java8_zoneddateapi.html 时区日期时间的API正在使用当时区要被考虑时. 让我们来看看他们的操作. 选择使用任何编辑 ...

  10. UICollectionView 讲解

    什么是UICollectionView UICollectionView是一种新的数据展示方式,简单来说可以把他理解成多列的UITableView(请一定注意这是 UICollectionView的最 ...