Powered By HC TECH : AutoACer Engine

4400-4499
This commit is contained in:
KiritoTRw 2016-09-06 13:31:25 +08:00 committed by GitHub
parent 53497060fc
commit a34c463144
84 changed files with 8062 additions and 0 deletions

96
HDOJ/4400_autoAC.cpp Normal file
View File

@ -0,0 +1,96 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
using namespace std;
const int maxn = 110005;
const int inf = 2000000005;
struct NODE{
int y, dis;
NODE(){
}
NODE(int _y, int _dis){
y = _y; dis = _dis;
}
bool operator <(const NODE &tmp)const{
if(y == tmp.y) return dis < tmp.dis;
return y < tmp.y;
}
};
struct POINT{
int x, y, dis;
POINT() {
}
POINT(int _x, int _y, int _dis){
x = _x;
y = _y;
dis = _dis;
}
}df[maxn], myque[1111111];
int n, m, has[maxn], num;
vector<NODE>mygraph[maxn];
void init(){
num = 0;
for(int i = 0; i < maxn; i++) mygraph[i].clear();
}
void readdata(){
for(int i = 1; i <= n ; i++){
scanf("%d%d%d", &df[i].x, &df[i].y, &df[i].dis);
has[num++] = df[i].x;
}
sort(has, has + num);
num = unique(has, has + num) - has;
for(int i = 1; i <= n; i++){
int id = lower_bound(has, has + num, df[i].x) - has;
mygraph[id].push_back(NODE(df[i].y, df[i].dis));
}
}
int fuckit(int fuckid){
int head = 0, tail = 0, ret = 0;
vector<NODE>::iterator vectoriterator1, vectoriterator2, tmpvectoriterator;
myque[tail++] = POINT(df[fuckid].x, df[fuckid].y, df[fuckid].dis);
while(head < tail){
POINT now = myque[head++];
int pos1 = lower_bound(has, has + num, now.x - now.dis) - has;
int pos2 = upper_bound(has, has + num, now.x + now.dis) - has;
for(; pos1 != pos2; pos1++){
int fucknum = has[pos1];
int fucky = now.dis - abs(fucknum - now.x);
int id = lower_bound(has, has + num, fucknum) - has;
vectoriterator1 = lower_bound(mygraph[id].begin(), mygraph[id].end(), NODE(now.y - fucky, -1));
vectoriterator2 = upper_bound(mygraph[id].begin(), mygraph[id].end(), NODE(now.y + fucky, inf));
tmpvectoriterator = vectoriterator1;
for(; vectoriterator1 != vectoriterator2; vectoriterator1++){
NODE tmp = *vectoriterator1;
myque[tail++] = POINT(fucknum, tmp.y, tmp.dis);
ret++;
}
mygraph[id].erase(tmpvectoriterator, vectoriterator2);
}
}
return ret;
}
int main(){
int testcases = 0;
while(scanf("%d", &n) != EOF && n){
init();
readdata();
printf("Case #%d:\n", ++testcases);
for(int i = 0; i < num; i++) {
sort(mygraph[i].begin(), mygraph[i].end());
}
scanf("%d", &m);
for(int i = 1; i <= m; i++){
int k;
scanf("%d", &k);
int sum = fuckit(k);;
printf("%d\n", sum);
}
}
return 0;
}

111
HDOJ/4401_autoAC.cpp Normal file
View File

@ -0,0 +1,111 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define PI acos(-1.0)
using namespace std;
struct Node
{
double h,x;
};
Node a[1005],b[1005],st[1005];
double L;
int n,T1,T2,top;
inline int DB(double x)
{
if(x>1e-10) return 1;
if(x<-1e-10) return -1;
return 0;
}
double cal1(Node a,Node b)
{
double k=(a.h-b.h)/(a.x-b.x);
double x=a.x-a.h/k;
return x;
}
double cal2(double x1,double x2,double h,int t)
{
double a=sqrt(h*h+x2*x2)-sqrt(h*h+x1*x1);
double b=(x2-x1)*cos(PI/12.0*t);
double c=12.0/PI;
return c*(a-b);
}
double cal(double x1,double x2,double h,int t)
{
double B1,B2,B3;
if(DB(x1)==0) B1=PI/2;
else B1=atan(fabs(h/x1));
B2=atan(fabs(h/x2));
B3=PI/12*t;
if(DB(B3-B1)>=0) return cal2(x1,x2,h,t);
if(DB(B2-B3)>=0) return 0;
double k=-tan(B3);
return cal2(-h/k,x2,h,t);
}
int OK(Node a,Node b,Node c)
{
double k1=(a.h-b.h)/(a.x-b.x);
double k2=(b.h-c.h)/(b.x-c.x);
if(DB(k1-k2)==-1) return 1;
return 0;
}
double solve(int t,Node a[])
{
top=0;
st[++top]=a[1];
double ans=0;
int i,j;
double x1,x2;
for(i=2;i<=n;i++)
{
x1=a[i-1].x;
x2=a[i-1].x;
for(j=top;j>=2;j--)
{
x2=cal1(st[j-1],st[j]);
if(DB(x2-a[i].x)>=0) break;
ans+=cal(x1-st[j].x,x2-st[j].x,st[j].h,t);
x1=x2;
}
ans+=cal(x1-st[j].x,a[i].x-st[j].x,st[j].h,t);
while(top>=2&&OK(st[top-1],st[top],a[i])||top>=1&&DB(a[i].h-st[top].h)>=0)
top--;
st[++top]=a[i];
}
return ans;
}
int cmp(Node a,Node b)
{
return a.x<b.x;
}
double deal(int t)
{
double ans=0;
int i;
for(i=1;i<=n;i++)
{
b[i]=a[n+1-i];
b[i].x=L-b[i].x;
}
double xx,yy,zz;
if(t<=6) ans=solve(t,a);
else
{
ans=solve(6,a);
ans+=solve(6,b)-solve(12-t,b);
}
return ans;
}
int main()
{
while(scanf("%lf%d",&L,&n),n||L)
{
scanf("%d%d",&T1,&T2);
int i;
for(i=1;i<=n;i++) scanf("%lf%lf",&a[i].x,&a[i].h);
sort(a+1,a+n+1,cmp);
printf("%.5lf\n",deal(T2-6)-deal(T1-6));
}
return 0;
}

60
HDOJ/4403_autoAC.cpp Normal file
View File

@ -0,0 +1,60 @@
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std;
char seq[50];
char str[50];
int n,ans,eq;
bool ok(int len)
{
long long a,sum=0;
int i,flg=1;
queue<char> q;
q.push('+');
seq[len]='\0';
for(i=0; i<len; i++)
{
a=0;
while(i<=len&&seq[i]>='0'&&seq[i]<='9') a=a*10+seq[i]-'0',i++;
q.push(seq[i]);
if(flg&&q.front()=='+')
sum+=a;
else
flg=0,sum-=a;
q.pop();
}
if(sum==0) return 1;
else return 0;
}
void dfs(int cur,int q)
{
if(eq>1) return;
if(q>n)
{
if(ok(cur)) ans++;
return;
}
seq[cur]=str[q];
dfs(cur+1,q+1);
seq[cur]='+';
seq[cur+1]=str[q];
dfs(cur+2,q+1);
seq[cur]='=';
seq[cur+1]=str[q];
eq++;
dfs(cur+2,q+1);
eq--;
}
int main()
{
while(cin>>str,str[0]!='E')
{
ans=0;eq=0;
seq[0]=str[0];
n=strlen(str)-1;
dfs(1,1);
cout<<ans<<endl;
}
return 0;
}

69
HDOJ/4404_autoAC.cpp Normal file
View File

