Powered By HC TECH : AutoACer Engine

4200-4299
This commit is contained in:
KiritoTRw 2016-09-04 15:25:29 +08:00 committed by GitHub
parent 37c7dfef72
commit 1c7c39b791
86 changed files with 7042 additions and 0 deletions

123
HDOJ/4200_autoAC.cpp Normal file
View File

@ -0,0 +1,123 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
#define INF 1000000000
#define N 110
#define M 210
int n, d;
bool mat[N][N], val[N];
void print(bool a[N][N], int n)
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n+1; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
printf("\n");
}
int dfs(int col, int cnt)
{
if(col == n+1)
{
int res = cnt;
for(int i = 1; i <= n; i++)
{
if(val[i] ^ mat[i][n+1]) res++;
}
return res;
}
int res = 0;
for(int i = 1; i <= n; i++)
val[i] ^= mat[i][col];
res = dfs(col+1, cnt+1);
for(int i = 1; i <= n; i++)
val[i] ^= mat[i][col];
res = min(dfs(col+1, cnt), res);
return res;
}
int gauss()
{
int row = 1;
int r = n;
for(int i = 1; i <= r; i++, row++)
{
int p = -1;
for(int j = row; j <= n; j++)
{
if(mat[j][i] == 1)
{
p = j;
break;
}
}
if(p == -1)
{
for(int j = 1; j <= n; j++)
{
swap(mat[j][i], mat[j][r]);
}
//print(mat, n);
r--;
i--;
row--;
continue;
}
if(p != row)
{
for(int j = i; j <= n+1; j++)
{
swap(mat[row][j], mat[p][j]);
}
}
for(int j = 1; j <= n; j++)
{
if(mat[j][i] == 1 && j != row)
for(int k = i; k <= n+1; k++)
{
mat[j][k] ^= mat[row][k];
}
}
}
for(int i = row; i <= n; i++)
{
if(mat[i][n+1] == 1)
{
return -1;
}
}
memset(val, 0, sizeof(val));
int ans = dfs(row, 0);
return ans;
}
int main()
{
int t; scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &d);
memset(mat, 0, sizeof(mat));
for(int i = 1; i <= n; i++)
{
scanf("%d", &mat[i][n+1]);
}
for(int i = 1; i <= n; i++)
{
int st = max(1, i-d), ed = min(n, i+d);
for(int j = st; j <= ed; j++)
{
mat[i][j] = 1;
}
}
int ans = gauss();
if(ans != -1)
printf("%d\n", ans);
else printf("impossible\n");
}
return 0;
}

24
HDOJ/4203_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include<cstdio>
#include<stdlib.h>
#include<cstring>
#include<queue>
using namespace std;
int main(){
int i,t,T,k,s;
scanf("%d",&T);
for(t=1;t<=T;t++){
scanf("%d %d",&s,&k);
if(k&1){
if(s&1)printf("1\n");
else printf("0\n");
}
else{
int tem=s%(k+1);
if(tem==k)printf("%d\n",k);
else{
if(tem&1)printf("1\n");
else printf("0\n");
}
}
}
}

64
HDOJ/4205_autoAC.cpp Normal file
View File

@ -0,0 +1,64 @@
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
#define N 1010
#define inf 2000000010
struct node{
int x,li,id;
}w[N];
bool cmp(const node &a,const node &b){
if(a.x!=b.x) return a.x<b.x;
return a.li>b.li;
}
int dp[N][N];
int main(){
int T,n,id;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
int Min=-inf;
for(int i=0;i<n;++i){
scanf("%d%d",&w[i].x,&w[i].li);
w[i].id=i;
}
sort(w,w+n,cmp);
for(int i=0;i<n;++i){
if(!w[i].id) id=i;
for(int j=0;j<=n;++j)
dp[i][j]=inf;
}
if(id){
dp[0][0]=Min;
dp[0][1]=w[0].x;
}
else dp[0][1]=w[0].x+(w[0].li+1>>1);
for(int i=1;i<n;++i){
if(i==id){
int l=w[i].x-(w[i].li+1>>1);
int r=w[i].x+(w[i].li+1>>1);
for(int j=0;j<=i;++j)
if(dp[i-1][j]<=l)
dp[i][j+1]=r;
continue;
}
for(int j=0;j<=i;++j){
dp[i][j]=min(dp[i][j],dp[i-1][j]);
if(dp[i-1][j]!=inf){
if(dp[i-1][j]<=w[i].x){
int temp=max(dp[i-1][j]+w[i].li,w[i].x);
dp[i][j+1]=min(dp[i][j+1],temp);
}
}
}
}
for(int i=n;i;--i)
if(dp[n-1][i]!=inf){
printf("%d\n",i);
break;
}
}
return 0;
}

40
HDOJ/4206_autoAC.cpp Normal file
View File

@ -0,0 +1,40 @@
#include <iostream>
#include <cmath>
using namespace std;
int main(void)
{
int cas;
scanf("%d", &cas);
while(cas--)
{
int n, a, b, z, y;
scanf("%d", &n);
int tempn = (int)sqrt((double)n);
int ansY, ansZ;
ansY = ansZ = INT_MAX;
for(int i = 1; i <= tempn; i++)
{
if(n % i == 0)
{
a = n / i;
b = i;
if((a + b) % 2 == 0)
{
z = (a + b) / 2;
y = z - min(a, b);
if(z < ansZ || y < ansY)
{
ansY = y, ansZ = z;
}
}
}
}
if(ansZ == INT_MAX)
{
printf("IMPOSSIBLE\n");
}
else
printf("%d %d\n", ansY, ansZ);
}
return 0;
}

9
HDOJ/4207_autoAC.cpp Normal file
View File

@ -0,0 +1,9 @@
432
5678
-------
3456
3024
2592
2160
-------
2452896

33
HDOJ/4209_autoAC.cpp Normal file
View File

@ -0,0 +1,33 @@
#include<iostream>
using namespace std;
struct node
{
int d;
int p;
};
typedef int Case;
node nod[10];
const double PI=3.1415926;
int main()
{
int n;
Case cas=1;
while(cin>>n,n)
{
double min=1000000,area,sq;
int rem;
for(int i=0;i<n;i++)
{
cin>>nod[i].d>>nod[i].p;
area=PI*(nod[i].d/2.0)*(nod[i].d/2.0);
sq=nod[i].p/area;
if(min>sq)
{
min=sq;
rem=nod[i].d;
}
}
cout<<"Menu "<<cas++<<": "<<rem<<endl;
}
return 0;
}

156
HDOJ/4210_autoAC.cpp Normal file
View File

@ -0,0 +1,156 @@
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
const int N = 10;
struct Domino
{
int x,y;
}d[40];
struct Uncover
{
int x,y;
}u[N*N];
int map[N][N],st[3][9],t,num;
int dir[2][2]={{0,1},{1,0}};
bool vis[N][N],flag;
void init()
{
for(int i=1;i<=8;i++)
for(int j=i+1;j<=9;j++)
{
d[t].x=i,d[t].y=j;
t++;
}
}
bool isfinish()
{
for(int i=0;i<3;i++)
for(int j=0;j<9;j++)
if(st[i][j]!=((1<<9)-1))
return false;
return true;
}
bool check(int x,int y,int xx,int yy,int first,int second)
{
int ss1=(x/3)*3+y/3,ss2=(xx/3)*3+yy/3;
first--,second--;
if((st[0][x]&(1<<first))||(st[0][xx]&(1<<second)))
return false;
if((st[1][y]&(1<<first))||(st[1][yy]&(1<<second)))
return false;
if((st[2][ss1]&(1<<first))||(st[2][ss2]&(1<<second)))
return false;
st[0][x]|=(1<<first),st[0][xx]|=(1<<second);
st[1][y]|=(1<<first),st[1][yy]|=(1<<second);
st[2][ss1]|=(1<<first),st[2][ss2]|=(1<<second);
return true;
}
void restore(int x,int y,int xx,int yy,int first,int second)
{
int ss1=(x/3)*3+y/3,ss2=(xx/3)*3+yy/3;
first--,second--;
st[0][x]&=~(1<<first),st[0][xx]&=~(1<<second);
st[1][y]&=~(1<<first),st[1][yy]&=~(1<<second);
st[2][ss1]&=~(1<<first),st[2][ss2]&=~(1<<second);
}
void set(int x,int y,int a)
{
int ss=(x/3)*3+y/3;
a--;
st[0][x]|=(1<<a);
st[1][y]|=(1<<a);
st[2][ss]|=(1<<a);
}
void dfs(int n)
{
int x=u[n].x,y=u[n].y;
if(isfinish())
{
flag=true;
return ;
}
for(int i=0;i<t;i++)
{
if(vis[d[i].x][d[i].y])
continue;
vis[d[i].x][d[i].y]=true;
for(int f=0;f<2;f++)
{
int first=d[i].x,second=d[i].y;
if(f&1) swap(first,second);
for(int k=0;k<2;k++)
{
int temp=n;
int xx=x+dir[k][0];
int yy=y+dir[k][1];
if(xx<0 || xx>=9 || yy<0 || yy>=9 || map[xx][yy]!=0)
continue;
if(!check(x,y,xx,yy,first,second))
continue;
map[x][y]=first,map[xx][yy]=second;
if(map[u[temp+1].x][u[temp+1].y]==0)
dfs(temp+1);
else {
while(temp<num && map[u[temp+1].x][u[temp+1].y]!=0)
temp++;
dfs(temp+1);
}
if(flag) return;
restore(x,y,xx,yy,first,second);
map[x][y]=0,map[xx][yy]=0;
}
}
vis[d[i].x][d[i].y]=false;
}
}
int main()
{
t=0;
init();
int m,cas=0;
char s1[5],s2[5];
int a,b,c,d;
while(scanf("%d",&m)==1 && m)
{
memset(vis,false,sizeof(vis));
memset(map,0,sizeof(map));
memset(st,0,sizeof(st));
for(int i=0;i<m;i++)
{
scanf("%d %s %d %s",&a,s1,&b,s2);
int x=s1[0]-'A',y=s1[1]-'0'-1;
map[x][y]=a;
set(x,y,a);
x=s2[0]-'A',y=s2[1]-'0'-1;
map[x][y]=b;
set(x,y,b);
vis[a][b]=vis[b][a]=true;
}
for(int i=1;i<=9;i++)
{
scanf("%s",s1);
int x=s1[0]-'A',y=s1[1]-'0'-1;
set(x,y,i);
map[x][y]=i;
}
num=0;
for(int i=0;i<9;i++)
for(int j=0;j<9;j++)
if(map[i][j]==0)
{
u[num].x=i,u[num].y=j;
num++;
}
flag=false;
dfs(0);
printf("Puzzle %d\n",++cas);
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
printf("%d",map[i][j]);
puts("");
}
}
return 0;
}

30
HDOJ/4211_autoAC.cpp Normal file
View File

@ -0,0 +1,30 @@
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
int main()
{
char str[100];
int a[200];
while(gets(str) != NULL)
{
if(strcmp(str, "END") == 0)
break;
int l=strlen(str);
int i;
memset(a, 0, sizeof(a));
for( i=0; i < l; i++ )
{
if(str[i] >= 'A' && str[i] <= 'Z')
a[str[i]]++;
}
for( i='A'; i <= 'Z'; i++ )
{
if(a[i] > 1)
break;
}
if(i > 'Z')
puts(str);
}
return 0;
}

91
HDOJ/4212_autoAC.cpp Normal file
View File

@ -0,0 +1,91 @@
#include <iostream>
#include <vector>
#include <set>
#include<cstdio>
using namespace std;
vector<int> a[23];
int ans,nn;
int b[22];
int tp=1<<22;
void init()
{
for(int i=1;i<tp;i++)
{
int tmp=i;
int k=0;
int sum=0;
while(tmp)
{
++k;
if(tmp&1)
{
sum+=k;
if(sum>22) break;
}
tmp>>=1;
}
if(!tmp) a[sum].push_back(i);
}
}
inline int total_one(int p)
{
int c=0;
while(p)
{
p&=p-1;
c++;
}
return c;
}
int main()
{
int T=0;
int n,m;
init();
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n+m==0) break;
nn=1<<n;
++T;
for(int i=0;i<m;i++)
{
scanf("%d",&b[i]);
}
ans=0;
set<int> now;
now.insert(0);
for(int i=0;i<m;i++)
{
set<int> next;
for(set<int>::iterator it=now.begin();it!=now.end();it++)
{
int temp=a[b[i]].size();
int sign=0;
int tmp=*it;
for(int j=0;j<temp;j++)
{
if(a[b[i]][j]>=nn) continue;
if((tmp & a[b[i]][j]) ==0)
{
sign=1;
next.insert(tmp | a[b[i]][j]);
}
}
if(!sign)
{
int _tmp=total_one(tmp);
ans=ans>_tmp?ans:_tmp;
}
}
now=next;
if(now.size()==0) break;
}
for(set<int>::iterator it=now.begin();it!=now.end();it++)
{
int _tmp=total_one(*it);
ans=ans>_tmp?ans:_tmp;
}
printf("Game %d: %d\n",T,ans);
}
return 0;
}

103
HDOJ/4213_autoAC.cpp Normal file
View File

@ -0,0 +1,103 @@
#include<cstdio>
#include<cstring>
#define maxn 20
char map[maxn][maxn];
bool aim[maxn][maxn];
bool complete;
int dirx[4]={-1,1,0,0},diry[4]={0,0,-1,1};
int startx,starty;
int n,m;
void run(char *s)
{
for(;*s;s++)
{
int dir;
if(*s=='U') dir=0;
else if(*s=='D') dir=1;
else if(*s=='L') dir=2;
else dir=3;
int tx=startx+dirx[dir],ty=starty+diry[dir];
if(map[tx][ty]!='#')
{
int nextx=tx+dirx[dir],nexty=ty+diry[dir];
bool flag=false;
if(map[tx][ty]=='b'&&map[nextx][nexty]!='b'&&map[nextx][nexty]!='#')
{
map[tx][ty]='w';
map[nextx][nexty]='b';
map[startx][starty]='.';
startx=tx,starty=ty;
flag=true;
}
else if(map[tx][ty]=='.'||map[tx][ty]=='+')
{
map[tx][ty]='w';
map[startx][starty]='.';
startx=tx,starty=ty;
}
if(flag)
{
int t1=0,t2=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(aim[i][j])
{
t1++;
if(map[i][j]=='b')
t2++;
}
}
if(t1==t2)
{
complete=true;
return;
}
}
}
}
}
int main()
{
int cas=1;
while(scanf("%d %d",&n,&m),n||m)
{
int i,k;
complete=false;
for(i=0;i<n;i++)
for(k=0;k<m;k++)
{
scanf(" %c",&map[i][k]);
aim[i][k]=false;
if(map[i][k]=='+'||map[i][k]=='B'||map[i][k]=='W')
{
aim[i][k]=true;
if(map[i][k]!='+')
map[i][k]+=32;
}
if(map[i][k]=='w')
startx=i,starty=k;
}
char str[100];
scanf("%s",str);
run(str);
printf("Game %d: ",cas++);
puts(complete?"complete":"incomplete");
for(i=0;i<n;i++)
{
for(k=0;k<m;k++)
{
if(aim[i][k])
{
if(map[i][k]=='b'||map[i][k]=='w')
map[i][k]-=32;
else
map[i][k]='+';
}
printf("%c",map[i][k]);
}
printf("\n");
}
}
return 0;
}

18
HDOJ/4215_autoAC.cpp Normal file
View File

@ -0,0 +1,18 @@
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int answer[31]={0,1,1,2,2,4,5,5,6,6,7,7,7,7,7,7,7,7,8,8,8,8,8,8,9,9,9,9,9,9,10};
int n,c;
scanf("%d",&c);
for(int t=1;t<=c;t++)
{
scanf("%d",&n);
if(n<30)
printf("Case %d: %d\n",t,answer[n]);
else
printf("Case %d: 10\n",t);
}
return 0;
}

56
HDOJ/4216_autoAC.cpp Normal file
View File

@ -0,0 +1,56 @@
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define D 10000
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
int num[105][2];
int dp[2][20005][2];
int T,n,x,y;
double dis(int a,int b)
{
double c=(double)a,d=(double)b;
return sqrt((c-x)*(c-x)+(d-y)*(d-y));
}
int main()
{
scanf("%d",&T);
for(int cas=1; cas<=T; ++cas)
{
scanf("%d%d%d",&n,&x,&y);
x+=D;
y+=D;
for(int i=1; i<=n; ++i)
{
scanf("%d%d",&num[i][0],&num[i][1]);
}
memset(dp,-1,sizeof(dp));
dp[0][D][0]=dp[0][D][1]=D;
for(int i=1; i<=n; ++i)
{
for(int j=0; j<=20000; ++j)
for(int k=0; k<2; ++k)
{
if(dp[(i-1)&1][j][k]!=-1)
{
if(dp[i&1][j+num[i][1]][0]==-1||dp[i&1][j+num[i][1]][0]>dp[(i-1)&1][j][k]+num[i][0])
dp[i&1][j+num[i][1]][0]=dp[(i-1)&1][j][k]+num[i][0];
if(dp[i&1][j-num[i][1]][0]==-1||dp[i&1][j-num[i][1]][0]>dp[(i-1)&1][j][k]-num[i][0])
dp[i&1][j-num[i][1]][0]=dp[(i-1)&1][j][k]-num[i][0];
if(dp[i&1][j+num[i][1]][1]==-1||dp[i&1][j+num[i][1]][1]<dp[(i-1)&1][j][k]+num[i][0])
dp[i&1][j+num[i][1]][1]=dp[(i-1)&1][j][k]+num[i][0];
if(dp[i&1][j-num[i][1]][1]==-1||dp[i&1][j-num[i][1]][1]<dp[(i-1)&1][j][k]-num[i][0])
dp[i&1][j-num[i][1]][1]=dp[(i-1)&1][j][k]-num[i][0];
}
}
}
double dist=0.0;
for(int i=0; i<=20000; ++i)
for(int j=0; j<2; ++j)
if(dp[n&1][i][j]!=-1)
dist=max(dist,dis(dp[n&1][i][j],i));
printf("Case %d: %.3f\n",cas,dist);
}
return 0;
}

48
HDOJ/4217_autoAC.cpp Normal file
View File

@ -0,0 +1,48 @@
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
struct Seg
{
int l,r,mid;
int sum;
}T[1111111];
void build(int l,int r,int k)
{
T[k].l=l;
T[k].r=r;
T[k].mid=(l+r)>>1;
if(l==r) {T[k].sum=1;return ;}
build(l,T[k].mid,2*k);
build(T[k].mid+1,r,2*k+1);
T[k].sum=T[2*k].sum+T[2*k+1].sum;
}
int find(int tot,int k)
{
int ans;
if(T[k].l==T[k].r) {T[k].sum=0;return T[k].l;}
if(tot<=T[2*k].sum) ans=find(tot,2*k);
else ans=find(tot-T[2*k].sum,2*k+1);
T[k].sum=T[2*k].sum+T[2*k+1].sum;
return ans;
}
int main()
{
int T,Case;
int n,m;
int t,temp;
__int64 ans;
scanf("%d",&T);
for(Case=1;Case<=T;Case++)
{
scanf("%d%d",&n,&m);
build(1,n,1);
ans=0;
while(m--)
{
scanf("%d",&t);
ans+=find(t,1);
}
printf("Case %d: %I64d\n",Case,ans);
}
return 0;
}

27
HDOJ/4218_autoAC.cpp Normal file
View File

@ -0,0 +1,27 @@
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
int main()
{
int n,r;
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf("%d",&r);
printf("Case %d:\n",i);
for(int j=0; j <2 * r+1; j++)
{
for(int k=0; k<2 * r + 1; k++)
{
if( abs((j-r)*(j-r)+(k-r)*(k-r)-r*r+0.0)<=3 )
putchar('*');
else
putchar(' ');
}
puts("");
}
}
return 0;
}

86
HDOJ/4219_autoAC.cpp Normal file
View File

@ -0,0 +1,86 @@
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#define N 70
#define S 600
using namespace std;
int n,l,s,vis[N],fa[N];
vector<int>gra[N];
double dp[N][S];
void dfs(int d,int p)
{
vis[d]=1;fa[d]=p;
int len=gra[d].size();
if(p!=-1&&len==1)
{
dp[d][0]=1;
for(int i=1;i<=s;i++)dp[d][i]=0;
return ;
}
for(int i=0;i<len;i++)
if(!vis[gra[d][i]])dfs(gra[d][i],d);
double sum[S]={0};
for(int i=0;i<=s;i++)
{
if(i*2<=s)
{
dp[d][i]=1;
for(int j=0;j<len;j++)
{
int e=gra[d][j];
if(fa[e]!=d)continue;
double tmp=0;
for(int k=0;k<=min(l,i);k++)
for(int h=0;h<=i-k;h++)
tmp+=dp[e][h];
tmp/=(l+1);
dp[d][i]*=tmp;
}
if(i>0)dp[d][i]-=sum[i-1],sum[i]=dp[d][i]+sum[i-1];
else sum[i]=dp[d][i];
}
else
{
int op=s-i;
dp[d][i]=0;
for(int j=0;j<len;j++)
{
int e=gra[d][j];
if(fa[e]!=d)continue;
double tmp1=0;
for(int k=0;k<=min(l,op);k++)
for(int h=0;h<=op-k;h++)
tmp1+=dp[e][h];
tmp1/=(l+1);
double tmp2=0;
for(int k=0;k<=min(l,i);k++)
tmp2+=dp[e][i-k];
tmp2/=(l+1);
dp[d][i]+=sum[op]*tmp2/tmp1;
}
}
}
}
int main()
{
int ncase;
scanf("%d",&ncase);
for(int u=1;u<=ncase;u++)
{
scanf("%d%d%d",&n,&l,&s);
for(int i=1;i<=n;i++)gra[i].clear();
for(int i=1;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
gra[a].push_back(b);
gra[b].push_back(a);
}
memset(vis,0,sizeof(vis));
dfs(1,-1);
double ans=0;
for(int i=0;i<=s;i++)ans+=dp[1][i];
printf("Case %d: %.6lf\n",u,ans);
}
}

33
HDOJ/4221_autoAC.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <stdio.h>
#include <algorithm>
using namespace std;
struct TT
{
int c,d;
bool operator < (const TT& s)const
{
return d < s.d;
}
}A[100002];
int main()
{
int z,n;
__int64 t,ans;
scanf("%d",&z);
for(int k=1;k<=z;k++)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d%d",&A[i].c,&A[i].d);
sort(A,A+n);
t = ans = 0;
for(int i=0;i<n;i++)
{
t += A[i].c;
if(ans < t - A[i].d)
ans = t - A[i].d;
}
printf("Case %d: %I64d\n",k,ans);
}
return 0;
}

