类似墨墨的等式

设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. C++ 中的 C_str() 函数用法

    转中转 ~\(≧▽≦)/~ :http://blog.csdn.net/nancy_m/article/details/7583550 语法: const char *c_str(); c_str() ...

  2. angular中的this指向问题

    this是指向当前$scope的. 例如在ng-click的使用中,this是指向当前的$scope而并不是dom元素的. 我们可以使用this的一些方法和属性 我们打印一下this就会发现,this ...

  3. JS设置页面中方法执行一次的思想

    思想:在JS中定义一全局变量,在方法执行的时候根据全局变量的值判断是否需要执行,在方法中修改全局变量的值,可以使得方法只执行一次.: 例如: 定义全局变量: var isLoad = false;// ...

  4. Scrapy学习-1-入门

    基础知识 爬虫发展史   爬虫去重 1. 存储到数据库中 存取速度慢 2. 存储到内存中的集合里,内存占用十分大 当爬取数据有1亿条时 1*10**8*2Byte*50str_len/1024/102 ...

  5. 有向图最小路径覆盖方法浅析、证明 //hdu 3861

    路径覆盖就是在图中找一些路径,使之覆盖了图中的所有顶点,且任何一个顶点有且只有一条路径与之关联. 对于一个有向无环图怎么求最小路径覆盖? 先构造二分图: 对于原图,先拆点,吧每个点i拆成ii,iii. ...

  6. Jetson TK1 一:调整屏幕分辨率

    先点击分辨率调整的那个对话框,就是1024×768之类的那个,选定一个分辨率,我选的是1366x768(16:9)的那个,然后连续按7下tab按键,然后回车,然后再点击弹出的选项右边一个就能保存了.

  7. (入门SpringBoot)SpringBoot项目数据源以及整合mybatis(二)

    1.配置tomcat数据源: #   数据源基本配置spring.datasource.url=jdbc:mysql://localhost:3306/shoptest?useUnicode=true ...

  8. 关于Properties 集 添加数据 遍历数据

    public static void fun1(){ Properties v = new Properties(); v.setProperty("a","1" ...

  9. 【hql】spring data jpa中 @Query使用hql查询 问题

    spring data jpa中 @Query使用hql查询 问题 使用hql查询, 1.from后面跟的是实体类 不是数据表名 2.字段应该用实体类中的字段 而不是数据表中的属性 实体如下 hql使 ...

  10. 一个炫酷的Actionbar效果

    今天在网上看到一个炫酷的Actionbar效果,一个老外做的DEMO,目前很多流行的app已经加入了这个效果. 当用户初始进入该界面的时候,为一个透明的 ActiionBar ,这样利用充分的空间显示 ...