@ -0,0 +1,69 @@
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
const double Pi=acos(-1.0);
const double EP=1e-10;
struct Point{
double x,y;
};
double x_mult(Point sp, Point ep, Point op){
return (sp.x-op.x)*(ep.y-op.y)-(sp.y-op.y)*(ep.x-op.x);
}
double cross(Point a,Point b,Point c){
return (a.x-c.x)*(b.x-c.x)+(a.y-c.y)*(b.y-c.y);
}
double dist(Point a,Point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double cal_area(Point a,Point b,Point c,double r){
double A,B,C,x,y,tS;
A=dist(b,c);
B=dist(a,c);
C=dist(b,a);
if(A<r&&B<r)
return x_mult(a,b,c)/2;
else if(A<r&&B>=r){
x=(cross(a,c,b)+sqrt(r*r*C*C-x_mult(a,c,b)*x_mult(a,c,b)))/C;
tS=x_mult(a,b,c)/2;
return asin(tS*(1-x/C)*2/r/B*(1-EP))*r*r/2+tS*x/C;
}
else if(A>=r&&B<r){
y=(cross(b,c,a)+sqrt(r*r*C*C-x_mult(b,c,a)*x_mult(b,c,a)))/C;
tS=x_mult(a,b,c)/2;
return asin(tS*(1-y/C)*2/r/A*(1-EP))*r*r/2+tS*y/C;
}
else if(fabs(x_mult(a,b,c))>=r*C||cross(b,c,a)<=0||cross(a,c,b)<=0){
if(cross(a,b,c)<0)
if(x_mult(a,b,c)<0)
return (-acos(-1.0)-asin(x_mult(a,b,c)/A/B*(1-EP)))*r*r/2;
else return (acos(-1.0)-asin(x_mult(a,b,c)/A/B*(1-EP)))*r*r/2;
else return asin(x_mult(a,b,c)/A/B*(1-EP))*r*r/2;
}
else{
x=(cross(a,c,b)+sqrt(r*r*C*C-x_mult(a,c,b)*x_mult(a,c,b)))/C;
y=(cross(b,c,a)+sqrt(r*r*C*C-x_mult(b,c,a)*x_mult(b,c,a)))/C;
tS=x_mult(a,b,c)/2;
return (asin(tS*(1-x/C)*2/r/B*(1-EP))+asin(tS*(1-y/C)*2/r/A*(1-EP)))*r*r/2+tS*((y+x)/C-1);
}
}
int main(){
Point p[105],cir;
double r,area, x0, y0, v, angle, t, g;
int i, n;
while(scanf("%lf%lf%lf%lf%lf%lf%lf", &x0, &y0, &v, &angle, &t, &g, &r)!=EOF){
if(fabs(x0)+fabs(y0)+fabs(v)+fabs(angle)+fabs(t)+fabs(g)+
fabs(r)<0.0000001)break;
scanf("%d", &n);
for(i=0; i<n; i++)
scanf("%lf%lf", &p[i].x, &p[i].y);
area=0;cir.x=x0+v*cos(angle/180.0*Pi)*t;
cir.y=y0+v*sin(angle/180.0*Pi)*t-0.5*g*t*t;
for(i=0;i<n;i++){
area+=cal_area(p[i], p[(i+1)%n], cir, r);
}
printf("%.2f\n",fabs(area));
}
return 0;
}

59
HDOJ/4405_autoAC.cpp Normal file
View File

@ -0,0 +1,59 @@
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
const int N=100005;
struct node
{
int y,next;
};
bool vis[N];
node path[N];
int first[N],t;
double dp[N];
void add(int x,int y)
{
path[t].y=y;
path[t].next=first[x];
first[x]=t++;
}
int main()
{
double s;
int n,m,v;
while(cin>>n>>m)
{
if(m==0&&n==0) break;
memset(dp,0,sizeof(dp));
memset(vis,0,sizeof(vis));
memset(first,0,sizeof(first));
int x,y;
t=1;
while(m--)
{
cin>>x>>y;
add(y,x);
}
dp[n]=-1;
for(int i=n; i>=0; i--)
{
if(!vis[i])
{
vis[i]=true;
s=0;
for(int k=1; k<=6; k++)
s+=dp[i+k];
s/=6;
dp[i]+=(s+1);
}
for(int k=first[i]; k; k=path[k].next)
{
v=path[k].y;
dp[v]=dp[i];
vis[v]=true;
}
}
printf("%.4lf\n",dp[0]);
}
return 0;
}

142
HDOJ/4406_autoAC.cpp Normal file
View File

@ -0,0 +1,142 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <string>
#include <set>
#define LL long long
#define myabs(x) ((x)>0?(x):(-(x)))
using namespace std;
const int inf=0x3f3f3f3f;
const double tinf=100000;
const int maxn=100;
const int maxm=1500*2;
struct Edge
{
int from,to,next;
double val,cap,flow;
};
Edge e[maxm];
int head[maxn];
int cre[maxn],bas[maxn],vis[maxn];
double dis[maxn],score[110];
int p[maxn];
int n,kin,m,tot,s,t;
void addedge(int from,int to,double cap,double val)
{
e[tot].from=from; e[tot].to=to;
e[tot].cap=cap; e[tot].flow=0;
e[tot].val=val;
e[tot].next=head[from];
head[from]=tot++;
e[tot].from=to; e[tot].to=from;
e[tot].cap=0; e[tot].flow=0;
e[tot].next=head[to];
e[tot].val=-val;
head[to]=tot++;
}
double mfmc()
{
queue<int> q;
int f;
double sum=0;
int i;
for(;;)
{
for(i=s;i<=t;i++) dis[i]=-inf;
memset(vis,0,sizeof(vis));
dis[s]=0;
vis[s]=1;
q.push(s);
int i,v;
while(!q.empty())
{
f=q.front(); q.pop();
vis[f]=0;
for(i=head[f];i!=-1;i=e[i].next)
{
v=e[i].to;
if(e[i].cap>e[i].flow&&dis[f]+e[i].val>dis[v])
{
p[v]=i;
dis[v]=dis[f]+e[i].val;
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
if(dis[t]<=0) break;
double a=(double)inf;
for(i=t;i!=s;)
{
a=min(a,e[p[i]].cap-e[p[i]].flow);
i=e[p[i]].from;
}
sum+=a*dis[t];
for(i=t;i!=s;)
{
e[p[i]].flow+=a;
e[p[i]^1].flow-=a;
i=e[p[i]].from;
}
}
return sum;
}
int main()
{
int i;
for(i=60;i<=100;i++) score[i]=4.0-3.0*(100-i)*(100-i)/1600;
while(~scanf("%d%d%d",&n,&kin,&m))
{
if(!n&&!kin&&!m) break;
memset(head,-1,sizeof(head));
int j,k,tem;
s=0; t=n+m+1;
tot=0;
double w=0,basic=0,sum=0;
for(i=1;i<=m;i++)
{
scanf("%d",&cre[i]);
w+=cre[i];
}
for(i=1;i<=m;i++)
{
scanf("%d",&bas[i]);
if(bas[i]<60)
{
addedge(0,i,60-bas[i],tinf);
sum=sum+(60-bas[i])*tinf;
bas[i]=60;
}
basic=basic+score[bas[i]]*cre[i];
for(j=bas[i]+1;j<=100;j++)
addedge(0,i,1,(score[j]-score[j-1])*cre[i]);
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%d",&tem);
if(tem)
addedge(j,m+i,inf,0);
}
}
for(i=m+1;i<t;i++)
addedge(i,t,kin,0);
double ans=mfmc();
if(ans>=sum)
{
ans=(basic+(ans-sum))/w;
printf("%.6lf\n",ans);
}
else printf("0.000000\n");
}
return 0;
}

98
HDOJ/4407_autoAC.cpp Normal file
View File

@ -0,0 +1,98 @@
#include <map>
#include <stdio.h>
typedef __int64 LL;
using namespace std;
#define N 650
bool np[N];
int prime[120],fac[9],F_top,p;
LL ans;
void get_prime()
{
int top = -1;
for(int i=2;i<N;i++)
if(!np[i])
{
prime[++top] = i;
for(int j=i+i;j<N;j+=i)
np[j] = true;
}
}
void Div(int n)
{
F_top = 0;
for(int i=0;prime[i]*prime[i]<=n;i++)
{
if(n % prime[i])
continue;
while(n % prime[i] == 0)
n /= prime[i];
fac[F_top++] = prime[i];
}
if(n != 1)
fac[F_top++] = n;
}
LL PreSum(int n)
{
return (LL)n*(n+1)/2;
}
void dfs(int i,int cnt,int m,int n,int num,int x)
{
if(cnt == m)
{
LL sum = num * PreSum(x/num);
m&1 ? ans -= sum : ans += sum;
return ;
}
if(num*fac[i] > x || n-i < m-cnt)
return ;
dfs(i+1,cnt+1,m,n,num*fac[i],x);
dfs(i+1,cnt,m,n,num,x);
}
LL sovle(int n)
{
ans = PreSum(n);
for(int m=1;m<=F_top;m++)
dfs(0,0,m,F_top,1,n);
return ans;
}
int gcd(int a,int b)
{
return b ? gcd(b,a%b) : a;
}
int main()
{
int z,n,Q,cmd,a,b;
get_prime();
scanf("%d",&z);
map<int,int> M;
while(z--)
{
M.clear();
scanf("%d%d",&n,&Q);
while(Q--)
{
scanf("%d",&cmd);
if(cmd == 1)
{
scanf("%d%d%d",&a,&b,&p);
Div(p);
ans = sovle(b) - sovle(a-1);
for(map<int,int>::iterator it=M.begin();it!=M.end();it++)
if((*it).first >= a && (*it).first <= b)
{
if(gcd((*it).first,p) == 1)
ans -= (*it).first;
if(gcd((*it).second,p) == 1)
ans += (*it).second;
}
printf("%I64d\n",ans);
}
else
{
scanf("%d%d",&a,&b);
M[a] = b;
}
}
}
return 0;
}

125
HDOJ/4408_autoAC.cpp Normal file
View File

@ -0,0 +1,125 @@
#include <map>
#include <stack>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define N 405
#define M 4005
#define E
#define inf 0x3f3f3f3f
#define dinf 1e10
#define linf (LL)1<<60
#define LL long long
#define clr(a,b) memset(a,b,sizeof(a))
using namespace std;
LL mod;
struct Edge
{
int a,b,c;
bool operator<(const Edge & t)const
{
return c<t.c;
}
}edge[M];
int n,m;
LL ans;
int fa[N],ka[N],vis[N];
LL gk[N][N],tmp[N][N];
vector<int>gra[N];
int findfa(int a,int b[]){return a==b[a]?a:b[a]=findfa(b[a],b);}
LL det(LL a[][N],int n)
{
for(int i=0;i<n;i++)for(int j=0;j<n;j++)a[i][j]%=mod;
long long ret=1;
for(int i=1;i<n;i++)
{
for(int j=i+1;j<n;j++)
while(a[j][i])
{
LL t=a[i][i]/a[j][i];
for(int k=i;k<n;k++)
a[i][k]=(a[i][k]-a[j][k]*t)%mod;
for(int k=i;k<n;k++)
swap(a[i][k],a[j][k]);
ret=-ret;
}
if(a[i][i]==0)return 0;
ret=ret*a[i][i]%mod;
}
return (ret+mod)%mod;
}
int main()
{
while(scanf("%d%d%I64d",&n,&m,&mod)==3)
{
if(n==0 && m==0 && mod==0)break;
memset(gk,0,sizeof(gk));
memset(tmp,0,sizeof(tmp));
memset(fa,0,sizeof(fa));
memset(ka,0,sizeof(ka));
memset(tmp,0,sizeof(tmp));
for(int i=0;i<N;i++)gra[i].clear();
for(int i=0;i<m;i++)
scanf("%d%d%d",&edge[i].a,&edge[i].b,&edge[i].c);
sort(edge,edge+m);
for(int i=1;i<=n;i++)fa[i]=i,vis[i]=0;
int pre=-1;
ans=1;
for(int h=0;h<=m;h++)
{
if(edge[h].c!=pre||h==m)
{
for(int i=1;i<=n;i++)
if(vis[i])
{
int u=findfa(i,ka);
gra[u].push_back(i);
vis[i]=0;
}
for(int i=1;i<=n;i++)
if(gra[i].size()>1)
{
for(int a=1;a<=n;a++)
for(int b=1;b<=n;b++)
tmp[a][b]=0;
int len=gra[i].size();
for(int a=0;a<len;a++)
for(int b=a+1;b<len;b++)
{
int la=gra[i][a],lb=gra[i][b];
tmp[a][b]=(tmp[b][a]-=gk[la][lb]);
tmp[a][a]+=gk[la][lb];tmp[b][b]+=gk[la][lb];
}
long long ret=(long long)det(tmp,len);
ret%=mod;
ans=(ans*ret%mod)%mod;
for(int a=0;a<len;a++)fa[gra[i][a]]=i;
}
for(int i=1;i<=n;i++)
{
ka[i]=fa[i]=findfa(i,fa);
gra[i].clear();
}
if(h==m)break;
pre=edge[h].c;
}
int a=edge[h].a,b=edge[h].b;
int pa=findfa(a,fa),pb=findfa(b,fa);
if(pa==pb)continue;
vis[pa]=vis[pb]=1;
ka[findfa(pa,ka)]=findfa(pb,ka);
gk[pa][pb]++;gk[pb][pa]++;
}
int flag=0;
for(int i=2;i<=n&&!flag;i++)if(ka[i]!=ka[i-1])flag=1;
ans%=mod;
printf("%I64d\n",flag?0:ans);
}
return 0;
}

144
HDOJ/4409_autoAC.cpp Normal file
View File

@ -0,0 +1,144 @@
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
using namespace std;
#define M 30005
map<int,string> hasht;
map<string,int> hashx;
struct cmp
{
bool operator()(const int &a, const int &b)
{
return hasht.find(a)->second<hasht.find(b)->second;
}
};
struct point
{
int pre;
set<int,cmp> son;
string name;
}node[M];
struct px
{
int idx,len;
} temp;
int cnt;
stack<struct px> father;
void dfs(int idx,int len)
{
for(int i=0; i<len; ++i) printf(".");
printf("%s\n",node[idx].name.c_str());
for(set<int,cmp>::iterator it=node[idx].son.begin(); it!=node[idx].son.end(); ++it)
dfs(*it,len+1);
}
void out_all()
{
dfs(0,0);
}
void out_child()
{
char s[70];
scanf("%s",s);
if(hashx.count(string(s))==0)
{
printf("0\n");
return;
}
if(string(s)==node[0].name)
{
printf("1\n");
return;
}
int idx=hashx.find(string(s))->second;
printf("%d\n",node[node[idx].pre].son.size());
}
void out_pre()
{
char a[70],b[70];
scanf("%s%s",a,b);
set<int> ax;
int ida=hashx.find(string(a))->second;
int idb=hashx.find(string(b))->second;
for(; ida!=-1;)
{
ida=node[ida].pre;
ax.insert(ida);
}
for(; idb!=-1;)
{
idb=node[idb].pre;
if(ax.count(idb)!=0) break;
}
printf("%s\n",node[idb].name.c_str());
}
int main()
{
int n,m,pre;
char s[70],c,ord[10];
string sx;
for(; scanf("%d",&n),n;)
{
for(int i=0; i<=n; ++i)
{
node[i].pre=-1;
node[i].son.clear();
}
hashx.clear();
hasht.clear();
for(; !father.empty(); father.pop());
cnt=0;
temp.idx=0;
temp.len=0;
scanf("%s",s);
sx=string(s);
node[cnt++].name=sx;
hashx.insert(make_pair(sx,0));
hasht.insert(make_pair(0,sx));
father.push(temp);
for(int i=1; i<n; ++i)
{
for(; getchar()!='.';);
int ll=1;
for(; (c=getchar())=='.'; ++ll);
s[0]=c;
int llt;
for(llt=1;(s[llt]=getchar())!='\n';++llt);
s[llt]='\0';
temp.len=ll;
temp.idx=cnt;
for(;;)
{
struct px tx=father.top();
if(tx.len>=temp.len)
father.pop();
else
break;
}
sx=string(s);
int tx=father.top().idx;
node[cnt].pre=tx;
node[cnt].name=sx;
hasht.insert(make_pair(cnt,sx));
hashx.insert(make_pair(sx,cnt));
node[tx].son.insert(cnt);
cnt++;
father.push(temp);
}
scanf("%d",&m);
for(; m--;)
{
scanf("%s",ord);
if(ord[0]=='L')
out_all();
else if(ord[0]=='b')
out_child();
else if(ord[0]=='c')
out_pre();
}
}
return 0;
}

168
HDOJ/4410_autoAC.cpp Normal file
View File

@ -0,0 +1,168 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;
const double INF = 1e19;
double EPS = 1e-12;
bool zero(double t){return -EPS<t&&t<EPS;}
struct cvector{
double x,y;
cvector(double a,double b){x=a,y=b;}
cvector(){}
};
cvector operator-(cvector a,cvector b){
return cvector(a.x-b.x,a.y-b.y);
}
cvector operator+(cvector a,cvector b){
return cvector(a.x+b.x,a.y+b.y);
}
double operator^(cvector a,cvector b){
return a.x*b.y-b.x*a.y;
}
double operator*(cvector a,cvector b){
return a.x*b.x+a.y*b.y;
}
struct cpoint{
double x,y;
void get(){scanf("%lf%lf",&x,&y);}
} p[19];
cvector operator-(cpoint a,cpoint b){
return cvector(a.x-b.x,a.y-b.y);
}
double dist(cpoint a,cpoint b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
struct segline{
cpoint a,b;
segline(cpoint x,cpoint y){a=x,b=y;}
segline(){}
};
bool intersect(segline s1,segline s2){
return ((s1.a-s2.a)^(s2.b-s2.a))*((s1.b-s2.a)^(s2.b-s2.a))<-EPS&&
((s2.a-s1.a)^(s1.b-s1.a))*((s2.b-s1.a)^(s1.b-s1.a))<-EPS;
}
int n;
double solve4(){
return min(fabs(p[0].x-p[2].x),fabs(p[0].y-p[2].y));
}
double solve6()
{
double re[10];
int t=0,k;
for(int i=0;i<n;i++){
re[i]=(p[i]-p[(i+n-1)%n])^(p[(i+1)%n]-p[i]);
if(re[i]>0) t++;
}
if(t==1){
for(int i=0;i<6;i++)
if(re[i]>0) k=i;
}else{
for(int i=0;i<6;i++)
if(re[i]<0) k=i;
}
return min(dist(p[k],p[(k+3)%n]),min(fabs(p[(k+2)%n].x-p[(k+n-2)%n].x),fabs(p[(k+2)%n].y-p[(k+n-2)%n].y)));
}
double oor6(int a,int b)
{
cpoint t1 = p[(a+n-2)%n],t2 = p[(b+3)%n];
return min(fabs(t1.x-t2.x),min(fabs(t1.y-t2.y),max(dist(p[a],p[(a+n-3)%n]),dist(p[b],p[(b+3)%n]))));
}
double oor5(int a,int b)
{
cpoint t1 = p[(a+n-2)%n],t2 = p[(b+2)%n];
return min(fabs(t1.x-t2.x),min(fabs(t1.y-t2.y),min(dist(p[a],p[(a+n-3)%n]),dist(p[b],p[(b+3)%n]))));
}
double oor4(int a,int b)
{
double ans;
if(zero(p[(a-2+n)%n].y-p[(b+2)%n].y))
{
ans = min(fabs(p[(a-1+n)%n].x-p[(b+1)%n].x),fabs(p[(a-2+n)%n].y-p[(a+2)%n].y));
}else{
ans = min(fabs(p[(a-1+n)%n].y-p[(b+1)%n].y),fabs(p[(a-2+n)%n].x-p[(a+2)%n].x));
}
ans = min(ans,min(dist(p[a],p[(a-3+n)%n]),dist(p[b],p[(b+3)%n])));
return ans;
}
double oor3(int a,int b)
{
cpoint t1 = p[(a+2)%n],t2 = p[(b+2)%n];
double ans = min(fabs(t1.x-t2.x),fabs(t1.y-t2.y));
double A1=INF,A2=INF,B1=INF,B2=INF;
if(intersect(segline(p[a],p[(a+3)%n]),segline(p[(a+4)%n],p[(a+5)%n])))
A1=min(dist(p[(a+1)%n],p[(a+2)%n]),dist(p[b],p[(a+1)%n]));
if(intersect(segline(p[a],p[(a-3+n)%n]),segline(p[(a-4+n)%n],p[(a-5+n)%n])))
A2=min(dist(p[(a-1+n)%n],p[(a-2+n)%n]),dist(p[b],p[(a-1+n)%n]));
if(intersect(segline(p[b],p[(b+3)%n]),segline(p[(b+4)%n],p[(b+5)%n])))
B1=min(dist(p[(b+1)%n],p[(b+2)%n]),dist(p[a],p[(b+1)%n]));
if(intersect(segline(p[b],p[(b-3+n)%n]),segline(p[(b-4+n)%n],p[(b-5+n)%n])))
B2=min(dist(p[(b-1+n)%n],p[(b-2+n)%n]),dist(p[a],p[(b-1+n)%n]));
if(A1!=INF)
ans = min(ans,max(B1,A1));
if(A2!=INF)
ans = min(ans,max(B2,A2));
// if(B1!=INF)
// ans = min(ans,max(dist(p[b],p[(b+5)%n]),B1));
// if(B2!=INF)
// ans = min(ans,max(dist(p[b],p[(b+3)%n]),B2));
//double A = max(min(dist(p[a],p[(a+3)%n]),A1),min(dist(p[a],p[(a+5)%n]),A2));
// double B = max(min(dist(p[b],p[(b+3)%n]),B1),min(dist(p[b],p[(b+5)%n]),B2));
ans = min(ans,max(min(dist(p[a],p[(a-3+n)%n]),dist(p[b],p[(b+3)%n])),
min(dist(p[a],p[(a+3)%n]),dist(p[b],p[(b-3+n)%n]))));
return ans;
}
double solve8()
{
double re[10];
int t=0;
for(int i=0;i<n;i++){
re[i]=(p[i]-p[(i+n-1)%n])^(p[(i+1)%n]-p[i]);
if(re[i]>0) t++;
}
if(t==2){
}else{
for(int i=0;i<n;i++) re[i] = 0-re[i];
}
int a,b;
int s = 0,k=0;
for(int i=0;i<(n<<1);i++)
{
if(re[i%n]>0){
s=max(s,k);
k=0;
if(i<n)
a=b,b=i;
}else{
k++;
}
}
if(b-a>4) swap(a,b);
if(s==6)
return oor6(a,b);
if(s==5)
return oor5(a,b);
if(s==4)
return oor4(a,b);
if(s==3)
return oor3(a,b);
return 0;
}
int main()
{
while(scanf("%d",&n)&&n)
{
for(int i=0;i<n;i++)
p[i].get();
if(n==4)
printf("%.2lf\n",solve4());
if(n==6)
printf("%.2lf\n",solve6());
if(n==8)
printf("%.2lf\n",solve8());
}
return 0;
}

127
HDOJ/4411_autoAC.cpp Normal file
View File

@ -0,0 +1,127 @@
#include<stdio.h>
#include<string.h>
#define clr(x)memset(x,0,sizeof(x))
#define INF 0x1f1f1f1f
#define min(a,b)(a)<(b)?(a):(b)
#define max(a,b)(a)>(b)?(a):(b)
struct node
{
int next,from,to,w,c;
}e[1000010];
int tot;
int head[210];
void add(int st,int u,int wi,int flow)
{
e[tot].from=st;
e[tot].to=u;
e[tot].w=wi;
e[tot].c=flow;
e[tot].next=head[st];
head[st]=tot++;
e[tot].from=u;
e[tot].to=st;
e[tot].w=-wi;
e[tot].c=0;
e[tot].next=head[u];
head[u]=tot++;
}
int q[100010];
int pre[2010];
int dis[2010];
int v[220];
int s,t;
int spfa()
{
int i,x,front,rear,k;
front=rear=0;
for(i=0;i<=t;i++)
dis[i]=INF;
clr(v);
pre[s]=-1;
q[rear++]=s;
dis[s]=0;
v[s]=1;
while(front<rear)
{
x=q[front++];
v[x]=0;
for(i=head[x];i!=-1;i=e[i].next)
{
k=e[i].to;
if(e[i].c&&dis[x]+e[i].w<dis[k])
{
dis[k]=dis[x]+e[i].w;
pre[k]=i;
if(!v[k])
{
v[k]=1;
if(dis[k]<=dis[x]&&front>0)
q[--front]=k;
else q[rear++]=k;
}
}
}
}
if(dis[t]!=INF)
return 1;
return 0;
}
int costflow()
{
int tt=0;
int tot=0,flow=0,u,minf=INF;
while(spfa())
{
for(u=pre[t];u!=-1;u=pre[e[u].from])
minf=min(minf,e[u].c);
for(u=pre[t];u!=-1;u=pre[e[u].from])
{
e[u].c-=minf;
e[u^1].c+=minf;
flow+=e[u].w*minf;
}
}
return flow;
}
int g[105][105];
int main()
{
int m,n,a,b,c,k,K,w,z,i,j;
while(scanf("%d%d%d",&n,&m,&K)!=EOF)
{
if(n==0&&m==0&&K==0)
break;
memset(g,INF,sizeof(g));
for(i=0;i<=n;i++)
g[i][i]=0;
while(m--)
{
scanf("%d%d%d",&a,&b,&w);
if(w<g[a][b])
g[a][b]=g[b][a]=w;
}
for(k=0;k<=n;k++)
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
if(g[i][k]+g[k][j]<g[i][j])
g[i][j]=g[i][k]+g[k][j];
int res;
s=2*n+1;
t=2*n+2;
tot=0;
memset(head,-1,sizeof(head));
add(s,0,0,K);
add(0,t,0,w);
for(i=1;i<=n;i++)
{
add(0,i,g[0][i],1);
add(i,i+n,-1000000,1);
add(i+n,t,g[0][i],1);
}
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
add(i+n,j,g[i][j],1);
printf("%d\n",costflow()+1000000*n);
}
return 0;
}

78
HDOJ/4412_autoAC.cpp Normal file
View File

@ -0,0 +1,78 @@
#include<stdio.h>
#include<iostream>
#include<map>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
#define MAXN 1005
using namespace std;
double dp[55][MAXN], cost[MAXN][MAXN];
map<int ,double> has;
struct Info{
double pos;
double p;
}info[MAXN];
int main()
{
int N, M, i, n, a, j, k, pos;
double l, r, suml, sumr;
double b, min, temp, t;
while(scanf("%d%d",&N, &M)&&(N + M))
{
has.clear();
for( i = 1; i <= N; ++ i )
{
scanf("%d", &n);++n;
while(--n)
{
scanf("%d%lf", &a, &b);
has[a]+=b;
}
}
N = 0;
for(map<int, double>::iterator it = has.begin(); it != has.end(); ++it)
{
info[++N].pos = it->first;
info[N].p = it->second;
}
for(j = N; j >= 1; j--)
{
pos = j;
cost[j][j] = 0;
l = r = suml = 0;
sumr = info[j].p;
for(k = j-1; k >= 1; --k )
{
suml += info[k].p;
l += (info[pos].pos-info[k].pos) * info[k].p;
temp = l + r;
while((pos > 1)&&(temp>(t = (l + r + (sumr - suml) * (info[pos].pos - info[pos - 1].pos)))))
{
l -= suml * (info[pos].pos - info[pos - 1].pos);
r += sumr * (info[pos].pos - info[pos - 1].pos);
--pos;
suml -= info[pos].p;
sumr +=info[pos].p;
temp = t;
}
cost[k][j] = temp;
}
}
for(i = 1; i <= N; i++)dp[0][i] = 1e300;
for(i = 0; i <= M; i++)dp[i][0] = 0;
for(i = 1; i <= M; i++)
{
for(j = N; j >= 1; j--)
{
min = dp[i-1][j-1] + cost[j][j];
for(k = j-1; k >= 1; --k )
{
if(dp[i - 1][k - 1] + cost[k][j] < min)min = dp[i - 1][k - 1] + cost[k][j];
}
dp[i][j] = min;
}
}
printf("%0.2lf\n",dp[M][N]);
}
return 0;
}

136
HDOJ/4413_autoAC.cpp Normal file
View File

@ -0,0 +1,136 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
typedef pair<string, int> node;
vector<node> ans;
bool vis[50];
int V[50];
bool cmp(string s1,string s2){
return s1+s2<s2+s1;
}
bool cmp2(string s1,string s2){
return s1+"+"+s2<s2+"+"+s1;
}
bool hmr(node a, node b) {
if (a.second==b.second) {
if (a.first.length()==b.first.length()) return a.first<b.first;
return a.first.length()<b.first.length();
}
return a.second<b.second;
}
bool operator==(node a, node b) {
return a.first==b.first && a.second==b.second;
}
string StInS(int s,int n,int ors){
int i;
vector<string> tmpAns;
for (i=0;i<(1<<n);i++){
if ((i&s)==(ors&s))
if (V[i]==0) return "";
}
if (s==0) return "1";
string tmp;
for (i=0;i<n;i++) if (s&(1<<i)){
tmp="";
if (!(ors&(1<<i)))
tmp+='-';
tmp+=(char)(i+'A');
tmpAns.push_back(tmp);
}
sort(tmpAns.begin(),tmpAns.end(),cmp);
tmp="";
for (i=0;i<tmpAns.size();i++)
tmp+=tmpAns[i];
return tmp;
}
void getans(int s, int n) {
int i,j;
int minsize = 0x3fff;
vector<node> tmp;
for (i=0;i<(1<<n);i++) {
int ctrl=__builtin_popcount(i);
if (ctrl>minsize) continue;
string tans2=StInS(i,n,s);
if (tans2.empty()) continue;
if (ctrl<minsize) {
minsize = ctrl;
tmp.clear();
}
int res = 0;
for (j = 0; j < 1<<n; ++j)
if ((j&i)==(s&i)) res |= 1<<j;
tmp.push_back(make_pair(tans2, res));
}
for (i = 0; i < tmp.size(); ++i)
ans.push_back(tmp[i]);
}
int N, M;
int all;
int best;
bool use[64], ause[64];
void dfs(int i, int len, int cover) {
if (len >= best) return;
if (cover==all) {
memcpy(ause, use, sizeof(use));
best = len;
return;
}
if (i==M) return;
use[i] = 1;
dfs(i+1, len+ans[i].first.length()+1, cover|ans[i].second);
use[i] = 0;
dfs(i+1, len, cover);
}
int main(){
int i,s,j,v;
int cas=0;
for (;;){
scanf("%d",&N);
if (N==0) break;
for (i=0;i<(1<<N);i++){
s=0;
vis[i] = 0;
for (j=0;j<N;j++){
scanf("%d",&v);
s^=(v<<j);
}
scanf("%d",&V[s]);
}
ans.clear();
memset(vis, 0, sizeof vis);
all = 0;
for (s = 0; s < 1<<N; ++s)
if (V[s]){
all |= 1<<s;
getans(s, N);
}
if (ans.size()==0) {
puts("-AA");
continue;
}
if (ans[0].first[0]=='1') {
puts("-A+A");
continue;
}
sort(ans.begin(), ans.end(), hmr);
M = unique(ans.begin(), ans.end())-ans.begin();
sort(ans.begin(), ans.begin()+M);
best = 0x3fffffff;
memset(use, 0, sizeof use);
memset(ause, 0, sizeof ause);
dfs(0, 0, 0);
vector<string> astr;
for (i = 0; i < M; ++i)
if (ause[i]) astr.push_back(ans[i].first);
sort(astr.begin(), astr.end(), cmp2);
string fin = astr[0];
for (i = 1; i < astr.size(); ++i)
fin += "+"+astr[i];
cout << fin << '\n';
}
return 0;
}

90
HDOJ/4414_autoAC.cpp Normal file
View File

@ -0,0 +1,90 @@
#include<stdio.h>
#include<string.h>
char grid[70][70];
int vis[70][70],midx[70],midy[70],top;
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
int N,acquire;
typedef struct node
{
int x,y;
}Node;
Node queue[30000];
int BFS(int x,int y)
{
int rear,front;
int nx,ny,ox,oy;
int i,j,k,cnt,step=4;
cnt=rear=front=0;
queue[rear].x=x;
queue[rear++].y=y;
vis[x][y]=1;
while(front<rear)
{
cnt=0;
ox=queue[front].x;oy=queue[front++].y;
for(i=0;i<4;i++)
{
nx=ox+dx[i];ny=oy+dy[i];
if(nx>=0&&nx<N&&ny>=0&&ny<N&&grid[nx][ny]=='#'&&!vis[nx][ny])
{
cnt++;
queue[rear].x=nx;queue[rear++].y=ny;
vis[nx][ny]=1;
}
}
if(cnt==1)step++;
}
step/=4;
if(rear==step*4+1)acquire++;
return 0;
}
int DFS(int x,int y)
{
int nx,ny;
int i,j,k,cnt=0;
for(i=0;i<4;i++)
{
nx=x+dx[i];ny=y+dy[i];
if(nx>=0&&nx<N&&ny>=0&&ny<N&&!vis[nx][ny]&&grid[nx][ny]=='#')
{
cnt++;
vis[nx][ny]=1;
DFS(nx,ny);
}
}
if(cnt==3)
{
midx[top]=x;
midy[top++]=y;
}
return 0;
}
int main()
{
int i,j,k;
while(scanf("%d",&N)!=EOF&&N)
{
memset(vis,0,sizeof(vis));
acquire=top=0;
for(i=0;i<N;i++)scanf("%s",grid[i]);
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(!vis[i][j]&&grid[i][j]=='#')
{
vis[i][j]=1;
DFS(i,j);
}
}
}
memset(vis,0,sizeof(vis));
for(i=0;i<top;i++)
{
if(!vis[midx[i]][midy[i]])BFS(midx[i],midy[i]);
}
printf("%d\n",acquire);
}
return 0;
}

112
HDOJ/4415_autoAC.cpp Normal file
View File

@ -0,0 +1,112 @@
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
struct px
{
int a,b;
} node;
vector<struct px> a,b;
bool cmp(const struct px &x,const struct px &y)
{
return x.a<y.a;
}
int main()
{
int tt;
int x,y,n,m,ida,idb;
scanf("%d",&tt);
for(int cas=1; cas<=tt; ++cas)
{
int ans=0,bx=0,mm=0,mn=0,ansn=0;
a.clear();
b.clear();
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++)
{
scanf("%d%d",&node.a,&node.b);
if(node.b!=0) a.push_back(node);
else b.push_back(node);
}
sort(a.begin(),a.end(),cmp);
sort(b.begin(),b.end(),cmp);
ida=0;
idb=b.size()-1;
for(int i=0; i<b.size(); ++i)
{
if(m-mn>=b[i].a)
{
ansn++;
mn+=b[i].a;
}
}
if(a.size()>0&&a[0].a<=m)
{
node=a[0];
mm+=node.a;
for(int i=0; i<a.size(); i++)
{
bx+=(a[i].b-1);
ans++;
}
bx++;
}
if(a.size()>0&&a[0].a<=m-mn)
{
node=a[0];
mn+=node.a;
for(int i=0; i<a.size(); i++)
ansn++;
}
for(; bx!=0&&idb>=0; --idb,--bx,++ans);
if(idb<0)
{
if(ans>ansn) printf("Case %d: %d %d\n",cas,ans,mm);
else if(ans<ansn) printf("Case %d: %d %d\n",cas,ansn,mn);
else printf("Case %d: %d %d\n",cas,ansn,mn>mm?mm:mn);
}
else
{
int i,j;
for(i=1,j=0; i<a.size()&&j<=idb;)
{
if(a[i].a<=b[j].a)
{
if(m>=a[i].a+mm)
{
mm+=a[i].a;
i++;
ans++;
idb--;
}
else break;
}
else
{
if(m>=b[j].a+mm)
{
mm+=b[j].a;
ans++;
j++;
}
else break;
}
}
for(;j<=idb;++j)
{
if(m>=b[j].a+mm)
{
mm+=b[j].a;
ans++;
j++;
}
else break;
}
if(ans>ansn) printf("Case %d: %d %d\n",cas,ans,mm);
else if(ans<ansn) printf("Case %d: %d %d\n",cas,ansn,mn);
else printf("Case %d: %d %d\n",cas,ansn,mn>mm?mm:mn);
}
}
return 0;
}

100
HDOJ/4416_autoAC.cpp Normal file
View File

@ -0,0 +1,100 @@
#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<queue>
#define inf 1<<28
#define M 6000005
#define N 1000005
#define maxn 300005
#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;
int wa[maxn],wb[maxn],wv[maxn],Ws[maxn];
int cmp(int *r,int a,int b,int l)
{return r[a]==r[b]&&r[a+l]==r[b+l];}
void da(const int *r,int *sa,int n,int m){
int i,j,p,*x=wa,*y=wb,*t;
for(i=0;i<m;i++) Ws[i]=0;
for(i=0;i<n;i++) Ws[x[i]=r[i]]++;
for(i=1;i<m;i++) Ws[i]+=Ws[i-1];
for(i=n-1;i>=0;i--) sa[--Ws[x[i]]]=i;
for(j=1,p=1;p<n;j*=2,m=p){
for(p=0,i=n-j;i<n;i++) y[p++]=i;
for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=0;i<n;i++) wv[i]=x[y[i]];
for(i=0;i<m;i++) Ws[i]=0;
for(i=0;i<n;i++) Ws[wv[i]]++;
for(i=1;i<m;i++) Ws[i]+=Ws[i-1];
for(i=n-1;i>=0;i--) sa[--Ws[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
return;
}
int sa[maxn],Rank[maxn],height[maxn];
void calheight(const int *r,int *sa,int n){
int i,j,k=0;
for(i=1;i<=n;i++) Rank[sa[i]]=i;
for(i=0;i<n;height[Rank[i++]]=k)
for(k?k--:0,j=sa[Rank[i]-1];r[i+k]==r[j+k];k++);
return;
}
int a[maxn],pos[maxn];
char str[maxn];
int main(){
int t,cas=0,n;
scanf("%d",&t);
while(t--){
int len,m=0,num=30;
scanf("%d",&n);
scanf("%s",str);
len=strlen(str);
for(int i=0;i<len;i++) a[m++]=str[i]-'a'+1;
for(int i=0;i<n;i++){
a[m++]=num++;
scanf("%s",str);
int l=strlen(str);
for(int j=0;j<l;j++)
a[m++]=str[j]-'a'+1;
}
a[m]=0;
da(a,sa,m+1,num+1);
calheight(a,sa,m);
mem(pos,0);
int tmp=inf;
for(int i=1;i<=m;i++){
if(sa[i]<len){
if(height[i]<tmp) tmp=height[i];
if(pos[sa[i]]<tmp) pos[sa[i]]=tmp;
}
else tmp=inf;
}
tmp=inf;
for(int i=m;i>=1;i--){
if(sa[i-1]<len){
if(height[i]<tmp) tmp=height[i];
if(pos[sa[i-1]]<tmp) pos[sa[i-1]]=tmp;
}
else tmp=inf;
}
for(int i=1;i<=m;i++)
if(sa[i]<len&& sa[i-1]<len)
if(pos[sa[i-1]]<height[i])
pos[sa[i-1]]=height[i];
LL ans=(LL)len*(len+1)/2;
for(int i=0;i<len;i++)
ans-=pos[i];
printf("Case %d: %I64d\n",++cas,ans);
}
return 0;
}

71
HDOJ/4417_autoAC.cpp Normal file
View File

@ -0,0 +1,71 @@
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 100010;
struct dit{
int num,id;
}dd[N];
struct ask{
int lp,rp,value,id;
}aa[N];
int cnt[N];
bool cmp1(dit a,dit b){
return a.num < b.num;
}
bool cmp2(ask a,ask b){
return a.value < b.value;
}
int inline lowbit(int x){
return x & (-x);
}
void inline update(int x){
while(x < N){
cnt[x]++;
x += lowbit(x);
}
}
int inline sum(int x){
int s = 0;
while(x > 0){
s += cnt[x];
x -= lowbit(x);
}
return s;
}
int main(){
int numcase;
scanf("%d",&numcase);
for(int ca = 1; ca <= numcase; ++ca){
int n,m;
memset(cnt,0,sizeof(cnt));
scanf("%d%d",&n,&m);
for(int i = 0; i < n; ++i){
scanf("%d",&dd[i].num);
dd[i].id = i + 1;
}
int x,y;
for(int i = 0; i < m; ++i){
scanf("%d%d%d",&x,&y,&aa[i].value);
aa[i].lp = x + 1;
aa[i].rp = y + 1;
aa[i].id = i + 1;
}
sort(dd,dd+n,cmp1);
sort(aa,aa+m,cmp2);
int ans[N] = {0};
for(int aski = 0,ditj = 0; aski < m; ++aski){
while(ditj < n && aa[aski].value >= dd[ditj].num){
update(dd[ditj].id);
ditj++;
}
ans[aa[aski].id] = sum(aa[aski].rp) - sum(aa[aski].lp - 1);
}
printf("Case %d:\n",ca);
for(int i = 1; i <= m; ++i){
printf("%d\n",ans[i]);
}
}
return 0;
}

109
HDOJ/4418_autoAC.cpp Normal file
View File

@ -0,0 +1,109 @@
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
#define M 205
#define eps 1e-8
int equ, var;
double a[M][M], x[M];
int Gauss ()
{
int i, j, k, col, max_r;
for (k = 0, col = 0; k < equ && col < var; k++, col++)
{
max_r = k;
for (i = k+1; i < equ; i++)
if (fabs (a[i][col]) > fabs (a[max_r][col]))
max_r = i;
if (k != max_r)
{
for (j = col; j < var; j++)
swap (a[k][j], a[max_r][j]);
swap (x[k], x[max_r]);
}
x[k] /= a[k][col];
for (j = col+1; j < var; j++) a[k][j] /= a[k][col];
a[k][col] = 1;
for (i = 0; i < equ; i++) if (i != k)
{
x[i] -= x[k] * a[i][k];
for (j = col+1; j < var; j++) a[i][j] -= a[k][j] * a[i][col];
a[i][col] = 0;
}
}
return 1;
}
int has[M], vis[M], k, e, n, m;
double p[M], sum;
int bfs (int u)
{
memset (has, -1, sizeof(has));
memset (a, 0, sizeof(a));
memset (vis, 0, sizeof(vis));
int v, i, flg = 0;
queue<int> q;
q.push (u);
k = 0;
has[u] = k++;
while (!q.empty ())
{
u = q.front ();
q.pop ();
if (vis[u]) continue;
vis[u] = 1;
if (u == e || u == n-e)
{
a[has[u]][has[u]] = 1;
x[has[u]] = 0;
flg = 1;
continue;
}
a[has[u]][has[u]] = 1;
x[has[u]] = sum;
for (i = 1; i <= m; i++)
{
if (fabs (p[i]) < eps) continue;
v = (u + i) % n;
if (has[v] == -1) has[v] = k++;
a[has[u]][has[v]] -= p[i];
q.push (v);
}
}
return flg;
}
int main()
{
int t, s, d, i;
scanf ("%d", &t);
while (t--)
{
scanf ("%d%d%d%d%d", &n, &m, &e, &s, &d);
n = 2*(n-1);
sum = 0;
for (i = 1; i <= m; i++)
{
scanf ("%lf", p+i);
p[i] = p[i] / 100;
sum += p[i] * i;
}
if (s == e)
{
puts ("0.00");
continue;
}
if (d > 0) s = (n - s) % n;
if (!bfs (s))
{
puts ("Impossible !");
continue;
}
equ = var = k;
Gauss ();
printf ("%.2f\n", x[has[s]]);
}
return 0;
}

119
HDOJ/4419_autoAC.cpp Normal file
View File

@ -0,0 +1,119 @@
#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<queue>
#define inf 1<<28
#define M 6000005
#define N 11005
#define maxn 300005
#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
#define lson step<<1
#define rson step<<1|1
using namespace std;
struct segment{
int x1,x2,y,val;
segment(){}
segment(int _x1,int _x2,int _y,int _v):x1(_x1),x2(_x2),y(_y),val(_v){}
bool operator<(const segment s)const{
return y<s.y;
}
}seg[N*2];
struct Node{
int left,right,len[8],cnt[5];
}L[N*2*4];
int x[N*2]={-1},cnt;
LL ans[8];
void Bulid(int step,int l,int r){
L[step].left=l;
L[step].right=r;
mem(L[step].len,0);
mem(L[step].cnt,0);
if(l==r) return;
int m=(l+r)>>1;
Bulid(lson,l,m);
Bulid(rson,m+1,r);
}
void Push_Up(int step){
int state=(L[step].cnt[1]>0?1:0)|(L[step].cnt[2]>0?2:0)|(L[step].cnt[4]>0?4:0);
if(state){
mem(L[step].len,0);
L[step].len[state]=x[L[step].right+1]-x[L[step].left];
for(int i=1;i<8;i++){
if(state!=(state|i)){
int tmp=L[lson].len[i]+L[rson].len[i];
L[step].len[state|i]+=tmp;
L[step].len[state]-=tmp;
}
}
}
else if(L[step].left!=L[step].right)
for(int i=1;i<8;i++) L[step].len[i]=L[lson].len[i]+L[rson].len[i];
else mem(L[step].len,0);
}
void Update(int step,int l,int r,int val){
if(L[step].left==l&&r==L[step].right)
val>0?++L[step].cnt[val]:--L[step].cnt[-val];
else{
int m=(L[step].left+L[step].right)>>1;
if(r<=m) Update(lson,l,r,val);
else if(l>m) Update(rson,l,r,val);
else{
Update(lson,l,m,val);
Update(rson,m+1,r,val);
}
}
Push_Up(step);
}
int Bin(int m){
int low=1,high=cnt,mid;
while(low<=high){
mid=(low+high)>>1;
if(x[mid]==m) return mid;
if(x[mid]<m) low=mid+1;
else high=mid-1;
}
}
int main(){
int t,cas=0,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=0;i<n;i++){
char str[6];
int x1,x2,y1,y2;
scanf("%s%d%d%d%d",str,&x1,&y1,&x2,&y2);
seg[2*i]=segment(x1,x2,y1,str[0]=='R'?1:(str[0]=='G'?2:4));
seg[2*i+1]=segment(x1,x2,y2,str[0]=='R'?-1:(str[0]=='G'?-2:-4));
x[2*i+2]=x1;x[2*i+1]=x2;
}
sort(seg,seg+2*n);
sort(x+1,x+1+2*n);
cnt=0;
for(int i=1;i<=2*n;i++)
if(x[i]!=x[cnt])
x[++cnt]=x[i];
Bulid(1,1,cnt);
mem(ans,0);
for(int i=0;i<2*n;i++){
int l=Bin(seg[i].x1),r=Bin(seg[i].x2)-1;
Update(1,l,r,seg[i].val);
if(seg[i].y!=seg[i+1].y)
for(int j=1;j<8;j++)
ans[j]+=(LL)(seg[i+1].y-seg[i].y)*(LL)L[1].len[j];
}
printf("Case %d:\n",++cas);
printf("%I64d\n%I64d\n%I64d\n%I64d\n%I64d\n%I64d\n%I64d\n",ans[1],ans[2],ans[4],ans[3],ans[5],ans[6],ans[7]);
}
return 0;
}

111
HDOJ/4421_autoAC.cpp Normal file
View File

@ -0,0 +1,111 @@
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
inline int min(int a,int b) { return a<b? a: b ; }
const int maxn = 4000 +10 ;
const int maxm = 4000000 + 10;
struct TwoSAT {
int col[maxn],dfn[maxn],low[maxn],s[maxn],cnt,scnt, stop,n;
int pre[maxm],ad[maxn],v[maxm], tot ;
void init()
{
tot=0 ;
memset(ad,0,sizeof(ad));
}
void add(int x,int y)
{
pre[++tot] = ad[x];
ad[x] = tot ;
v[tot] = y;
}
void add_clause(int x, int xval, int y, int yval)
{
add(x+xval*n,y+(!yval)*n);
add(y+yval*n,x+(!xval)*n);
}
void dfs(int u) {
dfn[u]=low[u]=++cnt ;
s[++stop]=u;
for (int j=ad[u];j;j=pre[j]){
if (!dfn[v[j]]){
dfs(v[j]);
low[u] = min(low[u],low[v[j]]) ;
}
else if (!col[v[j]])
low[u] = min(low[u] , dfn[v[j]]);
}
if(low[u]==dfn[u]){
++scnt;
while(1){
int x=s[stop--];
col[x]=scnt;
if(x==u)break;
}
}
}
bool slove()
{
memset(dfn,0,sizeof(dfn));
memset(col,0,sizeof(col));
cnt=scnt=stop= 0 ;
for (int i=0; i<2*n; ++i)
if (!dfn[i])
dfs(i);
for(int i=0;i<n;++i)
if (col[i] ==col[i+n])
return 0;
return 1;
}
};
TwoSAT TSAT;
int b[600][600];
int main()
{
int n ;
while (scanf("%d",&n)!=EOF) {
for (int i=0; i<n; ++i)
for (int j=0; j<n; ++j)
scanf("%d",&b[i][j]) ;
TSAT.n=n;
int flag = 1;
for (int k=0; k<31; ++k) {
TSAT.init() ;
for (int i=0; i<n; ++i)
for (int j=i; j<n; ++j)
if (i==j)
continue;
else if (i %2 ==1 && j%2 ==1) {
if (b[i][j] & (1<<k)){
TSAT.add_clause(i,1,j,1);
}
else{
TSAT.add_clause(i,0,i,0);
TSAT.add_clause(j,0,j,0);
}
}
else if (i%2==0 && j%2==0) {
if (b[i][j] & (1<<k)){
TSAT.add_clause(i,1,i,1);
TSAT.add_clause(j,1,j,1);
}
else {
TSAT.add_clause(i,0,j,0);
}
}
else {
if (b[i][j] & (1<<k)) {
TSAT.add_clause(i,0,j,0);
TSAT.add_clause(i,1,j,1);
}
else {
TSAT.add_clause(i,0,j,1);
TSAT.add_clause(i,1,j,0);
}
}
flag = TSAT.slove();
if (!flag) break ;
}
if (flag) puts("YES"); else puts("NO");
}
}

71
HDOJ/4422_autoAC.cpp Normal file
View File

@ -0,0 +1,71 @@
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
int main()
{
int n,num[11];
int i,l,j,k;
int flag;
int temp;
int ans;
while(scanf("%d",&n)!=-1)
{
for(i=0;i<n;i++) scanf("%d",&num[i]);
if(n<=3) {printf("1024\n");continue;}
if(n==4)
{
flag=0;
for(i=0;i<4;i++)
{
temp=0;
for(l=0;l<4;l++)
{
if(i==l) continue;
temp+=num[l];
}
if(temp%1024==0) flag=1;
if(flag) break;
}
if(flag) {printf("1024\n");continue;}
else
{
ans=0;
for(i=0;i<4;i++)
for(l=0;l<4;l++)
{
if(i==l) continue;
temp=num[i]+num[l];
while(temp>1024) temp-=1024;
ans=ans>temp?ans:temp;
}
printf("%d\n",ans);
continue;
}
}
else
{
flag=0;
ans=0;
for(i=0;i<5;i++)
for(l=0;l<5;l++)
for(j=0;j<5;j++)
{
if(i!=l && i!=j && l!=j)
{
temp=num[i]+num[l]+num[j];
if(temp%1024==0)
{
temp=0;
for(k=0;k<5;k++) if(k!=i && k!=l && k!=j) temp+=num[k];
while(temp>1024) temp-=1024;
ans=ans>temp?ans:temp;
flag=1;
}
}
}
if(flag) printf("%d\n",ans);
else printf("0\n");
}
}
return 0;
}

63
HDOJ/4424_autoAC.cpp Normal file
View File

@ -0,0 +1,63 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 200200
typedef long long LL;
int parent[MAXN];
LL sum[MAXN];
int cnt[MAXN];
struct Edge{
int u,v,w;
}edge[MAXN];
int n;
void Initiate()
{
memset(sum,0,(n+2)*sizeof(sum[0]));
for(int i=0;i<=n;i++){
parent[i]=i;
cnt[i]=1;
}
}
int Find(int x)
{
if(x==parent[x])
return x;
parent[x]=Find(parent[x]);
return parent[x];
}
void Union(int r1,int r2,LL w)
{
parent[r1]=r2;
cnt[r2]+=cnt[r1];
sum[r2]+=w;
}
int cmp(const Edge &p,const Edge &q)
{
return p.w>q.w;
}
int main()
{
while(~scanf("%d",&n))
{
Initiate();
for(int i=0;i<n-1;i++){
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
}
sort(edge,edge+n-1,cmp);
for(int i=0;i<n-1;i++){
int r1=Find(edge[i].u);
int r2=Find(edge[i].v);
LL w=(LL)edge[i].w;
LL tmp1=cnt[r2]*w+sum[r1];
LL tmp2=cnt[r1]*w+sum[r2];
if(tmp1>tmp2){
Union(r2,r1,tmp1-sum[r1]);
}else
Union(r1,r2,tmp2-sum[r2]);
}
printf("%I64d\n",sum[Find(1)]);
}
return 0;
}

200
HDOJ/4426_autoAC.cpp Normal file
View File

@ -0,0 +1,200 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <math.h>
using namespace std;
const int maxn=4e5+9,maxm=11371111;;
const long long mod=777777777;
char a[maxn];
int dp[maxn];
unsigned long long sum[maxn],po[maxn];
int n,m;
int score[maxn];
int has[maxm],lon;
struct D
{
int next,pos,size;
long long sum;
unsigned long long _key;
long long key;
bool operator <(const D & xx) const
{
return key<xx.key;
}
}data[maxn];
void getpal()
{
int top=0;
dp[0]=0,a[0]=-1,a[n+1]=-2;
for(int i=1,t,s;i<=n;i++)
{
if(top+dp[top]<i)
{
top=i;
t=i-1,s=i+1;
while(a[t]==a[s]) t--,s++;
dp[i]=s-i-1;
}
else
{
if(dp[top-(i-top)]+i<top+dp[top])
dp[i]=dp[top-(i-top)];
else
{
s=top+dp[top]+1;
t=i-(s-i);
while(a[t]==a[s]) t--,s++;
dp[i]=s-i-1;
top=i;
}
}
}
}
bool insert(int pos,int size,long long ff)
{
if(a[pos]=='#') pos++;
unsigned long long _key=(sum[pos+(size-1)*2]-sum[pos-2]);
_key*=po[n-pos];
int key=_key%maxm;
for(int k=has[key];k!=-1;k=data[k].next)
{
if(data[k]._key==_key)
{
data[k].sum+=ff;
return false;
}
}
data[++lon].sum=ff;
data[lon].pos=pos;
data[lon].size=size;
data[lon]._key=_key;
data[lon].next=has[key];
has[key]=lon;
return true;
}
void getkey()
{
sum[0]=0;
for(int i=2;i<=n;i+=2)
{
sum[i]=sum[i-2];
sum[i]+=(a[i]-'a'+1)*po[i];
}
}
struct R
{
int t,key;
bool operator <(const R & xx) const
{
return key<xx.key;
}
};
void gethash()
{
memset(has,-1,sizeof(has));
lon=0;
getkey();
for(int i=1;i<=n;i++)
if(dp[i]>0)
insert(i,dp[i]+1>>1,1);
priority_queue <R> q;
while(!q.empty()) q.pop();
for(int i=1;i<=lon;i++)
{
R now;
now.key=data[i].size;
now.t=i;
q.push(now);
}
while(!q.empty())
{
int pos=data[q.top().t].pos;
int size=data[q.top().t].size;
long long ff=data[q.top().t].sum;
size--;
q.pop();
if(size&&insert(pos,size,ff))
{
R now;
now.t=lon;
now.key=size;
q.push(now);
}
}
}
long long nn[maxn],pp[maxn];
void getkey(int t)
{
data[t].key=nn[data[t].pos];
int ff=data[t].pos+data[t].size*2;
if(ff>n) return ;
long long tmp=(nn[ff]*pp[data[t].size])%mod;
data[t].key=((data[t].key+mod-tmp))%mod;
}
void getnow()
{
pp[0]=1;
for(int i=1;i<=n+1;i++)
{
pp[i]=(pp[i-1]*26)%mod;
}
nn[n+1]=0;
for(int i=n-1;i>=1;i-=2)
{
nn[i]=(nn[i+2]*26)%mod;
nn[i]=(nn[i]+score[(a[i]-'a'+1)])%mod;
}
}
void solve(long long num)
{
getnow();
for(int i=1;i<=lon;i++)
getkey(i);
sort(data+1,data+1+lon);
long long now=0;
for(int i=1;i<=lon;i++)
{
now+=data[i].sum;
if(now>=num)
{
printf("%I64d\n",data[i].key);
break;
}
}
if(now<num)
printf("0\n");
}
int main()
{
po[0]=1;
for(int i=1;i<maxn;i++)
po[i]=po[i-1]*27;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
scanf("%s",a+1);
for(int i=n;i>=1;i--)
{
a[i*2]=a[i];
a[i*2-1]='#';
}
n<<=1;
a[++n]='#';
getpal();
gethash();
for(int i=1;i<=m;i++)
{
long long tmp;
scanf("%I64d",&tmp);
for(int j=1;j<=26;j++)
scanf("%d",&score[j]);
solve(tmp);
}
cout<<endl;
}
return 0;
}