31
HDOJ/4223_autoAC.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <stdio.h>
int abs(int s)
{
if(s<0)
return -s;
return s;
}
int main()
{
int t,n,i,j,a[1010],sum,min,k=1;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
min=abs(a[1]);
for(i=2;i<=n;i++)
{
sum=0;
for(j=i;j>=1;j--)
{
sum+=a[j];
if(min>abs(sum))
min=abs(sum);
}
}
printf("Case %d: %d\n",k++,min);
}
return 0;
}

31
HDOJ/4224_autoAC.cpp Normal file
View File

@ -0,0 +1,31 @@
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
for(int k=1;k<=t;k++)
{
int u1,d1,u2,d2,u3,d3;
scanf("%d %d %d %d %d %d",&u1,&d1,&u2,&d2,&u3,&d3);
int a=0,b=0,c=0;
if(u1<d1)
a=-1;
else if(u1>d1)
a=1;
if(u2<d2)
b=-1;
else if(u2>d2)
b=1;
if(u3<d3)
c=-1;
else if(u3>d3)
c=1;
if( ((a+b+c)==3)||((a+b+c)==-3)||((a*b*c==0)&&((a+b+c==1)||(a+b+c==-1)))||(a+b+c==2)||(a+b+c)==-2)
printf("Case %d: No\n",k);
else
printf("Case %d: Yes\n",k);
}
return 0;
}

226
HDOJ/4225_autoAC.cpp Normal file
View File

@ -0,0 +1,226 @@
#include <stdio.h>
#include <iostream>
#include <memory.h>
#include <algorithm>
#include <vector>
using namespace std;
typedef vector<int> Vx;
char str[100000],len = 0;
int idx = 0;
Vx getexp(int &f);
Vx getval(int &f);
bool Isnumber(char x){
return x >= '0' && x <= '9';
}
int getnum(){
int num = 0;
int tt = 0;
if(str[idx] == '-'){
idx++;
Vx tmp = getval(tt);
return -tmp[0];
}
if(str[idx] == '+'){
idx++;
Vx tmp = getval(tt);
return tmp[0];
}
while(Isnumber(str[idx])){
num = num * 10 + str[idx] - '0';
idx++;
}
return num;
}
int op(int a,int b,char opt){
int Ret = 0;
if(opt == '+') Ret = a + b;
if(opt == '-') Ret = a - b;
if(opt == '*') Ret = a * b;
return Ret;
}
void display(Vx x){
}
Vx cal(Vx a,Vx b,char opt){
Vx Ret;
int i;
if(a.size() == 1 && b.size() == 1){
Ret.push_back(op(a[0],b[0],opt));
return Ret;
}
if(a.size() == 1) {
for(i = 0;i < b.size();i++){
Ret.push_back(op(a[0],b[i],opt));
}
return Ret;
}
if(b.size() == 1) {
for(i = 0;i < a.size();i++) {
Ret.push_back(op(a[i],b[0],opt));
}
return Ret;
}
for(i = 0;i < a.size();i++) {
Ret.push_back(op(a[i],b[i],opt));
}
return Ret;
}
Vx vary(int val){
int i;
Vx Ret;
Ret.clear();
if(val == 0) return Ret;
if(val > 0) {
for(i = 1;i <= val;i++) Ret.push_back(i);
}else{
for(i = -val;i >= 1;i--) Ret.push_back(i);
}
return Ret;
}
Vx getval(int &f){
Vx Ret;
int i;
if(Isnumber(str[idx]) || str[idx] == '-'){
Ret.push_back(getnum());
f = 0;
return Ret;
}
if(str[idx] == 'v'){
idx = idx + 5;
int tt = 0;
Vx tmp = getexp(tt);
idx++;
Ret = vary(tmp[0]);
f = 1;
return Ret;
}
if(str[idx] == 'b'){
idx = idx + 6;
while(str[idx] != ')'){
int tt = 0;
Vx tmp = getexp(tt);
Ret.push_back(tmp[0]);
if(str[idx] == ')') break;
idx++;
}
f = 1;
idx++;
return Ret;
}
if(str[idx] == 'd' && str[idx+1] == 'a'){
idx = idx + 6;
int tt = 0;
Vx tmp = getexp(tt);
idx++;
if(tt == 1){
int ans = tmp[tmp.size() - 1],p;
if(tmp.size() % 2 == 0) p = -1;
else p = 1;
for(i = tmp.size() - 2;i >= 0;i--){
if(p == 1) ans = tmp[i] + ans;
else ans = tmp[i] - ans;
p = -p;
}
f = 0;
Ret.push_back(ans);
}else{
int p = 1,num = tmp[0];
if(num > 0) {
for(i = num;i >= 1;i--) {
Ret.push_back(i * p);
p = -p;
}
}else if(num < 0){
for(i = num;i <= -1;i++){
Ret.push_back(i * p);
p = -p;
}
}
f = 1;
}
return Ret;
}
if(str[idx] == 'd' && str[idx+1] == 'u'){
idx = idx + 5;
int tt = 0;
Vx tmp = getexp(tt);
idx++;
int ans = tmp[tmp.size() - 1];
for(i = tmp.size() - 2;i >= 0;i--){
ans = tmp[i] - ans;
}
Ret.push_back(ans);
f = 0;
return Ret;
}
if(str[idx] == 's'){
//sum()
idx = idx + 4;
int tt = 0;
Vx tmp = getexp(tt);
idx++;
int ans = 0;
for(i = 0;i < tmp.size();i++) {
ans += tmp[i];
}
Ret.push_back(ans);
f = 0;
return Ret;
}
return Ret;
}
bool isop(char x){
if(x == '+' || x == '-' || x == '*') return true;
else return false;
}
Vx getexp(int &f){
int i;
int tmp = 0;
Vx beg = getval(tmp);
f = tmp;
vector<pair<Vx,char> >E;E.clear();
while(idx < len && str[idx] == ' '){
if(isop(str[idx+1]) == false || str[idx+2] != ' ') break;
while(str[idx] == ' ')idx++;
char op = str[idx];
idx++;
while(str[idx] == ' ')idx++;
int t = 0;
Vx tmp = getval(t);
f = (f|t);
E.push_back(make_pair(tmp,op));
}
if(E.size() == 0) return beg;
Vx Ret = E[E.size() - 1].first;
for(i = E.size() - 1;i >= 0;i--){
if(i > 0){
Ret = cal(E[i-1].first,Ret,E[i].second);
}else Ret = cal(beg,Ret,E[i].second);
}
return Ret;
}
int main(){
int i,j,k,cas = 0;
int t;
scanf("%d",&t);
getchar();
while(t--){
gets(str);
idx = 0;
int f = 0;
len = strlen(str);
Vx ans = getexp(f);
if(f == 0) {
printf("Case %d: %d\n",++cas,ans[0]);
}else{
printf("Case %d: ",++cas);
printf("{");
for(i = 0;i < ans.size();i++){
printf("%d",ans[i]);
if(i != ans.size() - 1)
printf(", ");
}
printf("}\n");
}
}
return 0;
}

70
HDOJ/4226_autoAC.cpp Normal file
View File

@ -0,0 +1,70 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 1111;
const double PI = 3.14159265358979323846;
const double oo = (1 << 30) * 1.0;
char kind[maxn];
double len[maxn];
double dp[maxn][12];
int n, m;
double calc(int a, int b, int c)
{
if (kind[a] == 'R' || kind[a] == 'L')
{
if (b == c)
{
double r;
if (kind[a] == 'R')
r = (b - 1) * 10.0 + 5 + len[a];
else r = (m - b) * 10.0 + 5 + len[a];
return (PI / 2.0 * r);
}
else return oo;
}
else
{
if (len[a] < abs((c - b)) * 100.0)
return oo;
else
return (sqrt(len[a] * len[a] + (b - c) * 10 * (b - c) * 10));
}
}
double DP()
{
for (int i = 0; i <= m; ++i)
dp[0][i] = 0.0;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
dp[i][j] = oo;
for (int k = 1; k <= m; ++k)
{
dp[i][j] = min(dp[i][j], dp[i-1][k] + calc(i, k, j));
}
}
}
double ans = oo;
for (int i = 1; i <= m; ++i)
ans = min(ans, dp[n][i]);
return ans;
}
int main()
{
while (scanf("%d %d", &n, &m))
{
if (n == 0 && m == 0)
break;
char ch;
for (int i = 1; i <= n; ++i)
{
scanf("\n%c %lf", &kind[i], &len[i]);
}
printf("%.2lf\n", DP());
}
return 0;
}

73
HDOJ/4227_autoAC.cpp Normal file
View File

@ -0,0 +1,73 @@
#include <iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[13], p[13];
int sum, i, ans;
bool use[13];
void dfs(int k) {
int i;
if (k == 12) {
ans++;
return;
}
for (i = 0; i < 12; i++) {
if (i > 0 && !use[i - 1] && p[k - 1] == a[i])
continue;
if (!use[i]) {
p[k] = a[i];
if (k == 3) {
if (p[0] + p[1] + p[2] + p[3] != sum) {
continue;
}
}
if (k == 6) {
if (p[3] + p[4] + p[5] + p[6] != sum) {
continue;
}
}
if (k == 8) {
if (p[0] + p[8] + p[7] + p[6] != sum) {
continue;
}
}
if (k == 10) {
if (p[10] + p[1] + p[8] + p[9] != sum) {
continue;
}
}
if (k == 11) {
if (p[10] + p[2] + p[4] + p[11] != sum) {
continue;
}
if (p[9] + p[7] + p[5] + p[11] != sum) {
continue;
}
}
use[i] = 1;
dfs(k + 1);
use[i] = 0;
}
}
}
int main() {
while (1) {
sum = 0;
for (i = 0; i < 12; i++) {
scanf("%d", &a[i]);
sum += a[i];
}
sort(a, a + 12);
if (!sum)
break;
if (sum % 3) {
printf("0\n");
continue;
}
ans = 0;
sum /= 3;
dfs(0);
printf("%d\n", ans / 12);
}
return 0;
}

49
HDOJ/4228_autoAC.cpp Normal file
View File

@ -0,0 +1,49 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<bitset>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson i<<1,l,mid
#define rson i<<1|1,mid+1,r
#define INF 510010
#define maxn 400010
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll p[1010];
ll prime[30]= {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53};
void getartprime(ll cur,int cnt,int limit,int k)
{
if(cur>(1LL<<60) || cnt>150) return ;
if(p[cnt]!=0 && p[cnt]>cur)
p[cnt]=cur;
if(p[cnt]==0)
p[cnt]=cur;
ll temp=cur;
for(int i=1; i<=limit; i++)
{
temp=temp*prime[k];
if(temp>(1LL<<60)) return;
getartprime(temp,cnt*(i+1),i,k+1);
}
}
int main()
{
int n;
getartprime(1,1,75,0);
for(int i=1; i<=75; i++)
{
if(p[i*2-1]!=0 && p[i*2]!=0)
p[i]=min(p[i*2-1],p[i*2]);
else if(p[i*2]!=0) p[i]=p[i*2];
else p[i]=p[i*2-1];
}
while(scanf("%d",&n),n)
printf("%I64d\n",p[n]);
return 0;
}

33
HDOJ/4229_autoAC.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a,b,c,d,i,j,k,h,t;
while(cin>>a>>b>>c>>d)
{
if(a==0&& b==0&& c==0&& d==0)
break;
else
{
if(a==b&& b==c&& c==d)
t=0;
else
{
t=0;
while(a|| b || c|| d)
{
i=abs(a-b);
j=abs(b-c);
k=abs(c-d);
h=abs(d-a);
a=i,b=j,c=k,d=h;
t++;
}
t-=1;
}
cout<<t<<endl;
}
}
return 0;
}

115
HDOJ/4230_autoAC.cpp Normal file
View File

@ -0,0 +1,115 @@
#include"iostream"
#include"cstdio"
#include"queue"
#include"cstring"
using namespace std;
const int N=106;
const int mod=1000000;
const int inf=123456789;
int n,m,s_x,s_y,s_face,e_x,e_y;
int step[N][N][4],cnt[N][N][4],flag[N][N][4];
char map[N][N];
int dir[4][2]={-1,0, 0,-1, 1,0, 0,1};
struct node{
int x,y,step,face;
};
void bfs()
{
int i;
node now,next;
queue<node>q;
now.x=s_x;
now.y=s_y;
now.step=0;
now.face=s_face;
step[now.x][now.y][now.face]=0;
cnt[now.x][now.y][now.face]=1;
memset(flag,0,sizeof(flag));
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
if(flag[now.x][now.y][now.face]) continue;
flag[now.x][now.y][now.face]=1;
for(i=0;i<4;i++) if(step[now.x][now.y][now.step]<=step[e_x][e_y][i]) break;
if(i>=4) return ;
next.x=now.x+dir[now.face][0];
next.y=now.y+dir[now.face][1];
next.step=now.step+1;
next.face=now.face;
while(0<=next.x && next.x<n && 0<=next.y && next.y<m && map[next.x][next.y]!='*')
{
if(next.step<step[next.x][next.y][next.face])
{
step[next.x][next.y][next.face]=next.step;
cnt[next.x][next.y][next.face]=cnt[now.x][now.y][now.face]%mod;
q.push(next);
}
else if(next.step==step[next.x][next.y][next.face])
{
cnt[next.x][next.y][next.face]=(cnt[next.x][next.y][next.face]+cnt[now.x][now.y][now.face])%mod;
q.push(next);
}
next.x+=dir[now.face][0];
next.y+=dir[now.face][1];
}
next.x=now.x;
next.y=now.y;
next.step=now.step+1;
next.face=(now.face+1)%4;
if(next.step<step[next.x][next.y][next.face])
{
step[next.x][next.y][next.face]=next.step;
cnt[next.x][next.y][next.face]=cnt[now.x][now.y][now.face];
q.push(next);
}
else if(next.step==step[next.x][next.y][next.face])
{
cnt[next.x][next.y][next.face]=(cnt[next.x][next.y][next.face]+cnt[now.x][now.y][now.face])%mod;
q.push(next);
}
next.step=now.step+1;
next.face=(now.face+3)%4;
if(next.step<step[next.x][next.y][next.face])
{
step[next.x][next.y][next.face]=next.step;
cnt[next.x][next.y][next.face]=cnt[now.x][now.y][now.face];
q.push(next);
}
else if(next.step==step[next.x][next.y][next.face])
{
cnt[next.x][next.y][next.face]=(cnt[next.x][next.y][next.face]+cnt[now.x][now.y][now.face])%mod;
q.push(next);
}
}
}
int main()
{
int i,l,j;
while(scanf("%d%d",&n,&m),n||m)
{
for(i=0;i<n;i++)
{
scanf("%s",map[i]);
for(l=0;l<m;l++)
{
if(map[i][l]=='X') {e_x=i;e_y=l;}
if(map[i][l]=='N') {s_x=i;s_y=l;s_face=0;}
else if(map[i][l]=='W') {s_x=i;s_y=l;s_face=1;}
else if(map[i][l]=='S') {s_x=i;s_y=l;s_face=2;}
else if(map[i][l]=='E') {s_x=i;s_y=l;s_face=3;}
for(j=0;j<4;j++) step[i][l][j]=inf;
}
}
map[s_x][s_y]='.';
memset(cnt,0,sizeof(cnt));
bfs();
int ans=0,minstep=inf;
for(i=0;i<4;i++) if(step[e_x][e_y][i]<minstep) minstep=step[e_x][e_y][i];
for(i=0;i<4;i++) if(step[e_x][e_y][i]==minstep) ans=(ans+cnt[e_x][e_y][i])%mod;
if(minstep==inf) printf("0 0\n");
else printf("%d %d\n",minstep,ans);
}
return 0;
}

189
HDOJ/4231_autoAC.cpp Normal file
View File

@ -0,0 +1,189 @@
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
#define M 105
struct info
{
char side;
int sou;
int v;
};
info fold[30];
int w,h,n;
int we,he;
int x,y;
void getdata(void);
int cal(int ws,int hs,int px,int py,int step);
int main()
{
while (scanf("%d%d%d",&w,&h,&n),w+h+n)
{
getdata();
printf("%d\n",cal(we,he,x,y,n));
}
return 0;
}
void getdata(void)
{
int i;
we=w;
he=h;
for (i=1;i<=n;i++)
{
scanf(" %c%d",&fold[i].side,&fold[i].v);
switch(fold[i].side)
{
case 'T':
case 'B':
fold[i].sou=he;
if (he-fold[i].v>=fold[i].v)
he-=fold[i].v;
else
{
he=fold[i].v;
/*fold[i].v=he-fold[i].v;
fold[i].side=fold[i].side=='B'?'T':'B';
he-=fold[i].v;*/
}
break;
case 'L':
case 'R':
fold[i].sou=we;
if (we-fold[i].v>=fold[i].v)
we-=fold[i].v;
else
{
we=fold[i].v;
/*fold[i].v=we-fold[i].v;
fold[i].side=fold[i].side=='R'?'L':'R';
we-=fold[i].v;*/
}
break;
}
}
scanf("%d%d",&x,&y);
}
int cal(int ws,int hs,int px,int py,int step)
{
int ans=0;
int line;
if (step<1)
return 1;
else
{
switch (fold[step].side)
{
case 'T':
if (hs>fold[step].v)
{
if (py<hs-fold[step].v)
{
ans+=cal(ws,hs+fold[step].v,px,py,step-1);
}
else
{
ans+=cal(ws,hs+fold[step].v,px,py,step-1);
ans+=cal(ws,hs+fold[step].v,px,hs+(hs-py),step-1);
}
}
else
{
line=2*fold[step].v-fold[step].sou;
if (py>line)
{
ans+=cal(ws,fold[step].sou,px,hs+(hs-py)-line,step-1);
ans+=cal(ws,fold[step].sou,px,py-line,step-1);
}
else
{
ans+=cal(ws,fold[step].sou,px,hs+(hs-py)-line,step-1);
}
}
break;
case 'B':
if (hs>fold[step].v)
{
if (py>fold[step].v)
{
ans+=cal(ws,hs+fold[step].v,px,py+fold[step].v,step-1);
}
else
{
ans+=cal(ws,hs+fold[step].v,px,py+fold[step].v,step-1);
ans+=cal(ws,hs+fold[step].v,px,fold[step].v-py,step-1);
}
}
else
{
line=2*fold[step].v-fold[step].sou;
if (py<hs-line)
{
ans+=cal(ws,fold[step].sou,px,py+fold[step].v,step-1);
ans+=cal(ws,fold[step].sou,px,fold[step].v-py,step-1);
}
else
{
ans+=cal(ws,fold[step].sou,px,fold[step].v-py,step-1);
}
}
break;
case 'L':
if (ws>fold[step].v)
{
if (px>fold[step].v)
{
ans+=cal(ws+fold[step].v,hs,px+fold[step].v,py,step-1);
}
else
{
ans+=cal(ws+fold[step].v,hs,px+fold[step].v,py,step-1);
ans+=cal(ws+fold[step].v,hs,fold[step].v-px,py,step-1);
}
}
else
{
line=2*fold[step].v-fold[step].sou;
if (px<ws-line)
{
ans+=cal(fold[step].sou,hs,px+fold[step].v,py,step-1);
ans+=cal(fold[step].sou,hs,fold[step].v-px,py,step-1);
}
else
{
ans+=cal(fold[step].sou,hs,fold[step].v-px,py,step-1);
}
}
break;
case 'R':
if (ws>fold[step].v)
{
if (px<ws-fold[step].v)
{
ans+=cal(fold[step].sou,hs,px,py,step-1);
}
else
{
ans+=cal(fold[step].sou,hs,px,py,step-1);
ans+=cal(fold[step].sou,hs,ws+(ws-px),py,step-1);
}
}
else
{
line=2*fold[step].v-fold[step].sou;
if (px>line)
{
ans+=cal(fold[step].sou,hs,px-line,py,step-1);
ans+=cal(fold[step].sou,hs,ws+(ws-px)-line,py,step-1);
}
else
{
ans+=cal(fold[step].sou,hs,ws+(ws-px)-line,py,step-1);
}
}
break;
}
}
return ans;
}

93
HDOJ/4233_autoAC.cpp Normal file
View File

@ -0,0 +1,93 @@
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;
#define maxn 100010
int N, K, root;
int w[maxn];
bool select[maxn];
vector<int> G[maxn];
int f[maxn];
void dfs(int now)
{
int nex;
int mx = -1e9;
for(int i = 0, sz = G[now].size(); i < sz; i++)
{
nex = G[now][i];
dfs(G[now][i]);
f[now] = max(f[now], max(f[nex], w[nex] + mx));
w[now] = max(w[now], w[nex]);
mx = max(mx, w[nex]);
}
return;
}
void solve()
{
memset(select, 0, sizeof(select));
priority_queue<pair<int, int> > Q;
Q.push(make_pair(w[root], root));
int ret = 0;
while(K)
{
if(Q.empty())
{
puts("0");
return;
}
int u = Q.top().second;
Q.pop();
if(select[u])
{
K++;
select[u] = 0;
ret -= w[u];
for(int i = 0, sz = G[u].size(); i < sz; i++) Q.push(make_pair(w[G[u][i]], G[u][i]));
}
else
{
K--;
select[u] = 1;
ret += w[u];
Q.push(make_pair(f[u] - w[u], u));
}
}
printf("%d\n", ret);
return;
}
int main()
{
while(scanf("%d %d", &N, &K), N || K)
{
for(int i = 1; i <= N; i++) G[i].clear();
int p;
for(int i = 1; i <= N; i++)
{
f[i] = -1e9;
scanf("%d %d", &p, w + i);
if(p == 0) root = i;
else G[p].push_back(i);
}
dfs(root);
solve();
}
return 0;
}

80
HDOJ/4234_autoAC.cpp Normal file
View File

