2014 ACM/ICPC Asia Regional Xi'an Online
03 hdu5009
状态转移方程很好想,dp[i] = min(dp[j]+o[j~i]^2,dp[i]) ,o[j~i]表示从j到i颜色的种数。
普通的O(n*n)是会超时的,可以想到o[]最大为sqrt(n),问题是怎么快速找到从i开始往前2种颜色、三种、四种。。。o[]种的位置。
离散化之后,可以边走边记录某个数最后一个出现的位置,初始为-1,而所要求的位置就等于
if(last[a[i]]==-1) 该数没有出现过,num[i][1] = i,num[i][j+1] = num[i-1][j];
else last[a[i]]之前 num[i][1] = i,num[i][j+1] = num[i-1][j],之后num[i][j]= num[i-1][j];
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define N 50010
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
int a[N];
int dp[N];
int num[][],last[N];
map<int,int>f;
int main()
{
int i,j,n;
while(scanf("%d",&n)!=EOF)
{
f.clear();
int g = ;
for(i = ; i<= n ;i++)
{
scanf("%d",&a[i]);
if(!f[a[i]])
{
f[a[i]] = ++g;
a[i] = g;
}
else a[i] = f[a[i]];
}
for(i = ; i <= n; i++)
dp[i] = INF;
memset(last,-,sizeof(last));
memset(num,,sizeof(num));
int k = sqrt(n*1.0)+;
int tk = ;
dp[] = ;
last[a[]] = ;
num[][] = ;
dp[] = ;
for(i = ; i <= n ;i++)
{
if(last[a[i]]==-)
{
tk+=;
num[i%][] = i;
for(j = ; j <= min(tk-,k-) ; j++)
num[i%][j+] = num[(i-)%][j];
}
else
{ num[i%][] = i;
for(j = ; j < min(k,tk) ; j++)
{
if(last[a[i]]==num[(i-)%][j]) break;
num[i%][j+] = num[(i-)%][j];
}
for(int g = j+ ; g <= min(tk,k) ; g++)
num[i%][g] = num[(i-)%][g];
}
last[a[i]] = i;
for(j = ; j <= min(k,tk); j++)
{
int po = num[i%][j+];
dp[i] = min(dp[i],dp[po]+j*j);
// cout<<dp[po]<<" "<<po<<endl;
}
}
printf("%d\n",dp[n]); }
return ;
}
09 hdu5015
构造矩阵
先构造出1*(n+2)的矩阵 (233, 233+a1, 233+a1+a2, 233+a1+a2+a3, ..., 233+a1+a2+..+an, 1),表示第一列上的值。
此矩阵为A,然后想要使A*B = C,C为第二列的值,所以B可以为
10 10 10 10 10 .......0
0 1 1 1 1........0
0 0 1 1 1........0
0 0 0 1 1........0
0 0 0 0 1........0
3 3 3 3 3........1
然后快速幂就可以了。。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 12
#define LL __int64
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
#define mod 10000007
struct Mat
{
LL mat[N][N];
};
int n;
int a[N];
Mat operator * (Mat a,Mat b)
{
Mat c;
memset(c.mat,,sizeof(c.mat));
int i,j,k;
for(k = ; k < n ; k++)
{
for(i = ; i < n ; i++)
{
if(a.mat[i][k]==) continue;//优化
for(j = ; j < n ; j++)
{
if(b.mat[k][j]==) continue;//优化
c.mat[i][j] = (c.mat[i][j]+(a.mat[i][k]*b.mat[k][j])%mod)%mod;
}
}
}
return c;
}
Mat operator ^(Mat a,int k)
{
Mat c;
int i,j;
for(i = ; i < n ; i++)
for(j = ; j < n ; j++)
c.mat[i][j] = (i==j);
for(; k ; k >>= )
{
if(k&) c = c*a;
a = a*a;
}
return c;
}
int main()
{
int m,i,j;
Mat c;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(c.mat,,sizeof(c.mat));
for(i = ; i <= n; i++)
{
c.mat[][i] = ;
for(j = ; j <= i ; j++)
c.mat[j][i] = ;
}
for(i = ; i <= n ; i++)
c.mat[n+][i] = ;
c.mat[n+][n+] = ;
LL s = ;
Mat ans;
memset(ans.mat,,sizeof(ans));
ans.mat[][] = s;
for(i = ; i <= n; i++)
{
scanf("%d",&a[i]);
s+=a[i];
s%=mod;
ans.mat[][i] = s;
}
ans.mat[][n+] = ;
int tn = n;
n+=;
if(m>)
ans = ans*(c^(m-));
// for(i = 0 ; i < n ; i++)
// {for(j = 0; j< n ; j++)
// cout<<c.mat[i][j]<<" ";
// puts("");
// }
// for(i = 0 ; i < n ; i++)
// {
// for(j = 0; j < n ; j++)
// cout<<ans.mat[i][j]<<" ";
// puts("");
// }
printf("%I64d\n",ans.mat[][tn]);
}
return ;
2014 ACM/ICPC Asia Regional Xi'an Online的更多相关文章
- hdu 5016 点分治(2014 ACM/ICPC Asia Regional Xi'an Online)
Mart Master II Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- HDU 5010 Get the Nut(2014 ACM/ICPC Asia Regional Xi'an Online)
思路:广搜, 因为空格加上动物最多只有32个那么对这32个进行编号,就能可以用一个数字来表示状态了,因为只有 ‘P’ 'S' 'M' '.' 那么就可以用4进制刚好可以用64位表示. 接下去每次就 ...
- 2014 ACM/ICPC Asia Regional Xi'an Online Paint Pearls
传说的SB DP: 题目 Problem Description Lee has a string of n pearls. In the beginning, all the pearls have ...
- 2014 ACM/ICPC Asia Regional Xi'an Online(HDU 5007 ~ HDU 5017)
题目链接 A题:(字符串查找,水题) 题意 :输入字符串,如果字符串中包含“ Apple”, “iPhone”, “iPod”, “iPad” 就输出 “MAI MAI MAI!”,如果出现 “Son ...
- HDU 5000 2014 ACM/ICPC Asia Regional Anshan Online DP
Clone Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/65536K (Java/Other) Total Submiss ...
- HDU 5029 Relief grain(离线+线段树+启发式合并)(2014 ACM/ICPC Asia Regional Guangzhou Online)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5029 Problem Description The soil is cracking up beca ...
- HDU 5000 Clone(离散数学+DP)(2014 ACM/ICPC Asia Regional Anshan Online)
Problem Description After eating food from Chernobyl, DRD got a super power: he could clone himself ...
- 2014 ACM/ICPC Asia Regional Shanghai Online
Tree http://acm.hdu.edu.cn/showproblem.php?pid=5044 树链剖分,区间更新的时候要用on的左++右--的标记方法,要手动扩栈,用c++交,综合以上的条件 ...
- 2014 ACM/ICPC Asia Regional Guangzhou Online
Wang Xifeng's Little Plot http://acm.hdu.edu.cn/showproblem.php?pid=5024 预处理出每个点八个方向能走的最远距离,然后枚举起点,枚 ...
随机推荐
- 本机ip+端口不能访问web server,外部却可以访问
本机ip+端口不能访问web server,外部却可以访问! 这个奇葩的问题困扰了我好久,别人通过ip访问我的server一切正常,自己却访问不了,一度怀疑win10的问题,久寻无果! 最后关闭ads ...
- VIM 常用快捷键
一,光标移动 大家不要觉得光标移动不重要,其实它是基础,更好的光标移动,复制,粘贴,删除等才能更加的得心应手,进入了编辑器里面后,鼠标就不能用了. 光标移动 h 或 向左箭头键(←) 20h或者20( ...
- Terra Vista 6.2
最近在做虚拟仿真相关工作,想把GIS中的一些想法用虚拟显示技术实现,在保证准确性的同时,提高展现效果. 前不久在朋友圈获得了一个强大的三维地形构建软件Terra Vista 6.2,据说这个软件是加拿 ...
- insmod过程详解【转】
转自:http://blog.csdn.net/chrovery/article/details/51088425 转自 http://blog.chinaunix.net/xmlrpc.php?r= ...
- header中Content-Disposition的作用
在servlet3.0中 支持文件上传的注解@MultipartConfig 发现有个例子开头打印的信息中有Content-Disposition,一时好奇,所以了解了一下.顺便学习一下文件上传所需要 ...
- 【转】创建SVN仓库的步骤
转载地址:http://www.cnblogs.com/ivan0626/p/3783053.html 今天在客户现场联调,两个开发人员之间的代码想用SVN来管理,所以就临时在本地机器上搭建一个S ...
- C# 线程通信 一
C#多线程通信 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- 2016年12月29日 星期四 --出埃及记 Exodus 21:24
2016年12月29日 星期四 --出埃及记 Exodus 21:24 eye for eye, tooth for tooth, hand for hand, foot for foot,以眼还眼, ...
- Scala相关
vim conf for scala: http://stackoverflow.com/questions/3626203/text-editor-for-scala http://fengshen ...
- 【前端】require函数实现原理
// require函数实现原理: function require(modulePath) { var regExp = /\w+$/g; var moduleName = regExp.exec( ...