68
HDOJ/4427_autoAC.cpp Normal file
View File

@ -0,0 +1,68 @@
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
const int maxn=1010;
const int mod=1000000007;
int dp[2][maxn][maxn];
int lcm[maxn][maxn];
int fac[maxn];
int gcd(int a,int b)
{
int t;
while(b)
{
t=a%b;
a=b;
b=t;
}
return a;
}
int main()
{
int n,m,k,cnt,i,j,p,q,e,f,t,v,mul,tt;
for(i=1; i<maxn; i++)
for(j=i; j<maxn; j++)
lcm[i][j]=lcm[j][i]=i*j/gcd(i,j);
while(~scanf("%d%d%d",&n,&m,&k))
{
cnt=0;
for(i=1; i<=m; i++)
{
if(m%i==0)
fac[cnt++]=i;
}
v=0;
memset(dp,0,sizeof dp);
for(i=0; i<cnt; i++)
dp[v][fac[i]][fac[i]]=1;
for(i=1; i<k; i++)
{
for(e=i; e<=n; e++)
for(f=0; f<cnt; f++)
dp[v^1][e][fac[f]]=0;
for(j=i; j<n; j++) //sum
{
for(p=0; p<cnt; p++)
{
mul=fac[p];
if(!dp[v][j][mul])
continue;
for(q=0; q<cnt; q++)
{
tt=j+fac[q];
if(tt>n)
break;
t=lcm[mul][fac[q]];
dp[v^1][tt][t]+=dp[v][j][mul];
dp[v^1][tt][t]%=mod;
}
}
}
v^=1;
}
printf("%d\n",dp[v][n][m]);
}
return 0;
}

170
HDOJ/4429_autoAC.cpp Normal file
View File

@ -0,0 +1,170 @@
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#define maxn 2220
using namespace std;
struct node{
int xl,yl;
int xr,yr;
int num;
int deep;
int flag;
int l;
int r;
int fat;
void output()
{
printf("%d %d %d %d\n",xl,yl,xr,yr);
printf("l = %d r = %d %d\n",l,r,fat);
}
bool inside(int x,int y)
{
return (xl<x && x<xr && yl<y&&y<yr);
}
bool in(int x,int y)
{
return (xl<=x && x<=xr && yl<=y&&y<=yr);
}
}seq[maxn];
int tot;
int total;
void build(int xl,int yl,int xr,int yr,int root,int fat)
{
seq[root].xl = xl;
seq[root].yl = yl;
seq[root].xr = xr;
seq[root].yr = yr;
seq[root].deep = 0;
seq[root].l = 0;
seq[root].r = 0;
seq[root].flag = 0;
seq[root].fat = fat;
seq[root].num = 0;
return ;
}
int n,m;
int get(int xl,int yl,int xr,int yr)
{
for (int i=tot;i>=1;i--)
if (seq[i].flag == 0 && seq[i].in(xl,yl) && seq[i].in(xr,yr))
return i;
return 0;
}
void update(int xl,int yl,int xr,int yr)
{
int root = get(xl,yl,xr,yr);
int xxl = seq[root].xl;
int yyl = seq[root].yl;
int xxr = seq[root].xr;
int yyr = seq[root].yr;
seq[root].flag = 1;
if (xl == xxl){
build(xxl,yyl,xr,yr,++tot,root);
seq[root].l = tot;
build(xl,yl,xxr,yyr,++tot,root);
seq[root].r = tot;
}else{
build(xxl,yyl,xr,yr,++tot,root);
seq[root].l = tot;
build(xl,yl,xxr,yyr,++tot,root);
seq[root].r = tot;
}
return ;
}
int find(int x,int y)
{
for (int i=tot;i>=1;i--)
if (seq[i].flag==0 && seq[i].inside(x,y))
return i;
return 0;
}
bool fff[maxn][maxn];
void dfs(int root,int niuniu)
{
fff[niuniu][root] = true;
if (seq[root].l > 0)
dfs(seq[root].l,niuniu);
if (seq[root].r > 0)
dfs(seq[root].r,niuniu);
return ;
}
void dfs1(int root,int deep)
{
int l = seq[root].l;
int r = seq[root].r;
seq[root].deep = deep;
if (l > 0){
dfs1(l,deep+1);
seq[root].num += seq[l].num;
}
if (r > 0){
dfs1(r,deep+1);
seq[root].num += seq[r].num;
}
return ;
}
void init()
{
total = 0;
for (int i=1;i<=tot;i++)
if (seq[i].flag==0){
seq[i].num = 1;
total++;
}
memset(fff,0,sizeof(fff));
dfs1(1,1);
for (int i=1;i<=tot;i++)
dfs(i,i);
return ;
}
int solve(int u,int v)
{
for (int i=tot;i>=1;i--)
{
if (fff[i][u] && fff[i][v]){
int res = total - seq[i].num + 1;
return res;
}
}
return 0;
}
int main()
{
int xl,yl,xr,yr;
while (scanf("%d%d%d%d",&xl,&yl,&xr,&yr)==4)
{
tot = 0;
build(xl,yl,xr,yr,++tot,0);
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
{
scanf("%d%d%d%d",&xl,&yl,&xr,&yr);
if (xl == xr)
{
if (yl > yr)
{
swap(yl,yr);
}
}
if (yl==yr)
{
if (xl > xr)
{
swap(xl,xr);
}
}
update(xl,yl,xr,yr);
}
init();
for (int i=1;i<=m;i++)
{
scanf("%d%d%d%d",&xl,&yl,&xr,&yr);
int p1 = find(xl,yl);
int p2 = find(xr,yr);
int ans = solve(p1,p2);
printf("%d\n",ans);
}
}
return 0;
}

50
HDOJ/4430_autoAC.cpp Normal file
View File

@ -0,0 +1,50 @@
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define ll __int64
ll n;
ll bin(ll s)
{
ll l = 2,r = n,mid,i;
while(l<=r)
{
mid = (l+r)/2;
ll sum = 1,ans = 0;;
for(i = 1; i<=s; i++)
{
if(n/sum<mid)
{
ans = n+1;
break;
}
sum*=mid;
ans+=sum;
if(ans>n)
break;
}
if(ans == n || ans == n-1) return mid;
else if(ans<n-1) l = mid+1;
else r = mid-1;
}
return -1;
}
int main()
{
ll s,k,l,r,mid,i;
while(~scanf("%I64d",&n))
{
l = 1,r = n-1;
for(i = 2; i<=45; i++)
{
k = bin(i);
if(k!=-1 && i*k<l*r)
{
l = i,r = k;
}
}
printf("%I64d %I64d\n",l,r);
}
return 0;
}

132
HDOJ/4431_autoAC.cpp Normal file
View File

@ -0,0 +1,132 @@
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<cmath>
#define LL long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
map<string,int>m;
void Init()
{
m["1m"]=1;m["2m"]=2;m["3m"]=3;
m["4m"]=4;m["5m"]=5;m["6m"]=6;
m["7m"]=7;m["8m"]=8;m["9m"]=9;
m["1s"]=10;m["2s"]=11;m["3s"]=12;
m["4s"]=13;m["5s"]=14;m["6s"]=15;
m["7s"]=16;m["8s"]=17;m["9s"]=18;
m["1p"]=19;m["2p"]=20;m["3p"]=21;
m["4p"]=22;m["5p"]=23;m["6p"]=24;
m["7p"]=25;m["8p"]=26;m["9p"]=27;
m["1c"]=28;m["2c"]=29;m["3c"]=30;
m["4c"]=31;m["5c"]=32;m["6c"]=33;
m["7c"]=34;
}
int c[35];int b[14];
int c13[13]={1,9,10,18,19,27,28,29,30,31,32,33,34};
bool judge1()
{
int c1=0,c2=0;
for(int i=0;i<13;i++)
{
if(c[c13[i]]) c1++;
if(c[c13[i]]>1) c2++;
}
if(c1==13&&c2==1) return true;
return false;
}
bool judge2()
{
for(int i=0;i<14;i+=2)
{
if((i&&b[i]==b[i-1])||b[i]!=b[i+1]) return false;
}
return true;
}
bool flag;
bool check(int idx)
{
if(idx>=1&&idx<=7) return true;
if(idx>=10&&idx<=16) return true;
if(idx>=19&&idx<=25) return true;
return false;
}
void dfs(int idx,int k1,int k2)
{
if(flag) return;
if(k1>4||k2>1) return ;
if(idx==14)
{
if(k1==4&&k2==1){flag=true;}
return ;
}
for(int i=1;i<=34;i++)
{
int mark=0;
if(c[i]>=3) {c[i]-=3;mark++;dfs(idx+3,k1+1,k2);c[i]+=3;}
if(c[i]>=2) {c[i]-=2;mark++;dfs(idx+2,k1,k2+1);c[i]+=2;}
if(check(i)&&c[i]&&c[i+1]&&c[i+2]) {c[i]--;c[i+1]--;c[i+2]--;mark++;dfs(idx+3,k1+1,k2);c[i]++;c[i+1]++;c[i+2]++;}
if(c[i]&&mark==0) return ;
if(c[i]) break;
}
}
int a[14];
int slove()
{
memcpy(b,a,sizeof(a));
sort(b,b+14);
mem(c,0);
for(int i=0;i<14;i++)
{
c[b[i]]++;
if(c[b[i]]>4)return false;
}
if(judge1()) return true;
if(judge2()) return true;
flag=false;
dfs(0,0,0);
return flag;
}
int main()
{
m.clear();
Init();
int t;
cin>>t;
while(t--)
{
for(int i=0;i<13;i++)
{
string s;
cin>>s;
a[i]=m[s];
}
int ans[50],cnt=0;
for(int i=1;i<=34;i++)
{
a[13]=i;
if(slove()) ans[cnt++]=i;
}
if(cnt==0) cout<<"Nooten"<<endl;
else
{
cout<<cnt;
for(int i=0;i<cnt;i++)
{
for(map<string,int>::iterator it=m.begin();it!=m.end();it++)
{
if(it->second==ans[i])
{
cout<<" "<<(it->first);
break;
}
}
}
cout<<endl;
}
}
return 0;
}

53
HDOJ/4432_autoAC.cpp Normal file
View File

@ -0,0 +1,53 @@
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
int bit[100];
int cnt;
void change(int n,int base)
{
cnt=0;
while(n)
{
bit[cnt++]=n%base;
n/=base;
}
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int sum=0;
int t=(int)sqrt(n*1.0);
for(int i=1;i<=t;i++)
{
if(n%i==0)
{
int tmp=i;
while(tmp)
{
sum+=((tmp%m)*(tmp%m));
tmp/=m;
}
tmp=n/i;
if(tmp==i)continue;
while(tmp)
{
sum+=((tmp%m)*(tmp%m));
tmp/=m;
}
}
}
change(sum,m);
for(int i=cnt-1;i>=0;i--)
{
if(bit[i]>9)printf("%c",bit[i]-10+'A');
else printf("%d",bit[i]);
}
printf("\n");
}
return 0;
}

58
HDOJ/4433_autoAC.cpp Normal file
View File

@ -0,0 +1,58 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define MAXN 1000+100
using namespace std;
int f[MAXN][12][12];
int times[12][12];
int n,m;
char s[MAXN],t[MAXN];
int a[MAXN],b[MAXN];
int min(int a,int b){return (a<b)?a:b;}
int main()
{
for (int i=0;i<10;++i){
int j=1;
while ((i+j)%10!=i) {
times[i][(i+j)%10]=j;
j++;
}
}
while (scanf("%s%s",s,t)!=EOF)
{
n=strlen(s);
for (int i=0;i<=n;++i)
for (int j=0;j<=10;++j)
for (int k=0;k<=10;++k) f[i][j][k]=n*10;
int ans=n*10;
for (int i=0;i<n;++i)
{
a[i+1]=s[i]-'0';
b[i+1]=t[i]-'0';
}
f[0][a[1]][a[2]]=0;
for (int i=0;i<n;++i)
for (int j=0;j<10;++j)
for (int k=0;k<10;++k)
if (f[i][j][k]<10*n)
{
int t=times[j][b[i+1]];
for (int l=0;l<=t;++l)
for(int p=0;p<=l;++p){
f[i+1][(k+l)%10][(a[i+3]+p)%10]=
min(f[i+1][(k+l)%10][(a[i+3]+p)%10],f[i][j][k]+t);
}
for (int l=0;l<=10-t;++l)
for(int p=0;p<=l;++p){
f[i+1][(k-l+10)%10][(a[i+3]-p+10)%10]=
min(f[i+1][(k-l+10)%10][(a[i+3]-p+10)%10],f[i][j][k]+10-t);
}
}
for (int i=0;i<10;++i)
for (int j=0;j<10;++j)
if(f[n][i][j]<ans) ans=f[n][i][j];
cout << ans << endl;
}
return 0;
}

