类似墨墨的等式

设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. bzoj3874&2832 [Ahoi2014]宅男计划 模拟退火,三分

    [Ahoi2014&Jsoi2014]宅男计划 Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 962  Solved: 371[Submit][ ...

  2. poj 3304 判断是否存在一条直线与所有线段相交

    Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8579   Accepted: 2608 Descript ...

  3. 济南学习 Day 5 T2 晚

    等比数列(sequence) [题目描述] 判断一个数列是否为等比数列. 等比数列的定义为能被表示成a,aq,aq^2,aq^3...的数列,其中a和q不等于0. [输入说明] 输入文件的第一行有一个 ...

  4. Scott Mitchell的ASP.NET2.0数据指南中文版索引

    原文发布时间为:2008-08-03 -- 来源于本人的百度文章 [由搬家工具导入] 原文http://www.cnblogs.com/ilovejolly/archive/2006/10/05/52 ...

  5. 解析XML字符串为json对象

    var overtime='<?xml version="1.0" encoding="UTF-8"?><response><co ...

  6. IOC基本理解

    什么是IOC? IOC全称为控制反转(Inversion Of Control),别名依赖注入(Dependency Injection). 控制反转即指我们获取依赖的方式发生了反转. 假设存在如下情 ...

  7. 关于傅里叶变换NTT(FNT)的周边

    NTT:快速数论变化,对于FFT精度减少的情况,NTT可以避免但是会慢一点,毕竟是数论有Mod,和快速米 引用:http://blog.csdn.net/zz_1215/article/details ...

  8. linux svn配置hooks

    先创建仓库: svnadmin create /data/svn/my.com 再配置权限: #cd /data/svn/my.com/conf/ #vim svnserve.conf 配置 [gen ...

  9. linux 系统命令----修改主机名

    1. hostname    ***** 2.修改/etc/sysconfig/network 3./etc/hosts 最后第三步没有必要!

  10. 一步步走向国际乱码大赛-- 恶搞C语言

    大家都一直强调规范编码.可是这个世界上有个大师们娱乐的竞赛--国际乱码大赛. 能写出来的都是对语言深入了解的master.我从没想自己也能"恶搞"C,一直都是老老实实编码.就在前几 ...