CodeChef:Little Elephant and Colored Coins
类似墨墨的等式
设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的更多相关文章
- CodeChef-LECOINS Little Elephant and Colored Coins 题解
CodeChef-LECOINS Little Elephant and Colored Coins Little Elephant and Colored Coins The Little Elep ...
- codechef Little Elephant and Permutations题解
The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numb ...
- CodeChef Little Elephant and Movies [DP 排列]
https://www.codechef.com/FEB14/problems/LEMOVIE 题意: 对于一个序列,定义其“激动值”为序列中严格大于前面所有数的元素的个数.给定n个数p1;,p2.. ...
- CodeChef Little Elephant and Mouses [DP]
https://www.codechef.com/problems/LEMOUSE 题意: 有一个n *m的网格.有一头大象,初始时在(1,1),要移动到(n,m),每次只能向右或者向下走.有些格子中 ...
- codechef Little Elephant and Bombs题解
The Little Elephant from the Zoo of Lviv currently is on the military mission. There are N enemy bui ...
- 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 ...
- codechef Chef and The Colored Grid
难度 \(hard\) 题意 \(3\times n\)的方格,前两行已分别填入\(n-\)排列,要求求第三行填入\(n-\)排列,使得每行每列数不重复的方案数(数据保证前两行合法)\(n\le 10 ...
- 【翻译】A Next-Generation Smart Contract and Decentralized Application Platform
原文链接:https://github.com/ethereum/wiki/wiki/White-Paper 当中本聪在2009年1月启动比特币区块链时,他同时向世界引入了两种未经测试的革命性的新概念 ...
- Ethereum White Paper
https://github.com/ethereum/wiki/wiki/White-Paper White Paper EditNew Page James Ray edited this pag ...
随机推荐
- Java程序性能优化之缓冲优化
优化前的代码: package com; import javax.swing.*; import java.awt.*; /** * 使用Eclipse,右键Run As,Java Applet运行 ...
- bzoj3874&2832 [Ahoi2014]宅男计划 模拟退火,三分
[Ahoi2014&Jsoi2014]宅男计划 Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 962 Solved: 371[Submit][ ...
- python简易爬虫,帮助理解re模块
20161203更新: 1.使用了BS4解析html 2.使用了mysql-connector插入了数据库表 pip install mysql-connector import urllib.req ...
- 从ip addr add和ifconfig的区别看linux网卡ip地址的结构
今天一个老外在邮件列表上问了一个问题,就是ip addr add和ifconfig的区别,我给他进行了解答,可能因为英语不好吧,解答的很简单,因此我还是要在这里详细说明一下.其实它们之间没有什么区别, ...
- 玩转css样式选择器----当父元素只有一个子元素时居中显示,多个水平排列
- PXC小结
PXC使用到的端口号 3306 数据库对外服务的端口号(视具体情况而定) 4444 请求SST SST: 指数据一个镜象传输 xtrabackup , rsync ,mysqldump 4567 : ...
- 树莓派用gobot测试舵机的使用
package main import ( "gobot.io/x/gobot" "gobot.io/x/gobot/drivers/gpio" "g ...
- koa2 从入门到进阶之路 (一)
首先我们先来了解一下 Koa 是什么,https://koa.bootcss.com/,这是 Koa 的官方网站,映入眼帘的第一句就是 Koa -- 基于 Node.js 平台的下一代 web 开发框 ...
- CAS 跨域原理
http://www.blogjava.net/rain1102/articles/227739.html CAS(Central Authentication Service) 是 Yale 大学发 ...
- Cocos2d-x JSB 自己主动绑定bindings
Javascript Binding (简称JSB) 自己主动绑定教程. Cocos2d-x JSB 自己主动绑定bindings-generator (以下简称B-G) 使用心得 假设想弄清深入原理 ...