94
HDOJ/4435_autoAC.cpp Normal file
View File

@ -0,0 +1,94 @@
#include <stdio.h>
#include <string.h>
#include <math.h>
#define clr(x)memset(x,0,sizeof(x))
const double eps = 1e-8;
#define maxn 130
struct pos
{
double x, y;
}p[maxn];
double d[maxn][maxn];
double dis(pos a, pos b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double D;
int n;
int res[maxn];
bool ok()
{
int tot = 1;
int q[maxn];
bool v[maxn];
int front = 0, rear = 0;
int u, i;
memset(v,false,sizeof(v));
v[0] = true;
q[rear++] = 0;
while(front < rear)
{
u = q[front++];
for(i=0; i<n; i++)
{
if(!v[i])
{
if(res[i] == 1)
{
if(d[u][i] < D+eps)
{
v[i] = true;
q[rear++] = i;
tot++;
}
}
else if(d[u][i] < D/2.0+eps)
{
v[i] = true;
tot++;
}
}
}
}
return tot == n ? true:false;
}
int main()
{
int i, j;
while(scanf("%d %lf",&n, &D)!=EOF)
{
for(i=0; i<n; i++)
scanf("%lf %lf",&p[i].x, &p[i].y);
for(i=0; i<n; i++)
for(j=0; j<n; j++)
d[i][j] = ceil(dis(p[i], p[j]));
for(i=0; i<n; i++)
res[i] = 1;
int f = 0;
for(i=n; i>0; i--)
{
res[i] = 0;
if(!ok()){
res[i] = 1;
if(i == n)
{ f = 1;
break;
}
}
}
if(f == 1)
{
printf("-1\n");
continue;
}
int flag = 0;
for(i=n-1; i>=0; i--)
if(flag || res[i] !=0)
{
printf("%d",res[i]);
flag = 1;
}
putchar('\n');
}
return 0;
}

95
HDOJ/4436_autoAC.cpp Normal file
View File

@ -0,0 +1,95 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#define maxn 300000
#define MOD 2012
using namespace std;
int wa[maxn],wb[maxn],wv[maxn];
int ran[maxn],height[maxn],wS[maxn],sa[maxn],P[maxn],Pow[maxn],V[maxn],
T[maxn],Len[maxn],S[maxn],arrive[maxn],n,len,value;
char s[maxn];
int cmp(int *r,int a,int b,int l) {return r[a]==r[b]&&r[a+l]==r[b+l];}
void da(int *r,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=0;i<m;i++) wS[i]=0;
for(i=0;i<n;i++) wS[x[i]=r[i]]++;
for(i=1;i<m;i++) wS[i]+=wS[i-1];
for(i=n-1;i>=0;i--) sa[--wS[x[i]]]=i;
for(j=1,p=1;p<n;j*=2,m=p)
{
for(p=0,i=n-j;i<n;i++) y[p++]=i;
for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=0;i<n;i++) wv[i]=x[y[i]];
for(i=0;i<m;i++) wS[i]=0;
for(i=0;i<n;i++) wS[wv[i]]++;
for(i=1;i<m;i++) wS[i]+=wS[i-1];
for(i=n-1;i>=0;i--) sa[--wS[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
return;
}
void calheight(int *r,int *sa,int n)
{
int i,j,k=0;
for(i=0;i<n;i++) ran[sa[i]]=i;
for(i=0;i<n;height[ran[i++]]=k)
if(ran[i])for(k?k--:0,j=sa[ran[i]-1];r[i+k]==r[j+k];k++);
return;
}
int get(int l,int r)
{
if(l>r) return 0;
int ans=(T[r+1]-T[l])%MOD;
ans-=V[l]*(Pow[r-l+1]);
ans%=MOD;
if (ans<0) ans+=MOD;
return ans;
}
int main()
{
Pow[0]=1;Pow[1]=10;
for (int i=2;i<200000;++i)
Pow[i]=(Pow[i-1]+1)*10%MOD;
while (scanf("%d",&n)!=EOF)
{
len = 0; value = 0;
for (int i=0;i < n ;++i){
scanf("%s",s);
Len[i]= strlen(s);
for (int j = 0 ; j < Len[i]; ++j){
S[len]=s[j]-'0'+1;
value = (value*10+S[len]-1)%MOD;
V[len+1]=value;
T[len+1]=(T[len]+value)%MOD;
P[len]=i;
len++;
}
S[len]=11;
value = (value*10+10)%MOD;
V[len+1]=value;
T[len+1]=(T[len]+value)%MOD;
P[len]=i;
len++;
arrive[i]=len-2;
}
S[len-1]=0;
da(S,sa,len,12);
calheight(S,sa,len);
int ans = 0;
for (int i = 0;i < len;++i)
if (S[i]%10 !=1){
if (i+height[ran[i]]<= arrive[P[i]]){
ans+=get(i,arrive[P[i]])-get(i,i+height[ran[i]]-1);
ans%=MOD;if (ans<0) ans+=MOD;
}
}
cout << ans << endl;
}
return 0;
}

21
HDOJ/4438_autoAC.cpp Normal file
View File

@ -0,0 +1,21 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
int X,Y;
double P,Q;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%lf%lf",&X,&Y,&P,&Q);
double ans1=(1-Q)*X+Q*(P*X+P*Y);
double ans2=Q*Y+(1-Q)*(P*X+P*Y);
if(ans1>ans2)printf("tiger %.4lf\n",ans1);
else printf("wolf %.4lf\n",ans2);
}
return 0;
}

104
HDOJ/4439_autoAC.cpp Normal file
View File

@ -0,0 +1,104 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int maxn = 1011;
const double eps = 1e-8;
const double pi = acos(-1.0);
inline double dcmp(double x) {return (x > eps) - (x < -eps);}
inline double Sqr(double x) {return x * x;}
struct Point
{
double x, y, v;
Point(){}
Point(double x_, double y_){x = x_, y = y_;}
double Dis(Point &b){return sqrt(Sqr(x - b.x) + Sqr(y - b.y));}
};
Point p[maxn], th;
int n;
void AngManage(double &x)
{
while(x + pi < -eps) x += pi + pi;
while(x - pi > -eps) x -= pi + pi;
}
inline double CounterAng(double s, double e)
{return e > s + eps ? e - s : pi + pi + e - s;}
struct Cov
{
double s, e;
void Read(double s_, double e_)
{
AngManage(s_), AngManage(e_);
s = s_, e = e_;
}
bool operator<(const Cov &b)const
{
if(!dcmp(s - b.s)) return CounterAng(s, e) > CounterAng(b.s, b.e);
return s < b.s;
}
} cover[maxn], cc[maxn];
int main()
{
int t, i, j, k, ans, cnt;
bool flag;
for(scanf("%d", &t); t --; )
{
flag = false;
scanf("%d", &n);
scanf("%lf%lf%lf", &th.v, &th.x, &th.y);
for(i = 0; i < n; ++ i)
{
scanf("%lf%lf%lf", &p[i].v, &p[i].x, &p[i].y);
if(p[i].v > th.v + eps || !dcmp(p[i].Dis(th))) flag = true;
}
if(flag) {printf("1\n"); continue;}
if(!dcmp(th.v)) {printf("0\n"); continue;}
for(i = 0; i < n; ++ i)
{
double ang = atan2(p[i].y - th.y, p[i].x - th.x);
if(p[i].v > th.v - eps)
{
cover[i].Read(ang - pi * 0.5, ang + pi * 0.5);
continue;
}
double c = p[i].Dis(th);
double T = sqrt(Sqr(c) / (Sqr(th.v) - Sqr(p[i].v)));
double cs = acos(c / T / th.v);
cover[i].Read(ang - cs, ang + cs);
}
sort(cover, cover + n);
for(i = k = 1; i < n; ++ i)
if(dcmp(cover[i].s - cover[i - 1].s)) cover[k ++] = cover[i];
n = k;
for(i = 0, ans = 0x3f3f3f3f; i < n; ++ i)
{
for(j = i, k = 0; ; j = (j + 1) % n)
{
cc[k].Read(cover[j].s - cover[i].s - pi, cover[j].e - cover[i].s - pi);
if(cc[k].s > cc[k].e) cc[k].e = pi + pi;
++ k;
if(j == (i + n - 1) % n) break;
}
double now = -pi - pi, nex = -pi;
flag = false;
for(j = cnt = 0; j < n; ++ j)
{
if(cc[j].e < now + eps) continue;
if(cc[j].s > nex + eps) break;
if(cc[j].s > now + eps)
{
++ cnt;
now = nex;
}
if(cc[j].e > nex + eps) nex = cc[j].e;
if(nex > pi - eps) {flag = true; break;}
}
if(flag) ans = min(ans, cnt);
}
if(ans == 0x3f3f3f3f) ans = 0;
printf("%d\n", ans);
}
return 0;
}

405
HDOJ/4441_autoAC.cpp Normal file
View File

@ -0,0 +1,405 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAXD 200010
typedef long long LL;
namespace SP
{
int T, node;
struct Splay
{
int ls, rs, pre, size, num[2], key;
LL sum;
void update(); void zig(int x); void zag(int x); void splay(int x, int goal);
void init()
{
ls = rs = pre = 0;
key = size = sum = num[0] = num[1] = 0;
}
}sp[MAXD];
void init()
{
T = 1, node = 2;
sp[0].init(), sp[1].init(), sp[2].init();
sp[1].rs = 2, sp[2].pre = 1;
sp[1].size = 2, sp[2].size = 1;
}
inline void Splay::update()
{
size = sp[ls].size + sp[rs].size + 1;
sum = sp[ls].sum + sp[rs].sum + key;
num[0] = sp[ls].num[0] + sp[rs].num[0] + (key < 0);
num[1] = sp[ls].num[1] + sp[rs].num[1] + (key > 0);
}
inline void Splay::zig(int x)
{
int y = rs, fa = pre;
rs = sp[y].ls, sp[rs].pre = x;
sp[y].ls = x, pre = y;
sp[y].pre = fa;
if(fa == 0) T = y;
else sp[fa].ls == x ? sp[fa].ls = y : sp[fa].rs = y;
update();
}
inline void Splay::zag(int x)
{
int y = ls, fa = pre;
ls = sp[y].rs, sp[ls].pre = x;
sp[y].rs = x, pre = y;
sp[y].pre = fa;
if(fa == 0) T = y;
else sp[fa].ls == x ? sp[fa].ls = y : sp[fa].rs = y;
update();
}
void Splay::splay(int x, int goal)
{
int y, z;
for(; pre != goal;)
{
y = pre;
if(sp[y].pre == goal) sp[y].rs == x ? sp[y].zig(y) : sp[y].zag(y);
else
{
z = sp[y].pre;
if(sp[z].rs == y)
{
if(sp[y].rs == x) sp[z].zig(z), sp[y].zig(y);
else sp[y].zag(y), sp[z].zig(z);
}
else
{
if(sp[y].ls == x) sp[z].zag(z), sp[y].zag(y);
else sp[y].zig(y), sp[z].zag(z);
}
}
}
update();
}
void newnode(int &cur)
{
cur = ++ node;
sp[cur].init();
}
int insert(int &cur, int fa, int key)
{
newnode(cur);
sp[cur].key = key, sp[cur].pre = fa, sp[cur].update();
return cur;
}
int find(int k)
{
if(sp[T].size < k) return -1;
int i = T;
for(;;)
{
int n = sp[sp[i].ls].size;
if(n + 1 == k) break;
if(k <= n) i = sp[i].ls;
else i = sp[i].rs, k -= n + 1;
}
return i;
}
}
int N, D, min[4 * MAXD], loc[MAXD][2];
void update(int i)
{
for(; i ^ 1; i >>= 1) min[i >> 1] = std::min(min[i], min[i ^ 1]);
}
void init()
{
for(D = 1; D < N + 2; D <<= 1);
memset(min, 0, sizeof(min[0]) * D * 2);
min[D] = 1, update(D);
SP::init();
}
int find()
{
int i = 1;
for(; i < D;)
{
if(min[i << 1] == 0) i <<= 1;
else i = i << 1 | 1;
}
return i - D;
}
void insert(int p)
{
int i = find(), x, y, t;
min[D + i] = 1, update(D + i);
x = SP::find(p + 1), SP::sp[x].splay(x, 0);
y = SP::find(p + 2), SP::sp[y].splay(y, x);
t = SP::insert(SP::sp[y].ls, y, i);
SP::sp[y].update(), SP::sp[x].update();
loc[i][1] = t, SP::sp[t].splay(t, 0);
int n = SP::sp[SP::sp[t].ls].num[1] + 1;
if(n > SP::sp[t].num[0])
{
int k = SP::sp[t].size;
x = SP::find(k - 1), SP::sp[x].splay(x, 0);
y = SP::find(k), SP::sp[y].splay(y, x);
t = SP::insert(SP::sp[y].ls, y, -i);
SP::sp[y].update(), SP::sp[x].update();
loc[i][0] = t;
}
else
{
int j = SP::T, k = n;
for(;;)
{
int n = SP::sp[SP::sp[j].ls].num[0], d = SP::sp[j].key < 0;
if(d == 1 && n + d == k) break;
if(k <= n) j = SP::sp[j].ls;
else j = SP::sp[j].rs, k -= n + d;
}
SP::sp[j].splay(j, 0);
x = SP::find(SP::sp[SP::sp[j].ls].size);
SP::sp[x].splay(x, j);
t = SP::insert(SP::sp[x].rs, x, -i);
SP::sp[x].update(), SP::sp[j].update();
loc[i][0] = t;
}
}
void remove(int i)
{
min[D + i] = 0, update(D + i);
for(int j = 0; j < 2; j ++)
{
SP::sp[loc[i][j]].splay(loc[i][j], 0);
int x, y, n = SP::sp[SP::sp[SP::T].ls].size + 1;
x = SP::find(n - 1), SP::sp[x].splay(x, 0);
y = SP::find(n + 1), SP::sp[y].splay(y, x);
SP::sp[y].ls = 0;
SP::sp[y].update(), SP::sp[x].update();
}
}
void query(int i)
{
int x = loc[i][1], y = loc[i][0];
SP::sp[x].splay(x, 0), SP::sp[y].splay(y, x);
printf("%I64d\n", SP::sp[SP::sp[y].ls].sum);
}
void solve()
{
char op[10];
for(int i = 0; i < N; i ++)
{
int k;
scanf("%s%d", op, &k);
if(op[0] == 'i') insert(k);
else if(op[0] == 'r') remove(k);
else query(k);
}
}
int main()
{
int t = 0;
while(scanf("%d", &N) == 1)
{
init();
printf("Case #%d:\n", ++ t);
solve();
}
return 0;
}#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAXD 200010
typedef long long LL;
namespace SP
{
int T, node;
struct Splay
{
int ls, rs, pre, size, num[2], key;
LL sum;
void update(); void zig(int x); void zag(int x); void splay(int x, int goal);
void init()
{
ls = rs = pre = 0;
key = size = sum = num[0] = num[1] = 0;
}
}sp[MAXD];
void init()
{
T = 1, node = 2;
sp[0].init(), sp[1].init(), sp[2].init();
sp[1].rs = 2, sp[2].pre = 1;
sp[1].size = 2, sp[2].size = 1;
}
inline void Splay::update()
{
size = sp[ls].size + sp[rs].size + 1;
sum = sp[ls].sum + sp[rs].sum + key;
num[0] = sp[ls].num[0] + sp[rs].num[0] + (key < 0);
num[1] = sp[ls].num[1] + sp[rs].num[1] + (key > 0);
}
inline void Splay::zig(int x)
{
int y = rs, fa = pre;
rs = sp[y].ls, sp[rs].pre = x;
sp[y].ls = x, pre = y;
sp[y].pre = fa;
if(fa == 0) T = y;
else sp[fa].ls == x ? sp[fa].ls = y : sp[fa].rs = y;
update();
}
inline void Splay::zag(int x)
{
int y = ls, fa = pre;
ls = sp[y].rs, sp[ls].pre = x;
sp[y].rs = x, pre = y;
sp[y].pre = fa;
if(fa == 0) T = y;
else sp[fa].ls == x ? sp[fa].ls = y : sp[fa].rs = y;
update();
}
void Splay::splay(int x, int goal)
{
int y, z;
for(; pre != goal;)
{
y = pre;
if(sp[y].pre == goal) sp[y].rs == x ? sp[y].zig(y) : sp[y].zag(y);
else
{
z = sp[y].pre;
if(sp[z].rs == y)
{
if(sp[y].rs == x) sp[z].zig(z), sp[y].zig(y);
else sp[y].zag(y), sp[z].zig(z);
}
else
{
if(sp[y].ls == x) sp[z].zag(z), sp[y].zag(y);
else sp[y].zig(y), sp[z].zag(z);
}
}
}
update();
}
void newnode(int &cur)
{
cur = ++ node;
sp[cur].init();
}
int insert(int &cur, int fa, int key)
{
newnode(cur);
sp[cur].key = key, sp[cur].pre = fa, sp[cur].update();
return cur;
}
int find(int k)
{
if(sp[T].size < k) return -1;
int i = T;
for(;;)
{
int n = sp[sp[i].ls].size;
if(n + 1 == k) break;
if(k <= n) i = sp[i].ls;
else i = sp[i].rs, k -= n + 1;
}
return i;
}
}
int N, D, min[4 * MAXD], loc[MAXD][2];
void update(int i)
{
for(; i ^ 1; i >>= 1) min[i >> 1] = std::min(min[i], min[i ^ 1]);
}
void init()
{
for(D = 1; D < N + 2; D <<= 1);
memset(min, 0, sizeof(min[0]) * D * 2);
min[D] = 1, update(D);
SP::init();
}
int find()
{
int i = 1;
for(; i < D;)
{
if(min[i << 1] == 0) i <<= 1;
else i = i << 1 | 1;
}
return i - D;
}
void insert(int p)
{
int i = find(), x, y, t;
min[D + i] = 1, update(D + i);
x = SP::find(p + 1), SP::sp[x].splay(x, 0);
y = SP::find(p + 2), SP::sp[y].splay(y, x);
t = SP::insert(SP::sp[y].ls, y, i);
SP::sp[y].update(), SP::sp[x].update();
loc[i][1] = t, SP::sp[t].splay(t, 0);
int n = SP::sp[SP::sp[t].ls].num[1] + 1;
if(n > SP::sp[t].num[0])
{
int k = SP::sp[t].size;
x = SP::find(k - 1), SP::sp[x].splay(x, 0);
y = SP::find(k), SP::sp[y].splay(y, x);
t = SP::insert(SP::sp[y].ls, y, -i);
SP::sp[y].update(), SP::sp[x].update();
loc[i][0] = t;
}
else
{
int j = SP::T, k = n;
for(;;)
{
int n = SP::sp[SP::sp[j].ls].num[0], d = SP::sp[j].key < 0;
if(d == 1 && n + d == k) break;
if(k <= n) j = SP::sp[j].ls;
else j = SP::sp[j].rs, k -= n + d;
}
SP::sp[j].splay(j, 0);
x = SP::find(SP::sp[SP::sp[j].ls].size);
SP::sp[x].splay(x, j);
t = SP::insert(SP::sp[x].rs, x, -i);
SP::sp[x].update(), SP::sp[j].update();
loc[i][0] = t;
}
}
void remove(int i)
{
min[D + i] = 0, update(D + i);
for(int j = 0; j < 2; j ++)
{
SP::sp[loc[i][j]].splay(loc[i][j], 0);
int x, y, n = SP::sp[SP::sp[SP::T].ls].size + 1;
x = SP::find(n - 1), SP::sp[x].splay(x, 0);
y = SP::find(n + 1), SP::sp[y].splay(y, x);
SP::sp[y].ls = 0;
SP::sp[y].update(), SP::sp[x].update();
}
}
void query(int i)
{
int x = loc[i][1], y = loc[i][0];
SP::sp[x].splay(x, 0), SP::sp[y].splay(y, x);
printf("%I64d\n", SP::sp[SP::sp[y].ls].sum);
}
void solve()
{
char op[10];
for(int i = 0; i < N; i ++)
{
int k;
scanf("%s%d", op, &k);
if(op[0] == 'i') insert(k);
else if(op[0] == 'r') remove(k);
else query(k);
}
}
int main()
{
int t = 0;
while(scanf("%d", &N) == 1)
{
init();
printf("Case #%d:\n", ++ t);
solve();
}
return 0;
}

35
HDOJ/4442_autoAC.cpp Normal file
View File

@ -0,0 +1,35 @@
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
struct A
{
__int64 a,b;
}E[100011];
int cmp(const void *a,const void *b)
{
struct A *c,*d;
c=(struct A *)a;
d=(struct A *)b;
__int64 t1,t2;
t1=(c->a)*(d->b);
t2=(d->a)*(c->b);
if(t1>t2) return 1;
else return -1;
}
int main()
{
int n;
int i,l;
int temp;
__int64 ans;
while(scanf("%d",&n),n)
{
for(i=0;i<n;i++) scanf("%I64d%I64d",&E[i].a,&E[i].b);
qsort(E,n,sizeof(E[0]),cmp);
ans=0;
temp=365*24*60*60;
for(i=0;i<n;i++) {ans+=E[i].a+ans*E[i].b;ans%=temp;}
printf("%I64d\n",ans);
}
return 0;
}

453
HDOJ/4443_autoAC.cpp Normal file
View File

