Merge pull request #23 from KiritoTRw/master

ACM Templates Added
This commit is contained in:
Kirigaya Kazuto 2016-08-11 09:27:51 +08:00 committed by GitHub
commit 28932bce41
9 changed files with 301 additions and 0 deletions

29
.ACM-Templates/LCIS.cpp Normal file
View File

@ -0,0 +1,29 @@
/// LCIS 最长公共上升子序列
namespace LCIS
{
const int MAXLEN_A = 500;
const int MAXLEN_B = 500;
int dp[MAXLEN_A+5][MAXLEN_B+5];
int deal(const char* a,const char* b)
{
int lena=strlen(a);
int lenb=strlen(b);
for(int i=1;i<=lenb;i++)
{
int k=0;
for(int j=1;j<=lena;j++)
{
dp[i][j]=dp[i-1][j];/// when b[i-1] != a[j-1]
if(b[i-1]>a[j-1]) k=max(k,dp[i-1][j]);
else if(b[i-1]==a[j-1]) dp[i][j]=k+1;
}
}
int ans=0;
for(int i=1;i<=lena;i++) ans=max(ans,dp[lenb][i]);
return ans;
}
}
//End of namespace LCIS

30
.ACM-Templates/LCS.cpp Normal file
View File

@ -0,0 +1,30 @@
/// LCS 最长子序列
namespace LCS
{
const int MAXLEN_A = 500;
const int MAXLEN_B = 500;
int dp[MAXLEN_A+5][MAXLEN_B+5];
int deal(const char* a,const char* b)
{
int lena=strlen(a);
int lenb=strlen(b);
for(int i=0;i<lenb;i++)
{
for(int j=0;j<lena;j++)
{
if(i==0) dp[i][j]=0;
else if(b[i]==a[j])
{
if(j!=0) dp[i][j]=dp[i-1][j-1]+1;
else dp[i][j]=1;
}
else
{
if(j!=0) dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
else dp[i][j]=dp[i-1][j];
}
}
}
return dp[lenb-1][lena-1];
}
}//End of namespace LCS

30
.ACM-Templates/LIS.cpp Normal file
View File

@ -0,0 +1,30 @@
#include <stdio.h>
#define MAXN 1010
int seq[MAXN];
int seqlen[MAXN];
int main()
{
int N;
while(scanf("%d",&N)==1)
{
int i,j,k,max,maxlen=1;
for(i=1; i<=N; i++)
seqlen[i]=1; //seqlen数组存以第i个数为终点的最长上升序列
for(i=1; i<=N; i++)
scanf("%d",&seq[i]); //seq数组保存序列数组
for (i=2; i<=N; i++)
{
max=0;
for (j=1; j<=i-1; j++)
{
if(seq[j]<seq[i]&&seqlen[j]>max) //在前i-1个序列中寻找以终点小于seq[i]的最长的子序列,即最优子状态
max=seqlen[j];
}
seqlen[i]=max+1;
if(seqlen[i]>maxlen) //seqlen中保存的是第i个数为终点的最长上升序列找出这个数组中最大的值即为最优序列长度
maxlen=seqlen[i];
}
printf("%d\n",maxlen);
}
return 0;
}

34
.ACM-Templates/RMQ-ST.cpp Normal file
View File

@ -0,0 +1,34 @@
#include <iostream>
#include <algorithm>
using namespace std;
namespace RMQ_ST
{
const int MAXN=10000;
int f[MAXN][MAXN];
int a[MAXN];
int n;
void init()
{
for(int i = 1;i<=n;i++)
{
f[i][0]=a[i];
}
for(int j=1;(1<<j)<=n;j++)
{
for(int i=1;i+(i<<j)-1<=n;i++)
{
f[i][j]=max(f[i][j-1],f[i+(1<<j-1)][j-1]);
}
}
}
int rmq(int L,int R)
{
int k=0;
while((1<<(k+1)<=R-L+1)) k++;
return max(f[L][k],f[R-(1<<k)+1][k]);
}
}/// End of namespace RMQ_ST

18
.ACM-Templates/floyd.cpp Normal file
View File