@ -0,0 +1,80 @@
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define for if(0); else for
const double PI=acos(-1.0);
struct Point{
double x,y,vx,vy,t;
Point(){}
Point(double x,double y){
this->x=x;
this->y=y;
}
Point(double x,double y,double d,double v){
this->x=x;
this->y=y;
this->vx=v*cos(d*PI/180.0);
this->vy=v*sin(d*PI/180.0);
this->t=0;
}
Point go(double t) const{
Point ret=*this;
ret.x+=vx*t;
ret.y+=vy*t;
return ret;
}
};
int n;
Point p[16];
double spd;
double dp[16][1<<15];
bool vis[16][1<<15];
double dis(const Point &a,const Point &b){
double dx=a.x-b.x;
double dy=a.y-b.y;
return sqrt(dx*dx+dy*dy);
}
double cost(const Point &a,const Point &b){
Point ta=Point(b.x-a.x,b.y-a.y);
Point v=Point(b.vx,b.vy);
double V=dis(v,Point(0,0));
double D=dis(ta,Point(0,0));
double A=V*V-spd*spd;
double C=D*D;
double B=-2.0*(ta.x*v.x+ta.y*v.y);
return (B-sqrt(B*B-4*A*C))/2.0/A;
}
int main() {
setbuf(stdout,NULL);
while(scanf("%d%lf",&n,&spd) &&(n!=0&&spd!=0)){
p[0]=Point(0,0,0,0);
for(int i=1;i<=n;i++){
double x,y,d,v;
scanf("%lf%lf%lf%lf",&x,&y,&d,&v);
p[i]=Point(x,y,d,v);
}
memset(vis,0,sizeof(vis));
vis[0][0]=1;
for(int stat=1;stat<1<<n;stat++){
for(int i=1;i<=n;i++){
int prev=stat;
prev &= ~ ( 1<< (i-1) );
if(prev==stat) continue;
for(int j=0;j<=n;j++) if(j!=i){
if(vis[j][prev]){
double val=dp[j][prev]+cost(p[j].go(dp[j][prev]),p[i].go(dp[j][prev]));
if(!vis[i][stat]) vis[i][stat]=1,dp[i][stat]=val;
else dp[i][stat]=min(dp[i][stat],val);
}
}
}
}
double ans=dp[1][(1<<n)-1];
for(int i=1;i<=n;i++) ans=min(ans,dp[i][(1<<n)-1]);
printf("%.2lf\n",ans);
}
return 0;
}

16
HDOJ/4235_autoAC.cpp Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
int a[591]={126,153,688,1206,1255,1260,1395,1435,1503,1530,1827,2187,3159,3784,6880,10251,10255,10426,10521,10525,10575,11259,11844,11848,12006,12060,12384,12505,12546,12550,12595,12600,12762,12843,12955,12964,13243,13545,13950,14035,14350,15003,15030,15246,15300,15435,15624,15795,16272,17325,17428,17437,17482,18225,18265,18270,19026,19215,21375,21586,21753,21870,25105,25375,25474,25510,28476,29632,31509,31590,33655,33696,36855,37840,37845,39784,41665,42898,44676,45684,45760,45864,47538,48672,49855,51759,52168,53865,56295,56875,62968,63895,67149,67392,67950,68800,71199,78975,100255,100525,101299,102051,102505,102510,102541,102550,102595,102942,102955,103968,104026,104260,104368,105021,105025,105075,105210,105250,105264,105295,105723,105750,107163,107329,108126,108135,108216,108612,108864,109525,110758,112468,112509,112590,114268,115672,115699,116478,116496,116725,116928,117067,118408,118440,118480,118575,118926,119848,120006,120060,120384,120600,120762,120843,121086,121576,121815,122746,122764,123084,123354,123538,123840,123894,124483,124488,124542,124978,125005,125050,125095,125248,125433,125460,125500,125950,125995,126000,126027,126108,126846,127417,127620,128403,128430,128943,129438,129505,129514,129550,129564,129595,129640,129775,129955,131242,132430,132565,132615,132655,133245,134275,134725,135045,135450,135828,135837,136525,136854,136948,138784,139500,139824,140035,140350,141345,142978,143500,143739,143793,145273,145314,145345,145683,146137,146520,146952,149364,149782,150003,150030,150246,150300,150435,150624,150826,152271,152406,152460,152608,152685,152946,153000,153436,154350,155277,156024,156240,156289,156325,156915,157950,158193,162072,162526,162720,162976,163255,163795,163854,163944,164583,165208,168520,171598,172246,172386,172822,173250,173925,174028,174082,174208,174280,174298,174370,174793,174802,174820,174982,175329,176215,178294,178942,179325,179428,179482,180225,180297,180621,182065,182250,182650,182700,182974,184126,186624,187029,189702,189742,190260,190827,191205,192150,192375,192685,192717,193257,193945,194229,197428,197482,197725,201852,205785,207391,208624,210375,210681,210753,211896,212868,213075,213466,213750,213759,214506,215086,215424,215455,215860,216733,217503,217530,217638,217854,218488,218700,223524,226498,226872,226876,227448,229648,231579,231673,233896,236754,236758,236925,238968,241506,241564,243175,245182,245448,246150,246928,250105,250510,251005,251050,251095,251896,253750,254740,255010,255100,256315,256410,256414,258795,259510,260338,261378,261783,262984,263074,263155,263736,267034,268398,279328,281736,283198,283648,284598,284760,285376,286416,286974,287356,289674,291375,291753,293625,295105,295510,296320,297463,297832,304717,307183,312475,312565,312655,312975,314199,314743,315009,315090,315490,315594,315625,315900,316255,319059,319536,325615,326155,326452,328419,328864,329346,329656,336195,336550,336960,338296,341284,341653,342688,346288,346725,346968,347913,352966,355995,361989,362992,365638,368104,368550,368784,369189,371893,373864,375156,375615,376992,378400,378418,378450,381429,384912,384925,386415,390847,392566,393246,393417,394875,397840,399784,404932,404968,414895,415575,416065,416259,416650,416988,419287,428980,429664,435784,439582,442975,446760,446976,447916,449676,449955,450688,451768,456840,457168,457600,458640,462672,465088,465984,468535,475380,475893,476892,486720,488592,489159,489955,490176,491688,493857,495328,497682,498550,515907,516879,517509,517590,519745,520168,520816,521608,521680,526792,529672,530379,531297,535968,536539,538650,549765,559188,562950,564912,567648,568750,571648,573768,588676,611793,611878,612598,614965,617728,618759,623758,629680,632875,638950,649638,661288,665919,667876,671409,671490,671944,673920,678892,679500,687919,688000,692712,697248,702189,702918,710496,711099,711909,711990,715959,719199,729688,736695,738468,741928,769792,773896,778936,782896,785295,789250,789525,789750,791289,792585,794088,798682,809919,809937,809964,815958,829696,841995,859968,899019,936985,939658,960988,1000255};
int main()
{
int x;
while (scanf("%d", &x)){
if (x == 0) break;
for (int i = 0; i < 591; ++i){
if (a[i] >= x){
printf("%d\n", a[i]);
break;
}
}
}
return 0;
}

22
HDOJ/4236_autoAC.cpp Normal file
View File

@ -0,0 +1,22 @@
#include<iostream>
#include<string>
using namespace std;
char ch[25];
int main()
{
int n, i, j;
scanf("%d", &n);
while(n--)
{
int a, b;
scanf("%d%d%*c%s", &a, &b, ch);
printf("%d ", a);
for(i = 0; ch[i] != '\0'; i++)
{
for(j = 0; j < b; j++)
printf("%c", ch[i]);
}
printf("\n");
}
return 0;
}

13
HDOJ/4237_autoAC.cpp Normal file
View File

@ -0,0 +1,13 @@
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
int t,r;
long long n,m,k;
scanf("%d",&t);
while(t--){
scanf("%d%lld%lld",&r,&k,&m);
printf("%d %lld\n",r,1+(k-m)*m);
}
return 0;
}

80
HDOJ/4238_autoAC.cpp Normal file
View File

@ -0,0 +1,80 @@
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#define ll long long
#define oo 1<<29
#define pi acos(-1.0)
#define esp 1e-30
using namespace std;
char s[45]={"PQWERTYUIOJ#SZK*?F@D!HNM&LXGABCV"},c,Q[30];
int A[30],C[30];
int main()
{
int cases,id,t,x,i,len;
scanf("%d",&cases);
while (cases--)
{
scanf("%d%s",&id,Q),len=strlen(Q);
if (Q[0]=='-') x=Q[1]-'0';
else x=Q[0]-'0';
if (x)
{
if (x>1) { printf("%d INVALID VALUE\n",id); continue; }
for (i=3;i<=19;i++)
if (Q[i]>='1' && Q[i]<='9') break;
if (i<=19) { printf("%d INVALID VALUE\n",id); continue; }
}
if (Q[0]=='-')
{
A[1]=1;
C[x=1]=1;
for (i=2;i<len;i++)
if (Q[i]>='0' && Q[i]<='9')
C[++x]=0-(Q[i]-'0');
len=x;
for (i=len;i>=1;i--)
if (C[i]<0) C[i-1]--,C[i]+=10;
C[1]=0;
}
else
{
A[1]=0;
x=0;
for (i=0;i<len;i++)
if (Q[i]>='0' && Q[i]<='9')
C[++x]=Q[i]-'0';
len=x;
}
for (i=2;i<=17;i++)
{
for (x=2;x<=len;x++) C[x]*=2;
for (x=1;x<=len;x++) C[x-1]+=C[x]/10,C[x]%=10;
if (C[1]) A[i]=1;
else A[i]=0;
C[1]=0;
while (len && !C[len]) len--;
}
if (len && A[1]) A[17]++;
for (x=20;x>=1;x--) A[x-1]+=A[x]/2,A[x]%=2;
x=0;
for (i=1;i<=5;i++)
if (A[i]) x+=1<<(5-i);
printf("%d %c",id,s[x]);
x=0,t=10;
for (i=6;i<=16;i++)
{
if (A[i]) x+=1<<t;
t--;
}
printf(" %d",x);
if (!A[17]) printf(" F\n");
else printf(" D\n");
}
return 0;
}

83
HDOJ/4239_autoAC.cpp Normal file
View File

@ -0,0 +1,83 @@
#include<stdio.h>
#include<string.h>
char map[33] = {'P' , 'Q' , 'W' , 'E' , 'R' , 'T' , 'Y' , 'U' , 'I' , 'O' , 'J' , '#' , 'S' , 'Z' , 'K' , '*' , '?' , 'F' , '@' , 'D' , '!' , 'H' , 'N' , 'M' , '&' , 'L' , 'X' , 'G' , 'A','B','C','V'};
char a[3] , b[3];
int n , num;
double ans;
char tans[10000];
char str[30];
void f(char t){
int d = 4;
for(int i = 0 ; i < 32 ; i ++){
if(t == map[i]){
while(i != 0){
str[d --] = i % 2 + '0';
i /= 2;
}
break;
}
}
while(d >= 0){
str[d --] = '0';
}
return ;
}
void ff(int t){
int d = 15;
while(t != 0){
str[d --] = t % 2 + '0';
t /= 2;
}
while(d >= 5){
str[d --] = '0';
}
return ;
}
void fff(char t){
if(t == 'F') str[16] = '0';
if(t == 'D') str[16] = '1';
}
int main(){
scanf("%d" , &n);
for(int i = 1 ; i <= n ; i ++){
ans = 0;
double c = 0.5;
scanf("%*d%s%d%s" , a , &num , b);
f(a[0]);
ff(num);
fff(b[0]);
str[17] = '\0';
printf("%d " , i);
if(str[0] == '0'){
for(int i = 1 ; i < 17 ; i ++){
ans += (str[i] - '0') * c;
c /= 2;
}
sprintf(tans , "%.16lf" , ans);
}
else{
for(int i = 1 ; i < 17 ; i ++){
ans += (str[i] - '0') * c;
c /= 2;
}
ans += -1.0;
sprintf(tans , "%.16lf" , ans);
}
if(ans == 0.0){
printf("0.0\n");
}
else if(ans == -1.0){
printf("-1.0\n");
}
else{
int di;
for(di = strlen(tans) - 1 ; di >= 0 ; di --){
if(tans[di] != '0') break;
}
tans[di + 1] = '\0';
printf("%s" , tans);
printf("\n");
}
}
return 0;
}

112
HDOJ/4240_autoAC.cpp Normal file
View File

@ -0,0 +1,112 @@
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 1010;
const int INF = 0x3f3f3f3f;
struct Edge
{
int from, to, cap, flow;
Edge(int from, int to, int cap, int flow): from(from), to(to), cap(cap), flow(flow) {}
};
int high, low;
struct EK
{
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn];
int p[maxn];
int a[maxn];
void init(int n)
{
this->n = n;
edges.clear();
for(int i = 0; i <= n; i++) G[i].clear();
}
void AddEdge(int from, int to, int cap)
{
edges.push_back(Edge (from, to, cap, 0));
edges.push_back(Edge (to, from, 0, 0));
m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BFS(int s, int t, int &flow)
{
memset(a, 0, sizeof(a));
queue<int> Q;
Q.push(s);
a[s] = INF;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i = 0; i < G[u].size(); i++)
{
Edge &e = edges[G[u][i]];
if(!a[e.to] && e.cap > e.flow)
{
p[e.to] = G[u][i];
Q.push(e.to);
a[e.to] = min(a[u], e.cap-e.flow);
}
}
}
if(a[t] == 0) return 0;
flow += a[t];
low = INF;
int u = t;
while(u != s)
{
edges[p[u]].flow += a[t];
edges[p[u]^1].flow -= a[t];
low = min(low, edges[p[u]].cap);
u = edges[p[u]].from;
}
high = max(high, low);
return 1;
}
int Maxflow(int s, int t)
{
this->s = s; this->t = t;
int flow = 0;
while(BFS(s, t, flow)) ;
return flow;
}
};
void readint(int &x)
{
char c;
while(!isdigit(c)) c = getchar();
x = 0;
while(isdigit(c)) { x = x*10 + c-'0'; c = getchar(); }
}
void writeint(int x)
{
if(x > 9) writeint(x/10);
putchar(x%10+'0');
}
EK solver;
int n, m, s, t;
int kase;
void solve()
{
scanf("%d%d%d%d%d", &kase, &n, &m, &s, &t);
solver.init(n+3);
high = -INF;
while(m--)
{
int x, y, z;
readint(x), readint(y), readint(z);
solver.AddEdge(x, y, z);
}
int ans = solver.Maxflow(s, t);
printf("%d %.3lf\n", kase, ans*1.0/high);
}
int main()
{
int T;
for(readint(T); T > 0; T--) solve();
return 0;
}

54
HDOJ/4242_autoAC.cpp Normal file
View File

@ -0,0 +1,54 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
char a[20];
double funx(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){
return (y4-y2+(y1-y2)*x2/(x1-x2)-(y3-y4)*x4/(x3-x4))/((y1-y2)/(x1-x2)-(y3-y4)/(x3-x4));
}
double funy(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){
return y2+(y1-y2)*(funx(x1,y1,x2,y2,x3,y3,x4,y4)-x2)/(x1-x2);
}
double area(double x0,double y0,double x1,double y1,double x2,double y2){
return fabs(x0*y1+x2*y0+x1*y2-x2*y1-x0*y2-x1*y0);
}
double dis(double x1,double y1,double x2,double y2){
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
int main(){
double ax=0.0,ay=0.0,bx,by=0.0,cx,cy,dx,dy;
double aax,aay,bbx,bby,ccx,ccy,ddx,ddy;
int t,n;
while(~scanf("%d",&t)){
while(t--){
scanf("%d%lf%lf%lf%lf%lf",&n,&bx,&cx,&cy,&dx,&dy);
aax=(bx+cx)/2;
aay=(by+cy)/2;
bbx=(cx+dx)/2;
bby=(cy+dy)/2;
ccx=(ax+dx)/2;
ccy=(ay+dy)/2;
ddx=(ax+bx)/2;
ddy=(ay+by)/2;
double x1=funx(ax,ay,aax,aay,bx,by,bbx,bby);
double y1=funy(ax,ay,aax,aay,bx,by,bbx,bby);
double x2=funx(bx,by,bbx,bby,cx,cy,ccx,ccy);
double y2=funy(bx,by,bbx,bby,cx,cy,ccx,ccy);
double x3=funx(cx,cy,ccx,ccy,dx,dy,ddx,ddy);
double y3=funy(cx,cy,ccx,ccy,dx,dy,ddx,ddy);
double x4=funx(ax,ay,aax,aay,dx,dy,ddx,ddy);
double y4=funy(ax,ay,aax,aay,dx,dy,ddx,ddy);
double al,bob,chas,dave,emily;
al =area(ax,ay,bx,by,x1,y1)/2/160;
bob =area(bx,by,cx,cy,x2,y2)/2/160;
chas=area(cx,cy,dx,dy,x3,y3)/2/160;
dave=area(dx,dy,ax,ay,x4,y4)/2/160;
double all=(area(ax,ay,bx,by,cx,cy)+area(ax,ay,cx,cy,dx,dy))/2/160;
emily=all-al-bob-chas-dave;
int d=(int)ceil((dis(x1,y1,x2,y2)+dis(x2,y2,x3,y3)+dis(x3,y3,x4,y4)+dis(x4,y4,x1,y1))*16.5);
printf("%d %.3f %.3f %.3f %.3f %.3f %d\n",n,al,bob,chas,dave,emily,d);
}
}
return 0;
}

48
HDOJ/4243_autoAC.cpp Normal file
View File

@ -0,0 +1,48 @@
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stack>
#include<queue>
#include<math.h>
#include<cstdio>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn=500;
const int inf=0x7fffff;
double A(double x)
{
double r=1;
for(double i=1;i<=x;i++)
r*=i;
return r;
}
double C(double m,double n)
{
double r=1;
for(double i=n-m+1;i<=n;i++)
r*=i;
for(double i=m;i>=1;i--)
r/=i;
return r;
}
int main()
{
int t,a;
double n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d%lf%lf",&a,&n,&m);
double ans=0;
if(m==1) ans+=A(n-1);
else
for(double i=1;i<m;i++)
{
if(i==1)
ans+=A(n-2);
else ans+=A(n-1-i)*A(i)*C(i-1,m-2);
}
printf("%d %0.lf\n",a,ans);
}
return 0;
}

295
HDOJ/4244_autoAC.cpp Normal file
View File

@ -0,0 +1,295 @@
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <iterator>
#include <utility>
using namespace std;
template< class T > T _abs(T n) { return (n < 0 ? -n : n); }
template< class T > T _max(T a, T b) { return (!(a < b) ? a : b); }
template< class T > T _min(T a, T b) { return (a < b ? a : b); }
template< class T > T sq(T x) { return x * x; }
#define ALL(p) p.begin(),p.end()
#define MP(x, y) make_pair(x, y)
#define SET(p) memset(p, -1, sizeof(p))
#define CLR(p) memset(p, 0, sizeof(p))
#define MEM(p, v) memset(p, v, sizeof(p))
#define CPY(d, s) memcpy(d, s, sizeof(s))
#define SZ(c) (int)c.size()
#define PB(x) push_back(x)
#define ff first
#define ss second
#define ld long double
#define pii pair< int, int >
#define psi pair< string, int >
const double EPS = 1e-9;
const int INF = 0x7f7f7f7f;
struct point {
double x,y,z;
};
bool eql(point &a,point &b) {
return fabs(a.x-b.x)<EPS && fabs(a.y-b.y)<EPS && fabs(a.z-b.z)<EPS;
}
double dist(point a,point b) {
b.x-=a.x;
b.y-=a.y;
b.z-=a.z;
return sqrt(sq(b.x)+sq(b.y)+sq(b.z));
}
double ar(point a,point b,point c) {
double x=dist(a,b);
double y=dist(b,c);
double z=dist(c,a);
double s=0.5*(x+y+z);
return sqrt(s*(s-x)*(s-y)*(s-z));
}
int main() {
int i,j,k,l,T,I,cn;
double a,b,c,d,x,y,z;
point tt[200];
double g[3][2]={0};
cin>>T;
while(T--) {
cin>>I>>g[0][1]>>g[1][1]>>g[2][1]>>a>>b>>c>>d;
vector < pair < point , point > > r,tr;
for(i=0;i<2;i++) {
cn=0;
x=g[0][i];
for(j=0;j<2;j++) {
y=g[1][j];
z=(d-b*y-a*x)/c;
if(z>=0 && z<=g[2][1]) {
tt[cn].x=x;
tt[cn].y=y;
tt[cn].z=z;
cn++;
}
}
if(cn==2) {
r.PB(make_pair(tt[0],tt[1]));
continue;
}
for(j=0;j<2;j++) {
z=g[2][j];
y=(d-c*z-a*x)/b;
if(y>=0 && y<=g[1][1]) {
tt[cn].x=x;
tt[cn].y=y;
tt[cn].z=z;
cn++;
}
}
if(cn==2) r.PB(make_pair(tt[0],tt[1]));
else if(cn==3) {
bool f=1;
for(j=0;j<3 && f;j++) for(l=0;l<j && f;l++) if(!eql(tt[j],tt[l])) {
r.PB(make_pair(tt[j],tt[l]));
f=0;
}
}
}
for(i=0;i<2;i++) {
cn=0;
y=g[1][i];
for(j=0;j<2;j++) {
x=g[0][j];
z=(d-b*y-a*x)/c;
if(z>=0 && z<=g[2][1]) {
tt[cn].x=x;
tt[cn].y=y;
tt[cn].z=z;
cn++;
}
}
if(cn==2) {
r.PB(make_pair(tt[0],tt[1]));
continue;
}
for(j=0;j<2;j++) {
z=g[2][j];
x=(d-c*z-b*y)/a;
if(x>=0 && x<=g[0][1]) {
tt[cn].x=x;
tt[cn].y=y;
tt[cn].z=z;
cn++;
}
}
if(cn==2) r.PB(make_pair(tt[0],tt[1]));
else if(cn==3) {
bool f=1;
for(j=0;j<3 && f;j++) for(l=0;l<j && f;l++) if(!eql(tt[j],tt[l])) {
r.PB(make_pair(tt[j],tt[l]));
f=0;
}
}
}
for(i=0;i<2;i++) {
cn=0;
z=g[2][i];
for(j=0;j<2;j++) {
y=g[1][j];
x=(d-b*y-c*z)/a;
if(x>=0 && x<=g[0][1]) {
tt[cn].x=x;
tt[cn].y=y;
tt[cn].z=z;
cn++;
}
}
if(cn==2) {
r.PB(make_pair(tt[0],tt[1]));
continue;
}
for(j=0;j<2;j++) {
x=g[0][j];
y=(d-c*z-a*x)/b;
if(y>=0 && y<=g[1][1]) {
tt[cn].x=x;
tt[cn].y=y;
tt[cn].z=z;
cn++;
}
}
if(cn==2) r.PB(make_pair(tt[0],tt[1]));
else if(cn==3) {
bool f=1;
for(j=0;j<3 && f;j++) for(l=0;l<j && f;l++) if(!eql(tt[j],tt[l])) {
r.PB(make_pair(tt[j],tt[l]));
f=0;
}
}
}
for(i=0;i<r.size();i++) if(!eql(r[i].first,r[i].second)) tr.PB(r[i]);
r=tr;
vector < point > ls;
j=0;
ls.push_back(r[0].first);
j=0;
for(i=0;i<r.size();i++) {
for(k=0;k<r.size();k++) if(k!=j) {
if(eql(ls.back(),r[k].first)) {
ls.PB(r[k].second);
break;
}
if(eql(ls.back(),r[k].second)) {
ls.PB(r[k].first);
break;
}
}
j=k;
}
double res=0;
for(i=0;i<ls.size();i++)
for(i=2;i<ls.size()-1;i++) res+=ar(ls[0],ls[i-1],ls[i]);
z=g[2][1];
if(fabs(r.back().first.z-z)<=EPS && fabs(r.back().second.z-z)<=EPS) {
r.clear();
r.PB(tr.back());
for(i=0;i<2;i++) {
x=g[0][i];
if(a*x+b*g[1][0]+c*z<=d && a*x+b*g[1][1]+c*z<=d) {
y=(d-c*z-a*x)/b;
tt[0].x=x;
tt[0].y=g[1][0];
tt[0].z=z;
tt[1].x=x;
tt[1].y=g[1][1];
tt[1].z=z;
r.PB(make_pair(tt[0],tt[1]));
}
else if(a*x+b*g[1][0]+c*z<=d) {
y=(d-c*z-a*x)/b;
tt[0].x=x;
tt[0].y=g[1][0];
tt[0].z=z;
tt[1].x=x;
tt[1].y=y;
tt[1].z=z;
r.PB(make_pair(tt[0],tt[1]));
}
else if(a*x+b*g[1][1]+c*z<=d) {
y=(d-c*z-a*x)/b;
tt[0].x=x;
tt[0].y=y;
tt[0].z=z;
tt[1].x=x;
tt[1].y=g[1][1];
tt[1].z=z;
r.PB(make_pair(tt[0],tt[1]));
}
}
for(i=0;i<2;i++) {
y=g[1][i];
if(a*g[0][0]+b*y+c*z<=d && a*g[0][1]+b*y+c*z<=d) {
x=(d-c*z-b*y)/a;
tt[0].x=g[0][0];
tt[0].y=y;
tt[0].z=z;
tt[1].x=g[0][1];
tt[1].y=y;
tt[1].z=z;
r.PB(make_pair(tt[0],tt[1]));
}
else if(a*g[0][0]+b*y+c*z<=d) {
x=(d-c*z-b*y)/a;
tt[0].x=g[0][0];
tt[0].y=y;
tt[0].z=z;
tt[1].x=x;
tt[1].y=y;
tt[1].z=z;
r.PB(make_pair(tt[0],tt[1]));
}
else if(a*g[0][1]+b*y+c*z<=d) {
x=(d-c*z-b*y)/a;
tt[0].x=x;
tt[0].y=y;
tt[0].z=z;
tt[1].x=g[0][1];
tt[1].y=y;
tt[1].z=z;
r.PB(make_pair(tt[0],tt[1]));
}
}
tr.clear();
for(i=0;i<r.size();i++) if(!eql(r[i].first,r[i].second)) tr.PB(r[i]);
r=tr;
for(i=0;i<r.size();i++) {
}
vector < point > ls;
j=0;
ls.push_back(r[0].first);
j=0;
for(i=0;i<r.size();i++) {
for(k=0;k<r.size();k++) if(k!=j) {
if(eql(ls.back(),r[k].first)) {
ls.PB(r[k].second);
break;
}
if(eql(ls.back(),r[k].second)) {
ls.PB(r[k].first);
break;
}
}
j=k;
}
for(i=0;i<ls.size();i++)
for(i=2;i<ls.size()-1;i++) res+=ar(ls[0],ls[i-1],ls[i]);
}
cout<<I<<' '<<ceil(res-EPS)<<'\n';
}
return 0;
}