@ -0,0 +1,453 @@
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;
#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif
#define FOR(i,a,b) for( int i = (a) ; i <= (b) ; i ++)
#define FF(i,a) for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a,b) for( int i = (a) ; i >= (b) ; i --)
#define S64(a) scanf(in64,&a)
#define SS(a) scanf("%d",&a)
#define LL(a) ((a)<<1)
#define RR(a) (((a)<<1)+1)
#define pb push_back
#define pf push_front
#define X first
#define Y second
#define CL(Q) while(!Q.empty())Q.pop()
#define MM(name,what) memset(name,what,sizeof(name))
#define MC(a,b) memcpy(a,b,sizeof(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-9;
const double pi = acos(-1.0);
const int maxn = 100011;
vector<int>v[maxn];
vector<int>g[maxn];
vector<int>s;
int n;
double nn;
int t[maxn];
int f[maxn];
int dfn[maxn];
int low[maxn];
int df,nf;
bool ins[maxn];
int tot;
bool hash[maxn];
int yes[33];
double go[33][33];
double back[33][33];
double dpx[maxn];
double dpy[maxn];
bool vis[maxn];
int cnt[maxn];
double dp[maxn];
double dps[maxn];
vector<double>ans;
void tarjan(int now)
{
low[now] = dfn[now] = df++;
s.push_back(now);
ins[now]=true;
int to;
for(int i=0;i<g[now].size();i++)
{
to = g[now][i];
if(!dfn[to])
{
t[to]=now;
tarjan(to);
low[now] = min(low[to],low[now]);
}
else if(to!=t[now])
{
low[now]=min(dfn[to],low[now]);
}
}
if(dfn[now] == low[now])
{
while(s.back() != now)
{
to = s.back();
f[to] = nf;
s.pop_back();
ins[to]=false;
}
to = s.back();
s.pop_back();
ins[to]=false;
f[to] = nf++;
}
return ;
}
void dfs(int now)
{
vis[now] = true;
int to;
for(int i=0;i<g[now].size();i++)
{
to = g[now][i];
if(!ins[to] && !vis[to])
{
cnt[now]++;
}
}
for(int i=0;i<g[now].size();i++)
{
to = g[now][i];
if(!ins[to] && !vis[to])
{
dfs(to);
dps[now]+=dpx[to];
if(ins[now])
{
dpx[now]+=2.0*dpx[to]/double(cnt[now]+1.0);
}
else
{
dpx[now]+=dpx[to]/double(cnt[now]);
}
}
}
if(ins[now])
{
double temp = 2.0/double(cnt[now]+2.0);
dpx[now] += nn*temp;
}
else
{
double temp = 1.0/double(cnt[now]+1.0);
dpx[now] += nn*temp;
}
return ;
}
void df2(int now)
{
vis[now]=true;
int to;
for(int i=0;i<g[now].size();i++)
{
to = g[now][i];
if(!ins[to] && !vis[to])
{
dpy[to] = dpy[now]/double(cnt[now]);
dpy[to] += nn*double(cnt[to])/double(cnt[to]+1.0);
if(ins[now])
{
dpy[to]+=(dps[now]-dpx[to])/(cnt[now]+1.0);
}
else
{
dpy[to]+=(dps[now]-dpx[to])/double(cnt[now]);
}
df2(to);
}
}
if(!cnt[now])
{
dp[now] = dpy[now];
}
return ;
}
void find()
{
int now=s[0];
int to;
vis[now] = true;
bool ok;
while(true)
{
ok = false;
for(int i=0;i<g[now].size();i++)
{
to = g[now][i];
if(ins[to] && !vis[to])
{
vis[to]=true;
s.pb(to);
now = to;
ok = true;
break;
}
}
if(!ok) break;
}
return ;
}
void start()
{
for(int i=0;i<=n;i++)
{
f[i]=0;
t[i]=0;
dfn[i]=0;
low[i]=0;
ins[i]=false;
}
s.clear();
df=nf=1;
tarjan(1);
nf--;
for(int i=1;i<=n;i++)
{
v[f[i]].pb(i);
}
int tv;
for(int i=1;i<=nf;i++)
{
if(v[i].size()>1)
{
tv=i;
break;
}
}
s.clear();
tot = v[tv].size();
for(int i=0;i<=n;i++)
{
ins[i]=false;
vis[i]=false;
}
for(int i=0;i<v[tv].size();i++)
{
ins[v[tv][i]] = true;
}
s.pb(v[tv][0]);
find();
int now,to;
MM(yes,0);
for(int i=0;i<s.size();i++)
{
now = s[i];
for(int j=0;j<g[now].size();j++)
{
to = g[now][j];
if(!ins[to])
{
yes[i]++;
}
}
}
MM(go,0);
MM(back,0);
int ss = s.size();
for(int i=0;i<ss;i++)
{
if(yes[i])
{
go[i][(i+1)%ss]=1.0/(yes[i]+1.0);
back[i][(i-1+ss)%ss]=1.0/(yes[i]+1.0);
}
else
{
go[i][(i+1)%ss]=1.0;
back[i][(i-1+ss)%ss]=1.0;
}
}
for(int u=2;u<ss;u++)
{
for(int i=0;i<ss;i++)
{
go[i][(i+u)%ss]=go[i][(i+u-1)%ss]*go[(i+u-1)%ss][(i+u)%ss];
back[i][(i-u+ss)%ss]=back[i][(i-(u-1)+ss)%ss]*back[(i-(u-1)+ss)%ss][(i-u+ss)%ss];
}
}
for(int i=0;i<=n;i++)
{
vis[i]=false;
dpx[i]=0;
dpy[i]=0;
dp[i]=0;
dps[i]=0;
cnt[i]=0;
}
double temp;
for(int i=0;i<ss;i++)
{
if(yes[i])
{
dfs(s[i]);
}
}
for(int i=0;i<ss;i++)
{
if(!yes[i]) continue;
for(int j=0;j<ss;j++)
{
if(j==i)
{
temp = double(yes[i])/(yes[i]+2.0);
dpy[s[j]]+=temp*nn;
continue;
}
if(!yes[j]) continue;
temp = dpx[s[i]]*go[i][j];
temp *= yes[i]+1.0;
temp /= 2.0;
if((i-j+ss)%ss==1)
{
dpy[s[j]]+=temp;
}
else
{
temp/=yes[j]+1.0;
temp*=yes[j];
dpy[s[j]]+=temp;
}
temp = dpx[s[i]]*back[i][j];
temp *= yes[i]+1.0;
temp /= 2.0;
if((i-j+ss)%ss==ss-1)
{
dpy[s[j]]+=temp;
}
else
{
temp/=yes[j]+1.0;
temp*=yes[j];
dpy[s[j]]+=temp;
}
}
}
for(int i=0;i<ss;i++)
{
if(yes[i]) continue;
for(int j=0;j<ss;j++)
{
if(yes[j])
{
temp = nn*go[i][j]*0.5;
if((i-j+ss)%ss==1)
{
dpy[s[j]]+=temp;
}
else
{
temp/=yes[j]+1.0;
temp*=yes[j];
dpy[s[j]]+=temp;
}
temp = nn*back[i][j]*0.5;
if((i-j+ss)%ss==ss-1)
{
dpy[s[j]]+=temp;
}
else
{
temp/=yes[j]+1.0;
temp*=yes[j];
dpy[s[j]]+=temp;
}
}
}
}
for(int i=0;i<=n;i++)
{
vis[i]=0;
}
for(int i=0;i<ss;i++)
{
if(yes[i])
{
df2(s[i]);
}
}
for(int i=0;i<ss;i++) if(yes[i])
{
if(!yes[(i+1)%ss])
{
temp = dpx[s[i]]*back[i][(i+1)%ss];
temp *= yes[i]+1.0;
temp /= 2.0;
dp[s[(i+1)%ss]] += temp;
}
if(!yes[(i-1+ss)%ss])
{
temp = dpx[s[i]]*go[i][(i-1+ss)%ss];
temp *= yes[i]+1.0;
temp /= 2.0;
dp[s[(i-1+ss)%ss]] += temp;
}
}
for(int i=0;i<ss;i++) if(!yes[i])
{
if(!yes[(i+1)%ss])
{
dp[s[(i+1)%ss]] += nn*back[i][(i+1)%ss]*0.5;
}
if(!yes[(i-1+ss)%ss])
{
dp[s[(i-1+ss)%ss]] += nn*go[i][(i-1+ss)%ss]*0.5;
}
}
ans.clear();
for(int i=1;i<=n;i++)
{
if(dp[i]>eps)
{
ans.pb(dp[i]);
}
}
for(int i=1;i<=5;i++)
{
ans.pb(0.0);
}
sort(ans.begin(),ans.end());
return ;
}
int main()
{
while(cin>>n)
{
if(!n) return 0;
nn = n;
nn = 1.0/nn;
for(int i=0;i<=n;i++)
{
v[i].clear();
g[i].clear();
}
int now,to;
for(int i=1;i<=n;i++)
{
SS(now);
SS(to);
g[now].pb(to);
g[to].pb(now);
}
start();
double re=0.0;
int ix = ans.size()-1;
for(int i=1;i<=5;i++)
{
re+=ans[ix--];
}
printf("%.5lf\n",re);
}
return 0;
}

161
HDOJ/4444_autoAC.cpp Normal file
View File

@ -0,0 +1,161 @@
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <map>
#include <iostream>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N=109;
bool visit[N<<1][N<<1];
map<int,int> mpx;
map<int,int> mpy;
int n;
struct cpoint{
int x,y;
void get()
{
scanf("%d%d",&x,&y);x<<=1;y<<=1;
}
} st,en,re[59][2];
int hashx[N<<1],hashy[N<<1],cnx,cny;
bool can[N][N][4][4];
void init()
{
mpx.clear();mpy.clear();
memset(can,false,sizeof(can));
cnx=0;cny=0;
for(int i=0;i<n;i++)
{
hashx[cnx++] = re[i][0].x;
hashx[cnx++] = re[i][0].x+1;
hashx[cnx++] = re[i][1].x;
hashx[cnx++] = re[i][1].x+1;
hashy[cny++] = re[i][0].y;
hashy[cny++] = re[i][0].y+1;
hashy[cny++] = re[i][1].y;
hashy[cny++] = re[i][1].y+1;
}
sort(hashx,hashx+cnx);sort(hashy,hashy+cny);
int tx=0,ty=0;
mpx[hashx[0]] = 0;
mpy[hashy[0]] = 0;
for(int i=1;i<cnx;i++)
if(hashx[tx]!=hashx[i])
{
hashx[++tx] = hashx[i];
mpx[hashx[i]] = tx;
}
for(int i=1;i<cny;i++)
if(hashy[ty]!=hashy[i])
{
hashy[++ty] = hashy[i];
mpy[hashy[i]] = ty;
}
cnx = tx+1,cny = ty+1;
memset(visit,true,sizeof(visit));
for(int i=1;i<n;i++)
{
int X = mpx[re[i][1].x],Y = mpy[re[i][1].y];
for(int j=mpx[re[i][0].x]+1;j<=X;j++)
for(int k=mpy[re[i][0].y]+1;k<=Y;k++)
visit[j][k] = 0;
}
cnx>>=1;cny>>=1;
for(int i=0;i<cnx;i++)
for(int j=0;j<cny;j++)
{
if((visit[i<<1][j<<1]&&visit[i<<1][j<<1|1])||(visit[i<<1|1][j<<1]&&visit[i<<1|1][j<<1|1]))
can[i][j][3][3] = can[i][j][1][1] = 1;
if((visit[i<<1][j<<1]&&visit[i<<1|1][j<<1])||(visit[i<<1][j<<1|1]&&visit[i<<1|1][j<<1|1]))
can[i][j][2][2] = can[i][j][0][0] = 1;
if(visit[i<<1][j<<1])
can[i][j][2][3] = can[i][j][1][0] = 1;
if(visit[i<<1][j<<1|1])
can[i][j][2][1] = can[i][j][3][0] = 1;
if(visit[i<<1|1][j<<1])
can[i][j][0][3] = can[i][j][1][2] = 1;
if(visit[i<<1|1][j<<1|1])
can[i][j][0][1] = can[i][j][3][2] = 1;
if(visit[i<<1][j<<1]&&visit[i<<1][j<<1|1]&&visit[i<<1|1][j<<1])
can[i][j][0][1] = can[i][j][3][2] = 1;
if(visit[i<<1][j<<1]&&visit[i<<1][j<<1|1]&&visit[i<<1|1][j<<1|1])
can[i][j][1][2] = can[i][j][0][3] = 1;
if(visit[i<<1][j<<1]&&visit[i<<1|1][j<<1]&&visit[i<<1|1][j<<1|1])
can[i][j][2][1] = can[i][j][3][0] = 1;
if(visit[i<<1][j<<1|1]&&visit[i<<1|1][j<<1]&&visit[i<<1|1][j<<1|1])
can[i][j][1][0] = can[i][j][2][3] = 1;
}
}
queue<int> que;
bool oor(int x,int y)
{
if(x<0||x>=cnx) return false;
if(y<0||y>=cny) return false;
return true;
}
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
int dis[N][N][4];
bool in[N][N][4];
void solve()
{
memset(in,0,sizeof(in));
memset(dis,INF,sizeof(dis));
while(!que.empty()) que.pop();
int stx=mpx[st.x]/2,sty=mpy[st.y]/2,enx=mpx[en.x]/2,eny=mpy[en.y]/2;
for(int i=0;i<4;i++)
{
que.push((stx<<12)|(sty<<2)|i);
dis[stx][sty][i]= 0;
}
while(!que.empty())
{
int e = que.front(); que.pop();
int ed,ex,ey;
ed = e&3;e>>=2;
ey = e&1023;e>>=10;
ex = e;
in[ex][ey][ed] = false;
for(int i=0;i<4;i++)
{
int tx=ex+dx[i],ty=ey+dy[i];
if(i==ed&&oor(tx,ty)&&can[ex][ey][ed][i]&&dis[tx][ty][i]>dis[ex][ey][ed])
{
dis[tx][ty][i]=dis[ex][ey][ed];
if(!in[tx][ty][i]) que.push((tx<<12)|(ty<<2)|i);
}
else if(i!=ed&&oor(tx,ty)&&can[ex][ey][ed][i]&&dis[tx][ty][i]>dis[ex][ey][ed]+1)
{
dis[tx][ty][i]=dis[ex][ey][ed]+1;
if(!in[tx][ty][i]) que.push((tx<<12)|(ty<<2)|i);
}
}
}
int ans=INF;
for(int i=0;i<4;i++)
ans = min(dis[enx][eny][i],ans);
printf("%d\n",ans==INF?-1:ans);
}
int main()
{
while(~scanf("%d%d%d%d",&st.x,&st.y,&en.x,&en.y))
{
if(st.x==0&&st.y==0&&en.x==0&&en.y==0) break;
st.x<<=1;st.y<<=1;en.x<<=1;en.y<<=1;
re[0][0] = st;
re[0][1] = en;
scanf("%d",&n);n+=1;
for(int i=1;i<n;i++)
{
re[i][0].get(),re[i][1].get();
if(re[i][0].x>re[i][1].x) swap(re[i][0].x,re[i][1].x);
if(re[i][0].y>re[i][1].y) swap(re[i][0].y,re[i][1].y);
}
init();
solve();
}
return 0;
}

37
HDOJ/4445_autoAC.cpp Normal file
View File

@ -0,0 +1,37 @@
#include<cstdio>
#include<cmath>
const double PI=acos(-1.0);
const double eps=PI/276;
int n;
double h,l1,r1,l2,r2,v[200];
int cal(double x)
{
int i,c=0;
for(i=0;i<n;i++)
{
double v0=v[i]*sin(x),v1=v[i]*cos(x);
double t=(sqrt(2*9.8*h+v1*v1)-v1)/9.8;
double s=v0*t;
if(s>=l2&&s<=r2) return 0;
if(s>=l1&&s<=r1) c++;
}
return c;
}
int main()
{
int i,ans;
while(scanf("%d",&n),n)
{
scanf("%lf%lf%lf%lf%lf",&h,&l1,&r1,&l2,&r2);
for(i=0;i<n;i++)scanf("%lf",&v[i]);
double j=0;ans=0;
while(j<PI)
{
int tmp=cal(j);
if(tmp>ans) ans=tmp;
j+=eps;
}
printf("%d\n",ans);
}
return 0;
}

127
HDOJ/4446_autoAC.cpp Normal file
View File

@ -0,0 +1,127 @@
#include <iostream>
#include <queue>
#include <cstring>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int ma=1000010;
const int inf=1<<25;
int n;
int c[ma<<2];
int rc[ma];
int add[ma<<2];
int rrt;
void pushup(int rt)
{
c[rt]=min(c[rt<<1],c[rt<<1|1]);
}
void pushdown(int rt,int m)
{
if(add[rt])
{
add[rt<<1]+=add[rt];
add[rt<<1|1]+=add[rt];
c[rt<<1]+=add[rt];
c[rt<<1|1]+=add[rt];
add[rt]=0;
}
}
void build(int l,int r,int rt)
{
add[rt]=0;
if(l==r)
{
scanf("%d",&c[rt]);
return ;
}
int m=(l+r)>>1;
build (lson);
build (rson);
pushup(rt);
}
int query(int l,int r,int rt)
{
if(l==r){rrt=rt;return l;}
pushdown(rt,r-l+1);
int m=(l+r)>>1;
if(c[rt<<1]<=c[rt<<1|1])
return query(lson);
else return query(rson);
}
void update(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
add[rt]--;
c[rt]--;
return ;
}
pushdown(rt,r-1+1);
int m=(l+r)>>1;
if(L<=m) update(L,R,lson);
if(m<R)update(L,R,rson);
pushup(rt);
}
void update2(int p,int l,int r,int rt)
{
if(l==r)
{
c[rt]=inf;
return ;
}
int m=(l+r)>>1;
if(p<=m)update2(p,lson);
else update2(p,rson);
pushup(rt);
}
int main()
{
while(scanf("%d",&n),n)
{
int id=0;
int flag=0;
build(1,n,1);
queue <int> a;
queue <int> b;
while(a.size()+b.size()!=2*n)
{
int qq=query(1,n,1);
if(c[rrt]<0)
{
flag=1;break;
}
if(c[rrt]==0)
{
a.push(qq);
b.push(-qq);
if(qq!=1)
update(1,qq-1,1,n,1);
update2(qq,1,n,1);
}
else
{
if(b.size()==0){flag=1;break;}
int t=b.front();
if(-t<n)
update(-t+1,n,1,n,1);
b.pop();
a.push(t);
}
}
if(flag)
{
printf("Impossible\n");
}else{
int d[200010];
int i=0;
while(a.size()!=0)
{d[i++]=a.front();a.pop();}
while(b.size()!=0)
{d[i++]=b.front();b.pop();}
for(i=2*n-1;i>=0;i--)
printf("%d ",d[i]);
puts("");
}
}
return 0;
}

124
HDOJ/4447_autoAC.cpp Normal file
View File

@ -0,0 +1,124 @@
#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 1110
vector <int> v[maxn];
struct polynomial
{
int coe[maxn];
int len;
polynomial()
{
memset(coe, 0, sizeof(coe));
len = 1;
}
void output()
{
printf("(");
for(int i = len - 1; i >= 0; i--)
{
if(coe[i] == 0) continue;
if(i == 0)
{
if(coe[i] > 0) printf("+");
printf("%d", coe[i]);
continue;
}
if(coe[i] > 0 && i != len - 1) printf("+");
if(coe[i] == -1) printf("-");
if(abs(coe[i]) != 1) printf("%d", coe[i]);
if(i > 1)
printf("x^%d", i);
else printf("x");
}
printf(")");
return;
}
polynomial operator / (const polynomial pol);
};
polynomial polynomial :: operator / (const polynomial pol)
{
polynomial ret;
ret.len = len - pol.len + 1;
for(int i = len - 1; i >= pol.len - 1; i--)
{
int tim = coe[i] / pol.coe[pol.len - 1];
if(tim != 0)
{
for(int j = 0; j < pol.len; j++)
coe[i - j] -= tim*pol.coe[pol.len - 1 - j];
ret.coe[i - pol.len + 1] = tim;
}
}
return ret;
}
polynomial p[maxn];
bool cmp(int i, int j)
{
if(p[i].len != p[j].len) return p[i].len < p[j].len;
for(int k = p[i].len - 1; k >= 0; k--)
if(p[i].coe[k] != p[j].coe[k])
{
if(abs(p[i].coe[k]) == abs(p[j].coe[k]))
return p[i].coe[k] < 0;
else return abs(p[i].coe[k]) < abs(p[j].coe[k]);
}
return false;
}
int main()
{
p[1].coe[0] = -1;
p[1].coe[1] = 1;
p[1].len = 2;
for(int i = 2; i <= 1100; i++)
{
p[i].coe[0] = -1;
p[i].coe[i] = 1;
p[i].len = i + 1;
p[i] = p[i]/p[1];
v[i].push_back(1);
for(int j = 2; j*j <= i; j++)
{
if(i % j == 0)
{
p[i] = p[i]/p[j];
v[i].push_back(j);
if(j*j != i) p[i] = p[i]/p[i / j], v[i].push_back(i / j);
}
}
v[i].push_back(i);
}
int n;
while(scanf("%d", &n), n)
{
if(n == 1)
{
printf("x-1\n");
continue;
}
sort(v[n].begin(), v[n].end(), cmp);
for(int i = 0, sz = v[n].size(); i < sz; i++)
p[v[n][i]].output();
printf("\n");
}
return 0;
}

323
HDOJ/4449_autoAC.cpp Normal file
View File

@ -0,0 +1,323 @@
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include <algorithm>
using namespace std;
#define INF (1<<30)
#define PI acos(-1)
#define SET(a,b) memset(a,b,sizeof(a))
#define M 10010
#define N 505
#define EPS 1e-8
struct pt
{
double x,y,z;
pt() {}
pt(double _x,double _y,double _z): x(_x),y(_y),z(_z) {}
pt operator - (const pt p1)
{
return pt(x-p1.x,y-p1.y,z-p1.z);
}
pt operator * (pt p)
{
return pt(y*p.z-z*p.y,z*p.x-x*p.z,x*p.y-y*p.x);
}
double operator ^ (pt p)
{
return x*p.x+y*p.y+z*p.z;
}
};
struct _3DCH
{
struct fac
{
int a,b,c;
bool ok;
};
int n;
pt P[N];
int cnt;
fac F[N*8];
int to[N][N];
double vlen(pt a)
{
return sqrt(a.x*a.x+a.y*a.y+a.z*a.z);
}
double area(pt a,pt b,pt c)
{
return vlen((b-a)*(c-a));
}
double volume(pt a,pt b,pt c,pt d)
{
return (b-a)*(c-a)^(d-a);
}
double ptof(pt &p,fac &f)
{
pt m=P[f.b]-P[f.a],n=P[f.c]-P[f.a],t=p-P[f.a];
return (m*n)^t;
}
void deal (int p,int a,int b)
{
int f=to[a][b];
fac add;
if(F[f].ok)
if(ptof(P[p],F[f])>EPS) dfs(p,f);
else
{
add.a=b; add.b=a; add.c=p; add.ok=1;
to[p][b]=to[a][p]=to[b][a]=cnt;
F[cnt++]=add;
}
}
void dfs(int p,int cur)
{
F[cur].ok=0;
deal(p,F[cur].b,F[cur].a);
deal(p,F[cur].c,F[cur].b);
deal(p,F[cur].a,F[cur].c);
}
bool same(int s,int t)
{
pt &a=P[F[s].a],&b=P[F[s].b],&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 construct()
{
cnt=0;
if(n<4) return;
bool sb=1;
for(int i=1;i<n;i++)
if(vlen(P[0]-P[i])>EPS)
{
swap(P[1],P[i]);
sb=0;
break;
}
if(sb) return; sb=1;
for(int i=2;i<n;i++)
if(vlen((P[0]-P[1])*(P[1]-P[i]))>EPS)
{
swap(P[2],P[i]);
sb=0;
break;
}
if(sb) return; sb=1;
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]);
sb=0;
break;
}
if(sb) return;
fac add;
for(int i=0;i<4;i++)
{
add.a=(i+1)%4;
add.b=(i+2)%4;
add.c=(i+3)%4;
add.ok=1;
if(ptof(P[i],add)>0) swap(add.b,add.c);
to[add.a][add.b]=to[add.b][add.c]=to[add.c][add.a]=cnt;
F[cnt++]=add;
}
for(int i=4;i<n;i++)
for(int j=0;j<cnt;j++)
if(F[j].ok && ptof(P[i],F[j])>EPS)
{
dfs(i,j);
break;
}
int tmp=cnt;
cnt=0;
for(int i=0;i<tmp;i++)
if(F[i].ok) F[cnt++]=F[i];
}
double ptoface(pt 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])));
}
void get_panel(double &a,double &b,double &c,double &d,pt p1,pt p2,pt p3)
{
a=(p2.y-p1.y)*(p3.z-p1.z)-(p2.z-p1.z)*(p3.y-p1.y);
b=(p2.z-p1.z)*(p3.x-p1.x)-(p2.x-p1.x)*(p3.z-p1.z);
c=(p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);
d=-(a*p1.x+b*p1.y+c*p1.z);
}
inline double dist(pt p1, pt p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)+(p1.z-p2.z)*(p1.z-p2.z));
}
}hull;
inline int sign(double d)
{
if(d>EPS) return 1;
if(d<-EPS) return -1;
return 0;
}
struct point
{
double x,y,z;
point(double x=0,double y=0,double z=0): x(x),y(y),z(z) {}
point operator - (point p)
{
return point(x-p.x,y-p.y,z-p.z);
}
point operator + (point p)
{
return point(x+p.x,y+p.y,z+p.z);
}
point operator / (double len)
{
return point(x/len,y/len,z/len);
}
point operator * (double len)
{
return point(x*len,y*len,z*len);
}
double operator ^ (point p)
{
return x*p.x+y*p.y+z*p.z;
}
point operator * (point p)
{
return point(y*p.z-z*p.y,z*p.x-x*p.z,x*p.y-y*p.x);
}
double getlen()
{
return sqrt(x*x+y*y+z*z);
}
}con[N],ps[N],org;
double a,b,c,d;
int n;
inline point get_point(point u,point v,point p)
{
double a,b,t;
a=(v.x-u.x)*(v.x-u.x)+(v.y-u.y)*(v.y-u.y)+(v.z-u.z)*(v.z-u.z);
b=(p.x-u.x)*(v.x-u.x)+(p.y-u.y)*(v.y-u.y)+(p.z-u.z)*(v.z-u.z);
t=b/a;
point ans;
ans.x=v.x*t+(1-t)*u.x;
ans.y=v.y*t+(1-t)*u.y;
ans.z=v.z*t+(1-t)*u.z;
return ans;
}
inline double dist(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));
}
point rotate(point u,point v,point p,double ang)
{
point root=get_point(u,v,p),e,r;
point ans;
e=(v-u)/dist(u,v);
r=p-root;
e=e*r;
ans=r*cos(ang)+e*sin(ang)+root;
return ans;
}
double inter_pro(point u1,point v1,point u2,point v2)
{
return (v1.x-u1.x)*(v2.y-u2.y)-(v1.y-u1.y)*(v2.x-u2.x);
}
bool cmp(point a,point b)
{
return a.y<b.y || a.y==b.y && a.x<b.x;
}
void Graham(point* pol, int n, point* con, int& len){
sort(pol, pol+ n, cmp);
con[0] = pol[0];
con[1] = pol[1];
int top = 1;
for(int i = 2; i < n; i++){
while(top > 0 && inter_pro(con[top - 1], con[top], con[top - 1], pol[i]) <= 0) top--;
con[++top] = pol[i];
}
int tmp = top;
for(int i = n - 2; i >= 0; i--){
while(top > tmp && inter_pro(con[top - 1], con[top], con[top - 1], pol[i]) <= 0) top--;
con[++top] = pol[i];
}
len = top;
}
double polyArea(point* ps, int n){
ps[n] = ps[0];
int i;
double ans=0;
for(i = 0; i < n; i++){
ans += (ps[i].x*ps[i+1].y-ps[i].y*ps[i+1].x);
}
return fabs(ans/2.0);
}
double solve()
{
point tp(0,0,0),end(0,0,1),vec;
double ang;
int i,cn;
if(sign(a)) tp.x=d/a;
else if(sign(b)) tp.y=d/b;
else if(sign(c)) tp.z=d/c;
ps[n+1]=tp;
vec=(point(a,b,c))*(end);
if(sign(vec.x)==0) vec.x=0;
if(sign(vec.y)==0) vec.y=0;
if(sign(vec.z)==0) vec.z=0;
ang=(a*end.x+b*end.y+c*end.z)/(point(a,b,c).getlen());
ang=acos(ang);
if(sign(ang)!=-0 && sign(ang-PI)!=0)
for(i=0;i<n;i++)
ps[i]=rotate(org,vec,ps[i],ang);
for(int i=0;i<n;i++) ps[i].z=0;
Graham(ps,n,con,cn);
double ans=fabs(polyArea(con,cn));
return ans;
}
int main()
{
while (scanf("%d",&hull.n) && hull.n)
{
for(int i=0;i<hull.n;i++)
scanf("%lf%lf%lf",&hull.P[i].x,&hull.P[i].y,&hull.P[i].z);
if(n==1)
{
printf("0.000 0.000\n");
continue;
}
if(n==2)
{
printf("%.3lf 0.000\n",hull.dist(hull.P[0],hull.P[1]));
continue;
}
hull.construct();
n=hull.n;
double ans1=0,ans2=1e100;
if(hull.cnt==0)
{
double max0=0;
for(int i=0;i<hull.n;i++)
for(int j=0;j<hull.n;j++)
max0=max(max0,hull.dist(hull.P[i],hull.P[j]));
printf("%.3lf 0.000\n",max0);
continue;
}
n=hull.n;
for(int i=0;i<hull.cnt;i++)
{
for(int j=0;j<hull.n;j++)
ps[j].x=hull.P[j].x,
ps[j].y=hull.P[j].y,
ps[j].z=hull.P[j].z;
hull.get_panel(a,b,c,d,hull.P[hull.F[i].a],hull.P[hull.F[i].b],hull.P[hull.F[i].c]);
d=-d;
double dist=0;
for(int j=0;j<hull.n;j++)
dist=max(dist,hull.ptoface(hull.P[j],i));
double ans=solve();
if(dist>ans1) ans1=dist,ans2=ans;
else if(fabs(dist-ans1)<EPS) if(ans2>ans) ans1=dist,ans2=ans;
}
printf("%.3f %.3f\n",ans1,ans2);
}
return 0;
}