@ -0,0 +1,18 @@
for(int k=1;k<=n;k++)
{
for(int f=1;f<=n;f++)
{
for(int t=1;t<=n;t++)
{
if(f==t||f==k||t==k) continue;
if(m[f][k]!=INF&&m[k][t]!=INF)
{
int total=m[f][k]+m[k][t];
if(total<m[f][t]||m[f][t]==INF)
{
m[f][t]=total;
}
}
}
}
}

View File

@ -0,0 +1,20 @@
//Written by Coffee. 判断素数
bool isPrime(int num)
{
if (num == 2 || num == 3)
{
return true;
}
if (num % 6 != 1 && num % 6 != 5)
{
return false;
}
for (int i = 5; i*i <= num; i += 6)
{
if (num % i == 0 || num % (i+2) == 0)
{
return false;
}
}
return true;
}

1
.Tools/Readme.md Normal file
View File

@ -0,0 +1 @@
#Tools

59
HDOJ/1698_chendl111.cpp Normal file
View File

@ -0,0 +1,59 @@
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
#define lchild rt << 1, l, m
#define rchild rt << 1 | 1, m + 1, r
const int N=400010;
int tree[N],lazy[N],t,ans=1,L,R,delta,n,m;
//区间求和
void push_up(int rt){
tree[rt]=tree[rt << 1]+tree[rt << 1 | 1];
}
void push_down(int rt, int len) {
if(lazy[rt])
{
lazy[rt << 1]=lazy[rt << 1 | 1] = lazy[rt];
tree[rt << 1] = lazy[rt << 1] * (len - (len >> 1));
tree[rt << 1 | 1] = lazy[rt << 1 | 1] * (len >> 1);
lazy[rt] = 0;
}
}
void build(int rt = 1, int l = 1, int r = n) {
lazy[rt]=0;
if (l == r) { tree[rt]=1; return; }//注意对树的初始赋值
int m = (l + r) >> 1;
build(lchild); build(rchild);
push_up(rt);
}
void update(int L, int R, int delta, int rt = 1, int l = 1, int r = n) {
if (L <= l && r <= R) {
tree[rt] = delta * (r - l + 1);//数据更新
lazy[rt] = delta;
return;
}
push_down(rt, r - l + 1);
int m = (l + r) >> 1;
if (L <= m) update(L, R, delta, lchild);
if (R > m) update(L, R, delta, rchild);
push_up(rt);
}
int main()
{
for(scanf("%d",&t);t--;)
{
scanf("%d%d",&n,&m);
build();
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&L,&R,&delta);
update(L,R,delta);
}
printf("Case %d: The total value of the hook is %d.\n",ans++,tree[1]);
}
}

80
HDOJ/1698_libin56842.cpp Normal file
View File

@ -0,0 +1,80 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn = 100000+10;
int n,sum;
struct node
{
int l,r,n;
} a[maxn<<2];
void init(int l,int r,int i)
{
a[i].l = l;
a[i].r = r;
a[i].n = 1;
if(l!=r)
{
int mid = (l+r)>>1;
init(l,mid,2*i);
init(mid+1,r,2*i+1);
}
}
void insert(int i,int x,int y,int m)
{
if(a[i].n == m)//相同则不用修改了
return ;
if(a[i].l == x && a[i].r == y)//找到了区间,直接更新
{
a[i].n = m;
return ;
}
if(a[i].n != -1)//如果该区间只有一种颜色
{
a[2*i].n = a[2*i+1].n = a[i].n;//由于后面必定对子树操作,所以更新子树的值等于父亲的值
a[i].n = -1;//由于该区域颜色与修改不同,而且不是给定区域,所以该区域必定为杂色
}
//父区间为杂色时对所有子节点进行操作
int mid = (a[i].l+a[i].r)>>1;
if(x>mid)
insert(2*i+1,x,y,m);
else if(y<=mid)
insert(2*i,x,y,m);
else
{
insert(2*i,x,mid,m);
insert(2*i+1,mid+1,y,m);
}
}
int find(int i)//区间求和
{
if(a[i].n != -1)//纯色直接算这个区间
return (a[i].r - a[i].l+1)*a[i].n;
else//不存则左右子树去找
return find(i*2)+find(i*2+1);
}
int main()
{
int t,i,k,x,y,m;
int cas = 1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
init(1,n,1);
while(k--)
{
scanf("%d%d%d",&x,&y,&m);
insert(1,x,y,m);
}
printf("Case %d: The total value of the hook is %d.\n",cas++,find(1));
}
return 0;
}