38
HDOJ/4245_autoAC.cpp Normal file
View File

@ -0,0 +1,38 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char str[100];
int cas = 1;
int main()
{
while(gets(str))
{
printf("Case %d: ",cas++);
int i,j,len;
len = strlen(str);
if(str[1] == ' ')
printf("UNIQUE\n");
else
{
if(str[1] == '#')
{
if(str[0] == 'G')
printf("Ab");
else
printf("%cb",str[0]+1);
}
else if(str[1] == 'b')
{
if(str[0] == 'A')
printf("G#");
else
printf("%c#",str[0]-1);
}
for(i = 2;i<len;i++)
printf("%c",str[i]);
printf("\n");
}
}
return 0;
}

38
HDOJ/4246_autoAC.cpp Normal file
View File

@ -0,0 +1,38 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char str[100];
int cas = 1;
int main()
{
while(gets(str))
{
printf("Case %d: ",cas++);
int i,j,len;
len = strlen(str);
if(str[1] == ' ')
printf("UNIQUE\n");
else
{
if(str[1] == '#')
{
if(str[0] == 'G')
printf("Ab");
else
printf("%cb",str[0]+1);
}
else if(str[1] == 'b')
{
if(str[0] == 'A')
printf("G#");
else
printf("%c#",str[0]-1);
}
for(i = 2;i<len;i++)
printf("%c",str[i]);
printf("\n");
}
}
return 0;
}

15
HDOJ/4247_autoAC.cpp Normal file
View File

@ -0,0 +1,15 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
int a[4],cas = 1;
int x,y,ans;
while(~scanf("%d%d%d%d",&a[0],&a[1],&a[2],&a[3]))
{
sort(a,a+4);
printf("Case %d: %d\n",cas++,a[2]+a[3]);
}
return 0;
}

48
HDOJ/4248_autoAC.cpp Normal file
View File

@ -0,0 +1,48 @@
#include <cstdio>
#include <cstring>
typedef long long Long;
const int MOD = 1000000007;
int C[11111][111];
void init() {
C[0][0] = C[1][0] = C[1][1] = 1;
for (int i = 2; i <= 10101; i++) {
for (int j = 0; j <= i && j <= 100; j++) {
if (j == 0) C[i][j] = 1;
else C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD;
}
}
}
int dp[111][11111];
int a[111];
int main() {
init();
for (int ii = 1;;ii++) {
int n; if (scanf("%d",&n) == -1) break;
memset(dp,0,sizeof(dp));
memset(a,0,sizeof(a));
for (int i = 0; i < n; i++) {
scanf("%d",a + i);
}
for (int i = 0; i <= a[0]; i++) dp[0][i] = 1;
int maxlen = a[0];
for (int now = 0; now < n - 1; now ++) {
int next = now + 1;
for (int len = 0; len <= maxlen; len++) {
if (dp[now][len] == 0) continue;
dp[next][len] += dp[now][len];
if (dp[next][len] >= MOD) dp[next][len] -= MOD;
Long tmp = dp[now][len];
for (int put = 1; put <= a[next]; put++) {
Long tt = tmp * C[len + put][put] % MOD;
dp[next][len + put] += tt;
if (dp[next][len + put] >= MOD) dp[next][len + put] -= MOD;
}
}
maxlen += a[next];
}
int ans = 0;
for (int i = 1; i <= maxlen; i++) ans = (ans + dp[n - 1][i]) % MOD;
printf("Case %d: %d\n",ii,ans);
}
return 0;
}

85
HDOJ/4249_autoAC.cpp Normal file
View File

@ -0,0 +1,85 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char str[105];
int a[15];
int b[15];
int c[15];
long long dp[10][2];
int main()
{
int i,j,n,tag,t,aup,bup,cup,cnt=1,x,y,z,p,q,r;
while(scanf("%s",str)!=EOF)
{
memset(dp,0,sizeof(dp));
n=strlen(str);
tag=0;
aup=bup=cup=0;
for (i=n-1;i>=0;i--)
{
if (str[i]=='=' || str[i]=='+') tag++;
else
{
t=str[i]=='?'?-1:(str[i]-'0');
if (tag==0) c[cup++]=t;
if (tag==1) b[bup++]=t;
if (tag==2) a[aup++]=t;
}
}
if (cup<max(aup,bup))
{
printf("Case %d: 0\n",cnt++);
continue;
}
for (i=aup;i<=cup;i++)
{
a[i]=0;
}
for (i=bup;i<=cup;i++)
{
b[i]=0;
}
for (i=0;i<cup;i++)
{
for (x=0;x<=9;x++)
{
if (i==aup-1 && aup!=1 && x==0 && a[i]==-1) continue;
if (a[i]!=-1 && x>=1) continue;
for (y=0;y<=9;y++)
{
if (i==bup-1 && bup!=1 && y==0 && b[i]==-1) continue;
if (b[i]!=-1 && y>=1) continue;
for (z=0;z<=9;z++)
{
if (i==cup-1 && cup!=1 && z==0 && c[i]==-1) continue;
if (c[i]!=-1 && z>=1) continue;
p=a[i]==-1?x:a[i];
q=b[i]==-1?y:b[i];
r=c[i]==-1?z:c[i];
if (p+q==r)
{
if (i==0) dp[i][0]++;
else dp[i][0]+=dp[i-1][0];
}
if (p+q==10+r)
{
if (i==0) dp[i][1]++;
else dp[i][1]+=dp[i-1][0];
}
if (p+q+1==r && i!=0)
{
dp[i][0]+=dp[i-1][1];
}
if (p+q+1==r+10 && i!=0)
{
dp[i][1]+=dp[i-1][1];
}
}
}
}
}
printf("Case %d: %I64d\n",cnt++,dp[cup-1][0]);
}
return 0;
}

82
HDOJ/4251_autoAC.cpp Normal file
View File

@ -0,0 +1,82 @@
#include<iostream>
#include<algorithm>
using namespace std;
const int N=100010;
int srted[N];
struct node
{
int num[N];
int val[N];
}t[40];
int n,m;
void build(int l,int r,int d)
{
if(l==r) return;
int mid=(l+r)>>1;
int midd=srted[mid];
int same=mid-l+1,samed=0,zn=l-1,yn=mid,i;
for(i=l;i<=r;++i)
if(t[d].val[i]<midd) --same;
for(i=l;i<=r;++i)
{
if(i==l) t[d].num[i]=0;
else t[d].num[i]=t[d].num[i-1];
if(t[d].val[i]<midd)
{
++t[d].num[i];
t[d+1].val[++zn]=t[d].val[i];
}else if(t[d].val[i]>midd)
{
t[d+1].val[++yn]=t[d].val[i];
}else
{
if(samed<same)
{
++samed;
++t[d].num[i];
t[d+1].val[++zn]=t[d].val[i];
}else
t[d+1].val[++yn]=t[d].val[i];
}
}
build(l,mid,d+1);
build(mid+1,r,d+1);
}
int query(int a,int b,int k,int l,int r,int d)
{
if(a==b) return t[d].val[a];
int mid=(l+r)>>1;
int sx=t[d].num[a-1],sy=t[d].num[b];
if(a-1<l) sx=0;
if(sy-sx>=k)
return query(l+sx,l+sy-1,k,l,mid,d+1);
else
{
int s1=(a==1?0:a-l-sx);
int s2=(b-a+1)-(sy-sx);
int nk=k-(sy-sx);
return query(mid+1+s1,mid+s1+s2,nk,mid+1,r,d+1);
}
}
int main()
{
int cas=1;
int i,a,b;
while(cin>>n)
{
cout<<"Case "<<cas++<<":"<<endl;
for(i=1;i<=n;++i)
{
cin>>srted[i];
t[0].val[i]=srted[i];
}
sort(srted+1,srted+1+n);
build(1,n,0);
cin>>m;
for(i=1;i<=m;++i)
{
cin>>a>>b;
cout<<query(a,b,(a+b)/2-a+1,1,n,0)<<endl;
}
}
}

37
HDOJ/4252_autoAC.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int a[1000005],x,len;
int main()
{
int n,i,cas = 1,ans,j;
while(~scanf("%d",&n))
{
ans = 0;
for(i = 0; i<n; i++)
scanf("%d",&a[i]);
if(a[0]==0)
ans++;
for(i = 1; i<n; i++)
{
if(a[i]==0)
ans++;
else
{
for(j = i-1; j>=0; j--)
{
if(a[i]>a[j])
break;
else if(a[i]==a[j])
{
ans++;
break;
}
}
}
}
printf("Case %d: %d\n",cas++,n-ans);
}
return 0;
}

114
HDOJ/4253_autoAC.cpp Normal file
View File

@ -0,0 +1,114 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
int v1,v2;
int w;
} s[100005],t[100005];
int n,m,k,cnts,cntt;
int father[100005],sum;
int cmp(const node a,const node b)
{
if(a.w<b.w)
return 1;
else
return 0;
}
int find(int x)
{
int i=x,root;
while(x!=father[x])
x=father[x];
root=x;
x=i;
while(x!=father[x])
{
i=father[x];
father[x]=root;
x=i;
}
return root;
}
int deal(int x)
{
for(int i=0; i<=n; i++)
father[i]=i;
int lens=0,lent=0,pp=0;
sum=0;
while(lens<cnts||lent<cntt)
{
if(s[lens].w+x<=t[lent].w)
{
int s1=find(s[lens].v1);
int s2=find(s[lens].v2);
if(s1!=s2)
{
father[s1]=s2;
sum+=s[lens].w+x;
pp++;
}
lens++;
}
else
{
int s1=find(t[lent].v1);
int s2=find(t[lent].v2);
if(s1!=s2)
{
father[s1]=s2;
sum+=t[lent].w;
}
lent++;
}
}
if(pp>=k) return 1;
else return 0;
}
int main()
{
int text=0;
while(scanf("%d%d%d",&n,&m,&k)>0)
{
cnts=0,cntt=0;
for(int i=0; i<m; i++)
{
int v1,v2,w,tmp;
scanf("%d%d%d%d",&v1,&v2,&w,&tmp);
if(tmp==0)
{
s[cnts].v1=v1;
s[cnts].v2=v2;
s[cnts].w=w;
cnts++;
}
if(tmp==1)
{
t[cntt].v1=v1;
t[cntt].v2=v2;
t[cntt].w=w;
cntt++;
}
}
sort(s,s+cnts,cmp);
sort(t,t+cntt,cmp);
t[cntt].w=s[cnts].w=(1<<29);
printf("Case %d: ",++text);
int l=-1000,r=1000;
int ans=0;
while(l<=r)
{
int mid=(l+r)/2;
if(deal(mid))
{
l=mid+1;
ans=sum-mid*k;
}
else r=mid-1;
}
printf("%d\n",ans);
}
return 0;
}

24
HDOJ/4254_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include<stdio.h>
#include<math.h>
int N, P, Q;
double f[200005];
double logC(int n, int m) {
return f[n]-f[m]-f[n-m];
}
int main() {
int test = 0;
for(int i=1; i<=200000; i++)
f[i] = f[i-1] + log(0.0+i);
while(scanf("%d%d%d", &N, &P, &Q) != EOF) {
double a = 0, b = 0;
for(int red=Q; red<=N-(P-Q); red++) {
int blue = N - red;
double p = exp(logC(red, Q) + logC(N-red, P - Q) - logC(N, P));
double q = (red - Q + 0.0) / (N - P);
a += p * q;
b += p;
}
printf("Case %d: %.4lf\n", ++test, a/b);
}
return 0;
}

110
HDOJ/4255_autoAC.cpp Normal file
View File

@ -0,0 +1,110 @@
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define N 40400
#define MAX 210
#define CNT 4000
using namespace std;
bool isprime[N];
int prime[CNT],cnt;
int map[MAX][MAX];
bool visited[MAX][MAX];
struct point{int x,y,step;};
void find_prime()
{
int i,j;
cnt=0;
memset(isprime,true,sizeof(isprime));
isprime[1]=false;
for(i=2;i<N;i++)
{
if(isprime[i])
{
prime[cnt++]=i;
for(j=2*i;j<N;j=j+i) isprime[j]=false;
}
}
}
void build_map(int p,int q,int s,int c)
{
if(s==0) return ;
int i;
for(i=q;i<q+c;i++) map[p][i]=s--;
for(i=p+1;i<p+c;i++) map[i][q+c-1]=s--;
for(i=q+c-2;i>q;i--) map[p+c-1][i]=s--;
for(i=p+c-1;i>p;i--) map[i][q]=s--;
build_map(p+1,q+1,s,c-2);
}
int t_i,t_j;
void find(int u)
{
int i,j;
for(i=1;i<=200;i++)
{
for(j=1;j<=200;j++)
{
if(u==map[i][j])
{
t_i=i;t_j=j;
return ;
}
}
}
}
int BFS(int u,int v)
{
find(u);
point t,tt;
t.x=t_i;t.y=t_j;t.step=0;
queue<point>Q;
while(!Q.empty()) Q.pop();
Q.push(t);
visited[t.x][t.y]=true;
while(!Q.empty())
{
t=Q.front();Q.pop();
if(map[t.x][t.y]==v) return t.step;
if(t.x-1>=1&&!visited[t.x-1][t.y]&&!isprime[map[t.x-1][t.y]])
{
tt.x=t.x-1;tt.y=t.y;tt.step=t.step+1;
Q.push(tt);
visited[tt.x][tt.y]=true;
}
if(t.x+1<=200&&!visited[t.x+1][t.y]&&!isprime[map[t.x+1][t.y]])
{
tt.x=t.x+1;tt.y=t.y;tt.step=t.step+1;
Q.push(tt);
visited[tt.x][tt.y]=true;
}
if(t.y-1>=1&&!visited[t.x][t.y-1]&&!isprime[map[t.x][t.y-1]])
{
tt.x=t.x;tt.y=t.y-1;tt.step=t.step+1;
Q.push(tt);
visited[tt.x][tt.y]=true;
}
if(t.y+1<=200&&!visited[t.x][t.y+1]&&!isprime[map[t.x][t.y+1]])
{
tt.x=t.x;tt.y=t.y+1;tt.step=t.step+1;
Q.push(tt);
visited[tt.x][tt.y]=true;
}
}
return 0;
}
int main()
{
int u,v,k=1;
memset(map,0,sizeof(map));
find_prime();
build_map(1,1,40000,200);
while(scanf("%d%d",&u,&v)!=EOF)
{
memset(visited,false,sizeof(visited));
int ans;
ans=BFS(u,v);
if(ans) printf("Case %d: %d\n",k++,ans);
else printf("Case %d: impossible\n",k++);
}
return 0;
}

21
HDOJ/4256_autoAC.cpp Normal file
View File

@ -0,0 +1,21 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
char str[20][10] = {"#","I","II","III","IV","V","VI","VII","VIII","IX","X","XI","XII"};
char s[10];
int i,cas = 1;
while(~scanf("%s",s))
{
for(i = 1;i<=12;i++)
{
if(strcmp(s,str[i])==0)
{
printf("Case %d: %d\n",cas++,i);
}
}
}
return 0;
}

51
HDOJ/4258_autoAC.cpp Normal file
View File

@ -0,0 +1,51 @@
#include <cstdio>
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int N,C;
long long a[1000010];
long long f[1000010];
struct point
{
int id;
long long x,y;
point(int a,long long b,long long c)
{
id = a;x = b;y = c;
}
point(){};
};
long long solve()
{
static point s[1000010];
int head = 1,tail = 0;
long long temp;
for (int i = 1;i <= N;i++)
{
point p(i-1,a[i],f[i-1]+ a[i]*a[i]);
while(tail-1 >= head && (s[tail].x - s[tail-1].x)*(p.y - s[tail].y) - (s[tail].y - s[tail-1].y) *(p.x - s[tail].x) <0)
tail --;
s[++tail] = p;
while (head <= tail && (temp = s[head].y - 2*a[i]*s[head].x+a[i]*a[i] + C,temp < f[i]||f[i] == -1))
{
f[i] = temp;
head++;
}
head--;
}
return f[N];
}
int main()
{
while(scanf("%d%d",&N,&C),N+C)
{
a[0] = 0;
for(int i = 1;i <= N;i++)
scanf("%d",&a[i]);
memset(f,-1,sizeof(f));
f[0] = 0;
printf("%I64d\n",solve());
}
return 0;
}

81
HDOJ/4259_autoAC.cpp Normal file
View File

@ -0,0 +1,81 @@
#include <iostream>
#include <cmath>
#include <stack>
using namespace std;
const int MAXN = 1805;
int a[2][MAXN], vst[MAXN], cnt;
int myS[MAXN][MAXN], top[MAXN];
void init()
{
memset(vst, 0, sizeof(vst));
}
void dfs(int x)
{
cnt++;
vst[x] = 1;
int nx = a[1][x];
if(!vst[nx])
{
dfs(nx);
}
}
__int64 gcd1(__int64 a, __int64 b)
{
__int64 r = b, t;
while(r)
{
t = r;
r = a % r;
a = t;
}
return a;
}
__int64 gcd2(__int64 a, __int64 b)
{
return b == 0 ? a : gcd2(b, a % b);
}
int main(void)
{
int n, k;
while(scanf("%d %d", &n, &k), n || k)
{
init();
for(int i = 0; i < n; i++)
{
int in = i % k;
myS[in][top[in]++] = i;
}
int s = 0;
for(int i = 0; i < n; i++)
a[0][i] = i;
for(int in = 0; in < k; in++)
{
while(top[in])
{
a[1][s++] = myS[in][--top[in]];
}
}
int c = 0;
__int64 res[MAXN];
for(int i = 0; i < n; i++)
{
if(!vst[i])
{
cnt = 0;
dfs(i);
res[c++] = cnt;
}
}
__int64 tmp;
if(c >= 2)
{
tmp = res[0] / gcd2(res[0], res[1]) * res[1];
for(int i = 2; i < c; i++)
tmp = tmp / gcd2(tmp, res[i]) * res[i];
}
else if(c == 1)
tmp = res[0];
printf("%I64d\n", tmp);
}
return 0;
}

19
HDOJ/4260_autoAC.cpp Normal file
View File

@ -0,0 +1,19 @@
#include<iostream>
#include<string>
using namespace std;
string s;
long long cal(long long x,char goal)
{
for(long long i=x;i>=0;i--)
if(s[i]!=goal) return cal(i-1,'A'^'B'^'C'^goal^s[i])+(1LL<<i);
return 0;
}
int main()
{
while(cin>>s,s!="X")
{
long long ans=cal(s.length()-1,'B');
cout<<ans<<endl;
}
return 0;
}

82
HDOJ/4261_autoAC.cpp Normal file
View File