12
HDOJ/4450_autoAC.cpp Normal file
View File

@ -0,0 +1,12 @@
#include<cstdio>
int main()
{
int n,i,s,a;
while(scanf("%d",&n),n)
{
s=0;
while(n--){scanf("%d",&a);s+=a*a;}
printf("%d\n",s);
}
return 0;
}

31
HDOJ/4451_autoAC.cpp Normal file
View File

@ -0,0 +1,31 @@
#include<cstdio>
#include<cstring>
int v1[1001],v2[1001];
int main()
{
int n,m,k,x,y,p;
char s1[10],s2[10];
while(scanf("%d%d%d",&n,&m,&k),n||m||k)
{
memset(v1,0,sizeof(v1));
memset(v2,0,sizeof(v2));
int ans=n*m*k;
scanf("%d",&p);
while(p--)
{
scanf("%s%d%s%d",s1,&x,s2,&y);
if(s1[0]=='c')
{
v1[y]++;
ans-=(k-v2[y]);
}
else
{
v2[x]++;
ans-=(n-v1[x]);
}
}
printf("%d\n",ans);
}
return 0;
}

129
HDOJ/4452_autoAC.cpp Normal file
View File

@ -0,0 +1,129 @@
#include<iostream>
using namespace std;
int f[4][2]={-1,0,0,-1,1,0,0,1};
int n;
int x_1,y_1,x_2,y_2;
int dis_1,speed_1,time_1;
int dis_2,speed_2,time_2;
inline int Get_f(char f)
{
if(f=='N')
return 0;
else if(f=='W')
return 1;
else if(f=='S')
return 2;
else
return 3;
}
void Get_next(int &x,int &y,int speed,int &dis)
{
int temp=2*(n-1);
speed%=temp;
x+=speed*f[dis][0];
y+=speed*f[dis][1];
if(f[dis][0])
{
if(x<=0)
{
x+=temp;
dis=2;
if(x>n&&x<=temp)
{
x=2*n-x;
}
}
else if(x>temp)
{
x-=temp;
dis=0;
if(x>n&&x<=temp)
{
x=2*n-x;
}
}
else if(x>n&&x<=temp)
{
x=2*n-x;
dis=0;
}
}
else
{
if(y<=0)
{
y+=temp;
dis=3;
if(y>n&&y<=temp)
{
y=2*n-y;
}
}
else if(y>temp)
{
y-=temp;
dis=1;
if(y>n&&y<=temp)
{
y=2*n-y;
}
}
else if(y>n&&y<=temp)
{
y=2*n-y;
dis=1;
}
}
}
void run(int k)
{
int t_1=0;
int t_2=0;
x_1=y_1=1;
x_2=y_2=n;
while(k--)
{
Get_next(x_1,y_1,speed_1,dis_1);
Get_next(x_2,y_2,speed_2,dis_2);
t_1++;
t_2++;
if(x_1==x_2&&y_1==y_2)
{
int temp=dis_1;
dis_1=dis_2;
dis_2=temp;
t_1%=time_1;
t_2%=time_2;
continue;
}
if(t_1==time_1)
{
dis_1++;
dis_1%=4;
}
if(t_2==time_2)
{
dis_2++;
dis_2%=4;
}
t_1%=time_1;
t_2%=time_2;
}
}
int main()
{
int k;
char temp_1[2],temp_2[2];
while(scanf("%d",&n)!=EOF&&n)
{
scanf("%s%d%d",&temp_1,&speed_1,&time_1);
scanf("%s%d%d",&temp_2,&speed_2,&time_2);
dis_1=Get_f(temp_1[0]);
dis_2=Get_f(temp_2[0]);
scanf("%d",&k);
run(k);
printf("%d %d\n",x_1,y_1);
printf("%d %d\n",x_2,y_2);
}
return 0;
}

207
HDOJ/4453_autoAC.cpp Normal file
View File

@ -0,0 +1,207 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
#define LL(x) (ch[x][0])
#define RR(x) (ch[x][1])
#define Kt (ch[ ch[Rt][1] ][0])
#define MID(a,b) (a+((b-a)>>1))
const int N=1e6+5;
int n,m,k1,k2;
int a[N/2];
struct SplayTree
{
int Rt,top;
int pre[N],sz[N],ch[N][2];
int key[N],add[N],pos;
bool flip[N];
inline void Link(int x,int y,int f)
{
pre[x]=y; if(y) ch[y][f]=x;
}
inline void Rotate(int x,int f)
{
int y=pre[x],z=pre[y];
PushDown(y); PushDown(x);
Link(x,z,RR(z)==y);
Link(ch[x][f],y,!f);
Link(y,x,f);
PushUp(y);
}
inline void Splay(int x,int goal)
{
while(pre[x]!=goal)
{
int y=pre[x],z=pre[y];
int cx=(LL(y)==x),cy=(LL(z)==y);
if(z==goal) Rotate(x,cx);
else
{
if(cx==cy) Rotate(y,cy);
else Rotate(x,cx);
Rotate(x,cy);
}
}
PushUp(x);
if(goal==0) Rt=x;
}
inline void Select(int K,int goal)
{
int x=Rt;
PushDown(x);
while(1)
{
if(sz[LL(x)]>=K) x=LL(x);
else if(sz[LL(x)]+1==K) break;
else K-=sz[LL(x)]+1,x=RR(x);
PushDown(x);
}
Splay(x,goal);
}
inline void fun_add(int x,int valu)
{
add[x]+=valu;
key[x]+=valu;
}
inline void fun_flip(int x)
{
flip[x]^=1;
swap(LL(x),RR(x));
}
inline void PushDown(int x)
{
if(add[x])
{
fun_add(LL(x),add[x]);
fun_add(RR(x),add[x]);
add[x]=0;
}
if(flip[x])
{
fun_flip(LL(x));
fun_flip(RR(x));
flip[x]=0;
}
}
inline void PushUp(int x)
{
sz[x]=1+sz[LL(x)]+sz[RR(x)];
}
inline void Add(int x)
{
Select(1,0); Select(k2+2,Rt);
fun_add(Kt,x);
}
inline void Reverse()
{
Select(1,0); Select(k1+2,Rt);
fun_flip(Kt);
}
inline void Insert(int x,int pos)
{
Select(pos,0); Select(pos+1,Rt);
addNode(x,Kt,RR(Rt));
PushUp(RR(Rt)); PushUp(Rt);
}
inline int Delete(bool top)
{
int valu;
if(top)
{
Select(1,0); Select(3,Rt);
valu=key[Kt];
Kt=0;
PushUp(RR(Rt)); PushUp(Rt);
}
else
{
int len=sz[Rt];
Select(len-2,0);Select(len,Rt);
valu=key[Kt];
Kt=0;
PushUp(RR(Rt)); PushUp(Rt);
}
return valu;
}
inline void Move(int x)
{
if(x==1)
{
int valu=Delete(0);
Insert(valu,1);
}
else
{
int valu=Delete(1);
Insert(valu,sz[Rt]-1);
}
}
inline void Query()
{
Select(2,0);
printf("%d\n",key[Rt]);
}
void addNode(int valu,int &x,int f)
{
x=++top;
pre[x]=f; sz[x]=1; LL(x)=RR(x)=0;
key[x]=valu; add[x]=flip[x]=0;
}
void build(int lft,int rht,int &x,int f)
{
if(lft>rht) return;
int mid=MID(lft,rht);
addNode(a[mid],x,f);
build(lft,mid-1,LL(x),x);
build(mid+1,rht,RR(x),x);
PushUp(x);
}
void init()
{
Rt=top=0;
pre[0]=sz[0]=LL(0)=RR(0)=0;
addNode(0,Rt,0); addNode(0,RR(Rt),Rt);
build(0,n-1,Kt,RR(Rt));
PushUp(RR(Rt)); PushUp(Rt);
}
}spt;
int main()
{
int t_cnt=0;
while(scanf("%d%d%d%d",&n,&m,&k1,&k2)!=EOF)
{
if(n==0&&m==0&&k1==0&&k2==0) break;
for(int i=0;i<n;i++) scanf("%d",&a[i]);
spt.init();
printf("Case #%d:\n",++t_cnt);
char op[100]; int x;
while(m--)
{
scanf("%s",op);
if(op[0]=='a')
{
scanf("%d",&x); spt.Add(x);
}
else if(op[0]=='r') spt.Reverse();
else if(op[0]=='i')
{
scanf("%d",&x); spt.Insert(x,2);
}
else if(op[0]=='d') spt.Delete(1);
else if(op[0]=='m')
{
scanf("%d",&x); spt.Move(x);
}
else spt.Query();
}
}
return 0;
}

90
HDOJ/4454_autoAC.cpp Normal file
View File