@ -0,0 +1,82 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 70
#define MAXN 31290
#define OO (1<<31)-1
#define mod 100000000
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;
int n,m,ans,cnt,tot,flag;
int a[2005],dp[30][2005],w[2005][2005];
int main()
{
int i,j,k,t,num1,num2,sum;
while(scanf("%d%d",&n,&m),n|m)
{
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
priority_queue<int>lower;
priority_queue<int,vector<int>,greater<int> >upper;
sum=0;
for(j=i;j<=n;j++)
{
if(upper.empty()||a[j]>=upper.top())
{
upper.push(a[j]);
sum+=a[j];
}
else
{
lower.push(a[j]);
sum-=a[j];
}
num1=(j-i+1)/2; num2=j-i+1-num1;
if(lower.size()>num1)
{
upper.push(lower.top());
sum+=2*lower.top();
lower.pop();
}
else if(upper.size()>num2)
{
lower.push(upper.top());
sum-=2*upper.top();
upper.pop();
}
if(num2>num1) w[i][j]=sum-upper.top();
else w[i][j]=sum;
}
}
for(i=0;i<=m;i++) for(j=0;j<=n;j++) dp[i][j]=INF;
dp[0][0]=0;
for(i=1;i<=m;i++)
{
for(j=i;j<=n;j++)
{
for(k=0;k<j;k++)
{
dp[i][j]=min(dp[i][j],dp[i-1][k]+w[k+1][j]);
}
}
}
printf("%d\n",dp[m][n]);
}
return 0;
}

60
HDOJ/4262_autoAC.cpp Normal file
View File

@ -0,0 +1,60 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson u<<1
#define rson u<<1|1
#define MAXN 100005
int map[MAXN];
struct Node{
int lef,rig;
int sum;
}T[MAXN<<2];
void PushUp(int u){
T[u].sum=T[lson].sum+T[rson].sum;
}
void Build(int u,int l,int r){
T[u].lef=l;
T[u].rig=r;
if(l==r){T[u].sum=1;return;}
int mid=(l+r)>>1;
Build(lson,l,mid);
Build(rson,mid+1,r);
PushUp(u);
}
void Update(int u,int pos,int val){
if(T[u].lef==T[u].rig){T[u].sum=val;return;}
if(pos<=T[lson].rig)Update(lson,pos,val);
else Update(rson,pos,val);
PushUp(u);
}
int Query(int u,int l,int r){
if(l<=T[u].lef&&T[u].rig<=r)return T[u].sum;
if(r<=T[lson].rig)return Query(lson,l,r);
if(l>=T[rson].lef)return Query(rson,l,r);
return Query(lson,l,T[lson].rig)+Query(rson,T[rson].lef,r);
}
int main(){
int n;
while(scanf("%d",&n)&&n){
int tmp;
for(int i=1;i<=n;i++){
scanf("%d",&tmp);
map[tmp]=i;
}
Build(1,1,n);
int cur=1;
__int64 ans=n;
for(int i=1;i<n;i++){
if(i==1)tmp=Query(1,cur,map[i])-1;
else if(map[i]>cur)tmp=Query(1,cur+1,map[i])-1;
else tmp=Query(1,map[i],cur-1);
ans+=min(tmp,n-i+1-tmp);
Update(1,map[i],0);
cur=map[i];
}
printf("%I64d\n",ans);
}
return 0;
}

55
HDOJ/4263_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxx = 1010;
int Find(int x,int father[])
{
while(x!=father[x]) x = father[x];
return x;
}
void Union(int r1, int r2, int& counter,int father[])
{
int a = Find(r1,father);
int b = Find(r2,father);
if(a!=b)
{
counter++;
father[a] = b;
}
}
void ini(int n,int father[])
{
for(int i=1; i<=n; i++)
father[i] = i;
}
int main()
{
int n,m,k;
int father_r[maxx];
int father_b[maxx];
while(scanf("%d %d %d",&n,&m,&k),n||m||k)
{
ini(n,father_r);
ini(n,father_b);
int counter_r,counter_b;
counter_r = counter_b = 0;
for(int i=1; i<=m; i++)
{
char ch=0;
while(ch!='B'&&ch!='R')
ch=getchar();
int u,v;
scanf("%d%d",&u,&v);
if(ch=='B')
Union(u,v,counter_b,father_b);
else
Union(u,v,counter_r,father_r);
}
if(k<=counter_b && k>=n-counter_r-1)
printf("1\n");
else
printf("0\n");
}
return 0;
}

163
HDOJ/4264_autoAC.cpp Normal file
View File

@ -0,0 +1,163 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define eps 1e-8
#define N 200
using namespace std;
const double PI=acos(-1.0);
int dlcmp(double x)
{
return x<-eps?-1:x>eps;
}
struct Point
{
double x,y;
Point(){}
Point(double a,double b):x(a),y(b){}
Point operator + (const Point a) const {return Point(x+a.x,y+a.y);}
Point operator - (const Point a) const {return Point(x-a.x,y-a.y);}
Point operator * (const double a) const {return Point(x*a,y*a);}
Point operator / (const double a) const {return Point(x/a,y/a);}
bool operator == (const Point a)
{
return !dlcmp(x-a.x)&&!dlcmp(y-a.y);
}
Point trunc(double d) const
{
double dis(Point,Point);
double len=dis(*this,Point(0,0));
return Point(x*d/len,y*d/len);
}
Point rotate(double a)
{
return Point(x*cos(a)-y*sin(a),y*cos(a)+x*sin(a));
}
};
struct Circle
{
Point o;
double r;
Circle(){}
Circle(Point a,double l):o(a),r(l){}
};
struct node
{
double s,e;
node(){}
node(double a,double b):s(a),e(b){}
};
Circle c[N],red,purple;
vector<node>arc;
double sqr(double x)
{
return x*x;
}
double fix(double x)
{
if (dlcmp(x-1)>=0)
return 1;
if (dlcmp(x+1)<=0)
return -1;
return x;
}
double dis(Point a,Point b)
{
return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));
}
void get_InCommonTangent(Circle c1,Circle c2,Point &s1,Point &e1,Point &s2,Point &e2)
{
double l=dis(c1.o,c2.o);
double d=l*c1.r/(c1.r+c2.r);
double tmp=c1.r/d;
tmp=fix(tmp);
double theta=acos(tmp);
Point vec=c2.o-c1.o;
vec=vec.trunc(c1.r);
s1=c1.o+vec.rotate(theta);
s2=c1.o+vec.rotate(-theta);
vec=c1.o-c2.o;
vec=vec.trunc(c2.r);
e1=c2.o+vec.rotate(theta);
e2=c2.o+vec.rotate(-theta);
}
void inter_circle_ray(Circle c,Point s,Point e,Point &p)
{
Point vec=e-s;
double A=sqr(vec.x)+sqr(vec.y);
double B=2*(vec.x*(s.x-c.o.x)+vec.y*(s.y-c.o.y));
double C=sqr(s.x-c.o.x)+sqr(s.y-c.o.y)-sqr(c.r);
double delta=sqr(B)-4*A*C;
delta=fabs(delta);
double k=(-B+sqrt(delta))/(2*A);
p=s+vec*k;
}
double get_angle(Point a)
{
return atan2(a.y,a.x);
}
double cmp(node a,node b)
{
if (dlcmp(a.s-b.s)==0)
return dlcmp(a.e-b.e)<0;
else
return dlcmp(a.s-b.s)<0;
}
int main()
{
int n,p,x,y,r,i,j;
double ans,sum,cur;
node u,v;
Point s1,e1,s2,e2,vec,p1,p2;
while (scanf("%d%d%d%d%d",&n,&p,&x,&y,&r),n||p||x||y||r)
{
purple.o=Point(0,0);
purple.r=(double)p;
red.o.x=(double)x;
red.o.y=(double)y;
red.r=(double)r;
arc.clear();
for (i=0;i<n;i++)
scanf("%lf%lf%lf",&c[i].o.x,&c[i].o.y,&c[i].r);
for (i=0;i<n;i++)
{
get_InCommonTangent(red,c[i],s1,e1,s2,e2);
if (s1==e1)
{
vec=c[i].o-red.o;
e1=s1+vec.rotate(PI/2);
e2=s2+vec.rotate(-PI/2);
}
inter_circle_ray(purple,s1,e1,p1);
inter_circle_ray(purple,s2,e2,p2);
u.s=get_angle(p1);
u.e=get_angle(p2);
if (u.s>u.e)
{
arc.push_back(node(u.s,PI));
arc.push_back(node(-PI,u.e));
}
else
arc.push_back(u);
}
sort(arc.begin(),arc.end(),cmp);
sum=arc[0].e-arc[0].s;
cur=arc[0].e;
for (i=1;i<arc.size();i++)
{
if (dlcmp(arc[i].e-cur)<=0)
continue;
if (dlcmp(arc[i].s-cur)>0)
sum+=arc[i].e-arc[i].s;
else
sum+=arc[i].e-cur;
cur=arc[i].e;
}
ans=(2*PI-sum)/(2*PI);
if (ans<0)
ans=0;
printf("%.4f\n",ans);
}
return 0;
}

185
HDOJ/4265_autoAC.cpp Normal file
View File

@ -0,0 +1,185 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <limits>
using namespace std;
typedef long long LL;
const int MAXN=1e+3;
const int MAXM=1e+5;
const int INF = numeric_limits<int>::max();
const LL LL_INF= numeric_limits<LL>::max();
struct Edge
{
int from,to;
LL ci;int next;
Edge(){}
Edge(int _from,int _to,LL _ci,int _next):from(_from),to(_to),ci(_ci),next(_next){}
}e[MAXM],e1[MAXM];
int head[MAXN],tot;
int dis[MAXN];
int top,sta[MAXN],cur[MAXN],tmp[MAXN];
int vis1[100],vis2[MAXM];
int n;
inline void init(){
memset(head,-1,sizeof(head));
tot=0;
}
inline void AddEdge(int u,int v,LL ci0,LL ci1=0){
e[tot]=Edge(u,v,ci0,head[u]);
head[u]=tot++;
e[tot]=Edge(v,u,ci1,head[v]);
head[v]=tot++;
}
inline bool bfs(int st,int et){
memset(dis,0,sizeof(dis));
dis[st]=1;
queue <int> q;
q.push(st);
while(!q.empty()){
int now=q.front();
q.pop();
for(int i=head[now];i!=-1;i=e[i].next){
int next=e[i].to;
if(vis2[i])continue;
if(e[i].ci&&!dis[next]){
dis[next]=dis[now]+1;
if(next==et)return true;
q.push(next);
}
}
}
return false;
}
LL Dinic(int st,int et){
LL ans=0;
while(bfs(st,et)){
top=0;
memcpy(cur,head,sizeof(head));
int u=st,i;
while(1){
if(u==et){
int pos;
LL minn=LL_INF;
for(i=0;i<top;i++)
{
if(minn>e[sta[i]].ci){
minn=e[sta[i]].ci;
pos=i;
}
}
for(i=0;i<top;i++){
e[sta[i]].ci-=minn;
e[sta[i]^1].ci+=minn;
}
top=pos;
u=e[sta[top]].from;
ans+=minn;
}
for(i=cur[u];i!=-1;cur[u]=i=e[i].next)
if(vis2[i]);
else if(e[i].ci&&dis[u]+1==dis[e[i].to])break;
if(cur[u]!=-1){
sta[top++]=cur[u];
u=e[cur[u]].to;
}
else {
if(top==0)break;
dis[u]=0;
u=e[sta[--top]].from;
}
}
}
return ans;
}
int main()
{
char ch;
while(~scanf("%d",&n)&&n){
init();
memset(vis2,0,sizeof(vis2));
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf(" %c",&ch);
if(ch=='Y'){
AddEdge(i,j+n,1);
}
}
}
memcpy(e1,e,sizeof(e));
memcpy(tmp,head,sizeof(head));
int st=500,et=501;
int ttot=tot;
int l=0,r=100,ans=0,mid;
while(l<=r){
mid=(l+r)>>1;
for(int i=1;i<=n;i++){
AddEdge(st,i,mid);
AddEdge(i+n,et,mid);
}
if(Dinic(st,et)==(LL)n*mid){
ans=mid;
//if(ans<mid)ans=mid;
l=mid+1;
}
else r=mid-1;
memcpy(e,e1,sizeof(e));
memcpy(head,tmp,sizeof(head));
tot=ttot;
}
printf("%d\n",ans);
for(int i=1;i<=n;i++){
AddEdge(st,i,ans);
AddEdge(i+n,et,ans);
}
Dinic(st,et);
for(int i=1;i<=n;i++){
for(int j=head[i];~j;j=e[j].next){
if(e[j].ci&&e[j].to!=st){
vis2[j^1]=vis2[j]=1;
}
}
}
memcpy(e,e1,sizeof(e));
memcpy(head,tmp,sizeof(head));
tot=ttot;
for(int j=1;j<=n;j++){
AddEdge(st,j,1);
AddEdge(j+n,et,1);
}
for(int i=0;i<ans;i++){
memset(vis1,0,sizeof(vis1));
tot=ttot;
Dinic(st,et);
for(int j=1;j<=n;j++){
for(int k=head[j];~k;k=e[k].next){
if(vis2[k])continue;
if(e[k].ci==0&&e[k].to!=st){
vis1[e[k].to-n]=j;
vis2[k^1]=vis2[k]=1;
break;
}
}
}
for(int j=1;j<n;j++){
printf("%d ",vis1[j]);
if(!vis1[j])while(1);
}
printf("%d\n",vis1[n]);
if(!vis1[n])while(1);
for(int j=head[st];~j;j=e[j].next){
e[j].ci=1;e[j^1].ci=0;
}
for(int j=head[et];~j;j=e[j].next){
e[j^1].ci=1;e[j].ci=0;
}
}
}
return 0;
}

179
HDOJ/4266_autoAC.cpp Normal file
View File

@ -0,0 +1,179 @@
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include <iostream>
using namespace std;
#define PR 1e-9
#define N 1100
struct TPoint
{
double x,y,z;
TPoint(){}
TPoint(double _x,double _y,double _z):x(_x),y(_y),z(_z){}
TPoint operator-(const TPoint p) {return TPoint(x-p.x,y-p.y,z-p.z);}
TPoint operator*(const TPoint p) {return TPoint(y*p.z-z*p.y,z*p.x-x*p.z,x*p.y-y*p.x);}
double operator^(const TPoint p) {return x*p.x+y*p.y+z*p.z;}
};
TPoint dd;
struct fac
{
int a,b,c;
bool ok;
};
TPoint xmult(TPoint u,TPoint v)
{
return TPoint(u.y*v.z-v.y*u.z,u.z*v.x-u.x*v.z,u.x*v.y-u.y*v.x);
}
double dmult(TPoint u,TPoint v)
{
return u.x*v.x+u.y*v.y+u.z*v.z;
}
TPoint subt(TPoint u,TPoint v)
{
return TPoint(u.x-v.x,u.y-v.y,u.z-v.z);
}
double vlen(TPoint u)
{
return sqrt(u.x*u.x+u.y*u.y+u.z*u.z);
}
TPoint pvec(TPoint a,TPoint b,TPoint c)
{
return xmult(subt(a,b),subt(b,c));
}
double Dis(TPoint a,TPoint b,TPoint c,TPoint d)
{
return fabs(dmult(pvec(a,b,c),subt(d,a)))/vlen(pvec(a,b,c));
}
struct T3dhull
{
int n;
TPoint ply[N];
int trianglecnt;
fac tri[N];
int vis[N][N];
double dist(TPoint a){return sqrt(a.x*a.x+a.y*a.y+a.z*a.z);}
double area(TPoint a,TPoint b,TPoint c){return dist((b-a)*(c-a));}
double volume(TPoint a,TPoint b,TPoint c,TPoint d){return (b-a)*(c-a)^(d-a);}
double ptoplane(TPoint &p,fac &f)
{
TPoint m=ply[f.b]-ply[f.a],n=ply[f.c]-ply[f.a],t=p-ply[f.a];
return (m*n)^t;
}
void deal(int p,int a,int b)
{
int f=vis[a][b];
fac add;
if(tri[f].ok)
{
if((ptoplane(ply[p],tri[f]))>PR) dfs(p,f);
else
{
add.a=b,add.b=a,add.c=p,add.ok=1;
vis[p][b]=vis[a][p]=vis[b][a]=trianglecnt;
tri[trianglecnt++]=add;
}
}
}
void dfs(int p,int cnt)
{
tri[cnt].ok=0;
deal(p,tri[cnt].b,tri[cnt].a);
deal(p,tri[cnt].c,tri[cnt].b);
deal(p,tri[cnt].a,tri[cnt].c);
}
bool same(int s,int e)
{
TPoint a=ply[tri[s].a],b=ply[tri[s].b],c=ply[tri[s].c];
return fabs(volume(a,b,c,ply[tri[e].a]))<PR
&&fabs(volume(a,b,c,ply[tri[e].b]))<PR
&&fabs(volume(a,b,c,ply[tri[e].c]))<PR;
}
void construct()
{
int i,j;
trianglecnt=0;
if(n<4) return ;
bool tmp=true;
for(i=1;i<n;i++)
{
if((dist(ply[0]-ply[i]))>PR)
{
swap(ply[1],ply[i]); tmp=false; break;
}
}
if(tmp) return;
tmp=true;
for(i=2;i<n;i++)
{
if((dist((ply[0]-ply[1])*(ply[1]-ply[i])))>PR)
{
swap(ply[2],ply[i]); tmp=false; break;
}
}
if(tmp) return ;
tmp=true;
for(i=3;i<n;i++)
{
if(fabs((ply[0]-ply[1])*(ply[1]-ply[2])^(ply[0]-ply[i]))>PR)
{
swap(ply[3],ply[i]); tmp=false; break;
}
}
if(tmp) return ;
fac add;
for(i=0;i<4;i++)
{
add.a=(i+1)%4,add.b=(i+2)%4,add.c=(i+3)%4,add.ok=1;
if((ptoplane(ply[i],add))>0) swap(add.b,add.c);
vis[add.a][add.b]=vis[add.b][add.c]=vis[add.c][add.a]=trianglecnt;
tri[trianglecnt++]=add;
}
for(i=4;i<n;i++)
{
for(j=0;j<trianglecnt;j++)
{
if(tri[j].ok&&(ptoplane(ply[i],tri[j]))>PR)
{
dfs(i,j); break;
}
}
}
int cnt=trianglecnt;
trianglecnt=0;
for(i=0;i<cnt;i++)
{
if(tri[i].ok)
tri[trianglecnt++]=tri[i];
}
}
double res()
{
double _min=1e300;
for(int i=0;i<trianglecnt;i++)
{
double now=Dis(ply[tri[i].a],ply[tri[i].b],ply[tri[i].c],dd);
if(_min>now) _min=now;
}
return _min;
}
}hull;
int main()
{
double now,_min;
while(scanf("%d",&hull.n)!=EOF)
{
if(hull.n==0) break;
int i,j,q;
for(i=0;i<hull.n;i++)
scanf("%lf%lf%lf",&hull.ply[i].x,&hull.ply[i].y,&hull.ply[i].z);
hull.construct();
scanf("%d",&q);
for(j=0;j<q;j++)
{
scanf("%lf%lf%lf",&dd.x,&dd.y,&dd.z);
printf("%.4lf\n",hull.res());
}
}
return 0;
}

52
HDOJ/4267_autoAC.cpp Normal file
View File

@ -0,0 +1,52 @@
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 50010;
int cnt[N][11][11];
int num[N];
int inline lowbit(int x){
return x & (-x);
}
void inline update(int x,int k,int mod,int add){
while(x > 0){
cnt[x][k][mod] += add;
x -= lowbit(x);
}
}
int inline sum(int x,int a){
int s = 0;
while(x < N){
for(int i = 1; i <= 10; ++i){
s += cnt[x][i][a%i];
}
x += lowbit(x);
}
return s;
}
int main(){
int n;
while(scanf("%d",&n) != EOF){
for(int i = 1; i <= n; ++i)
scanf("%d",&num[i]);
memset(cnt,0,sizeof(cnt));
int m;
scanf("%d",&m);
while(m--){
int op,a,b,k,add;
scanf("%d",&op);
if(op == 1){
scanf("%d%d%d%d",&a,&b,&k,&add);
update(b,k,a%k,add);
update(a-1,k,a%k,-add);
}
else{
scanf("%d",&a);
int ans = sum(a,a);
printf("%d\n",ans + num[a]);
}
}
}
return 0;
}

51
HDOJ/4268_autoAC.cpp Normal file
View File

@ -0,0 +1,51 @@
#include<iostream>
#include<set>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
multiset<int> myset;
multiset<int>::iterator it;
const int maxn=200000;
struct node
{
int x,y;
bool operator<(const node& b)const
{
return x<b.x;
}
}a[maxn],b[maxn];
int main()
{
int sec,n;
scanf("%d",&sec);
for(int z=1;z<=sec;z++)
{
myset.clear();
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
for(int i=1;i<=n;i++)
scanf("%d%d",&b[i].x,&b[i].y);
sort(a+1,a+1+n);
sort(b+1,b+1+n);
int j=1;int ans=0;
for(int i=1;i<=n;i++)
{
while(j<=n&&b[j].x<=a[i].x)
{
myset.insert(b[j].y);
j++;
}
it=myset.upper_bound(a[i].y);
if(myset.size()>0&&it!=myset.begin())it--;
if(myset.size()>0&&(*it)<=a[i].y)
{
ans++;
myset.erase(it);
}
}
printf("%d\n",ans);
}
return 0;
}

163
HDOJ/4269_autoAC.cpp Normal file
View File

@ -0,0 +1,163 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <sstream>
#include <map>
#include <cctype>
using namespace std;
struct Item {
int prz, typ, num;
int tn;
int us[65][2];
}a[110];
struct Name {
char name[20];
bool operator < (const Name &b) const {
return strcmp(name, b.name) == -1;
}
}b;
map <Name, int> mmap;
map <Name, int>::iterator it;
int id, fg;
int n;
char s[10000], ch, bf[1000];
int cnt, gold, p;
int main(void) {
int cas = 0;
while (~scanf("%d", &n)) {
mmap.clear();
cnt = gold = 0;
fg = 0;
cas++;
while (n--) {
scanf("%s", b.name);
it = mmap.find(b);
if (it == mmap.end()) {
mmap.insert(pair<Name, int>(b, fg++));
id = fg - 1;
} else
id = it->second;
scanf("%d", &a[id].prz);
a[id].typ = 0;
a[id].num = a[id].tn = 0;
}
scanf("%d", &n);
while (n--) {
scanf("%s", b.name);
it = mmap.find(b);
if (it == mmap.end()) {
mmap.insert(pair<Name, int>(b, fg++));
id = fg - 1;
} else
id = it->second;
scanf("%d", &a[id].prz);
a[id].typ = 1;
a[id].num = a[id].tn = 0;
getchar();
cin.getline(s, 10000);
stringstream ss(s);
while (ss >> b.name) {
it = mmap.find(b);
if (it == mmap.end()) {
mmap.insert(pair<Name, int>(b, fg++));
it = mmap.find(b);
}
a[id].us[a[id].tn][0] = it->second;
ss >> a[id].us[a[id].tn][1] >> bf;
a[id].tn++;
}
}
scanf("%d", &n);
while (n--) {
scanf("%s", b.name);
it = mmap.find(b);
if (it == mmap.end()) {
mmap.insert(pair<Name, int>(b, fg++));
id = fg - 1;
} else
id = it->second;
scanf("%d", &a[id].prz);
a[id].typ = 2;
a[id].num = a[id].tn = 0;
}
scanf("%d", &n);
while (n--) {
//cout << "Gold = " << gold << endl;
scanf("%s", s);
if (isdigit(s[1])) {
p = 0;
for (int i = 1; s[i] != 0; i++)
p = p*10 + s[i] - 48;
if (s[0] == '+')
gold += p;
else if (gold >= p)
gold -= p;
} else {
sscanf(s+1, "%s", b.name);
it = mmap.find(b);
if (it == mmap.end())
continue;
id = it->second;
if (s[0] == '+') {
if (a[id].typ == 0 && cnt < 6 && gold >= a[id].prz) {
a[id].num++;
cnt++;
gold -= a[id].prz;
} else if (a[id].typ == 2 && cnt < 6 && gold >= a[id].prz) {
if (a[id].num == 0) cnt++;
a[id].num++;
gold -= a[id].prz;
} else if (a[id].typ == 1 && gold >= a[id].prz) {
bool flag = 1;
int grid = 1;
for (int i = 0; i < a[id].tn && flag; i++) {
if (a[a[id].us[i][0]].num < a[id].us[i][1])
flag = 0;
else
grid -= a[id].us[i][1];
}
if (flag && cnt + grid <= 6) {
gold -= a[id].prz;
cnt += grid;
a[id].num++;
for (int i = 0; i < a[id].tn; i++)
a[a[id].us[i][0]].num -= a[id].us[i][1];
}
}
} else if (a[id].num != 0) {
if (a[id].typ == 0) {
cnt--;
a[id].num--;
gold += a[id].prz;
} else if (a[id].typ == 1) {
cnt--;
a[id].num--;
int cost = a[id].prz;
for (int i = 0; i < a[id].tn; i++)
cost += a[id].us[i][1] * a[a[id].us[i][0]].prz;
gold += cost;
} else {
cnt--;
gold += a[id].prz * a[id].num;
a[id].num = 0;
}
}
}
}
printf("Case %d:\n%d\n%d\n", cas, gold, cnt);
for (it = mmap.begin(); it != mmap.end(); it++) {
for (int i = 0; i < a[it->second].num; i++) {
if (a[it->second].typ != 2)
printf("%s: 1\n", (it->first).name);
else {
printf("%s: %d\n", (it->first).name, a[it->second].num);
break;
}
}
}
printf("\n");
}
return 0;
}

97
HDOJ/4270_autoAC.cpp Normal file
View File

@ -0,0 +1,97 @@
#include <cstdio>
#include <cstring>
const int MAXN = 200005;
struct SAMNode {
SAMNode *f, *ch[26];
int len;
bool *d;
int pos;
int v;
};
SAMNode *root, *last;
int p_cnt, d_cnt, seq_len, v_id;
SAMNode pool[MAXN*2];
bool is_d[MAXN*2];
char str[MAXN];
SAMNode *seq[MAXN];
bool is_del(SAMNode *x) {
return x == NULL || *x->d;
}
void append(char ch) {
int c = ch - 'a';
SAMNode *p = last, *np = pool + p_cnt++;
memset(np, 0, sizeof(*np));
np->pos = np->len = p->len + 1;
np->d = is_d + d_cnt++;
*np->d = false;
seq[seq_len = np->len] = np;
last = np;
for (; NULL != p && is_del(p->ch[c]); p = p->f) p->ch[c] = np;
if (NULL == p) {
np->f = root;
} else {
if (p->ch[c]->len == p->len + 1) {
np->f = p->ch[c];
} else {
SAMNode *q = p->ch[c], *nq = pool + p_cnt++;
*nq = *q;
nq->len = p->len + 1;
q->f = np->f = nq;
for (; NULL != p && p->ch[c] == q; p = p->f) p->ch[c] = nq;
}
}
}
void del(int len) {
while (len--) *seq[seq_len--]->d = true;
last = seq[seq_len];
}
int dfs_len;
int dfs(SAMNode *x, int l) {
if (l == dfs_len) return x->pos - l + 1;
if (x->v == v_id) return seq_len - l + 1;
for (int i = 0; i < 26; ++i) {
if (!is_del(x->ch[i])) {
return dfs(x->ch[i], l+1);
}
}
return x->len - l + 1;
}
int query(int len) {
++v_id;
for (SAMNode *p = last; p != root; p = p->f) p->v = v_id;
dfs_len = len;
int ret = dfs(root, 0);
return ret;
}
int main() {
while (EOF != scanf("%s", str)) {
root = last = pool;
memset(root, 0, sizeof(*root));
root->d = is_d;
is_d[0] = false;
p_cnt = 1;
d_cnt = 1;
v_id = 0;
seq[0] = root;
for (char *c = str; *c; ++c) append(*c);
int m, q, len;
scanf("%d", &m);
while (m--) {
scanf("%d", &q);
if (1 == q) {
scanf("%s", str);
for (char *c = str; *c; ++c) append(*c);
} else if (2 == q) {
scanf("%d", &len);
printf("%d\n", query(len));
} else if (3 == q) {
scanf("%d", &len);
del(len);
} else {
for(;;);
fprintf(stderr, "error cmd %d\n", q);
}
}
}
return 0;
}

88
HDOJ/4271_autoAC.cpp Normal file
View File

@ -0,0 +1,88 @@
#include <iostream>
#include <cstdio>
#include <string.h>
#define MIN(a , b) ((a) < (b) ? (a) : (b))
using namespace std;
const int inf = 1 << 29;
const int maxn = 100002;
const int maxm = 12;
char str[maxn],dtr[maxn + maxm],dic[maxm][maxm],huan[maxm];
int dp[maxn + maxm][maxm];
int m,len,llen;
int CompareString(char *s1,char *s2,int l1,int l2)
{
memset(dp,0,sizeof(dp));
for(int i=0;i<=l2;i++)
{
dp[0][i] = i;
}
for(int i=1;i<=l1;i++)
{
for(int j=1;j<=l2;j++)
{
int cost = (s1[i-1] == s2[j-1]) ? 0 : 1;
dp[i][j] = MIN(dp[i-1][j-1] + cost , MIN(dp[i-1][j] + 1 , dp[i][j-1] + 1));
}
}
int res = inf;
for(int i=0;i<=l1;i++)
{
res = MIN(res , dp[i][l2]);
}
return res;
}
void solve()
{
len = strlen(str);
strcpy(dtr , str);
for(int i=0;i<maxm;i++)
{
dtr[i+len] = str[i];
}
llen = len + maxm;
dtr[llen] = '\0';
scanf("%d",&m);
getchar();
int pos = -1,ans = inf;
for(int i=0;i<m;i++)
{
scanf("%s",dic[i]);
int l = strlen(dic[i]);
if((l << 1) < len)
{
int tmp = CompareString(dtr , dic[i] , llen , l);
if(tmp < ans || (tmp == ans && strcmp(dic[pos] , dic[i]) > 0))
{
ans = tmp;
pos = i;
}
}
else
{
for(int s=0;s<len;s++)
{
for(int j=0;j<len;j++)
{
huan[j] = str[(s+j) % len];
}
huan[len] = '\0';
int tmp = CompareString(huan , dic[i] , len , l);
if(tmp < ans || (tmp == ans && strcmp(dic[pos] , dic[i]) > 0))
{
ans = tmp;
pos = i;
}
}
}
}
printf("%s %d\n",dic[pos],ans);
return;
}
int main()
{
while(~scanf("%s",str))
{
solve();
}
return 0;
}

95
HDOJ/4272_autoAC.cpp Normal file
View File

@ -0,0 +1,95 @@
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <memory.h>
#define MIN(a , b) ((a) < (b) ? (a) : (b))
#define MAX(a , b) ((a) > (b) ? (a) : (b))
using namespace std;
const int maxn = 1300;
int x[maxn],stack[maxn],cnt[maxn];
bool dp[maxn][maxn],vis[maxn][maxn];
int n;
void read()
{
memset(cnt,0,sizeof(cnt));
memset(vis,false,sizeof(vis));
for(int i=0;i<n;i++)
{
scanf("%d",&stack[i]);
x[i] = stack[i];
}
return;
}
bool check()
{
if(n & 1) return false;
sort(x , x + n);
int m = x - unique(x , x + n);
for(int i=0;i<n;i++)
{
int idx = lower_bound(x , x + m , stack[i]) - x;
cnt[idx]++;
}
for(int i=0;i<m;i++)
{
if(cnt[i] & 1) return false;
}
return true;
}
bool dfs(int dep , int to , int st)
{
if(vis[dep][st]) return dp[dep][st];
bool flag = false;
int use = 0;
for(int i=dep-1;i>=0 && use < 5;i--)
{
if(st & (1 << (dep - i)))
{
use++;
if(stack[dep] == stack[i])
{
int cur = st;
cur ^= 1;
cur ^= (1 << (dep - i));
if(cur == 0)
{
flag = true;
break;
}
int pos = dep;
while((cur & 1) == 0)
{
cur >>= 1;
pos--;
}
int end = to-1;
while(end >= 0 && pos - end < 10)
{
cur |= (1 << (pos - end));
end--;
}
if(dfs(pos , end+1 , cur))
{
flag = true;
break;
}
}
}
}
vis[dep][st] = true;
return dp[dep][st] = flag;
}
int main()
{
while(~scanf("%d",&n))
{
read();
if(check() == false)
{
puts("0");
continue;
}
puts(dfs(n-1 , MAX(n-10 , 0) , (1 << MIN(10 , n)) - 1) ? "1" : "0");
}
return 0;
}

255
HDOJ/4273_autoAC.cpp Normal file
View File

@ -0,0 +1,255 @@
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
const int MAXN=550;
const double eps=1e-8;
struct Point
{
double x,y,z;
Point(){}
Point(double xx,double yy,double zz):x(xx),y(yy),z(zz){}
Point operator -(const Point p1)
{
return Point(x-p1.x,y-p1.y,z-p1.z);
}
Point operator +(const Point p1)
{
return Point(x+p1.x,y+p1.y,z+p1.z);
}
Point operator *(const Point p)
{
return Point(y*p.z-z*p.y,z*p.x-x*p.z,x*p.y-y*p.x);
}
Point operator *(double d)
{
return Point(x*d,y*d,z*d);
}
Point operator / (double d)
{
return Point(x/d,y/d,z/d);
}
double operator ^(Point p)
{
return (x*p.x+y*p.y+z*p.z);
}
};
struct CH3D
{
struct face
{
int a,b,c;
bool ok;
};
int n;
Point P[MAXN];
int num;
face F[8*MAXN];
int g[MAXN][MAXN];
double vlen(Point a)
{
return sqrt(a.x*a.x+a.y*a.y+a.z*a.z);
}
Point cross(const Point &a,const Point &b,const Point &c)
{
return Point((b.y-a.y)*(c.z-a.z)-(b.z-a.z)*(c.y-a.y),
(b.z-a.z)*(c.x-a.x)-(b.x-a.x)*(c.z-a.z),
(b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x)
);
}
double area(Point a,Point b,Point c)
{
return vlen((b-a)*(c-a));
}
double volume(Point a,Point b,Point c,Point d)
{
return (b-a)*(c-a)^(d-a);
}
double dblcmp(Point &p,face &f)
{
Point m=P[f.b]-P[f.a];
Point n=P[f.c]-P[f.a];
Point t=p-P[f.a];
return (m*n)^t;
}
void deal(int p,int a,int b)
{
int f=g[a][b];
face add;
if(F[f].ok)
{
if(dblcmp(P[p],F[f])>eps)
dfs(p,f);
else
{
add.a=b;
add.b=a;
add.c=p;
add.ok=true;
g[p][b]=g[a][p]=g[b][a]=num;
F[num++]=add;
}
}
}
void dfs(int p,int now)
{
F[now].ok=0;
deal(p,F[now].b,F[now].a);
deal(p,F[now].c,F[now].b);
deal(p,F[now].a,F[now].c);
}
bool same(int s,int t)
{
Point &a=P[F[s].a];
Point &b=P[F[s].b];
Point &c=P[F[s].c];
return fabs(volume(a,b,c,P[F[t].a]))<eps &&
fabs(volume(a,b,c,P[F[t].b]))<eps &&
fabs(volume(a,b,c,P[F[t].c]))<eps;
}
void create()
{
int i,j,tmp;
face add;
num=0;
if(n<4)return;
bool flag=true;
for(i=1;i<n;i++)
{
if(vlen(P[0]-P[i])>eps)
{
swap(P[1],P[i]);
flag=false;
break;
}
}
if(flag)return;
flag=true;
for(i=2;i<n;i++)
{
if(vlen((P[0]-P[1])*(P[1]-P[i]))>eps)
{
swap(P[2],P[i]);
flag=false;
break;
}
}
if(flag)return;
flag=true;
for(int i=3;i<n;i++)
{
if(fabs((P[0]-P[1])*(P[1]-P[2])^(P[0]-P[i]))>eps)
{
swap(P[3],P[i]);
flag=false;
break;
}
}
if(flag)return;
for(i=0;i<4;i++)
{
add.a=(i+1)%4;
add.b=(i+2)%4;
add.c=(i+3)%4;
add.ok=true;
if(dblcmp(P[i],add)>0)swap(add.b,add.c);
g[add.a][add.b]=g[add.b][add.c]=g[add.c][add.a]=num;
F[num++]=add;
}
for(i=4;i<n;i++)
{
for(j=0;j<num;j++)
{
if(F[j].ok&&dblcmp(P[i],F[j])>eps)
{
dfs(i,j);
break;
}
}
}
tmp=num;
for(i=num=0;i<tmp;i++)
if(F[i].ok)
F[num++]=F[i];
}
double area()
{
double res=0;
if(n==3)
{
Point p=cross(P[0],P[1],P[2]);
res=vlen(p)/2.0;
return res;
}
for(int i=0;i<num;i++)
res+=area(P[F[i].a],P[F[i].b],P[F[i].c]);
return res/2.0;
}
double volume()
{
double res=0;
Point tmp(0,0,0);
for(int i=0;i<num;i++)
res+=volume(tmp,P[F[i].a],P[F[i].b],P[F[i].c]);
return fabs(res/6.0);
}
int triangle()
{
return num;
}
int polygon()
{
int i,j,res,flag;
for(i=res=0;i<num;i++)
{
flag=1;
for(j=0;j<i;j++)
if(same(i,j))
{
flag=0;
break;
}
res+=flag;
}
return res;
}
Point barycenter()
{
Point ans(0,0,0),o(0,0,0);
double all=0;
for(int i=0;i<num;i++)
{
double vol=volume(o,P[F[i].a],P[F[i].b],P[F[i].c]);
ans=ans+(o+P[F[i].a]+P[F[i].b]+P[F[i].c])/4.0*vol;
all+=vol;
}
ans=ans/all;
return ans;
}
double ptoface(Point p,int i)
{
return fabs(volume(P[F[i].a],P[F[i].b],P[F[i].c],p)/vlen((P[F[i].b]-P[F[i].a])*(P[F[i].c]-P[F[i].a])));
}
};
CH3D hull;
int main()
{
while(scanf("%d",&hull.n)==1)
{
for(int i=0;i<hull.n;i++)
{
scanf("%lf%lf%lf",&hull.P[i].x,&hull.P[i].y,&hull.P[i].z);
}
hull.create();
Point p=hull.barycenter();
double minn=1e20;
for(int i=0;i<hull.num;i++)
{
minn=min(minn,hull.ptoface(p,i));
}
printf("%.3lf\n",minn);
}
return 0;
}

74
HDOJ/4274_autoAC.cpp Normal file
View File

@ -0,0 +1,74 @@
#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<set>
#define inf 110000000
#define M 10005
#define N 10005
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
#define pb(a) push_back(a)
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define MOD 1000000007
using namespace std;
struct Node{
int v,next;
}edge[N*2];
int n,start[N],tot;
int up[N],low[N];
void addedge(int u,int v){
edge[tot].v=v;
edge[tot].next=start[u];
start[u]=tot++;
}
void _addedge(int u,int v){
addedge(u,v);
addedge(v,u);
}
bool ans;
void dfs(int u,int pre){
if(ans) return;
if(up[u]!=-1&&low[u]>up[u]){ans=true;return;}
int tmp=0;
int leaf=1;
for(int i=start[u];i!=-1;i=edge[i].next){
int v=edge[i].v;
if(v==pre) continue;
dfs(v,u);
leaf=0;
tmp+=low[v];
}
if(leaf) return;
low[u]=max(tmp+1,low[u]);
if(up[u]!=-1&&low[u]>up[u]) ans=true;
}
int main(){
while(scanf("%d",&n)!=EOF){
tot=0;mem(start,-1);
for(int i=2;i<=n;i++){
int u;
scanf("%d",&u);
_addedge(u,i);
}
int m;
for(int i=1;i<=n;i++){low[i]=1;up[i]=-1;}
scanf("%d",&m);
for(int i=0;i<m;i++){
int u,w;
char str[5];
scanf("%d%s%d",&u,str,&w);
if(str[0]=='<') up[u]=w-1;
else if(str[0]=='>') low[u]=w+1;
else low[u]=up[u]=w;
}
ans=false;
dfs(1,0);
puts(ans?"Lie":"True");
}
return 0;
}

187
HDOJ/4275_autoAC.cpp Normal file
View File

@ -0,0 +1,187 @@
#pragma comment(linker, "/STACK:16777216")
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;
#define hash hhhhhhh
#define end eeeeend
#define maxn 50010
const lint mod = 1e9 + 7;
const lint A = 233, P = 23333, Q = 1e9 + 1027, B = 50009;
int n, m;
int dis[maxn];
bool flag;
vector<int> dia;
int start, end;
lint hash[maxn], cnt[maxn];
int idx[maxn];
lint fac[maxn];
vector<int> G[maxn];
int center;
void dfs(int now, int fa)
{
int nex;
for(int i = 0, sz = G[now].size(); i < sz; i++) if((nex = G[now][i]) != fa)
{
dis[nex] = dis[now] + 1;
dfs(nex, now);
}
return;
}
bool getDiameter(int now, int fa)
{
int nex;
for(int i = 0, sz = G[now].size(); i < sz; i++) if((nex = G[now][i]) != fa)
if(getDiameter(nex, now))
{
dia.push_back(now);
return true;
}
if(now == end)
{
dia.push_back(now);
return true;
}
return false;
}
void diameter()
{
dis[1] = 0;
dfs(1, -1);
start = 1;
for(int i = 1; i <= n; i++)
if(dis[start] < dis[i]) start = i;
dis[start] = 0;
dfs(start, -1);
end = start;
for(int i = 1; i <= n; i++)
if(dis[end] < dis[i]) end = i;
dia.clear();
getDiameter(start, -1);
if(dis[end] & 1)
{
flag = true;
int centerLeft = dia[(dia.size() >> 1) - 1];
int centerRight = dia[dia.size() >> 1];
center = n + 1;
G[center].push_back(centerLeft);
G[center].push_back(centerRight);
for(int i = 0, sz = G[centerLeft].size(); i < sz; i++)
if(G[centerLeft][i] == centerRight)
{
G[centerLeft][i] = center;
break;
}
for(int i = 0, sz = G[centerRight].size(); i < sz; i++)
if(G[centerRight][i] == centerLeft)
{
G[centerRight][i] = center;
break;
}
}
else
center = dia[dia.size() >> 1];
return;
}
lint quick(lint base, lint pow)
{
lint ret = 1;
while(pow)
{
if(pow & 1) ret = ret*base % mod;
base = base*base % mod;
pow >>= 1;
}
return ret;
}
lint C(int x, int y)
{
lint ret = 1;
for(int i = 1; i + x - y <= x; i++)
ret = ret*(x - y + i) % mod;
ret = ret*quick(fac[y], mod - 2) % mod;
return ret;
}
bool cmp(int x, int y)
{
return hash[x] < hash[y];
}
void calc(int now, int fa)
{
int nex;
for(int i = 0, sz = G[now].size(); i < sz; i++) if((nex = G[now][i]) != fa)
calc(nex, now);
if(G[now].size() == 1)
{
hash[now] = A*B % Q;//hash间负A*B % Q
cnt[now] = m;
return;
}
hash[now] = A*B % Q;
cnt[now] = m;
int num = 0;
for(int i = 0, sz = G[now].size(); i < sz; i++) if((nex = G[now][i]) != fa)
idx[num++] = nex;
sort(idx, idx + num, cmp);
for(int i = 0; i < num; i++)
{
nex = idx[i];
hash[now] *= P;
hash[now] ^= hash[nex];
hash[now] %= Q;
}
hash[now] = hash[now] * B % Q;
int st = 0;
while(st < num)
{
lint val = hash[idx[st]];
lint kinds = cnt[idx[st]];
lint count = 1;
st++;
while(st < num && val == hash[idx[st]])
count++, st++;
cnt[now] = cnt[now] * C(kinds + count - 1, count) % mod;
}
return;
}
int main()
{
fac[0] = 1;
for(int i = 1; i < maxn; i++) fac[i] = fac[i - 1] * i % mod;
while(~scanf("%d %d", &n, &m))
{
for(int i = 1; i <= n + 1; i++) G[i].clear();
for(int i = 1; i < n; i++)
{
int u, v;
scanf("%d %d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
flag = false;
diameter();
calc(center, -1);
if(flag)
printf("%I64d\n", cnt[center] * quick(m, mod - 2) % mod);
else printf("%I64d\n", cnt[center]);
}
return 0;
}

99
HDOJ/4276_autoAC.cpp Normal file
View File

@ -0,0 +1,99 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N=105;
int head[N],tot;
int dp[N][N*5];
struct Edge
{
int from,to,cost,pre;
Edge(){}
Edge(int a,int b,int c,int d){from=a;to=b;cost=c;pre=d;}
}edge[N*3];
void addEdge(int u,int v,int cost)
{
edge[tot]=Edge(u,v,cost,head[u]);
head[u]=tot++;
}
int spfa(int st,int ed)
{
queue<int> que;
int vis[N],dis[N],pre[N];
memset(vis,0,sizeof(vis));
memset(dis,0x7f,sizeof(dis));
memset(pre,-1,sizeof(pre));
que.push(st);
vis[st]=1; dis[st]=0;
while(!que.empty())
{
int u=que.front();
que.pop(); vis[u]=0;
for(int i=head[u];i!=-1;i=edge[i].pre)
{
int v=edge[i].to,cost=edge[i].cost;
if(dis[v]>dis[u]+cost)
{
dis[v]=dis[u]+cost;
pre[v]=i;
if(!vis[v])
{
vis[v]=1;
que.push(v);
}
}
}
}
for(int i=ed;i!=st;i=edge[pre[i]].from)
{
edge[pre[i]].cost=0;
edge[pre[i]^1].cost=0;
}
return dis[ed];
}
void dfs(int u,int fa,int coin)
{
for(int e=head[u];e!=-1;e=edge[e].pre)
{
int v=edge[e].to,cost=edge[e].cost*2;
if(v==fa||coin<cost) continue;
dfs(v,u,coin-cost);
for(int i=coin;i>=cost;i--)
{
for(int j=0;j+cost<=i;j++)
{
if(dp[u][i-j-cost]!=-1&&dp[v][j]!=-1)
dp[u][i]=max(dp[u][i],dp[u][i-j-cost]+dp[v][j]);
}
}
}
}
int main()
{
int n,t;
while(scanf("%d%d",&n,&t)!=EOF)
{
tot=0;
memset(dp,-1,sizeof(dp));
memset(head,-1,sizeof(head));
for(int i=0;i<n-1;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
addEdge(a,b,c);
addEdge(b,a,c);
}
for(int i=1;i<=n;i++) scanf("%d",&dp[i][0]);
t-=spfa(1,n);
if(t<0) puts("Human beings die in pursuit of wealth, and birds die in pursuit of food!");
else
{
dfs(1,0,t);
int res=0;
for(int i=0;i<=t;i++) res=max(res,dp[1][i]);
printf("%d\n",res);
}
}
return 0;
}

71
HDOJ/4277_autoAC.cpp Normal file
View File

@ -0,0 +1,71 @@
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<vector>
#include<string>
#define LL long long
#define N 25
using namespace std;
int num[N];
int n,summ,xj,xk;
int Case;
struct node
{
int x,y,z;
bool operator < (const struct node &a) const
{
return (x!=a.x)?(x<a.x):(y!=a.y?(y<a.y):(z<a.z));
}
}temp,ans;
set<struct node> hashx;
void dfs(int);
int main()
{
scanf("%d",&Case);
for(int c=0;c<Case;c++)
{
summ=0;hashx.clear();
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
summ+=num[i];
}
temp.x=num[1];temp.y=0;
if(n<3)
{
printf("%d\n",0);
continue;
}
dfs(2);
printf("%d\n",hashx.size());
}
return 0;
}
void dfs(int x)
{
if(x>n)
{
xj=temp.x;
xk=temp.y;
if(((xj+xk)>(summ-xj-xk))&&(summ-xk>xk)&&(summ-xj>xj))
{
ans.x=max(xj,max(summ-xj-xk,xk));
ans.z=min(xj,min(summ-xj-xk,xk));
if(xj!=ans.x&&xj!=ans.z) ans.y=xj;
else if(xk!=ans.x&&xk!=ans.z) ans.y=xk;
else ans.y=summ-ans.x-ans.z;
hashx.insert(ans);
}
return;
}
dfs(x+1);
temp.x+=num[x];
dfs(x+1);
temp.x-=num[x];
temp.y+=num[x];
dfs(x+1);
temp.y-=num[x];
}

28
HDOJ/4278_autoAC.cpp Normal file
View File

@ -0,0 +1,28 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
int n;
char num[15];
while(scanf("%s",num))
{
if(num[0]=='0')
break;
int l=strlen(num);
int i;
int sum=0;
for(i=0;i<l;i++)
{
if(num[i]<'3')
sum+=(int)(num[i]-'0')*pow(8.0,l*1.0-i-1);
else if(num[i]<'8')
sum+=(int)(num[i]-'0'-1)*pow(8.0,l*1.0-i-1);
else
sum+=(int)(num[i]-'0'-2)*pow(8.0,l*1.0-i-1);
}
printf("%s: %d\n",num,sum);
}
}