@ -0,0 +1,90 @@
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
const double eps = 1e-6;
const double PI = acos(-1.0);
double cx, cy, cr;
inline int sign(double x) {
return x < -eps ? -1 : x > eps ? 1 : 0;
}
struct Point {
double x, y;
Point (double xx = 0, double yy = 0) : x(xx), y(yy) {}
Point operator - (const Point &ot) const {
return Point(x - ot.x , y - ot.y);
}
double operator * (const Point &ot) const {
return x * ot.x + y * ot.y;
}
double operator ^ (const Point &ot) const {
return x * ot.y - y * ot.x;
}
bool operator < (const Point &ot) const {
if (sign(x - ot.x)) {
return sign(x - ot.x) < 0;
} else {
return sign(y - ot.y) < 0;
}
}
void show() {
printf("x = %.2f, y = %.2f\n", x, y);
}
};
Point it, rp[4];
double dist(const Point &a, const Point &b) {
return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
double dtoL(const Point &np, const Point &st, const Point &ed) {
double ret;
if (sign((ed-st)*(np-st)) > 0 && sign((st-ed)*(np-ed) > 0)) {
ret = fabs((st-np)^(ed-np)) / dist(st, ed);
} else {
ret = min(dist(np, st), dist(np, ed));
}
return ret;
}
double dtoR(const Point &np) {
double d1 = min(dtoL(np, rp[0], rp[1]), dtoL(np, rp[0], rp[2]));
double d2 = min(dtoL(np, rp[1], rp[3]), dtoL(np, rp[2], rp[3]));
return min(d1, d2);
}
double tsearch(double l, double r) {
double delta;
while (r - l >= eps) {
delta = (r - l) / 3.0;
Point Lp(cr*cos(l+delta), cr*sin(l+delta));
Point Rp(cr*cos(r-delta), cr*sin(r-delta));
double d1 = dist(it, Lp) + dtoR(Lp);
double d2 = dist(it, Rp) + dtoR(Rp);
if (sign(d1 - d2) > 0) {
l += delta;
} else {
r -= delta;
}
}
Point fp(cr*cos(r), cr*sin(r));
return dist(it, fp) + dtoR(fp);
}
void solve() {
printf("%.2f\n", min(tsearch(0, PI), tsearch(PI, 2*PI)));
}
int main() {
while (scanf("%lf %lf", &it.x, &it.y), sign(it.x) | sign(it.y)) {
scanf("%lf %lf %lf", &cx, &cy, &cr);
it.x -= cx, it.y -= cy;
scanf("%lf %lf", &rp[0].x, &rp[0].y);
rp[0].x -= cx, rp[0].y -= cy;
scanf("%lf %lf", &rp[1].x, &rp[1].y);
rp[1].x -= cx, rp[1].y -= cy;
rp[2].x = rp[0].x, rp[2].y = rp[1].y;
rp[3].x = rp[1].x, rp[3].y = rp[0].y;
sort(rp, rp+4);
double t = sqrt(2.0) / 2;
solve();
}
return 0;
}

56
HDOJ/4455_autoAC.cpp Normal file
View File

@ -0,0 +1,56 @@
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef __int64 LL;
const int maxn=1e6+10;
int c[maxn],a[maxn],pre[maxn],sum[maxn],num[maxn];
LL dp[maxn];
int main()
{
int n,m;
while(scanf("%d",&n)!=EOF)
{
if(n==0)break;
int i,j,k,t;
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
memset(c,0,sizeof(c));
memset(pre,0,sizeof(pre));
for(i=1;i<=n;i++)
{
c[i-pre[a[i]]]++;
pre[a[i]]=i;
}
sum[n]=c[n];
for(i=n-1;i>=1;i--)
sum[i]=sum[i+1]+c[i];
memset(c,0,sizeof(c));
num[1]=1;
c[a[n]]++;
for(i=2;i<=n;i++)
{
if(c[a[n-i+1]]==0)
{
num[i]=num[i-1]+1;
c[a[n-i+1]]=1;
}
else
num[i]=num[i-1];
}
dp[1]=n;
for(i=2;i<=n;i++)
{
dp[i]=dp[i-1]-num[i-1]+sum[i];
}
scanf("%d",&m);
for(i=0;i<m;i++)
{
scanf("%d",&t);
printf("%I64d\n",dp[t]);
}
}
return 0;
}

82
HDOJ/4456_autoAC.cpp Normal file
View File

@ -0,0 +1,82 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define MAXN 4001003
#define MAXM 88888
using namespace std;
int n, m;
int W;
int h[MAXN], cnt;
int a[MAXN];
int pp[MAXN], xx[MAXN], yy[MAXN], zz[MAXN];
inline int lowbit(int x)
{
return x & -x;
}
void ready(int x, int y)
{
for (int i = x; i <= W; i += lowbit(i)){
for (int j = y; j <= W; j += lowbit(j)) {
h[cnt++] = i * W + j;
}
}
}
void add(int x, int y, int val)
{
for(int i = x; i <= W; i += lowbit(i))
for(int j = y; j <= W; j += lowbit(j))
{
int pos = lower_bound(h, h + cnt, i * W + j) - h;
a[pos] += val;
}
}
int getsum(int x, int y)
{
int sum = 0;
for(int i = x; i > 0; i -= lowbit(i))
for(int j = y; j > 0; j -= lowbit(j))
{
int pos = lower_bound(h, h + cnt, i * W + j) - h;
if(h[pos] == i * W + j)
sum += a[pos];
}
return sum;
}
int main()
{
int p, x, y, z, xa, xb, ya, yb, newx, newy;
while(scanf("%d", &n) != EOF && n)
{
scanf("%d", &m);
W = n * 2;
cnt = 0;
memset(a, 0, sizeof(a));
for (int i = 0; i < m; i++)
{
scanf("%d%d%d%d", &pp[i], &xx[i], &yy[i], &zz[i]);
newx = xx[i] - yy[i] + n;
newy = xx[i] + yy[i];
if (pp[i] == 1)
ready(newx, newy);
}
sort(h, h + cnt);
cnt = unique(h, h + cnt) - h;
for(int i = 0; i < m; i++)
{
newx = xx[i] - yy[i] + n;
newy = xx[i] + yy[i];
if(pp[i] == 1) add(newx, newy, zz[i]);
else
{
xa = max(1, newx - zz[i]);
ya = max(1, newy - zz[i]);
xb = min(W, newx + zz[i]);
yb = min(W, newy + zz[i]);
printf("%d\n", getsum(xb, yb) - getsum(xa - 1, yb) - getsum(xb, ya - 1) + getsum(xa - 1, ya - 1));
}
}
}
return 0;
}

102
HDOJ/4458_autoAC.cpp Normal file
View File

@ -0,0 +1,102 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const double EPS=1e-8;
const int maxn=100;
int n;
struct Point
{
double x,y;
Point (double x=0,double y=0):x(x),y(y){}
};
Point poly[maxn];
typedef Point Vector;
Vector operator + (Vector A,Vector B) {return Vector(A.x+B.x,A.y+B.y);}
Vector operator - (Point A,Point B) {return Vector(A.x-B.x,A.y-B.y);}
Vector operator * (Vector A,double p) {return Vector(A.x*p,A.y*p);}
Vector operator / (Vector A,double p) {return Vector(A.x/p,A.y/p);}
bool operator < (const Point &a,const Point &b){
return a.x < b.x ||(a.x==b.x &&a.y < b.y);
}
int dcmp (double x){
if(fabs(x)<EPS)
return 0;
else return x < 0?-1:1;
}
bool operator == (const Point &a ,const Point &b){
return dcmp(a.x-b.x) ==0 && dcmp(a.y - b.y)==0;
}
double Cross (Vector A,Vector B) {return A.x*B.y - A.y*B.x;}
double Dot (Vector A,Vector B) {return A.x*B.x + A.y*B.y;}
bool isPointOnSegment (Point p,Point a1,Point a2)
{
return dcmp ( Cross(a1-p,a2-p) )==0 && dcmp(Dot (a1-p,a2-p))<0;
}
int online(Point a,Point b,Point p)
{
Point v1,v2;
v1=p-a;v2=p-b;
if( dcmp(Cross(v1,v2))!=0) //绉涓涓0 灏变ㄧ寸嚎涓
return 0;
else
{
if(dcmp(min(a.x,b.x)- p.x)<=0 && dcmp(p.x-max(a.x,b.x))<=0
&&dcmp(min(a.y,b.y)- p.y)<=0 &&dcmp( p.y-max(a.y,b.y))<=0)
return 1;
else
return 0;
}
}
int isPointInPolygon (Point p)
{
int wn=0;
for(int i=0;i<n;i++){
if( online (poly[i], poly[(i+1)%n],p)) return 0;
int k=dcmp (Cross(poly[(i+1)%n]-poly[i],p-poly[i]));
int d1=dcmp(poly[i].y-p.y);
int d2=dcmp(poly[(i+1)%n].y-p.y);
if(k>0 && d1<=0 && d2>0) wn++;
if(k<0 && d2<=0 && d1>0) wn--;
}
if(wn!=0) return 1;
return 0;
}
int main()
{
double v,b,g;
while (scanf("%lf %lf %lf",&v,&b,&g),v||b||g)
{
v=-v;
scanf("%d",&n);
bool flag=false;
int i;
double minx=1e12,maxx=-1e12,maxy=-1e12;
for(i=0;i<n;i++)
{
scanf("%lf %lf",&poly[i].x,&poly[i].y);
if(poly[i].x>maxx) maxx=poly[i].x;
if(poly[i].x<minx) minx=poly[i].x;
if(poly[i].y>maxy) maxy=poly[i].y;
}
double t;
for(int i=0;;i++)
{
t=i*0.001;
Point temp;
temp.x=v*t;temp.y=b*t-0.5*g*t*t;
if( isPointInPolygon(temp) )
{
printf("%.2f\n",t-EPS);
flag=true;
break;
}
if(!g && temp.y>maxy) break;
if(temp.y<0) break;
}
if(!flag) printf("Miss!\n");
}
return 0;
}

218
HDOJ/4459_autoAC.cpp Normal file
View File

@ -0,0 +1,218 @@
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
struct fac
{
LL fz,fm;
fac(){}
fac (LL a,LL b):fz(a),fm(b) {}
fac fix()
{
LL d=__gcd(fz,fm);
return fac(fz/d,fm/d);
}
fac operator + (const fac & b) const { return fac(fz*b.fm+fm*b.fz,fm*b.fm).fix(); }
fac operator * (const fac & b) const { return fac(fz*b.fz,fm*b.fm).fix(); }
fac operator / (const fac & b) const { return fac(fz*b.fm,fm*b.fz).fix(); }
fac operator - (const fac & b) const { return fac(fz*b.fm-fm*b.fz,fm*b.fm).fix() ;}
};
struct node
{
fac xs;
int d[5],x[5];
node () {
xs=fac(1,1);
for(int i=0;i<5;i++) { d[i]=x[i]=0;}
}
node operator * (const node & b) const {
node ans;
ans.xs=xs*b.xs;
for(int i=0;i<5;i++) {
ans.d[i]=d[i]+b.d[i];
ans.x[i]=x[i]+b.x[i];
}
return ans;
}
bool operator < (const node & b) const
{
for(int i=0;i<5;i++) {
if(d[i]!=b.d[i]) return d[i]<b.d[i];
}
return 0;
}
bool operator == (const node & b)
{
for(int i=0;i<5;i++) if(d[i]!=b.d[i]) return false;
return true;
}
void output()
{
cout << xs.fz << "/" << xs.fm << " ";
for(int i=0;i<5;i++){
cout << d[i] << " ";
}
for(int i=0;i<5;i++){
cout << x[i] << " ";
}
cout << endl;
}
};
vector <node> dep[10],ans;
vector <node> v_mult(vector <node> & a,vector <node> & b)
{
vector<node> ans;
int s1=a.size(),s2=b.size();
for(int i=0;i<s1;i++){
for(int j=0;j<s2;j++){
ans.push_back(a[i]*b[j]);
}
}
return ans;
}
vector <node> v_exp(vector <node> & a,int b)
{
vector <node> c=a;
while(--b){
c=v_mult(c,a);
}
return c;
}
void init()
{
node a;
dep[5].push_back(a);
for(int i=4;i>=0;i--){
int s=dep[i+1].size();
vector <node > tmp;
node b;
b.d[i]=1;
tmp.push_back(b);
for(int j=0;j<i;j++){
node b;
b.xs=fac(-1,1);
b.x[j]=1;
tmp.push_back(b);
}
for(int j=0;j<s;j++){
vector<node> q=v_exp(tmp,dep[i+1][j].x[i]+1);
int s1=q.size();
for(int k=0;k<s1;k++){
node tt=dep[i+1][j]*q[k];
tt.xs=tt.xs*fac(1,dep[i+1][j].x[i]+1);
dep[i].push_back(tt);
}
}
}
sort(dep[0].begin(),dep[0].end());
int ss=dep[0].size();
node pre=dep[0][0];
for(int i=1;i<ss;i++){
if(dep[0][i]==pre){
pre.xs=pre.xs+dep[0][i].xs;
}
else {
if(pre.xs.fz) ans.push_back(pre);
pre=dep[0][i];
}
}
if(pre.xs.fz) ans.push_back(pre);
}
struct work
{
int s1,s2,d;
bool operator < (const work & b) const{
return d<b.d;
}
}tt[10];
int n;
int a[5],d[5];
fac cal()
{
fac res=fac(0,1);
for(size_ i=0;i<ans.size();i++){
fac tmp=ans[i].xs;
for(int j=0;j<5;j++){
for(int k=0;k<ans[i].d[j];k++){
tmp.fz=tmp.fz*d[j];
}
}
tmp=tmp.fix();
res=res+tmp;
}
return res;
}
void dfs(fac & ans,int s,int t)
{
if(s==5){
for(int i=0;i<5;i++) d[i]=tt[i].d;
for(int i=0;i<5;i++){
if(a[i]){
for(int j=i;j<5;j++) d[j]-=tt[i].s2;
}
}
for(int i=3;i>=0;i--) {
d[i]=min(d[i],d[i+1]);
if(d[i]<0) return;
}
fac p=cal();
if(t) ans=ans+p;
else ans=ans-p;
return;
}
a[s]=1;
dfs(ans,s+1,1-t);
a[s]=0;
dfs(ans,s+1,t);
}
fac solve()
{
while(n<5){
tt[n].s2=1;
tt[n++].d=2000;
}
fac ans=fac(0,1);
dfs(ans,0,1);
for(int i=0;i<5;i++) ans.fm=ans.fm*tt[i].s2;
return ans.fix();
}
int main()
{
init();
while(scanf("%d",&n)&&n){
for(int i=0;i<n;i++) scanf("%d%d%d",&tt[i].s1,&tt[i].s2,&tt[i].d);
sort(tt,tt+n);
int flag=1;
for(int i=0;i<n;i++){
if(tt[i].s1==tt[i].s2){
for(int j=i+1;j<n;j++) tt[j].d-=tt[i].s1;
if(i==0) {
if(tt[i].s1>tt[i].d) { flag=0;break;}
}
else {
tt[i-1].d=min(tt[i-1].d,tt[i].d-tt[i].s1);
}
for(int j=i;j<n-1;j++){
tt[j]=tt[j+1];
}
i-- , n--;
}
}
for(int i=0;i<n;i++) {
for(int j=i;j<n;j++) tt[j].d-=tt[i].s1;
tt[i].s2-=tt[i].s1;
if(tt[i].d<0) { flag=0;break;}
}
if(!flag) puts("Poor boy!");
else if(n==0) puts("Congratulations!");
else {
fac ans=solve();
if(ans.fm<0) { ans.fz=-ans.fz , ans.fm=-ans.fm;}
if(ans.fz<=0) puts("Poor boy!");
else if(ans.fz>=ans.fm) puts("Congratulations!");
else printf("%I64d/%I64d\n",ans.fz,ans.fm);
}
}
}

77
HDOJ/4460_autoAC.cpp Normal file
View File

@ -0,0 +1,77 @@
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <vector>
#include<string>
using namespace std;
const int MAXN=1010;
const int INF=0x3f3f3f3f;
int dis[MAXN][MAXN];
bool used[MAXN];
vector<int>vec[MAXN];
queue<int>que;
void bfs(int i)
{
memset(used,false,sizeof(used));
dis[i][i]=0;
used[i]=true;
que.push(i);
while(!que.empty())
{
int t=que.front();
que.pop();
int m=vec[t].size();
for(int j=0;j<m;j++)
{
int v=vec[t][j];
if(used[v])continue;
if(dis[i][v]>dis[i][t]+1)
dis[i][v]=dis[i][t]+1;
que.push(v);
used[v]=true;
}
}
}
map<string,int>mp;
int main() {
string str;
string str2;
int n,m,i,j;
while(scanf("%d",&n)==1 && n)
{
mp.clear();
for(i=0;i<n;i++)
{
cin>>str;
mp[str]=i;
}
for(i=0;i<n;i++)
{
dis[i][i]=0;
for(j=i+1;j<n;j++)
dis[i][j]=dis[j][i]=INF;
}
scanf("%d",&m);
for(i=0;i<n;i++)vec[i].clear();
while(m--)
{
cin>>str>>str2;
int t1=mp[str];
int t2=mp[str2];
vec[t1].push_back(t2);
vec[t2].push_back(t1);
}
for(i=0;i<n;i++)bfs(i);
int ans=0;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
ans=ans>dis[i][j]?ans:dis[i][j];
if(ans==INF)ans=-1;
printf("%d\n",ans);
}
return 0;
}

76
HDOJ/4462_autoAC.cpp Normal file
View File

@ -0,0 +1,76 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define MAXN 60
bool flag;
int n,m;
int x[20],y[20],r[20];
int map[MAXN][MAXN];
int node[MAXN],total;
void func()
{
int L,R,T,D;
memset(map,0,sizeof(map));
for(int i=1;i<=m;i++)
map[x[i]][y[i]]=1;
for(int i=0;i<total;i++)
{
int k=node[i];
L=(x[k]-r[k]>0?x[k]-r[k]:1);
R=(x[k]+r[k]<=n?x[k]+r[k]:n);
T=(y[k]-r[k]>0?y[k]-r[k]:1);
D=(y[k]+r[k]<=n?y[k]+r[k]:n);
for(int j=L;j<=R;j++)
for(int l=T;l<=D;l++)
if(abs(j-x[k])+abs(l-y[k])<=r[k])
map[j][l]=1;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(!map[i][j]) return;
flag=1;
}
void dfs(int sel,int cnt)
{
if(sel==total)
func();
else
for(int i=cnt;i<=m;i++)
{
node[sel]=i;
dfs(sel+1,i+1);
}
}
void solve()
{
scanf("%d",&m);
for(int i=1;i<=m;i++)
scanf("%d%d",&x[i],&y[i]);
for(int i=1;i<=m;i++)
scanf("%d",&r[i]);
flag=0;
if(m>=n*n)
{
printf("0\n");
return;
}
for(int i=1;i<=m;i++)
{
total=i;
dfs(0,1);
if(flag==1)
{
printf("%d\n",i);
return;
}
}
printf("-1\n");
}
int main()
{
while(~scanf("%d",&n) && n)
solve();
return 0;
}

70
HDOJ/4463_autoAC.cpp Normal file
View File

@ -0,0 +1,70 @@
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"math.h"
int pre[100];
struct A
{
int a,b,len;
}eage[3000];
int k;
int x[100],y[100];
void build(int n)
{
int i;
for(i=0;i<n;i++) pre[i]=i;
}
int find(int k)
{
if(pre[k]==k) return k;
pre[k]=find(pre[k]);
return pre[k];
}
void Union(int f1,int f2)
{
pre[f1]=f2;
}
int cmp(const void *a,const void *b)
{
struct A *c,*d;
c=(struct A *)a;
d=(struct A *)b;
return c->len-d->len;
}
int main()
{
int n;
int i,l;
int d1,d2;
int f1,f2;
double ans;
while(scanf("%d",&n),n)
{
build(n);
scanf("%d%d",&d1,&d2);
d1--;d2--;
for(i=0;i<n;i++) scanf("%d%d",&x[i],&y[i]);
k=0;
for(i=1;i<n;i++)
for(l=0;l<i;l++)
{
eage[k].a=i;
eage[k].b=l;
eage[k].len=(x[i]-x[l])*(x[i]-x[l])+(y[i]-y[l])*(y[i]-y[l]);
k++;
}
qsort(eage,k,sizeof(eage[0]),cmp);
ans=sqrt((double)(x[d1]-x[d2])*(x[d1]-x[d2])+(y[d1]-y[d2])*(y[d1]-y[d2]));
Union(d1,d2);
for(i=0;i<k;i++)
{
f1=find(eage[i].a);
f2=find(eage[i].b);
if(f1==f2) continue;
Union(f1,f2);
ans+=sqrt((double)eage[i].len);
}
printf("%.2lf\n",ans);
}
return 0;
}

19
HDOJ/4464_autoAC.cpp Normal file
View File

@ -0,0 +1,19 @@
#include <stdio.h>
#include <string.h>
int main() {
int n, t = 0;
while(scanf("%d", &n)!=EOF) {
int maxx = 0;
while(n--) {
int i, tmp = 0;
char s[105];
scanf("%s", s);
for(i = 0; s[i]; i++)
tmp += s[i];
if(tmp > maxx)
maxx = tmp;
}
printf("Case %d: %d\n", ++t, maxx);
}
return 0;
}

30
HDOJ/4465_autoAC.cpp Normal file
View File

@ -0,0 +1,30 @@
#include<stdio.h>
#include<string.h>
#include<math.h>
#define N 400200
double f[N];
double logc(int n,int m)
{
return f[m]-f[n]-f[m-n];
}
int main ()
{
int n;
int i,j,k=0;
double p,ans,ret;
f[0]=0;
for (i=1;i<N;++i) f[i]=f[i-1]+log(1.0*i);
while (scanf("%d%lf",&n,&p)!=EOF)
{
ans=0;
double p1=log(p),p2=log(1-p);
double q1=(n+1)*p1,q2=(n+1)*p2;
for (i=0;i<n;++i)
{
ans+=((n-i)*exp(logc(n,n+i)+q1+i*p2));
ans+=((n-i)*exp(logc(n,n+i)+q2+i*p1));
}
printf("Case %d: %.6lf\n",++k,ans);
}
return 0;
}

75
HDOJ/4466_autoAC.cpp Normal file
View File

@ -0,0 +1,75 @@
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#define maxn 5000500
#define M 1000000007
#define ll long long
using namespace std;
ll f[maxn];
ll count(ll x)
{
ll tot=0;
for (int i=1; i<=x/3; i++)
{
int tep=x-i;
if (tep/2>=max(i,(tep-i)/2+1))
{
tot=(tot+tep/2-max(i,(tep-i)/2+1)+1);
if (tot>=M) tot-=M;
}
}
return tot;
}
ll get(ll x)
{
if (f[x]!=0) return f[x];
ll tot=0;
for (int i=3; i<=x/2; i++)
if (x%i==0)
{
tot=(tot+get(i));
if (tot>=M) tot-=M;
}
f[x]=count(x)-tot;
if (f[x]<0) f[x]+=M;
return f[x];
}
ll power(ll x,ll y)
{
ll tot=1;
while (y)
{
if (y&1) tot=(tot*x)%M;
x=(x*x)%M;
y>>=1;
}
return tot;
}
int main()
{
ll n,ans,tep,cas=0;
while (scanf("%I64d",&n)!=EOF)
{
ans=0;
for (int i=1; i*i<=n; i++)
{
if (n%i==0)
{
tep=get(i);
tep=(tep*power(2,n/i-1))%M;
ans+=tep;
if (ans>=M) ans-=M;
if (i*i!=n)
{
tep=get(n/i);
tep=(tep*power(2,i-1))%M;
ans+=tep;
if (ans>=M) ans-=M;
}
}
}
printf("Case %I64d: %I64d\n",++cas,ans);
}
return 0;
}

99
HDOJ/4467_autoAC.cpp Normal file
View File

@ -0,0 +1,99 @@
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long LL;
const int N = 101010;
int n, m;
LL ans[3], sum[N][2];
vector<LL> c[N];
vector<int> lin[N];
int color[N], key[N], deg[N];
struct node{
int a,b; LL c;
bool operator < (const node &tmp)const{
return (a<tmp.a)||(a==tmp.a&&b<tmp.b);
}
}e[N];
void init(){
int lim = (int)sqrt(n);
for(int i = 0; i <= n; i++) c[i].clear(), lin[i].clear();
memset( sum, 0, sizeof(sum));
memset( ans, 0, sizeof(ans));
memset( key, 0, sizeof(key));
memset( deg, 0, sizeof(deg));
for(int i = 1; i <= n; i++) scanf("%d", &color[i] );
for(int i = 1; i <= m; i++){
scanf("%d%d%I64d",&e[i].a,&e[i].b,&e[i].c);
if( e[i].a > e[i].b ) swap( e[i].a, e[i].b );
}
sort( e+1, e+m+1 );
int t = 1;
for(int i = 2; i <= m; i++)
if( e[i].a != e[t].a || e[i].b != e[t].b ) e[++t] = e[i];
else e[t].c += e[i].c;
m = t;
for(int i = 1; i <= m; i++){
int a = e[i].a, b = e[i].b;
ans[ color[a]+color[b] ] += e[i].c;
deg[a]++; deg[b]++;
}
lim = round(sqrt(n));
for(int i = 1; i <= n; i++) if( deg[i] > lim ) key[i] = 1;
for(int i = 1; i <= m; i++){
int a = e[i].a, b = e[i].b;
sum[a][ color[b] ] += e[i].c;
sum[b][ color[a] ] += e[i].c;
if( key[a] && key[b] ){
lin[a].push_back(b); c[a].push_back(e[i].c);
lin[b].push_back(a); c[b].push_back(e[i].c);
}
if( key[a] == 0 ) lin[a].push_back(b),c[a].push_back(e[i].c);
if( key[b] == 0 ) lin[b].push_back(a),c[b].push_back(e[i].c);
}
}
void change(int x){
if( key[x] ){
for(int i = 0; i < (int)(lin[x].size()); i++){
sum[ lin[x][i] ][ color[x] ] -= c[x][i];
sum[ lin[x][i] ][ color[x]^1 ] += c[x][i];
}
}
else{
sum[x][0] = sum[x][1] = 0;
for(int i = 0; i < (int)(lin[x].size()); i++){
sum[x][ color[ lin[x][i] ] ] += c[x][i];
if( key[ lin[x][i] ] ){
sum[ lin[x][i] ][ color[x] ] -= c[x][i];
sum[ lin[x][i] ][ color[x]^1 ] += c[x][i];
}
}
}
ans[ color[x]+0 ] -= sum[x][0]; ans[ color[x]+1 ] -= sum[x][1];
ans[ (color[x]^1)+0 ] += sum[x][0]; ans[ (color[x]^1)+1 ] += sum[x][1];
color[x] ^= 1;
}
int main(){
int Case = 1;
while( scanf("%d%d",&n,&m) != EOF){
printf("Case %d:\n", Case++);
init();
char op[110]; int Q, a, b;
scanf("%d", &Q );
while( Q-- ){
scanf("%s", op);
if( op[0] == 'A' ){
scanf("%d%d",&a,&b);
printf("%I64d\n", ans[a+b] );
}
else{
scanf("%d",&a);
change( a );
}
}
}
return 0;
}

77
HDOJ/4468_autoAC.cpp Normal file
View File

@ -0,0 +1,77 @@
#include<cstring>
#include<iostream>
#include<cstdio>
using namespace std;
#define MAXN 100005
int ne[MAXN];
char s[MAXN],p[MAXN];
void getnext(int n,int st)
{
for(int i=st+1;i<n;i++)
{
int j=ne[i];
while(j&&p[i]!=p[j])
{
j=ne[j];
}
if(p[i]==p[j])
{
ne[i+1]=j+1;
}
else
{
ne[i+1]=0;
}
}
}
int ans;
int updata(int last,int now,int st)
{
int len=st+1;
for(int i=last+1;i<=now;i++)
{
p[len++]=s[i];
}
p[len]='\0';
getnext(len,st);
return len;
}
int kmp(int m)
{
getnext(m,0);
int j=0;
int last;
for(int i=0;s[i];i++)
{
while(j&&s[i]!=p[j])
j=ne[j];
if(j==0&&s[i]!=p[j])
{
m=updata(last,i,m-1);
last=i;
}
if(s[i]==p[j])
j++;
if(j==m)
{
last=i;
}
}
for(int i=last+1;s[i];i++)
m++;
ans=m;
return -1;
}
int main()
{
int cas=1;
while(scanf("%s",&s)!=EOF)
{
memset(ne,0,sizeof(ne));
p[0]=s[0];
p[1]='\0';
ans=1;
kmp(1);
printf("Case %d: %d\n",cas++,ans);
}
}

94
HDOJ/4471_autoAC.cpp Normal file
View File

@ -0,0 +1,94 @@
#include<cstdio>
#include<algorithm>
using namespace std;
const int N =102;
const int mod = 1000000007;
int h;
struct matrix
{
int row,col;
int m[N][N];
void init(int row,int col)
{
this->row = row;
this->col = col;
for(int i=0; i<=row; ++i)
for(int j=0; j<=col; ++j)
m[i][j] = 0;
}
} A,pm[33],ans;
matrix operator*(const matrix & a,const matrix& b)
{
matrix res;
res.init(a.row,b.col);
for(int k=1; k<=a.col; ++k)
{
for(int i=1; i<= res.row; ++i)
{
if(a.m[i][k] == 0 ) continue;
for(int j = 1; j<=res.col; ++j)
{
if(b.m[k][j] == 0 ) continue;
res.m[i][j] = (1LL *a.m[i][k]*b.m[k][j] + res.m[i][j])%mod;
}
}
}
return res;
}
void cal(int x)
{
for(int i=0; i<=31; ++i)
if(x & (1<<i) ) ans = pm[i]*ans;
}
void getPm()
{
pm[0] = A;
for(int i=1; i<=31; ++i)
pm[i] = pm[i-1]*pm[i-1];
}
struct sp
{
int nk,tk;
int ck[N];
bool operator<(const sp & o)const
{
return nk<o.nk;
}
} p[N];
int main()
{
int n,m,q,t,f[N],c[N],kase=0;
while(~scanf("%d%d%d",&n,&m,&q))
{
for(int i=m; i>0; --i) scanf("%d",&f[i]);
scanf("%d",&t);
h =t;
for(int i=1; i<=t; ++i) scanf("%d",&c[i]);
for(int i=0; i<q; ++i)
{
scanf("%d%d",&p[i].nk,&p[i].tk);
if(p[i].tk > h) h = p[i].tk;
for(int j=1; j<=p[i].tk; ++j) scanf("%d",&p[i].ck[j]);
}
sort(p,p+q);
A.init(h,h);
for(int i=1; i<=t; ++i) A.m[1][i] = c[i];
for(int i=2; i<=h; ++i) A.m[i][i-1] = 1;
getPm();
ans.init(h,1);
for(int i = m; i > 0; --i) ans.m[i][1] = f[i];
int last=m;
for(int i=0; i<q; ++i)
{
if( p[i].nk <=last || p[i].nk >n ) continue;
cal( p[i].nk-last-1);
last = p[i].nk;
for(int j=1; j<=p[i].tk; ++j) A.m[1][j] = p[i].ck[j];
for(int j=p[i].tk+1; j<=h; ++j) A.m[1][j] = 0;
ans =A*ans;
}
cal(n-last);
printf("Case %d: %d\n",++kase,ans.m[1][1]);
}
return 0;
}

25
HDOJ/4472_autoAC.cpp Normal file
View File

@ -0,0 +1,25 @@
#include <cstdio>
#include <cstring>
#define N 1010
#define MOD 1000000007
long long dp[N];
int n;
void DP()
{
memset(dp,0,sizeof(dp));
dp[1]=1; dp[2]=1;
for(int i=3; i<=1000; i++)
for(int j=1; j<=i-1; j++)
if((i-1)%j == 0)
dp[i] = (dp[i]+dp[j])%MOD;
}
int main()
{
DP();
int ccase=0;
while(scanf("%d",&n)!=EOF)
{
printf("Case %d: %lld\n",++ccase,dp[n]);
}
return 0;
}

38
HDOJ/4473_autoAC.cpp Normal file
View File

@ -0,0 +1,38 @@
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<set>
#include <stack>
#include<queue>
#include<algorithm>
#include<cmath>
#define oo 1000000007
#define MAXN 100005
#define ll long long
#define pi acos(-1.0)
using namespace std;
ll n,i,j,k,ans=0;
int main()
{
int cases=0;
while (~scanf("%I64d",&n))
{
ans=0;
for (i=1;i*i*i<=n;i++) ans++;
for (i=1;i*i<=n;i++)
{
k=n/i/i;
if (k>=i) ans+=(k-1)*3;
else ans+=k*3;
}
for (i=1;i*i*i<=n;i++)
for (j=i+1;j<=n;j++)
{
k=n/i/j;
if (k>j) ans+=(k-j)*6;
else break;
}
printf("Case %d: %I64d\n",++cases,ans);
}
return 0;
}

62
HDOJ/4474_autoAC.cpp Normal file
View File

@ -0,0 +1,62 @@
#include<cstdio>
#include<cstring>
using namespace std;
struct{
int m,f;
char c;
}d[10010],tmp;
int n,m,l,r;
bool h[10010],k[10];
void out(int x){
if(x==-1)return;
out(d[x].f);
putchar(d[x].c);
}
int main()
{
int C=0;
while (~scanf("%d%d",&n,&m)){
printf("Case %d: ",++C);
memset(h,true,sizeof(h));
memset(k,true,sizeof(k));
l=r=0;
for(int i=0;i<m;i++){
int x;
scanf("%d",&x);
k[x]=0;
}
int ans=-1;
for(int i=1;i<10;i++)if(k[i]){
tmp.c=i+'0';
h[tmp.m=i%n]=false;
tmp.f=-1;
d[r++]=tmp;
if(tmp.m==0){
ans=r-1;
break;
}
}
while(l<r && ans==-1){
for(int i=0;i<10;i++)if(k[i]){
tmp.c=i+'0';
tmp.m=(d[l].m*10 + i)%n;
if(h[tmp.m]){
h[tmp.m]=false;
tmp.f=l;
d[r++]=tmp;
if(tmp.m==0){
ans=r-1;
break;
}
}
}
l++;
}
if(ans==-1) puts("-1");
else{
out(ans);
putchar('\n');
}
}
return 0;
}

26
HDOJ/4475_autoAC.cpp Normal file
View File

@ -0,0 +1,26 @@
#include<iostream>
#include<cstdio>
#include<math.h>
using namespace std;
const int con=1000003;
__int64 arr[con+1000];
int main()
{
int T;
__int64 n;
arr[0]=0;
arr[1]=2;
for(int i=2;i<=con;i++)
{
arr[i]=(arr[i-1]*2*i)%con;
}
scanf("%d",&T);
while(T--)
{
scanf("%I64d",&n);
if(n<=con)
printf("%I64d\n",arr[n%con]);
else printf("0\n");
}
return 0;
}

60
HDOJ/4476_autoAC.cpp Normal file
View File

@ -0,0 +1,60 @@
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <set>
using namespace std;
const int MaxN = 200000;
int N;
int bit[MaxN+5];
inline int lwb(int x) {
return x & -x;
}
void add(int x, int val) {
for (int i = x; i <= MaxN; i += lwb(i)) {
bit[i] += val;
}
}
int sum(int x) {
int ret = 0;
for (int i = x; i > 0; i -= lwb(i)) {
ret += bit[i];
}
return ret;
}
int cal(int x) {
if (x <= MaxN/2) {
int k = x << 1;
return sum(k) - sum(k-1) + sum(MaxN) - sum(x - 1);
} else {
return sum(MaxN) - sum(x - 1);
}
}
char vis[100005];
int que[100005];
int tail;
int main() {
int T, x;
scanf("%d", &T);
while (T--) {
scanf("%d", &N);
tail = 0;
memset(bit, 0, sizeof (bit));
memset(vis, 0, sizeof (vis));
for (int i = 0; i < N; ++i) {
scanf("%d", &x);
add(x<<1, 1);
if (!vis[x]) {
vis[x] = 1;
que[tail++] = x;
}
}
int Max = 0;
for (int i = 0; i < tail; ++i) {
Max = max(Max, cal(que[i]));
}
printf("%d\n", Max);
}
return 0;
}

34
HDOJ/4477_autoAC.cpp Normal file
View File

@ -0,0 +1,34 @@
#include <stdio.h>
#define N 50000
#define M 1000000
#define K 316
int main()
{
int dp[2][N+2];
int ans[N+2];
for(int i=3;i<=N;i++)
{
ans[i]=dp[0][i]=dp[0][i-2]+1;
}
for(int k=3;k<=K;k++)
{
int *p1=dp[k&1];int *p2=dp[(k+1)&1];
for(int i=0;i<=k*(k+1)/2-1;i++) p1[i]=0;
for(int i=k*(k+1)/2;i<=N;i++)
{
p1[i]=p1[i-k]+p2[i-k];
if(p1[i]>=M)
p1[i]-=M;
ans[i]+=p1[i];
if(ans[i]>=M)
ans[i]-=M;
}
}
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
printf("%d\n",ans[n]);
}
}

81
HDOJ/4478_autoAC.cpp Normal file
View File

@ -0,0 +1,81 @@
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
int N, T, sx, sy;
char mp[105][105];
char vis[105][105][2];
struct Sta {
int x, y, t;
};
queue<Sta>q;
int dir[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
inline bool judge(int x, int y) {
if (x >= 0 && x < N && y >= 0 && y < N) return true;
return false;
}
void cover(const Sta &cur) {
Sta nxt;
bool flag = false;
for (int k = 0; k < 8; ++k) {
nxt.x = cur.x + dir[k][0];
nxt.y = cur.y + dir[k][1];
nxt.t = cur.t + 1;
if (judge(nxt.x, nxt.y) && mp[nxt.x][nxt.y] == '.') {
flag = true;
if (!vis[nxt.x][nxt.y][nxt.t&1]) {
vis[nxt.x][nxt.y][nxt.t&1] = 1;
if (nxt.t < T) {
q.push(nxt);
}
}
}
}
if (!flag) {
nxt = cur;
++nxt.t;
if (!vis[nxt.x][nxt.y][nxt.t&1]) {
vis[nxt.x][nxt.y][nxt.t&1] = 1;
if (nxt.t < T) {
q.push(nxt);
}
}
}
}
void solve() {
while (!q.empty()) q.pop();
memset(vis, 0, sizeof (vis));
Sta cur, nxt;
nxt.x = sx, nxt.y = sy, nxt.t = 0;
vis[sx][sy][0] = 1;
q.push(nxt);
while (!q.empty()) {
cur = q.front(), q.pop();
cover(cur);
}
int odd = 0, even = 0;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (vis[i][j][0]) ++even;
if (vis[i][j][1]) ++odd;
}
}
if (T & 1) printf("%d\n", odd);
else printf("%d\n", even);
}
int main() {
int C;
scanf("%d", &C);
while (C--) {
scanf("%d %d %d %d", &N, &T, &sx, &sy);
sx--, sy--;
for (int i = 0; i < N; ++i) {
scanf("%s", mp[i]);
}
solve();
}
return 0;
}

60
HDOJ/4479_autoAC.cpp Normal file
View File

@ -0,0 +1,60 @@
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
typedef long long LL;
int N, M;
struct Edge {
int x, y, d;
bool operator < (const Edge & ot) const {
return d < ot.d;
}
}e[50005];
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
LL dis[10005];
struct Rec {
int v;
LL d;
}rec[10005];
void update(int l, int r) {
int u, v, d, idx = 0;
for (int i = l; i <= r; ++i) {
u = e[i].x, v = e[i].y, d = e[i].d;
if (dis[u] != INF && dis[v] > dis[u] + d) {
rec[idx].v = v, rec[idx++].d = dis[u] + d;
}
if (dis[v] != INF && dis[u] > dis[v] + d) {
rec[idx].v = u, rec[idx++].d = dis[v] + d;
}
}
for (int i = 0; i < idx; ++i) {
dis[rec[i].v] = min(dis[rec[i].v], rec[i].d);
}
}
void solve() {
memset(dis, 0x3f, sizeof (dis));
dis[1] = 0;
for (int i = 0, j; i < M; i = j) {
for (j = i + 1; j < M; ++j) {
if (e[j].d != e[i].d) break;
}
update(i, j-1);
}
if (dis[N] == INF) puts("No answer");
else printf("%I64d\n", dis[N]);
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%d %d", &N, &M);
for (int i = 0; i < M; ++i) {
scanf("%d %d %d", &e[i].x, &e[i].y, &e[i].d);
}
sort(e, e + M);
solve();
}
return 0;
}

52
HDOJ/4483_autoAC.cpp Normal file
View File

@ -0,0 +1,52 @@
#include <stdio.h>
#include <iostream>
using namespace std;
#define maxn 100001
#define LL long long
const LL mod=1000000007;
LL x[maxn],y[maxn],z[maxn],phi[maxn],n;
void init()
{
LL i,j,k;
for(i=2;i<maxn;i++)phi[i]=0;
phi[1]=1;
for(i=2;i<maxn;i++)
if(!phi[i])
for(j=i;j<=maxn;j+=i){
if(!phi[j])phi[j]=j;
phi[j]=phi[j]/i*(i-1);
}
x[1]=z[1]=1;
y[1]=2;
for(i=2;i<maxn;i++)
{
x[i]=(x[i-1]+phi[i]*2)%mod;
y[i]=(y[i-1]+phi[i]*i*3)%mod;
z[i]=(z[i-1]+phi[i]*i%mod*i)%mod;
}
}
int main()
{
LL T;
init();
cin>>T;
while(T--)
{
LL k,t,ans,m;
cin>>n;
n++;
t=(n*n)%mod;
LL md=mod*6;
ans=t*(t-1)%md*(t-2)%md/6;
ans-=t*(n-1)%md*(n-2)*2%md/6;
t=0;
for(k=2;k<n;k++)
{
m=(n-1)/k;
t=(t+(k-1)*(x[m]*n%mod*n%mod-n*k%mod*y[m]%mod+k*k%mod*z[m]%mod))%mod;
}
ans-=t*2;
cout<<(ans%mod+mod)%mod<<endl;
}
return 0;
}