50
HDOJ/4279_autoAC.cpp Normal file
View File

@ -0,0 +1,50 @@
#include"iostream"
#include"cstdio"
#include"cstring"
using namespace std;
const unsigned __int64 limit=3037000498;
unsigned __int64 solve(unsigned __int64 x)
{
unsigned __int64 ans,low,mid,up,temp;
low=1;up=limit;
while(low<=up)
{
mid=(low+up)>>1;
temp=mid*mid+4*mid;
if(temp<=x) low=mid+1;
else up=mid-1;
}
low--;
ans=low*(low+1)/2 + (low/2 + low%2)*3;
temp=x-(low*low+4*low);
if(!temp) return ans;
if(temp<=(low+1)*2)
{
if((low+1)%2) ans+=temp/2 + temp%2;
else ans+=(temp-1)/2 + (temp-1)%2;
}
else
{
ans+=low+1;
if((low+1)%2) ans+=temp-(low+1)*2;
}
return ans;
}
int main()
{
int T;
unsigned __int64 a,b,ans[2];
cin>>T;
while(T--)
{
scanf("%I64u%I64u",&a,&b);
if(b<=5) {printf("0\n");continue;}
if(a<=6)a=0;
else a-=6;
b-=5;
ans[0]=solve(a);
ans[1]=solve(b);
printf("%I64u\n",ans[1]-ans[0]);
}
return 0;
}

105
HDOJ/4280_autoAC.cpp Normal file
View File

@ -0,0 +1,105 @@
#include"stdio.h"
#include"string.h"
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define inf 99999999
int min(int a,int b)
{
return a<b?a:b;
}
int e,head[100005];
int level[100005];
int q[100005];
struct node
{
int to,val;
int next;
}edge[200005];
void add_edge(int x,int y,int w)
{
edge[e].to=y;
edge[e].val=w;
edge[e].next=head[x];
head[x]=e++;
edge[e].to=x;
edge[e].val=w;
edge[e].next=head[y];
head[y]=e++;
}
bool bfs(int s,int t)
{
int front=0,rear=0;
int top,k;
q[rear++]=s;
memset(level,0,sizeof(level));
level[s]=1;
while(front<rear)
{
top=q[front++];
if(top==t)
return true;
for(k=head[top];k!=-1;k=edge[k].next)
{
if(!level[edge[k].to]&&edge[k].val>0)
{
level[edge[k].to]=level[top]+1;
q[rear++]=edge[k].to;
}
}
}
return false;
}
int dfs(int now,int maxf,int t)
{
int ret=0,f,k;
if(now==t)
return maxf;
for(k=head[now];k!=-1;k=edge[k].next)
{
if(level[edge[k].to]==level[now]+1&&edge[k].val>0)
{
f=dfs(edge[k].to,min(maxf-ret,edge[k].val),t);
edge[k].val-=f;
edge[k^1].val+=f;
ret+=f;
if(ret==maxf)
return ret;
}
}
if(ret==0)
level[now]=0;
return ret;
}
int dinic(int s,int t)
{
int ans=0;
while(bfs(s,t))
ans+=dfs(s,inf,t);
return ans;
}
int main( )
{
int T,i,n,m,s,t;
int x,y,w,ps,pt;
scanf("%d",&T);
while(T--)
{
e=0;s=inf;t=0;
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
if(s>x)
{s=x;ps=i;}
if(t<x)
{t=x;pt=i;}
}
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&w);
add_edge(x,y,w);
}
printf("%d\n",dinic(ps,pt));
}
return 0;
}

86
HDOJ/4281_autoAC.cpp Normal file
View File

@ -0,0 +1,86 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define inf 1<<30
struct Point{
int x,y;
}point[17];
int n,m,ans,val[17],dp1[1<<17];
int dp[1<<17][17],best[1<<17];
int Ok[1<<17];
int dist[17][17];
int Get_Dist(int i,int j)
{
return ceil(sqrt(double(point[i].x-point[j].x)*(point[i].x-point[j].x)+double(point[i].y-point[j].y)*(point[i].y-point[j].y)));
}
int Judge(int state)
{
int sum=0;
for(int i=0;i<n;i++){
if(state&(1<<i))sum+=val[i];
}
return sum<=m;
}
int Solve()
{
fill(dp1,dp1+(1<<n),inf);
dp1[0]=0;
for(int state=0;state<(1<<n);state++){
if(Ok[state]){
for(int i=0;i<(1<<n);i++){
if((state&i)==0&&dp1[i]!=inf){
dp1[state|i]=min(dp1[state|i],dp1[i]+1);
}
}
}
}
return dp1[(1<<n)-1];
}
int TSP()
{
fill(best,best+(1<<n),inf);
for(int i=0;i<(1<<n);i++)
for(int j=0;j<n;j++)dp[i][j]=inf;
dp[1][0]=0;
for(int state=0;state<(1<<n);state++){
if(Ok[state]){
for(int i=0;i<n;i++)if(state&(1<<i)){
if(dp[state][i]==inf)continue;
best[state]=min(best[state],dp[state][i]+dist[i][0]);
for(int j=0;j<n;j++)if(!(state&(1<<j))){
dp[state|(1<<j)][j]=min(dp[state|(1<<j)][j],dp[state][i]+dist[i][j]);
}
}
}
}
for(int state=0;state<(1<<n);state++){
if(state&1){
for(int substate=state&(state-1);substate;substate=state&(substate-1)){
best[state]=min(best[state],best[substate]+best[(state^substate)|1]);
}
}
}
return best[(1<<n)-1];
}
int main()
{
while(~scanf("%d%d",&n,&m)){
for(int i=0;i<n;i++)scanf("%d%d",&point[i].x,&point[i].y);
for(int i=0;i<n;i++)scanf("%d",&val[i]);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)dist[i][j]=Get_Dist(i,j);
for(int s=0;s<(1<<n);s++){
Ok[s]=Judge(s);
}
ans=Solve();
if(ans==inf){
puts("-1 -1");
continue;
}
printf("%d %d\n",ans,TSP());
}
return 0;
}

65
HDOJ/4282_autoAC.cpp Normal file
View File

@ -0,0 +1,65 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
using namespace std;
long long quickpow(long long a,long long b)
{
long long n=1;
while(b>0)
{
if(b&1)
{
n*=a;
}
b>>=1;
a*=a;
}
return n;
}
int main()
{
long long n;
while(scanf("%I64d",&n),n)
{
long long sign=0;
long long num=0;
for(int i=1;i<=sqrt(n);i++)
{
if(i*i==n)
{
num=i;
break;
}
}
if(num)
{
if(num%2==0)
sign+=(num/2-1);
else
sign+=(num/2);
}
for(long long k=3;k<=31;k++)
{
for(long long i=1;i<=1500;i++)
{
for(long long j=i+1;j<=1500;j++)
{
long long powsum=quickpow(j,k)+quickpow(i,k)+j*i*k;
if(powsum==n)
sign++;
if(powsum>n||powsum<0)
break;
}
if(quickpow(i,k)>n)
break;
}
}
printf("%I64d\n",sign);
}
}

38
HDOJ/4283_autoAC.cpp Normal file
View File

@ -0,0 +1,38 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int inf = 10000000;
int n,a[105],sum[105],dp[105][105];
int main()
{
int t,i,j,k,l,cas = 1;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(sum,0,sizeof(sum));
for(i = 1; i<=n; i++)
{
scanf("%d",&a[i]);
sum[i] = sum[i-1]+a[i];
}
memset(dp,0,sizeof(dp));
for(i = 1; i<=n; i++)
{
for(j = i+1; j<=n; j++)
dp[i][j] = inf;
}
for(l = 1; l<n; l++)
{
for(i = 1; i<=n-l; i++)
{
j = i+l;
for(k = 1; k<=j-i+1; k++)
dp[i][j] = min(dp[i][j],dp[i+1][i+k-1]+dp[i+k][j]+k*(sum[j]-sum[i+k-1])+a[i]*(k-1));
}
}
printf("Case #%d: %d\n",cas++,dp[1][n]);
}
return 0;
}

78
HDOJ/4284_autoAC.cpp Normal file
View File

@ -0,0 +1,78 @@
#include<stdio.h>
#include<string.h>
#define max(a,b)(a)>(b)?(a):(b)
#define INF 0x3f3f3f3f
int g[155][155];
int dp[1<<16][16];
int num[22];
int earn[22];
int cost[22];
int main()
{
int t,i,j,k,mon,n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&mon);
memset(g,INF,sizeof(g));
for(i=1;i<=n;i++)
g[i][i]=0;
int a,b,c;
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
if(c<g[a][b])
g[a][b]=g[b][a]=c;
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
if(g[i][k]!=INF)
for(j=1;j<=n;j++)
if(g[k][j]!=INF&&g[i][k]+g[k][j]<g[i][j])
g[i][j]=g[i][k]+g[k][j];
memset(dp,-1,sizeof(dp));
int h,tmp;
scanf("%d",&h);
for(i=0;i<h;i++)
scanf("%d%d%d",&num[i],&earn[i],&cost[i]);
for(i=0;i<h;i++)
{
tmp=mon-g[1][num[i]]-cost[i];
if(tmp>=0)
dp[1<<i][i]=tmp+earn[i];
}
int st=(1<<h)-1;
for(i=1;i<=st;i++)
for(j=0;j<h;j++)
{
if(dp[i][j]<0)
continue;
for(k=0;k<h;k++)
{
if(i&(1<<k))
continue;
tmp=dp[i][j]-g[num[j]][num[k]]-cost[k];
if(tmp>=0)
{
tmp+=earn[k];
int stat=i+(1<<k);
dp[stat][k]=max(dp[stat][k],tmp);
}
}
}
int flag=0;
for(i=0;i<h;i++)
{
tmp=dp[st][i]-g[num[i]][1];
if(tmp>=0)
{
flag=1;
break;
}
}
if(flag)
printf("YES\n");
else printf("NO\n");
}
return 0;
}

190
HDOJ/4285_autoAC.cpp Normal file
View File

@ -0,0 +1,190 @@
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int MAXD=15;
const int STATE=1000010;
const int HASH=300007;
const int MOD=1000000007;
int N,M,K;
int maze[MAXD][MAXD];
int code[MAXD];
int ch[MAXD];
int num;
struct HASHMAP
{
int head[HASH],next[STATE],size;
long long state[STATE];
int f[STATE];
void init()
{
size=0;
memset(head,-1,sizeof(head));
}
void push(long long st,int ans)
{
int i;
int h=st%HASH;
for(i=head[h];i!=-1;i=next[i])
if(state[i]==st)
{
f[i]+=ans;
f[i]%=MOD;
return;
}
state[size]=st;
f[size]=ans;
next[size]=head[h];
head[h]=size++;
}
}hm[2];
void decode(int *code,int m,long long st)
{
num=st&63;
st>>=6;
for(int i=m;i>=0;i--)
{
code[i]=st&7;
st>>=3;
}
}
long long encode(int *code,int m)
{
int cnt=1;
memset(ch,-1,sizeof(ch));
ch[0]=0;
long long st=0;
for(int i=0;i<=m;i++)
{
if(ch[code[i]]==-1)ch[code[i]]=cnt++;
code[i]=ch[code[i]];
st<<=3;
st|=code[i];
}
st<<=6;
st|=num;
return st;
}
void shift(int *code,int m)
{
for(int i=m;i>0;i--)code[i]=code[i-1];
code[0]=0;
}
void dpblank(int i,int j,int cur)
{
int k,left,up;
for(k=0;k<hm[cur].size;k++)
{
decode(code,M,hm[cur].state[k]);
left=code[j-1];
up=code[j];
if(left&&up)
{
if(left==up)
{
if(num>=K)continue;
int t=0;
for(int p=0;p<j-1;p++)
if(code[p])t++;
if(t&1)continue;
if(num<K)
{
num++;
code[j-1]=code[j]=0;
hm[cur^1].push(encode(code,j==M?M-1:M),hm[cur].f[k]);
}
}
else
{
code[j-1]=code[j]=0;
for(int t=0;t<=M;t++)
if(code[t]==up)
code[t]=left;
hm[cur^1].push(encode(code,j==M?M-1:M),hm[cur].f[k]);
}
}
else if(left||up)
{
int t;
if(left)t=left;
else t=up;
if(maze[i][j+1])
{
code[j-1]=0;
code[j]=t;
hm[cur^1].push(encode(code,M),hm[cur].f[k]);
}
if(maze[i+1][j])
{
code[j]=0;
code[j-1]=t;
hm[cur^1].push(encode(code,j==M?M-1:M),hm[cur].f[k]);
}
}
else
{
if(maze[i][j+1]&&maze[i+1][j])
{
code[j-1]=code[j]=13;
hm[cur^1].push(encode(code,j==M?M-1:M),hm[cur].f[k]);
}
}
}
}
void dpblock(int i,int j,int cur)
{
int k;
for(k=0;k<hm[cur].size;k++)
{
decode(code,M,hm[cur].state[k]);
code[j-1]=code[j]=0;
hm[cur^1].push(encode(code,j==M?M-1:M),hm[cur].f[k]);
}
}
char str[20];
void init()
{
scanf("%d%d%d",&N,&M,&K);
memset(maze,0,sizeof(maze));
for(int i=1;i<=N;i++)
{
scanf("%s",&str);
for(int j=1;j<=M;j++)
if(str[j-1]=='.')
maze[i][j]=1;
}
}
void solve()
{
int i,j,cur=0;
hm[cur].init();
hm[cur].push(0,1);
for(i=1;i<=N;i++)
for(j=1;j<=M;j++)
{
hm[cur^1].init();
if(maze[i][j])dpblank(i,j,cur);
else dpblock(i,j,cur);
cur^=1;
}
int ans=0;
for(i=0;i<hm[cur].size;i++)
if(hm[cur].state[i]==K)
{
ans+=hm[cur].f[i];
ans%=MOD;
}
printf("%d\n",ans);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
solve();
}
return 0;
}

218
HDOJ/4286_autoAC.cpp Normal file
View File