30
HDOJ/4484_autoAC.cpp Normal file
View File

@ -0,0 +1,30 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int cal(int n) {
int Max = n;
while (n!=1) {
if ((n&1) == 0) {
n /= 2;
} else {
n = n * 3 + 1;
}
if (n > Max) Max = n;
}
return Max;
}
void solve() {
int n; scanf("%d", &n);
int i, t, ca;
for (i = 0; i < n; ++i) {
scanf("%d%d", &t, &ca);
int ans = cal(ca);
printf("%d %d\n", t, ans);
}
}
int main(void) {
solve();
return 0;
}

26
HDOJ/4485_autoAC.cpp Normal file
View File

@ -0,0 +1,26 @@
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int p,a,b;
char s[10000000+1];
int main()
{
scanf("%d",&p);
while(p--)
{
scanf("%d%d%s",&a,&b,s);
int j=0;
int ans=0;
int l=strlen(s)-1;
for(int i=l;i>=0;i--)
{
ans+=(s[i]-'0')%(b-1);
ans%=b-1;
}
printf("%d %d\n",a,ans);
}
return 0;
}

25
HDOJ/4486_autoAC.cpp Normal file
View File

@ -0,0 +1,25 @@
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
int id,n,tot=0;
scanf("%d %d",&id,&n);
if(n%3==0) tot++;
int top=(int)ceil(n*1.0/3);
for(int i=n/2;i>=top;i--)
{
if(n-i==i) continue;
if((n-i)&1) {tot+=(i-(n-i+1)/2+1)*2;tot--;}
else {tot+=(i-(n-i)/2+1)*2;tot-=2;}
}
printf("%d %d\n",id,tot);
}
return 0;
}

42
HDOJ/4487_autoAC.cpp Normal file
View File

@ -0,0 +1,42 @@
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std;
const int N = 205;
double dp[2][N][N];
int main()
{
int t, n,cas;double L, R, ans;
scanf("%d", &t);
while(t --)
{
scanf("%d%d%lf%lf", &cas, &n, &L, &R);
CLR(dp, 0);dp[0][0][100] = 1;
for(int i = 1; i <= n; i ++)
{
CLR(dp[i&1], 0);
for(int j = 0; j <= i; j ++)
{
for(int k = 100 - i; k <= 100 + j; k ++)
{
dp[i&1][j][k] += dp[(i-1)&1][j][k-1] * R;
dp[i&1][j][k] += dp[(i-1)&1][j][k+1] * L;
dp[i&1][j][k] += dp[(i-1)&1][j][k] * (1.0 - L - R);
}
dp[i&1][j][100+j] += dp[(i-1)&1][j-1][99+j] * R;
}
}ans = 0;
for(int i = 0; i <= n; i ++)
{
for(int j = 100 - n; j <= 100 + n; j ++)
{
ans += i * dp[n&1][i][j];
}
}
printf("%d %.4lf\n", cas, ans);
}
}

66
HDOJ/4488_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define N 100000
using namespace std;
__int64 f[405][405][2],MIN,sum1,sum2;
__int64 getsMIN(__int64 x,__int64 y)
{
x=(x<0)?-x:x;
y=(y<0)?-y:y;
__int64 t=x%y;
while(t!=0)
{
x=y;
y=t;
t=x%y;
}
return y;
}
int main()
{
f[0][1][1]=1;f[0][1][2]=1;
f[1][2][1]=1;f[1][2][2]=2;
f[1][1][1]=1;f[1][1][2]=2;
int i,j,P,T,m,k,ans;
for(i=2;i<=400;i++)
{
for(j=i+1,sum1=0,sum2=1;j>1;j--)
{
f[i][j][1] = i*f[i-1][j-1][1];
f[i][j][2] = j*f[i-1][j-1][2];
MIN=getsMIN(f[i][j][1],f[i][j][2]);
f[i][j][1] /= MIN;
f[i][j][2] /= MIN;
if(sum2%f[i][j][2])
{
sum1 = sum1*f[i][j][2]+f[i][j][1]*sum2;
sum2 *= f[i][j][2];
}
else sum1 = sum1 + f[i][j][1]*(sum2/f[i][j][2]);
MIN=getsMIN(sum1,sum2);
sum1 /= MIN;
sum2 /= MIN;
}
f[i][1][2] = sum2;
f[i][1][1] = sum2-sum1;
MIN=getsMIN(f[i][1][1],f[i][1][2]);
f[i][1][1] /= MIN;
f[i][1][2] /= MIN;
}
scanf("%d",&P);
while(P--)
{
scanf("%d %d %d",&T,&m,&k);
if(!f[m][k][1])
printf("%d 0\n",T);
else
{
if(f[m][k][2]==1)
printf("%d %I64d\n",T,f[m][k][1]);
else printf("%d %I64d/%I64d\n",T,f[m][k][1],f[m][k][2]);
}
}
return 0;
}

43
HDOJ/4489_autoAC.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 1000000007
#define inf 0x3f3f3f3f
#define N 1000010
#define clr(a) (memset(a,0,sizeof(a)))
using namespace std;
LL dp[21][2],ans[21],c[21][21];
void init()
{
for(int i=0;i<=20;i++)c[i][0]=c[i][i]=1;
for(int i=1;i<=20;i++)
for(int j=1;j<i;j++)c[i][j]=c[i-1][j-1]+c[i-1][j];
dp[0][0]=dp[0][1]=dp[1][1]=dp[1][0]=1;ans[1]=1;
for(int i=2;i<=20;i++)
{
for(int j=1;j<=i;j++)
{
ans[i]+=c[i-1][j-1]*dp[j-1][0]*dp[i-j][1];
}
dp[i][0]=dp[i][1]=ans[i]/2;
}
}
int main()
{
int t,cas,n;init();
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&cas,&n);
printf("%d %I64d\n",cas,ans[n]);
}
}

149
HDOJ/4490_autoAC.cpp Normal file
View File

@ -0,0 +1,149 @@
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<cctype>
#include<map>
#include<string>
#include<set>
#include<queue>
#include<vector>
using namespace std;
const int N = 100005;
const int M = 8;
const int INF = 0x3f3f3f3f;
const double eps = 1e-5;
const double PI = acos(-1.0);
typedef __int64 ll;
map<int,int> mp[M][M];
struct node
{
int num[3];
string ans;
}ss,now;
int lcm[3][3];
int sa,sb,sc,ea,eb,ec;
struct wocao
{
struct node t[N];
int head,tail;
void init()
{
head = tail = 0;
}
bool empty()
{
return head == tail;
}
void push(struct node a)
{
t[tail] = a;
tail ++;
if(tail >= N)
tail -= N;
}
struct node front()
{
return t[head];
}
void pop()
{
head ++;
if(head >= N)
head -= N;
}
}q;
void bfs()
{
ss.ans.clear();
ss.num[0] = sa;ss.num[1] = sb;ss.num[2] = sc;
for(int i = 0;i < M;i ++)
for(int j = 0;j < M;j ++)
mp[i][j].clear();
mp[sa][sb][sc] = 1;
q.init();
q.push(ss);
while(!q.empty())
{
now = q.front();
q.pop();
int i;
for(i = 0;i < 3;i ++)
{
ss = now;
if(ss.num[i])
{
ss.num[i] --;
ss.num[0] += lcm[i][0];
ss.num[1] += lcm[i][1];
ss.num[2] += lcm[i][2];
if(ss.num[0] >= M || ss.num[1] >= M || ss.num[2] >= M)
continue;
if(mp[ss.num[0]][ss.num[1]].find(ss.num[2]) == mp[ss.num[0]][ss.num[1]].end())
{
mp[ss.num[0]][ss.num[1]][ss.num[2]] = 1;
ss.ans += 'A' + i;
if(ss.num[0] == ea && ss.num[1] == eb && ss.num[2] == ec)
{
cout<<ss.ans.length()<<" "<<ss.ans<<endl;
return;
}
q.push(ss);
}
}
}
for(i = 0;i < 3;i ++)
{
ss = now;
if(ss.num[0] >= lcm[i][0] && ss.num[1] >= lcm[i][1] && ss.num[2] >= lcm[i][2])
{
ss.num[0] -= lcm[i][0];
ss.num[1] -= lcm[i][1];
ss.num[2] -= lcm[i][2];
ss.num[i] ++;
if(ss.num[0] >= M || ss.num[1] >= M || ss.num[2] >= M)
continue;
if(mp[ss.num[0]][ss.num[1]].find(ss.num[2]) == mp[ss.num[0]][ss.num[1]].end())
{
mp[ss.num[0]][ss.num[1]][ss.num[2]] = 1;
ss.ans += 'a' + i;
if(ss.num[0] == ea && ss.num[1] == eb && ss.num[2] == ec)
{
cout<<ss.ans.length()<<" "<<ss.ans<<endl;
return;
}
q.push(ss);
}
}
}
}
puts("NO SOLUTION");
}
int main()
{
int cas,t,n;
int i,j;
scanf("%d",&t);
while(t --)
{
scanf("%d%d",&cas,&n);
for(i = 0;i < 3;i ++)
{
for(j = 0;j < 3;j ++)
{
scanf("%d",&lcm[i][j]);
}
}
int ca;
printf("%d %d\n",cas,n);
while(n --)
{
scanf("%d%d%d%d%d%d%d",&ca,&sa,&sb,&sc,&ea,&eb,&ec);
printf("%d ",ca);
bfs();
}
}
return 0;
}

109
HDOJ/4491_autoAC.cpp Normal file
View File

@ -0,0 +1,109 @@
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#define eps 1e-6
#define INF 0x1f1f1f1f
#define PI acos(-1.0)
#define ll __int64
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
using namespace std;
struct Point
{
double x,y;
}pp[22],p1,p2,p3;
Point oa,ob;
double dis(Point a)
{
return sqrt(a.x*a.x+a.y*a.y);
}
double dmult(Point a,Point b)
{
return a.x*b.x+a.y*b.y;
}
double xmult(Point a,Point b)
{
return a.x*b.y-a.y*b.x;
}
int main()
{
int t,d,m,s,be,aa;
double an;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d%lf",&d,&m,&s,&be,&an);
an=(an/180.0)*PI;
for(int i=1;i<=m;i++)
scanf("%d%lf%lf",&aa,&pp[i].x,&pp[i].y);
int la=be;
oa.x=cos(an),oa.y=sin(an);
ob.x=-oa.x,ob.y=-oa.y;
int flag,find;
double Max;
printf("%d ",d);
while(s--)
{
Max=-120;
for(int i=1;i<=m;i++)
{
if(i==be||i==la)
continue;
Point tmp;
tmp.x=pp[i].x-pp[be].x;
tmp.y=pp[i].y-pp[be].y;
double tt=xmult(oa,tmp);
if(tt>0)
{
double t=dmult(oa,tmp)/dis(oa)/dis(tmp);
if(t>Max)
{
Max=t;
flag=1;
find=i;
}
}
else if(tt<0)
{
double t=dmult(ob,tmp)/dis(ob)/dis(tmp);
if(t>Max)
{
Max=t;flag=2;find=i;
}
}
}
printf("%d",find);
la=be;
be=find;
if(s)
putchar(' ');
else
putchar('\n');
if(flag==1)
{
ob.x=pp[la].x-pp[be].x;
ob.y=pp[la].y-pp[be].y;
oa.x=-ob.x;
oa.y=-ob.y;
}
else
{
oa.x=pp[la].x-pp[be].x;
oa.y=pp[la].y-pp[be].y;
ob.x=-oa.x;
ob.y=-oa.y;
}
}
}
return 0;
}

32
HDOJ/4492_autoAC.cpp Normal file
View File

@ -0,0 +1,32 @@
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
char str1[200];
char str2[200];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int d,n;
scanf("%d%*c",&d);
gets(str1);
int len=strlen(str1);
scanf(" %d",&n);
int p=0;
for(int i=0;i<n;i++)
{
int x;
scanf("%d",&x);
p = (p+x+len)%len;
str2[i]=str1[p];
}
printf("%d ",d);
for(int i=0;i<n;i++)
printf("%c",str2[i]);
printf("\n");
}
return 0;
}

38
HDOJ/4493_autoAC.cpp Normal file
View File

@ -0,0 +1,38 @@
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
double sum = 0;
double a;
for(int i = 0;i < 12;i++)
{
scanf("%lf",&a);
sum += a;
}
sum /= 12;
char str[1000];
sprintf(str,"%.2f",sum);
int len = strlen(str);
if(str[len-2] == '0' && str[len-1] == '0')
str[len-3] = 0;
else if(str[len-1] == '0')
str[len-1] = 0;
printf("$%s\n",str);
}
return 0;
}

151
HDOJ/4494_autoAC.cpp Normal file
View File

@ -0,0 +1,151 @@
#include<iostream>
#include<cstring>
#include<cstring>
#include<cmath>
#include<cstdio>
#define inf 100000000
using namespace std;
const int Maxn = 600;
struct Edge{
int v;
int val;
int cost;
int next;
}edge[Maxn*600];
struct Point{
double x,y;
}p[Maxn];
int head[Maxn],n,m,k;
int e;
int dis[Maxn],pre[Maxn], pos[Maxn],sta[Maxn],en[Maxn],ty[Maxn][20],flow;
int que[Maxn*600];
double d[Maxn][Maxn];
bool vis[Maxn];
void add(int u, int v, int val, int cost)
{
edge[e].v = v;
edge[e].val = val;
edge[e].cost = cost;
edge[e].next = head[u];
head[u] = e++;
edge[e].v = u;
edge[e].val = 0;
edge[e].cost = -cost;
edge[e].next = head[v];
head[v] = e++;
}
double DIS(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void init()
{
memset(head,-1,sizeof(head));
e=0;
}
bool spfa(int s, int t)
{
int i;
memset(pre, -1, sizeof(pre));
memset(vis, 0, sizeof(vis));
int Head, tail;
Head = tail = 0;
for(i = 0; i < Maxn; i++)
dis[i] = inf;
que[tail++] = s;
pre[s] = s;
dis[s] = 0;
vis[s] = 1;
while(Head != tail)
{
int now = que[Head++];
vis[now] = 0;
for(i=head[now]; i != -1; i = edge[i].next)
{
int adj = edge[i].v;
if(edge[i].val > 0 && dis[now] + edge[i].cost < dis[adj])
{
dis[adj] = dis[now] + edge[i].cost;
pre[adj] = now;
pos[adj] = i;
if(!vis[adj])
{
vis[adj] = 1;
que[tail++] = adj;
}
}
}
}
return pre[t] != -1;
}
int MinCostFlow(int s, int t)
{
int i;
int cost = 0;
flow = 0;
while(spfa(s, t))
{
int f = 100000000;
for(i = t; i != s; i = pre[i])
if (edge[pos[i]].val < f)
f = edge[pos[i]].val;
flow += f;
cost += dis[t] * f;
for(i = t; i != s; i = pre[i])
{
edge[pos[i]].val -= f;
edge[pos[i] ^ 1].val += f;
}
}
return cost;
}
void build(int type)
{
int i,j;
init();
for(i=2;i<=n;i++){
add(1,i,ty[i][type],1);
add(i,i+2*n,ty[i][type],0);
add(1,i+n,ty[i][type],0);
add(i+2*n,3*n+1,ty[i][type],0);
for(j=2;j<=n;j++){
if(sta[i]+en[i]+d[i][j]<=sta[j]){
add(i+n,j+2*n,ty[i][type],0);
}
}
}
}
int solve()
{
int i,j,u,v;
int ans=0;
for(i=1;i<=m;i++){
build(i);
ans+=MinCostFlow(1,3*n+1);
}
return ans;
}
int main()
{
int i,j,u,v,c,t;
scanf("%d",&t);
while(t--)
{
init();
scanf("%d%d",&n,&m);
scanf("%lf%lf",&p[1].x,&p[1].y);
for(i=2;i<=n;i++){
scanf("%lf%lf%d%d",&p[i].x,&p[i].y,&sta[i],&en[i]);
for(j=1;j<=m;j++){
scanf("%d",&ty[i][j]);
}
}
for(i=1;i<=n;i++){
for(j=i+1;j<=n;j++){
d[i][j]=d[j][i]=DIS(p[i],p[j]);
}
}
printf("%d\n",solve());
}
return 0;
}

90
HDOJ/4495_autoAC.cpp Normal file
View File

@ -0,0 +1,90 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#define MAXN 505
#define INF 1000000007
using namespace std;
const int dir[4][2]={{1,1},{-1,1},{1,-1},{-1,-1}};
int n, m,ans;
int dp[MAXN][MAXN][4],to[4]={3,2,1,0};
char st[MAXN][MAXN];
long long p[MAXN], r[MAXN][MAXN], c[MAXN][MAXN];
void cal(int x,int y,int k)
{
int now=dp[x][y][k];
x+=dir[k][0],y+=dir[k][1];
while(x>=0&&x<n&&y>=0&&y<m)
{
if(dp[x][y][to[k]]<=now) now=dp[x][y][to[k]];
else if(dp[x][y][to[k]]>=now+2)
now+=2;
else ++now;
ans=max(now,ans);
x+=dir[k][0],y+=dir[k][1];
}
}
long long ac1(int rr,int x,int y)
{
int l=y-x+1;
if (!x) return r[rr][y];
return r[rr][y]-r[rr][x-1]*p[l];
}
long long ac2(int cc,int x,int y)
{
int l=y-x+1;
if (!x) return c[cc][y];
return c[cc][y]-c[cc][x-1]*p[l];
}
int main()
{
int cas;
scanf("%d",&cas);
p[0]=1;
for (int i=1;i<=500;i++) p[i]=p[i-1]*131;
while (cas--)
{
ans=1;
scanf("%d%d",&n,&m);
for (int i=0;i<n;i++)
scanf("%s",st[i]);
for (int i=0;i<n;i++)
{
r[i][0]=st[i][0];
for (int j=1;j<m;j++)
r[i][j]=r[i][j-1]*131+st[i][j];
}
for (int i=0;i<m;i++)
{
c[i][0]=st[0][i];
for (int j=1;j<n;j++)
c[i][j]=c[i][j-1]*131+st[j][i];
}
for(int i=0;i<n;++i)
for(int j=0;j<m;++j)
for(int k=0;k<4;++k)
{
int a=k&1?i+1:n-i,b=k<2?m-j:j+1,l=0,r,mid,nx,ny,x2,y2;
r=min(a-1,b-1);
while(l<r)
{
mid=(l+r+1)/2;
nx=i+mid*dir[k][0],ny=j+mid*dir[k][1];
x2=i,y2=j;
if(x2<nx) swap(x2,nx);
if(y2<ny) swap(y2,ny);
if(ac1(i,ny,y2)==ac2(j,nx,x2)) l=mid;
else r=mid-1;
}
dp[i][j][k]=l+1;
}
for(int i=0;i<m;++i)
cal(0,i,2),cal(0,i,0),cal(n-1,i,1),cal(n-1,i,3);
for(int i=0;i<n;++i)
cal(i,m-1,2),cal(i,m-1,3),cal(i,0,1),cal(i,0,0);
printf("%d\n",ans*(ans+1)/2);
}
return 0;
}

49
HDOJ/4496_autoAC.cpp Normal file
View File

@ -0,0 +1,49 @@
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MAXN = 10010;
int F[MAXN];
int find(int x)
{
if(F[x] == -1)return x;
return F[x] = find(F[x]);
}
pair<int,int>p[100010];
int ans[100010];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m) == 2)
{
memset(F,-1,sizeof(F));
for(int i = 1;i <= m;i++)
{
scanf("%d%d",&p[i].first,&p[i].second);
}
ans[m] = n;
for(int i = m;i > 1;i --)
{
int t1 = find(p[i].first);
int t2 = find(p[i].second);
if(t1 != t2)
{
ans[i-1] = ans[i] - 1;
F[t1] = t2;
}
else ans[i-1] = ans[i];
}
for(int i = 1;i <= m;i++)
printf("%d\n",ans[i]);
}
return 0;
}

72
HDOJ/4497_autoAC.cpp Normal file
View File

@ -0,0 +1,72 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <queue>
#include <deque>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <vector>
#include <utility>
#include <functional>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <cassert>
#include <ctime>
#include <iterator>
const int INF = 0x3f3f3f3f;
const int dir[8][2] = {{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};
using namespace std;
#define LL __int64
const int MAX=100002;
int prime[MAX];
bool flag[MAX];
int main(void)
{
int t;
scanf("%d", &t);
memset(flag, true, sizeof(flag));
int cnt = 0;
for (int i = 2; i * i <= 100000; ++i)
{
if (flag[i])
for (int j = i*2; j <= 100000; j+=i)
flag[j] = false;
}
for (int i = 2; i <= 100000; ++i) if(flag[i]) prime[cnt++] = i;
while (t--)
{
int G, L;
scanf("%d%d", &G, &L);
int ans = 1;
if (L % G)
{
printf("0\n");
continue;
}
int K = L / G, S = K;
for (int i = 0; i < cnt; ++i)
{
if (prime[i] * prime[i] > S) break;
if (K%prime[i] == 0)
{
int touch = 0;
while (K%prime[i] == 0)
{
K /= prime[i]; touch++;
}
ans *= touch * 6;
}
}
if (K != 1) ans *= 6;
printf("%d\n", ans);
}
return 0;
}

115
HDOJ/4498_autoAC.cpp Normal file
View File

@ -0,0 +1,115 @@
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
vector<double>x;
void add(int a1,int b1,int c1)
{
if(a1 == 0 && b1 == 0)
{
return;
}
if(a1 == 0)
{
double t = -c1*1.0/b1;
if(t >= 0 && t <= 100)
x.push_back(t);
return;
}
long long deta = b1*b1 - 4LL*a1*c1;
if(deta < 0)return;
if(deta == 0)
{
double t = (-1.0 * b1)/(2.0 * a1);
if(t >= 0 && t <= 100)
x.push_back(t);
}
else
{
double t1 = (-1.0 * b1 + sqrt(1.0*deta))/(2.0*a1);
double t2 = (-1.0 * b1 - sqrt(1.0*deta))/(2.0*a1);
if(t1 >= 0 && t1 <= 100)
x.push_back(t1);
if(t2 >= 0 && t2 <= 100)
x.push_back(t2);
}
}
int A[100],B[100],C[100];
int best;
double F(double x1)
{
return sqrt(1.0 + (x1*2*A[best] + 1.0 * B[best])*(x1*2*A[best] + 1.0 * B[best]));
}
double simpson(double a,double b)
{
double c = a + (b-a)/2;
return (F(a) + 4*F(c) + F(b))*(b-a)/6;
}
double asr(double a,double b,double eps,double A)
{
double c = a + (b-a)/2;
double L = simpson(a,c);
double R = simpson(c,b);
if(fabs(L+R-A) <= 15*eps)return L+R+(L+R-A)/15;
return asr(a,c,eps/2,L) + asr(c,b,eps/2,R);
}
double asr(double a,double b,double eps)
{
return asr(a,b,eps,simpson(a,b));
}
int main()
{
int T;
int k,a,b;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
A[0] = 0;
B[0] = 0;
C[0] = 100;
for(int i = 1;i <= n;i++)
{
scanf("%d%d%d",&k,&a,&b);
A[i] = k;
B[i] = -2*a*k;
C[i] = k*a*a + b;
}
x.clear();
for(int i = 0;i <= n;i++)
for(int j = i+1;j <= n;j++)
add(A[i]-A[j],B[i] - B[j],C[i] - C[j]);
double ans = 0;
x.push_back(0);
x.push_back(100);
sort(x.begin(),x.end());
int sz = x.size();
for(int i = 0;i < sz-1;i++)
{
double x1 = x[i];
double x2 = x[i+1];
if(fabs(x2-x1) < 1e-8)continue;
double mid = (x1 + x2)/2;
best = 0;
for(int j = 1;j <= n;j++)
{
double tmp1 = mid*mid*A[best] + mid*B[best] + C[best];
double tmp2 = mid*mid*A[j] + mid*B[j] + C[j];
if(tmp2 < tmp1)best = j;
}
ans += asr(x1,x2,1e-8);
}
printf("%.2lf\n",ans);
}
return 0;
}

74
HDOJ/4499_autoAC.cpp Normal file
View File

@ -0,0 +1,74 @@
#include <iostream>
#include <cstring>
#include <stdio.h>
using namespace std;
const int MAX_N = 5 + 2;
int vis[MAX_N][MAX_N];
int n, m, q, _max;
bool decide(int i, int j)
{
int flag = 0;
if(vis[i][j] == 1)
return false;
for(int k = i - 1; k >= 0; k--)
{
if(vis[k][j] == 1)
flag++;
else if(vis[k][j] == 2)
{
if(flag == 1)
return false;
else
flag++;
}
}
flag = 0;
for(int k = j - 1; k >= 0; k--)
{
if(vis[i][k] == 1)
flag++;
else if(vis[i][k] == 2)
{
if(flag == 1)
return false;
else
flag++;
}
}
return true;
}
void dfs(int cnt, int x, int y)
{
for(int i = x; i < n; i++)
{
for(int j = 0; j < m; j++)
{
if(x == i && j < y)
continue;
if(decide(i, j))
{
vis[i][j] = 2;
dfs(cnt + 1, i, j + 1);
vis[i][j] = 0;
}
}
}
_max = max(_max, cnt);
}
int main()
{
while(scanf("%d%d%d", &n, &m, &q) != EOF)
{
int a, b;
_max = 0;
memset(vis, 0, sizeof(vis));
for(int i = 0; i < q; i++)
{
scanf("%d%d", &a, &b);
vis[a][b] = 1;
}
dfs(0, 0, 0);
cout << _max << endl;
}
return 0;
}