@ -0,0 +1,218 @@
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<algorithm>
#define N 1000005
#define inf 1<<29
#define MOD 100000007
#define LL long long
#define Key_value ch[ch[root][1]][0]
#define _match(a,b) ((a)==(b))
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
int n,q,a[N],pl,pr;
int size[N],pre[N],rev[N],sum[5],val[N];
int ch[N][2],tot1,root,s[N],tot2;
inline void NewNode(int &r,int k,int father){
if(tot2)
r=s[tot2--];
else
r=++tot1;
ch[r][0]=ch[r][1]=0;
pre[r]=father;
size[r]=1;
rev[r]=0;
val[r]=k;
}
inline void Push_Up(int x){
int l=ch[x][0],r=ch[x][1];
size[x]=size[l]+size[r]+1;
}
inline void Push_Down(int r){
if(rev[r]){
rev[ch[r][0]]^= 1;
rev[ch[r][1]]^= 1;
swap(ch[r][0],ch[r][1]);
rev[r]=0;
}
}
inline void Bulid(int &r,int L,int R,int father){
if(L>R)
return ;
int mid=(L+R)/2;
NewNode(r,a[mid],father);
Bulid(ch[r][0],L,mid-1,r);
Bulid(ch[r][1],mid+1,R,r);
Push_Up(r);
}
void Init(){
tot1=tot2=root=0;
ch[root][0]=ch[root][1]=pre[root]=rev[root]=size[root]=0;
NewNode(root,-100,0);
NewNode(ch[root][1],-1,root);
Push_Up(root);
Bulid(Key_value,1,n,ch[root][1]);
Push_Up(ch[root][1]);
Push_Up(root);
}
inline void Rotate(int x,int kind){
int y=pre[x];
Push_Down(y);
Push_Down(x);
ch[y][!kind]=ch[x][kind];
pre[ch[x][kind]]=y;
if(pre[y])
ch[pre[y]][ch[pre[y]][1]==y]=x;
pre[x]=pre[y];
ch[x][kind]=y;
pre[y]=x;
Push_Up(y);
}
inline void Splay(int r,int goal){
Push_Down(r);
while(pre[r]!=goal){
if(pre[pre[r]]==goal)
Rotate(r,ch[pre[r]][0]==r);
else{
int y=pre[r];
int kind=(ch[pre[y]][0]==y);
if(ch[y][kind]==r){
Rotate(r,!kind);
Rotate(r,kind);
}
else{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
Push_Up(r);
if(goal==0) root=r;
}
inline void RotateTo(int k, int goal) {
int x=root;
Push_Down(x);
while(k!=size[ch[x][0]]+1){
if (k<=size[ch[x][0]]){
x=ch[x][0];
}else{
k-=(size[ch[x][0]]+1);
x=ch[x][1];
}
Push_Down(x);
}
Splay(x,goal);
}
void Print();
inline void Reversal(int pos,int k){
RotateTo(pos,0);
RotateTo(pos+k+1,root);
rev[Key_value]^=1;
}
inline void Insert(int pos,int k,int num){
RotateTo(pos,0);
RotateTo(pos+1,root);
NewNode(Key_value,num,ch[root][1]);
Push_Up(ch[root][1]);
Push_Up(root);
}
inline void Delete(int pos,int k){
RotateTo(pos,0);
RotateTo(pos+k+1,root);
Key_value=0;
Push_Up(ch[root][1]);
}
int cnt,ans[N];
inline void scanf_(int &num){
char in;
bool neg=false;
while(((in=getchar()) > '9' || in<'0') && in!='-') ;
if(in=='-'){
neg=true;
while((in=getchar()) >'9' || in<'0');
}
num=in-'0';
while(in=getchar(),in>='0'&&in<='9')
num*=10,num+=in-'0';
if(neg)
num=0-num;
}
inline void printf_(int num){
bool flag=false;
if(num<0){
putchar('-');
num=-num;
}
int ans[10],top=0;
while(num!=0){
ans[top++]=num%10;
num/=10;
}
if(top==0)
putchar('0');
for(int i=top-1;i>=0;i--){
char ch=ans[i]+'0';
putchar(ch);
}
}
void InOrder(int r){
if(r==0)
return;
Push_Down(r);
InOrder(ch[r][0]);
if(cnt) putchar(' ');
printf_(val[r]);
cnt++;
InOrder(ch[r][1]);
}
void Print(){
RotateTo(1,0);
RotateTo(n+2,root);
cnt=0;
InOrder(Key_value);
putchar('\n');
}
int main(){
int t;
scanf_(t);
while(t--){
scanf_(n);
for(int i=1;i<=n;i++)
scanf_(a[i]);
Init();
char str[20];
int k=1,pl,r1;
scanf_(pl);scanf_(pr);scanf_(q);
while(q--){
scanf("%s",str);
if(str[0]=='M'&&str[4]=='R'){
scanf("%s",str);
if(str[0]=='L') pl++;
else pr++;
}
else if(str[0]=='M'&&str[4]=='L'){
scanf("%s",str);
if(str[0]=='L') pl--;
else pr--;
}
else if(str[0]=='I'){
scanf("%s",str);
scanf_(a[1]);
if(str[0]=='L') {Insert(pl,k,a[1]);}
else {Insert(pr+1,k,a[1]);}
n++;pr++;
}
else if(str[0]=='D'){
scanf("%s",&str);
if(str[0]=='L') {Delete(pl,k);}
else {Delete(pr,k);}
n--;pr--;
}
else if(str[0]=='R')
Reversal(pl,pr-pl+1);
}
Print();
}
return 0;
}

88
HDOJ/4287_autoAC.cpp Normal file
View File

@ -0,0 +1,88 @@
#include<stdio.h>
#include<string.h>
int hash[128];
struct TrieNode{
int no;
TrieNode *next[10];
}node[30005];
TrieNode *root = &node[0];
int cnt,result[5005];
char word[10],s[10];
void init()
{
hash['a']=hash['b']=hash['c']=2;
hash['d']=hash['e']=hash['f']=3;
hash['g']=hash['h']=hash['i']=4;
hash['j']=hash['k']=hash['l']=5;
hash['m']=hash['n']=hash['o']=6;
hash['p']=hash['q']=hash['r']=hash['s']=7;
hash['t']=hash['u']=hash['v']=8;
hash['w']=hash['x']=hash['y']=hash['z']=9;
}
void initRoot()
{
int i;
for(i=0;i<10;i++)
{
root->next[i]=NULL;
}
}
void insert(char str[],int num)
{
TrieNode *p = root;
int len=strlen(str),i,j;
for(i=0;i<len;i++)
{
if(p->next[str[i]-'0']==NULL)
{
p->next[str[i]-'0']=&node[cnt];
for(j=0;j<10;j++)node[cnt].next[j]=NULL;
node[cnt].no=-1;
cnt++;
}
p=p->next[str[i]-'0'];
}
p->no=num;
}
void query(char str[])
{
int len=strlen(str),i;
TrieNode *p=root;
for(i=0;i<len;i++)
{
p=p->next[hash[str[i]]];
if(p==NULL)break;
}
if(p==NULL)return;
else{
if(p->no!=-1)result[p->no]++;
}
}
int main()
{
int t,m,n,i;
scanf("%d",&t);
init();
while(t--)
{
cnt=1;
initRoot();
memset(result,0,sizeof(result));
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%s",word);
insert(word,i);
}
for(i=0;i<m;i++)
{
scanf("%s",s);
query(s);
}
for(i=0;i<n;i++)
{
printf("%d\n",result[i]);
}
}
return 0;
}

50
HDOJ/4288_autoAC.cpp Normal file
View File

@ -0,0 +1,50 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int n;
int a[100005],len;
char str[10];
__int64 sum;
int main()
{
int i,j,k;
while(~scanf("%d",&n))
{
len = 0;
while(n--)
{
scanf("%s",str);
if(!strcmp(str,"add"))
{
scanf("%d",&k);
for(i = len++; i>0; i--)
{
if(a[i-1]>k)
a[i] = a[i-1];
else
break;
}
a[i] = k;
}
else if(!strcmp(str,"del"))
{
scanf("%d",&k);
for(i = 0; i<len; i++)
if(a[i] == k)
break;
for(; i<len; i++)
a[i] = a[i+1];
len--;
}
else if(!strcmp(str,"sum"))
{
sum = 0;
for(i = 2; i<len; i+=5)
sum+=a[i];
printf("%I64d\n",sum);
}
}
}
return 0;
}

116
HDOJ/4289_autoAC.cpp Normal file
View File

@ -0,0 +1,116 @@
#include<cstdio>
#include<cstring>
using namespace std;
const int inf=1<<30;
const int nMax=10105;
const int mMax=3000005;
struct Node
{
int c,u,v,next;
void insert(int nu,int nv,int nc,int nnext)
{
u=nu;
v=nv;
c=nc;
next=nnext;
}
}edge[mMax];
int ne,head[nMax];
int cur[nMax],ps[nMax],dep[nMax];
void addedge(int u,int v,int w)
{
edge[ne].insert(u,v,w,head[u]);
head[u]=ne++;
edge[ne].insert(v,u,0,head[v]);
head[v]=ne++;
}
int dinic(int s, int t){
int tr, res = 0;
int i, j, k, f, r, top;
while(1){
memset(dep, -1, sizeof(dep));
for(f = dep[ps[0]=s] = 0, r = 1; f != r;)
{
for(i = ps[f ++], j = head[i]; j; j = edge[j].next)
{
if(edge[j].c && dep[k=edge[j].v] == -1)
{
dep[k] = dep[i] + 1;
ps[r ++] = k;
if(k == t)
{
f = r; break;
}
}
}
}
if(dep[t] == -1) break;
memcpy(cur, head, sizeof(cur));
i = s, top = 0;
while(1)
{
if(i == t)
{
for(tr =inf, k = 0; k < top; k ++)
{
if(edge[ps[k]].c < tr)
{
tr = edge[ps[f=k]].c;
}
}
for(k = 0; k < top; k ++)
{
edge[ps[k]].c -= tr;
edge[ps[k]^1].c += tr;
}
i = edge[ps[top=f]].u;
res += tr;
}
for(j = cur[i]; cur[i]; j = cur[i] =edge[cur[i]].next)
{
if(edge[j].c && dep[i]+1 == dep[edge[j].v])
{
break;
}
}
if(cur[i])
{
ps[top ++] = cur[i];
i = edge[cur[i]].v;
}
else
{
if(top == 0)
{
break;
}
dep[i] = -1;
i = edge[ps[-- top]].u;
}
}
}
return res;
}
int main()
{
int i,j,a,b,s,t,m,n;
while(~scanf("%d%d%d%d",&n,&m,&s,&t))
{
ne=2;
memset(head,0,sizeof(head));
for(i=1;i<=n;i++)
{
scanf("%d",&a);
addedge(i,i+n,a);
}
for(i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
addedge(a+n,b,inf);
addedge(b+n,a,inf);
}
int res=dinic(s,t+n);
printf("%d\n",res);
}
return 0;
}

79
HDOJ/4290_autoAC.cpp Normal file
View File

@ -0,0 +1,79 @@
#include<stdio.h>
#include<string.h>
#define MAXD 33
#define MOD 1000000007
typedef __int64 LL;
int N, M, K;
LL C[MAXD][MAXD][MAXD], f[MAXD][MAXD], X[MAXD], S[MAXD];
LL fac(LL a, int n)
{
int i;
LL ans = 1;
for(i = 0; i < n; i ++) ans = ans * a % MOD;
return ans;
}
void exgcd(LL a, LL b, LL &x, LL &y)
{
if(b == 0) x = 1, y = 0;
else exgcd(b, a % b, y, x), y -= x * (a / b);
}
LL getinv(LL a)
{
LL x, y;
exgcd(a, MOD, x, y);
x %= MOD;
if(x < 0) x += MOD;
return x;
}
void prepare()
{
int i, j, t;
memset(C, 0, sizeof(C));
C[1][0][0] = 1;
for(i = 1; i <= 32; i ++)
{
C[1][i][0] = 1;
for(j = 1; j <= i; j ++)
C[1][i][j] = (C[1][i - 1][j] + C[1][i - 1][j - 1]) % MOD;
}
for(t = 2; t <= 32; t ++)
for(i = 0; i <= 32; i ++)
for(j = 0; j <= 32; j ++)
C[t][i][j] = fac(C[1][i][j], t);
}
void solve()
{
int i, j, k, t;
LL ans;
for(t = 1; t <= N; t ++)
{
memset(f, 0, sizeof(f)), f[0][0] = 1;
for(i = 1; i <= K; i ++)
for(j = 0; j <= M; j ++)
for(k = 0; k <= j; k ++)
f[i][j] = (f[i][j] + f[i - 1][k] * C[t][M - k][j - k]) % MOD;
X[t] = f[K][M];
}
S[0] = 1, S[1] = X[1];
for(i = 2; i <= N; i ++)
{
S[i] = 0;
for(j = 1; j <= i; j ++)
{
if(j & 1) S[i] = (S[i] + S[i - j] * X[j] + MOD) % MOD;
else S[i] = (S[i] - S[i - j] * X[j] + MOD) % MOD;
}
S[i] = S[i] * getinv(i) % MOD;
}
ans = S[N];
for(i = 2; i <= N; i ++) ans = ans * i % MOD;
if(ans < 0) ans += MOD;
printf("%I64d\n", ans);
}
int main()
{
prepare();
while(scanf("%d%d%d", &N, &M, &K) == 3)
solve();
return 0;
}

102
HDOJ/4292_autoAC.cpp Normal file
View File

@ -0,0 +1,102 @@
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define MAXN 1111
#define MAXM 88888888
struct Edge{
int v,cap,next;
}edge[MAXM];
int head[MAXN];
int pre[MAXN];
int cur[MAXN];
int level[MAXN];
int gap[MAXN];
int NE,NV,vs,vt,n,f,d;
int Food[MAXN];
int Drink[MAXN];
void Insert(int u,int v,int cap,int cc=0){
edge[NE].v=v;edge[NE].cap=cap;
edge[NE].next=head[u];head[u]=NE++;
edge[NE].v=u;edge[NE].cap=cc;
edge[NE].next=head[v];head[v]=NE++;
}
int SAP(int vs,int vt){
memset(pre,-1,sizeof(pre));
memset(level,0,sizeof(level));
memset(gap,0,sizeof(gap));
for(int i=0;i<NV;i++)cur[i]=head[i];
int u=pre[vs]=vs,maxflow=0,aug=-1;
gap[0]=NV;
while(level[vs]<NV){
loop:
for(int &i=cur[u];i!=-1;i=edge[i].next){
int v=edge[i].v;
if(edge[i].cap&&level[u]==level[v]+1){
aug==-1?aug=edge[i].cap:aug=min(aug,edge[i].cap);
pre[v]=u;
u=v;
if(v==vt){
maxflow+=aug;
for(u=pre[u];v!=vs;v=u,u=pre[u]){
edge[cur[u]].cap-=aug;
edge[cur[u]^1].cap+=aug;
}
aug=-1;
}
goto loop;
}
}
int minlevel=NV;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].v;
if(edge[i].cap&&minlevel>level[v]){
cur[u]=i;
minlevel=level[v];
}
}
gap[level[u]]--;
if(gap[level[u]]==0)break;
level[u]=minlevel+1;
gap[level[u]]++;
u=pre[u];
}
return maxflow;
}
int main(){
char str[MAXN];
while(~scanf("%d%d%d",&n,&f,&d)){
vs=0,vt=f+2*n+d+1,NV=vt+1,NE=0;
memset(head,-1,sizeof(head));
for(int i=1;i<=f;i++){
scanf("%d",&Food[i]);
Insert(vs,i,Food[i]);
}
for(int i=1;i<=d;i++){
scanf("%d",&Drink[i]);
Insert(i+2*n+f,vt,Drink[i]);
}
for(int i=1;i<=n;i++){
Insert(i+f,i+f+n,1);
}
for(int i=1;i<=n;i++){
scanf("%s",str+1);
for(int j=1;j<=f;j++){
if(str[j]=='Y'){
Insert(j,i+f,1);
}
}
}
for(int i=1;i<=n;i++){
scanf("%s",str+1);
for(int j=1;j<=d;j++){
if(str[j]=='Y'){
Insert(i+f+n,f+2*n+j,1);
}
}
}
printf("%d\n",SAP(vs,vt));
}
return 0;
}

29
HDOJ/4293_autoAC.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[510][510];
int tt[510][510], an[510];
int main()
{
int i, j, k;
int n, a, b;
while(scanf("%d", &n) != EOF)
{
memset(dp, 0, sizeof(dp));
memset(an, 0, sizeof(an));
memset(tt, 0, sizeof(tt));
for(i = 0; i < n; ++i) {
scanf("%d %d", &a, &b);
if(a+b < n && tt[a][n-b] < (n-a-b))
tt[a][n-b]++;
}
for(i = 1; i <= n; ++i)
for(j = 0; j < i; ++j) {
dp[j][i] = an[j]+tt[j][i];
an[i] = max(an[i], dp[j][i]);
}
printf("%d\n", an[n]);
}
return 0;
}

81
HDOJ/4294_autoAC.cpp Normal file
View File

@ -0,0 +1,81 @@
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 1000020
int n, k, num[2], m, ans, sl, ssl;
int ss[N], s[N];
bool v[10002];
struct node {
int pre, val, mod;
} q[N];
void bfs() {
memset(v, false, sizeof(v));
int l = 1, r = 1;
for (int i=0; i<m; i++) if (num[i]) {
q[r].pre = 0; q[r].mod = num[i] % n;
q[r].val = num[i];
if (q[r].mod == 0) { ans = r; break; }
r++;
}
while (l < r && ans == -1) {
for (int i=0; i<m; i++) {
q[r].mod = (q[l].mod * k + num[i]) % n;
if (!v[q[r].mod]) {
v[q[r].mod] = true;
q[r].pre = l;
q[r].val = num[i];
if (q[r].mod == 0) { ans = r; break; }
r++;
}
}
l++;
}
}
int cmp() {
if (sl < ssl) return -1;
if (sl > ssl) return 1;
for (int i=0; i<sl; i++) if (s[i] != ss[i]) return s[i]-ss[i];
return 0;
}
void cpy() {
ssl = sl;
for (int i=0; i<sl; i++) ss[i] = s[i];
}
void get_s(int x) {
if (x == 0) return ;
get_s(q[x].pre); s[sl++] = q[x].val;
}
void print() {
for (int i=0; i<ssl; i++) printf("%d", ss[i]);
printf("\n");
}
int main() {
while (scanf("%d%d", &n, &k) == 2) {
ssl = 0;
for (int i=1; i<k; i++) {
num[0] = i, m = 1;
ans = -1;
bfs();
if (ans != -1) {
sl = 0; get_s(ans);
if (cmp() < 0 || ssl == 0) cpy();
}
}
if (ssl == 0) {
for (int i=0; i<k; i++) for (int j=i+1; j<k; j++) {
num[0] = i, num[1] = j; m = 2;
ans = -1;
bfs();
if (ans != -1) {
sl = 0; get_s(ans);
if (cmp() < 0 || ssl == 0) cpy();
}
}
}
print();
}
return 0;
}

47
HDOJ/4295_autoAC.cpp Normal file
View File

@ -0,0 +1,47 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=4100;
char s[N],ss[70];
short l,sl[4],maxl;
short flag[N],d1[N][16][65],d2[N][16][65];
int main()
{
int i,j,k,x;
while(~scanf("%s",s))
{
l=strlen(s);
memset(flag,0,sizeof(flag[0])*l);
for(i=0;i<4;i++)
{
scanf("%s",ss);
sl[i]=strlen(ss);
for(j=0;j+sl[i]<=l;j++)
{
for(k=0;ss[k];k++)if(s[j+k]!=ss[k])break;
if(ss[k]=='\0') flag[j]|=1<<i;
}
if(sl[i]>maxl) maxl=sl[i];
}
memset(d1,0x3f,sizeof(d1[0])*(l+1));
memset(d2,0xc3,sizeof(d1[0])*(l+1));
d1[0][0][0]=d2[0][0][0]=0;
for(i=0;i<l;i++)for(j=0;j<16;j++)for(k=0;k<=maxl;k++)
{
d1[i+1][j][k?k-1:0]=min(d1[i][j][k],d1[i+1][j][k?k-1:0]);
d2[i+1][j][k?k-1:0]=max(d2[i][j][k],d2[i+1][j][k?k-1:0]);
for(x=0;x<4;x++)
{
if((1<<x&~j)&&(1<<x&flag[i]))
{
short t=max((short)k,sl[x]);
d1[i][1<<x|j][t]=min((short)(d1[i][j][k]+t-k),d1[i][1<<x|j][t]);
d2[i][1<<x|j][t]=max((short)(d2[i][j][k]+t-k),d2[i][1<<x|j][t]);
}
}
}
printf("%hd %hd\n",d1[l][15][0],d2[l][15][0]);
}
return 0;
}

20
HDOJ/4296_autoAC.cpp Normal file
View File

@ -0,0 +1,20 @@
#include<cstdio>
#define ll __int64
int main()
{
int n,w,s;
while(~scanf("%d",&n))
{
ll max=0,ans=0,sum=0;
while(n--)
{
scanf("%d%d",&w,&s);
if(w+s>max) max=w+s;
sum+=w;
}
sum-=max;
if(sum>ans) ans=sum;
printf("%I64d\n",ans);
}
return 0;
}

199
HDOJ/4297_autoAC.cpp Normal file
View File

@ -0,0 +1,199 @@
#pragma comment(linker, "/STACK:16777216")
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <queue>
using namespace std;
const int maxn = 1000010;
struct node {
int be, ne, weight;
void init(int b, int e, int v) {
be = b;
ne = e;
weight = v;
}
};
int rmq[maxn * 2];
struct RMQ {
int lg[maxn + 1], minn[20][maxn + 1], n;
RMQ() {
lg[0] = -1;
for (int i = 1; i <= maxn; i++) {
lg[i] = ((i & (i - 1)) == 0) ? lg[i - 1] + 1 : lg[i - 1];
minn[0][i] = i;
}
}
void init(int n) {
this->n = n;
for (int i = 1; i <= lg[n]; i++)
for (int j = 1; j <= n + 1 - (1 << i); j++) {
int a = minn[i - 1][j];
int b = minn[i - 1][j + (1 << (i - 1))];
minn[i][j] = rmq[a] < rmq[b] ? a : b;
}
}
int getmin(int s, int t) {
if (s > t) {
int temp = t;
t = s;
s = temp;
}
int k = lg[t - s + 1];
int a = minn[k][s];
int b = minn[k][t - (1 << k) + 1];
if (rmq[a] < rmq[b])
return a;
else
return b;
}
} st;
int root[maxn];
struct LCA {
int n, len, E[maxn];
node buf[maxn * 2];
int F[maxn * 2], pos[maxn], cnt;
int dis[maxn];
void init(int n) {
this->n = n;
len = 0;
memset(E, -1, sizeof(E));
}
void addedge(int a, int b, int w) {
buf[len].init(b, E[a], w);
E[a] = len++;
buf[len].init(a, E[b], w);
E[b] = len++;
}
int query(int a, int b) {
int k = st.getmin(pos[a], pos[b]);
return F[k];
}
void dfs(int a, int lev) {
F[++cnt] = a;
rmq[cnt] = lev;
pos[a] = cnt;
for (int i = E[a]; i != -1; i = buf[i].ne) {
int b = buf[i].be;
if (pos[b] != -1)
continue;
dis[b] = dis[a] + buf[i].weight;
dfs(b, lev + 1);
F[++cnt] = a;
rmq[cnt] = lev;
}
}
void dfs2(int a, int r) {
root[a] = r;
for (int i = E[a]; i != -1; i = buf[i].ne)
if (root[buf[i].be] == -1)
dfs2(buf[i].be, r);
}
void solve(int root) {
cnt = 0;
memset(pos, -1, sizeof(pos));
memset(dis, 0, sizeof(dis));
dfs(root, 0);
st.init(2*n-1);
}
} lca;
int nxt[maxn],n;
int cc[maxn],pos[maxn],len[maxn],num;
int f[maxn];
int find(int x) {
if (x != f[x])
f[x] = find(f[x]);
return f[x];
}
void circle() {
for (int i = 1; i <= n; i++)
f[i] = i;
num = 0;
memset(cc, -1, sizeof(cc));
memset(pos, -1, sizeof(pos));
for (int i = 1; i <= n; i++) {
int a = find(i);
int b = find(nxt[i]);
if (a == b)
cc[i]=++num;
f[a] = b;
}
for (int i = 1; i <= n; i++)
if (cc[i] != -1) {
int k = nxt[i];
int cnt = 0;
pos[i] = ++cnt;
root[i] = i;
while (k != i) {
pos[k] =++cnt;
root[k]=k;
cc[k]=cc[i];
k=nxt[k];
}
len[cc[i]] = cnt;
}
}
int main() {
int m;
while (scanf("%d %d", &n, &m) != EOF) {
lca.init(n + 1);
memset(root, -1, sizeof(root));
for (int i = 1; i <= n; i++)
scanf("%d", nxt + i);
circle();
for (int i = 1; i <= n; i++)
if (root[i]!=i)
lca.addedge(nxt[i], i, 1);
for (int i = 1; i <= n; i++){
if (root[i] == i){
lca.dfs2(i,i);
lca.addedge(0, i, 0);
}
}
lca.solve(0);
int a, b;
while (m-- > 0) {
scanf("%d %d", &a, &b);
int p = lca.query(a, b);
if (p != 0)
printf("%d %d\n", (lca.dis[a] - lca.dis[p]), (lca.dis[b]-lca.dis[p]));
else {
int ra = root[a], rb = root[b];
if (cc[ra] != cc[rb])
printf("-1 -1\n");
else {
int cnt=len[cc[ra]];
int temp1 = lca.dis[a];
int temp2 = lca.dis[b];
if (pos[ra] < pos[rb])
temp1 += pos[rb] - pos[ra];
else
temp1 += (cnt - pos[ra] + pos[rb]);
int temp3 = lca.dis[a];
int temp4 = lca.dis[b];
if (pos[rb]<pos[ra])
temp4 += pos[ra]- pos[rb];
else
temp4 +=(cnt-pos[rb]+pos[ra]);
if (max(temp1, temp2) < max(temp3, temp4))
printf("%d %d\n", temp1, temp2);
if (max(temp1, temp2) > max(temp3, temp4))
printf("%d %d\n", temp3, temp4);
if (max(temp1, temp2) == max(temp3, temp4)) {
if (min(temp1, temp2) < min(temp3, temp4))
printf("%d %d\n", temp1, temp2);
if (min(temp1, temp2) > min(temp3, temp4))
printf("%d %d\n", temp3, temp4);
if (min(temp1,temp2)==min(temp3, temp4)) {
if (temp1>=temp2)
printf("%d %d\n",temp1,temp2);
else
printf("%d %d\n",temp3,temp4);
}
}
}
}
}
}
return 0;
}