Powered By HC TECH : AutoACer Engine

1500-1599
This commit is contained in:
KiritoTRw 2016-08-14 17:42:04 +08:00 committed by GitHub
parent e56ca966bc
commit ed2de2e423
94 changed files with 5629 additions and 0 deletions

25
HDOJ/1500_autoAC.cpp Normal file
View File

@ -0,0 +1,25 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define NN 5010
#define Min(a,b) (a>b?b:a)
int set[NN];
int dp[1010][NN];
int main()
{
int t,k,n,i,j;
scanf("%d",&t);
while(t--){
scanf("%d%d",&k,&n);
k += 8;
for(i=n;i>=1;i--)
scanf("%d",&set[i]);
for(i=1;i<=k;i++){
dp[i][i*3] = dp[i-1][i*3-2] + (set[i*3-1] - set[i*3])*(set[i*3-1] - set[i*3]);
for(j=i*3+1;j<=n;j++)
dp[i][j] = Min(dp[i][j-1],dp[i-1][j-2] +(set[j-1] -set[j])*(set[j-1] - set[j]));
}
printf("%d\n",dp[k][n]);
}
return 0;
}

38
HDOJ/1501_autoAC.cpp Normal file
View File

@ -0,0 +1,38 @@
#include<stdio.h>
#include<string.h>
int v[205][205];
char a[205],b[205],c[410];
int lena,lenb,lenc;
int dfs(int i,int j,int k)
{
if(c[k]=='\0')
return 1;
if(v[i][j])return 0;
v[i][j]=1;
if(lena<lenc)
if(a[i]==c[k] && dfs(i+1,j,k+1))return 1;
if(lenb<lenc)
if(b[j]==c[k] && dfs(i,j+1,k+1))return 1;
return 0;
}
int main()
{
int t,k;
while(scanf("%d",&t)!=EOF)
{
k=1;
while(k<=t){
scanf("%s%s%s",&a,&b,&c);
lena=strlen(a);
lenb=strlen(b);
lenc=strlen(c);
memset(v,0,sizeof(v));
if(dfs(0,0,0)==1)
printf("Data set %d: yes\n",k);
else
printf("Data set %d: no\n",k);
k++;
}
}
return 0;
}

73
HDOJ/1502_autoAC.cpp Normal file
View File

@ -0,0 +1,73 @@
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#define N 1000000
int f[70][70][70][30] = {0};
int main()
{
int i, j, k, n, p;
f[0][0][0][0] = f[1][0][0][0] = f[1][1][0][0] = f[1][1][1][0] = 1;
for(i = 2; i < 65; i++)
{
for(j = 0; j <= i; j++)
{
for(k = 0; k <=j; k++)
{
if(i - 1 >= j)
{
for(p = 0; p < 30; p++)
{
f[i][j][k][p] += f[i - 1][j][k][p];
if(f[i][j][k][p] >= N)
{
f[i][j][k][p + 1] += f[i][j][k][p]/N;
f[i][j][k][p] %= N;
}
}
}
if(j - 1 >= k) {
for(p = 0; p < 30; p++)
{
f[i][j][k][p] += f[i][j - 1][k][p];
if(f[i][j][k][p] >= N)
{
f[i][j][k][p + 1] += f[i][j][k][p]/N;
f[i][j][k][p] %= N;
}
}
}
if(k - 1 >= 0)
{
for(p = 0; p < 30; p++)
{
f[i][j][k][p] += f[i][j][k - 1][p];
if(f[i][j][k][p] >= N)
{
f[i][j][k][p + 1] += f[i][j][k][p]/N;
f[i][j][k][p] %= N;
}
}
}
}
}
}
while(scanf("%d",&n) != EOF)
{
for(i = 29; i >= 0; i--)
{
if(f[n][n][n][i])
{
printf("%d",f[n][n][n][i]);
break;
}
}
for(i--; i >= 0; i--)
{
printf("%06d",f[n][n][n][i]);
}
printf("\n\n");
}
return 0;
}

71
HDOJ/1503_autoAC.cpp Normal file
View File

@ -0,0 +1,71 @@
#include <cstdio>
#include <cstring>
using namespace std;
char str1[105],str2[105];
int dp[105][105];
int record[205][205];
void output(int l1,int l2)
{
if(l1==0&&l2==0)return;
if(record[l1][l2]==2)
{
output(l1-1,l2-1);
printf("%c",str1[l1-1]);
}
if(record[l1][l2]==1)
{
output(l1,l2-1);
printf("%c",str2[l2-1]);
}
if(record[l1][l2]==3)
{
output(l1-1,l2);
printf("%c",str1[l1-1]);
}
}
int main()
{
while(~scanf("%s%s",&str1,&str2))
{
memset(dp,0,sizeof(dp));
int len1=strlen(str1);
int len2=strlen(str2);
int count=0;
for(int i=1;i<=len1;i++)
{
dp[i][0]=i;
record[i][0]=3;
}
for(int i=1;i<=len2;i++)
{
dp[0][i]=i;
record[0][i]=1;
}
for(int i=1;i<=len1;i++)
{
for(int j=1;j<=len2;j++)
{
if(str1[i-1]==str2[j-1])
{
dp[i][j]=dp[i-1][j-1]+1;
record[i][j]=2;
}
else
{
if(dp[i-1][j]<dp[i][j-1])
{
dp[i][j]=dp[i-1][j]+1;
record[i][j]=3;
}
else
{
dp[i][j]=dp[i][j-1]+1;
record[i][j]=1;
}
}
}
}
output(len1,len2);
printf("\n");
}
}

87
HDOJ/1504_autoAC.cpp Normal file
View File

@ -0,0 +1,87 @@
#include<stdio.h>
#include<string.h>
struct node{
node *child;
node *brother;
char key[10];
};
int n,m;
node *root;
void getname(char *str,char *key,int &j)
{
int i;
for(i=0;str[j]!='\0'&&str[j]!='\\';i++,j++)
key[i]=str[j];
key[i]='\0';
}
node *insert(node *parent,char *key)
{
node *p,*q,*t;
if(!parent->child||strcmp(parent->child->key,key)>0)
{
t=new node;
strcpy(t->key,key);
t->child=NULL;
t->brother=parent->child;
parent->child=t;
return t;
}
if(strcmp(parent->child->key,key)==0)
return parent->child;
for(p=parent->child,q=p->brother;q&&strcmp(q->key,key)<0;p=q,q=q->brother);
if(!q||strcmp(q->key,key)>0)
{
t=new node;
strcpy(t->key,key);
t->brother=p->brother;
p->brother=t;
t->child=NULL;
return t;
}
return q;
}
void read()
{
char str[90],key[9];
int i,cur;
node *p;
root=new node;
root->child=NULL;
scanf("%d",&n);
for(i=m=0;i<n;i++)
{
cur=0;
scanf("%s",str);
getname(str,key,cur);
for(p=insert(root,key);str[cur]!='\0';)
{
getname(str,key,++cur);
p=insert(p,key);
}
}
}
void find(node *p,int k)
{
int i;
for(;p;p=p->brother)
{
for(i=0;i<k;i++)
putchar(' ');
puts(p->key);
if(p->child)
find(p->child,k+1);
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
read();
find(root->child,0);
if(t)
printf("\n");
}
return 0;
}

65
HDOJ/1505_autoAC.cpp Normal file
View File

@ -0,0 +1,65 @@
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
char str[1100][1100];
int s[1100][1100];
struct Q
{
int num,left,right;
}q[1100];
int main()
{
int t;
int row,col;
int i,j,max;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&row,&col);
for(i=1;i<=row;i++)
for(j=1;j<=col;j++)
{
cin>>str[i][j];
}
for(i=0;i<=col;i++)
s[0][i]=0;
for(i=1;i<=row;i++)
for(j=1;j<=col;j++)
{
if(str[i][j]!='R')
s[i][j]=s[i-1][j]+1;
else
s[i][j]=0;
}
max=0;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
q[j].num=s[i][j];
q[j].left=j;
q[j].right=j;
}
q[0].num=-1;
q[col+1].num=-1;
for(j=1;j<=col;j++)
{
while(q[q[j].left-1].num>=q[j].num)
q[j].left=q[q[j].left-1].left;
}
for(j=col;j>=1;j--)
{
while(q[q[j].right+1].num>=q[j].num)
q[j].right=q[q[j].right+1].right;
}
for(j=1;j<=col;j++)
{
if(max<q[j].num*(q[j].right-q[j].left+1))
max=q[j].num*(q[j].right-q[j].left+1);
}
}
printf("%d\n",max*3);
}
return 0;
}

45
HDOJ/1506_autoAC.cpp Normal file
View File

@ -0,0 +1,45 @@
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
#define maxn 100005
int h[maxn];
int z[maxn], y[maxn];
int main()
{
int n;
while(scanf("%d", &n), n)
{
int i, j, k=0, x;
__int64 max=0, sum;
for(i=0; i<n; i++)
{
z[i] = i;
y[i] = i;
}
for(i=0; i<n; i++)
{
scanf("%d", &h[i]);
j = i-1;
while(z[i] && h[i] <= h[j])
{
z[i] = z[j];
j = z[j] - 1;
}
}
for(i=n-1; i>=0; i--)
{
j = i+1;
while(y[i] != n-1 && h[i] <= h[j])
{
y[i] = y[j];
j = y[j] + 1;
}
sum = (__int64)h[i] * (y[i] - z[i] + 1);
if(max < sum)max = sum;
}
printf("%I64d\n", max);
}
return 0;
}

90
HDOJ/1507_autoAC.cpp Normal file
View File

@ -0,0 +1,90 @@
#include <stdio.h>
#include <string.h>
int map[105][105];
int m,n,max;
int x_la[50],y_la[50];
int x_temp[50],y_temp[50];
int can_move(int x,int y)
{
if(x>0&&x<=n&&y>0&&y<=m)
{
return 1;
}
return 0;
}
void DFS(int x,int y,int num)
{
int i,j,k;
if(num>max)
{
max=num;
for(i=0;i<num*2;i++)
{
x_la[i]=x_temp[i];
y_la[i]=y_temp[i];
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(map[i][j]==0)
{
for(k=1;k<3;k++)
{
if(k==1)
{
if(can_move(i+1,j)&&map[i+1][j]==0)
{
map[i][j]=1;
map[i+1][j]=1;
x_temp[num*2]=i;
y_temp[num*2]=j;
x_temp[num*2+1]=i+1;
y_temp[num*2+1]=j;
DFS(i,j,num+1);
}
}
else
{
if(can_move(i,j+1)&&map[i][j+1]==0)
{
map[i][j]=1;
map[i][j+1]=1;
x_temp[num*2]=i;
y_temp[num*2]=j;
x_temp[num*2+1]=i;
y_temp[num*2+1]=j+1;
DFS(i,j,num+1);
}
}
}
}
}
}
}
int main()
{
int t;
int a,b,i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0)
break;
memset(map,0,sizeof(map));
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d%d",&a,&b);
map[a][b]=1;
}
max=0;
DFS(1,1,0);
printf("%d\n",max);
for(i=0;i<max;i++)
{
printf("(%d,%d)--(%d,%d)\n",x_la[i*2],y_la[i*2],x_la[i*2+1],y_la[i*2+1]);
}
}
return 0;
}

42
HDOJ/1508_autoAC.cpp Normal file
View File

@ -0,0 +1,42 @@
#include<iostream>
#include<string>
using namespace std;
int f[100000];
int main()
{
string s;
while(cin>>s&&s!="0")
{
int i;
for(i=0;i<s.size()+1;i++)
f[i]=1;
i=s.size()-1;
while(s[i]=='0')
{
i--;if(i==0)goto end;
i--;if(i==0)goto end;
}
i--;
for(;i>=0;i--)
{
while(s[i]=='0')
{
i--;f[i]=f[i+2];if(i==0)goto end;
i--;f[i]=f[i+1];if(i==0)goto end;
if(s[i]=='0')continue;
i--;
}
if(s[i]<='2')
{
f[i]=f[i+1]+f[i+2];
}
else
{
f[i]=f[i+1];
}
}
end:
cout<<f[0]<<endl;
}
return 0;
}

53
HDOJ/1509_autoAC.cpp Normal file
View File

@ -0,0 +1,53 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
using namespace std;
struct node
{
char name[20];
int par,pri,id;
friend bool operator < (node a,node b)
{
if(a.pri!=b.pri)
{
return a.pri>b.pri;
}
else return a.id>b.id;
}
};
int main()
{
char c[5],na[20];
int pout,cnt=0,pcmp;
node t;
priority_queue<node> q;
while(scanf("%s",c)!=EOF)
{
if(c[0]=='P')
{
scanf("%s %d %d",na,&pout,&pcmp);
strcpy(t.name,na);
t.par=pout;
t.pri=pcmp;
t.id=++cnt;
q.push(t);
}
else
{
if(!q.empty())
{
t=q.top();
q.pop();
printf("%s %d\n",t.name,t.par);
}
else
{
printf("EMPTY QUEUE!\n");
}
}
}
return 0;
}

41
HDOJ/1510_autoAC.cpp Normal file
View File

@ -0,0 +1,41 @@
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
const int maxn=110;
char board[maxn];
int w[maxn][maxn];
int Min(int x,int y)
{
return (x<y)?x:y;
}
int main()
{
int n,ans,Max;
while(~scanf("%d",&n))
{
ans=0;
for(int i=1;i<=n;i++)
{
scanf("%s",&board);
w[i][n-1]=(board[n-1]=='#')?0:1;
for(int j=n-2;j>=0;j--)
{
if(board[j]=='#') w[i][j]=0;
else w[i][j]=w[i][j+1]+1;
}
for(int j=0;j<n;j++)
{
Max=maxn;
for(int k=i;k>0;k--)
{
Max=Min(Max,w[k][j]);
if(Max==0) break;
ans+=Max;
}
}
}
printf("%d\n",ans);
}
return 0;
}

58
HDOJ/1511_autoAC.cpp Normal file
View File

@ -0,0 +1,58 @@
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define MAX_N 100
#define max(a,b) (a)>(b)?(a):(b)
int leftdp[MAX_N],rightdp[MAX_N];
char s[MAX_N];
bool compare(int s1,int s2,int t1,int t2){
while(s[s1]=='0')
s1++;
while(s[t1]=='0')
t1++;
if(s2-s1!=t2-t1)
return s2-s1<t2-t1;
while(s1<s2){
if(s[s1]!=s[t1])
return s[s1]<s[t1];
s1++,t1++;
}
return false;
}
void OutputPath(int len){
int p=rightdp[0],i=0;
while(i<len){
while(i<p && i<len)
cout<<s[i++];
if(i<len)
cout<<",";
p=rightdp[p];
}
cout<<endl;
}
int main(){
while(scanf("%s",&s)!=EOF){
int i,j,len;
len=strlen(s);
if(len==1 && s[0]=='0')
break;
memset(leftdp,0,sizeof(leftdp));
memset(rightdp,0,sizeof(rightdp));
for(i=1;i<len;i++)
for(j=0;j<i;j++)
if(compare(leftdp[j],j+1,j+1,i+1))
leftdp[i]=max(leftdp[i],j+1);
int tlen=leftdp[len-1];
rightdp[tlen]=rightdp[tlen+1]=len;
i=tlen-1;
while(s[i]=='0')
rightdp[i--]=len;
for(i=tlen-1;i>=0;i--)
for(j=i;j<=tlen;j++)
if(compare(i,j,j,rightdp[j]))
rightdp[i]=max(rightdp[i],j);
OutputPath(len);
}
return 0;
}

121
HDOJ/1512_autoAC.cpp Normal file
View File

@ -0,0 +1,121 @@
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=100003;
int n,m;
int f[maxn];
typedef struct node
{
int sword;
int dis;
node* left;
node* right;
}*Pnode;
Pnode tree[maxn];
int distance(Pnode t)
{
return t?t->dis:0;
}
void fixdis(Pnode t)
{
if(distance(t->left)<distance(t->right))
swap(t->left,t->right);
t->dis=distance(t->right)+1;
}
Pnode merge(Pnode a,Pnode b)
{
if(!a)
return b;
if(!b)
return a;
if(b->sword>a->sword)
swap(a,b);
a->right=merge(a->right,b);
fixdis(a);
return a;
}
Pnode delMax(Pnode t)
{
if(t)
return merge(t->left,t->right);
return NULL;
}
void init(Pnode &t,int sword)
{
t=new node;
t->dis=1;
t->left=t->right=NULL;
t->sword=sword;
}
int find(int x)
{
int y,root,t;
y=x;
while(f[y]>0)
y=f[y];
root=y;
y=x;
while(f[y]>0)
{
t=f[y];
f[y]=root;
y=t;
}
return root;
}
int unio(int x,int y)
{
if(f[x]<f[y])
{
f[x]+=f[y];
return f[y]=x;
}
else
{
f[y]+=f[x];
return f[x]=y;
}
}
void solve(int u,int v)
{
int fu=find(u);
int fv=find(v);
if(fu==fv)
{
cout<<-1<<endl;
return;
}
Pnode p1,p2,p3,p4;
init(p1,tree[fu]->sword/2);
p2=delMax(tree[fu]);
p2=merge(p1,p2);
init(p3,tree[fv]->sword/2);
p4=delMax(tree[fv]);
p4=merge(p3,p4);
fv=unio(fu,fv);
tree[fv]=merge(p2,p4);
cout<<tree[fv]->sword<<endl;
}
int main()
{
int i,d,u,v;
while(cin>>n)
{
for(i=1;i<=n;++i)
{
cin>>d;
init(tree[i],d);
}
memset(f,-1,sizeof(f));
cin>>m;
for(i=0;i<m;++i)
{
cin>>u>>v;
solve(u,v);
}
for(i=1;i<=n;++i)
delete tree[i];
}
return 0;
}

42
HDOJ/1513_autoAC.cpp Normal file
View File

@ -0,0 +1,42 @@
#include<iostream>
#include<string>
using namespace std;
#define max 5000+10
#define Max(a,b) a>b?a:b
int a[2][max];
int commonorder(string s1,string s2)
{
int i,j;
memset(a,0,sizeof(a));
int m=1;
for(i=0;i<s1.length();i++)
{
for(j=0;j<s2.length();j++)
{
if(s1[i]==s2[j])
a[m][j+1]=a[(m+1)%2][j]+1;
else
a[m][j+1]=Max(a[m][j],a[(m+1)%2][j+1]);
}
m=(m+1)%2;
}
return a[(m+1)%2][s2.length()];
}
int main()
{
int n;
string str;
while(cin>>n)
{
cin>>str;
int j;
char temp[max];
int k=0;
temp[n]='\0';
for(j=n-1;j>=0;j--)
{temp[k]=str[j];k++;}
int sum=commonorder(str,temp);
printf("%d\n",n-sum);
}
return 0;
}

66
HDOJ/1514_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <set>
using namespace std;
const int candy_num = 40 + 5;
const int basket_num = 5 + 2;
int dp[candy_num][candy_num][candy_num][candy_num];
int h[basket_num], f[candy_num][basket_num];
int ans, n;
set<int> S;
set<int> ::iterator it;
int check(int a)
{
it = S.find(a);
if (it != S.end())
{
S.erase(a);
return 1;
}
S.insert(a);
return 0;
}
int dfs(int tmp)
{
if (dp[h[0]][h[1]][h[2]][h[3]] != -1)
return dp[h[0]][h[1]][h[2]][h[3]];
int tt = 0;
for (int i = 0; i < 4; i++)
{
if (h[i] < n && S.size() < 5)
{
int t = check(f[h[i]][i]);
if (S.size() < 5)
{
h[i]++;
tt = max(tt, dfs(tmp+t));
h[i]--;
}
tt = max(tt, tmp+t);
if (t == 1)
S.insert(f[h[i]][i]);
else
S.erase(f[h[i]][i]);
}
}
return dp[h[0]][h[1]][h[2]][h[3]] = tt;
}
int main()
{
while (scanf("%d", &n) != EOF && n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 4; j++)
scanf("%d", &f[i][j]);
}
ans = 0;
h[0] = h[1] = h[2] = h[3] = 0;
memset(dp, -1, sizeof(dp));
printf("%d\n", dfs(0));
}
return 0;
}

56
HDOJ/1515_autoAC.cpp Normal file
View File

@ -0,0 +1,56 @@
#include <cstdlib>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
const int maxn=100;
char x1[maxn];
char s1[maxn];
char s2[maxn];
char stack[maxn];
int len1,len2,len3;
void dfs(int x,int y,int ptr)
{
if(y==len2)
{
printf("\n");
for(int i=0;i<len3;i++)
printf("%c ",x1[i]);
}
else
{
if(x<len1)
{
char temp=stack[ptr];
x1[len3++]='i';
stack[ptr]=s1[x];
dfs(x+1,y,ptr+1);
stack[ptr]=temp;
len3--;
}
if(ptr>0&&stack[ptr-1]==s2[y])
{
x1[len3++]='o';
dfs(x,y+1,ptr-1);
len3--;
}
}
}
int main()
{
while(scanf("%s%s",&s1,&s2)!=EOF)
{
len1=strlen(s1);
len2=strlen(s2);
len3=0;
if(len1!=len2)
{
printf("[\n]\n");
continue;
}
printf("[");
dfs(0,0,0);
printf("\n]\n");
}
return 0;
}

43
HDOJ/1516_autoAC.cpp Normal file
View File

@ -0,0 +1,43 @@
#include<stdio.h>
#include<string.h>
#define SIZE 80
#define MAX 100000
int Min(int x,int y,int z){
int min= x<y ? x : y;
return min<z ? min : z;
}
int main(){
char s1[SIZE];
char s2[SIZE];
int count[SIZE+1][SIZE+1];
int flag[SIZE][SIZE];
while(scanf("%s%s",s1,s2)!=EOF){
//濮flag[][]
for(int i=0;i<SIZE;++i)
for(int j=0;j<SIZE;++j)
flag[i][j]= s1[i]==s2[j] ? 0 : 1 ;
for(int i=0;i<=SIZE;++i)
count[0][i]=count[i][0]=i;
for(int i=1;i<=strlen(s1);++i)
for(int j=1;j<=strlen(s2);++j)
count[i][j]=Min(count[i-1][j]+1,count[i][j-1]+1,count[i-1][j-1]+flag[i-1][j-1]);
printf("%d\n",count[strlen(s1)][strlen(s2)]);
for(int is1=strlen(s1),is2=strlen(s2),k=1;k<=count[strlen(s1)][strlen(s2)];){
int t;
if(is1==0&&is2!=0) t=1;
else if(is1!=0&&is2==0) t=3;
else if(is1!=0&&is2!=0){
if(count[is1][is2-1]==Min(count[is1-1][is2],count[is1-1][is2-1],count[is1][is2-1])) t=1;
if(count[is1-1][is2]==Min(count[is1-1][is2],count[is1-1][is2-1],count[is1][is2-1])) t=3;
if(count[is1-1][is2-1]==Min(count[is1-1][is2],count[is1-1][is2-1],count[is1][is2-1])) t= count[is1-1][is2-1]==count[is1][is2] ? 0:2;
}
switch(t){
case 0 : --is1;--is2;break;
case 1 : printf("%d Insert %d,%c\n",k++,is1+1,s2[--is2]);break;
case 2 : printf("%d Replace %d,%c\n",k++,is1--,s2[--is2]);break;
case 3 : printf("%d Delete %d\n",k++,is1--);break;
}
}
}
return 0;
}

16
HDOJ/1517_autoAC.cpp Normal file
View File

@ -0,0 +1,16 @@
#include<iostream>
using namespace std;
int main()
{
double n;
while(cin>>n)
{
while(n>18)
n/=18;
if(n<=9)
cout<<"Stan wins."<<endl;
else
cout<<"Ollie wins."<<endl;
}
return 0;
}

71
HDOJ/1518_autoAC.cpp Normal file
View File

@ -0,0 +1,71 @@
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int a[25],sji,d[25],ji,n;
bool b[25];
void dfs(int digit,int num,int bj)
{
if(num == ji)
{
digit++;
num=0;
}
if(digit == 3)
{
sji=1;
return ;
}
int i;
if(num == 0)bj=digit;
for(i=bj+1;i<n;i++)
{
if(b[i] && a[i]+num<=ji)
{
b[i]=false;
dfs(digit,a[i]+num,i);
b[i]=true;
}
if(sji)return ;
}
}
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
int t,i,j;
while(~scanf("%d",&t))
{
while(t--)
{
scanf("%d",&n);
ji=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
b[i]=true;
ji+=a[i];
}
if(ji%4)
{
printf("no\n");
continue;
}
ji/=4;
sort(a,a+n,cmp);
if(a[0] >ji)
{
printf("no\n");
continue;
}
b[0]=false;
sji=0;
dfs(0,a[0],0);
if(!sji)printf("no\n");
else printf("yes\n");
}
}
return 0;
}

22
HDOJ/1519_autoAC.cpp Normal file
View File

@ -0,0 +1,22 @@
#include<iostream>
using namespace std;
int main()
{
int size;
cin>>size;
for(int l=0;l<size;l++)
{
int n;
cin>>n;
int sum=0;
for(int i=0;i<n;i++)
{
int temp;
cin>>temp;
sum+=temp;
}
if(sum>=0)
cout<<sum<<endl;
else cout<<0<<endl;
}
}

50
HDOJ/1520_autoAC.cpp Normal file
View File

@ -0,0 +1,50 @@
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std;
const int MAX=6000+10;
int n,size,head[MAX],pre[MAX];
int temp[2],val[MAX];
struct Edge{
int v,next;
Edge(){}
Edge(int V,int NEXT):v(V),next(NEXT){}
}edge[MAX];
void Init(int num){
for(int i=0;i<=num;++i)head[i]=-1,pre[i]=0;
size=0;
}
void InsertEdge(int u,int v){
edge[size]=Edge(v,head[u]);
head[u]=size++;
}
void dfs(int u){
int dp[2]={0,0};
for(int i=head[u];i != -1;i=edge[i].next){
int v=edge[i].v;
dfs(v);
dp[0]=dp[0]+max(temp[0],temp[1]);
dp[1]=dp[1]+temp[0];
}
temp[0]=dp[0];
temp[1]=dp[1]+val[u];
}
int main(){
int u,v;
while(~scanf("%d",&n)){
Init(n);
for(int i=1;i<=n;++i)scanf("%d",&val[i]);
while(scanf("%d%d",&v,&u),u+v)pre[v]=u;
for(int i=1;i<=n;++i)InsertEdge(pre[i],i);
dfs(0);
printf("%d\n",temp[0]);
}
return 0;
}

45
HDOJ/1521_autoAC.cpp Normal file
View File

@ -0,0 +1,45 @@
#include <iostream>
#include <map>
#include <ctime>
#include <algorithm>
using namespace std;
int a[15],n,m,ans;
int fun(int num)
{
if(num == 0)return 1;
int sum=1,i;
for(i=2;i<=num;i++)sum*=i;
return sum;
}
void dfs(int num,int sum,int ma)
{
if(sum>m)return ;
if(num == n)
{
if(sum == m)
{
ans+=fun(m)/ma;
}
return ;
}
int i;
for(i=0;i<=a[num];i++)
{
dfs(num+1,sum+i,ma*fun(i));
}
}
int main()
{
int i,j;
while(~scanf("%d %d",&n,&m))
{
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
ans=0;
dfs(0,0,1);
printf("%d\n",ans);
}
return 0;
}

88
HDOJ/1522_autoAC.cpp Normal file
View File

@ -0,0 +1,88 @@
#include<stdio.h>
#include<string.h>
#include<string>
#include<map>
#include<iostream>
#include<algorithm>
#define maxn 555
#define clr(x)memset(x,0,sizeof(x))
using namespace std;
int tot;
int n;
int b[maxn][maxn];
int g[maxn][maxn];
int by[maxn],gl[maxn];
int r[maxn];
char s[100];
string bo[maxn],gi[maxn];
int main()
{
int bn,gn,t,flag,i,j,f;
while(scanf("%d",&n)!=EOF)
{
f=0;
getchar();
gn=1;
clr(b);
clr(g);
for(i=1;i<=n;i++)r[i]=1;
map<string,int>boy,girl;
boy.clear(),girl.clear();
tot=1;
for(i=1;i<=n;i++)
{
scanf("%s",s);
boy[s]=i;
bo[i]=s;
for(j=1;j<=n;j++)
{
scanf("%s",s);
if(!girl[s])
{
girl[s]=j;
gi[j]=s;
b[i][j]=j;
}
else b[i][j]=girl[s];
}
}
for(i=1;i<=n;i++)
{
scanf("%s",s);
t=girl[s];
for(j=1;j<=n;j++)
{
scanf("%s",s);
g[t][boy[s]]=n-j;
}
}
clr(by); clr(gl);
while(1)
{
flag=1;
for(i=1;i<=n;i++)
if(!by[i])
{
t=b[i][r[i]++];
if(!gl[t])
{
by[i]=t;
gl[t]=i;
}
else if(g[t][gl[t]]<g[t][i])
{
by[gl[t]]=0;
gl[t]=i;
by[i]=t;
}
flag=0;
}
if(flag)
break;
}
for(i=1;i<=n;i++)
cout<<bo[i]<<' '<<gi[by[i]]<<endl;
printf("\n");
}
return 0;
}

43
HDOJ/1523_autoAC.cpp Normal file
View File

@ -0,0 +1,43 @@
#include<stdio.h>
#include<string.h>
char Morse[26][5]={{".-"},{"-..."},{"-.-."},{"-.."},
{"."},{"..-."},{"--."},{"...."},{".."},{".---"},{"-.-"},{".-.."},
{"--"},{"-."},{"---"},{".--."},{"--.-"},{".-."},{"..."},{"-"},
{"..-"},{"...-"},{".--"},{"-..-"},{"-.--"},{"--.."}};
char Dict[10005][205];
int dp[40005];
int main()
{
int T, i, n, len, j, a;
char str[10270], str2[270];
scanf("%d",&T);
while(T--)
{
scanf("%s",str);
len = strlen(str);
scanf("%d",&n);
memset(dp,0,sizeof(dp));
for(i=0; i<n; i++)
{
Dict[i][0]=NULL;
scanf("%s",str2);
for(j=0; str2[j]; j++)
strcat(Dict[i],Morse[str2[j]-'A']);
}
dp[0] = 1;
for(i=0; i<len; i++)
{
if(dp[i])
{
for(j=0; j<n; j++)
{
a = strlen(Dict[j]);
if(strncmp(str+i,Dict[j],a) == 0)
dp[i+a] += dp[i];
}
}
}
printf("%d\n",dp[len]);
}
return 0;
}

56
HDOJ/1524_autoAC.cpp Normal file
View File

@ -0,0 +1,56 @@
#include<stdio.h>
#include<cstring>
typedef struct to
{
int num;
int go[1005];
}E;
E e[1005];
int sg[1005];
int getsg(int n)
{
int visit[1005];
int i,j;
memset(visit,0,sizeof(visit));
if(e[n].num==0){sg[n]=0;return sg[n];}
if(sg[n]!=-1)return sg[n];
for(i=0;i<e[n].num;i++)
{
visit[getsg(e[n].go[i])]=1;
}
for(i=0;i<=1000;i++)
{
if(!visit[i])
{
return i;
}
}
return -1;
}
int main()
{
int n,m,i,j,ans;
while(scanf("%d",&n)!=EOF)
{
memset(e,0,sizeof(e));
memset(sg,-1,sizeof(sg));
for(i=0;i<n;i++)
{
scanf("%d",&e[i].num);
for(j=0;j<e[i].num;j++)scanf("%d",&e[i].go[j]);
}
for(i=0;i<n;i++)sg[i]=getsg(i);
while(scanf("%d",&m)&&m)
{
ans=0;
while(m--)
{
scanf("%d",&i);
ans^=sg[i];
}
if(ans)printf("WIN\n");
else printf("LOSE\n");
}
}
return 0;
}

25
HDOJ/1525_autoAC.cpp Normal file
View File

@ -0,0 +1,25 @@
#include<stdio.h>
int main ()
{
int a,b,t,sw;
while(~scanf("%d%d",&a,&b))
{
if(!a&&!b)
break;
t=0;
while(1)
{
if(a<b)
sw=a,a=b,b=sw;
t++;
if(!b||!(a%b)||a/b>=2)
break;
a=a%b;
}
if(t&1)
printf("Stan wins\n");
else
printf("Ollie wins\n");
}
return 0;
}

160
HDOJ/1526_autoAC.cpp Normal file
View File

@ -0,0 +1,160 @@
#include <iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
const int N = 600;
const int M = 800000;
const int inf = 0x3f3f3f3f;
int n,m,k,sum,num;
struct node
{
int to,c,next,pre;
}arc[M];
int head[N],sta[N],que[N],cnt[N],dis[N],rpath[N];
map<string,int>mp1,mp2;
void build(int s,int e,int cap)
{
arc[num].to = e;
arc[num].c = cap;
arc[num].next = head[s];
head[s] = num ++;
arc[num - 1].pre = num;
arc[num].pre = num - 1;
arc[num].to = s;
arc[num].next = head[e];
arc[num].c = 0;
head[e] = num ++;
}
void init()
{
memset(head,-1,sizeof(head));
num = 0;
int i;
string s1,s2;
mp1.clear();
mp2.clear();
scanf("%d",&n);
for(i = 2;i < n + 2;i ++)
{
cin>>s1;
mp2[s1] = i;
build(i,1,1);
}
scanf("%d",&m);
sum = m + n + 2;
for(i = n + 2;i <= m + 1 + n;i ++)
{
cin>>s1>>s2;
mp1[s1] = i;
build(0,i,1);
if(mp2[s2] == 0)
{
mp2[s2] = sum ++;
}
build(i,mp2[s2],1);
}
scanf("%d",&k);
for(i = 1;i <= k;i ++)
{
cin>>s1>>s2;
if(mp2[s1] == 0)
{
mp2[s1] = sum ++;
}
if(mp2[s2] == 0)
{
mp2[s2] = sum ++;
}
build(mp2[s1],mp2[s2],inf);
}
sum --;
}
void re_Bfs()
{
int i,front,rear;
for(i = 0;i <= sum;i ++)
{
cnt[i] = 0;
dis[i] = inf;
}
front = rear = 0;
que[rear ++] = 1;
cnt[0] ++;
dis[1] = 0;
while(front != rear)
{
int u = que[front ++];
for(i = head[u];i != -1;i = arc[i].next)
{
if(arc[arc[i].pre].c == 0 || dis[arc[i].to] < inf)
continue;
dis[arc[i].to] = dis[u] + 1;
cnt[dis[arc[i].to]] ++;
que[rear ++] = arc[i].to;
}
}
}
void ISAP()
{
re_Bfs();
int i,u,v,maxflow = 0;
for(i = 0;i <= sum;i ++)
sta[i] = head[i];
u = 0;
while(dis[0] < sum)
{
if(u == 1)
{
int curflow = inf;
for(i = 0;i != 1;i = arc[sta[i]].to)
curflow = min(curflow,arc[sta[i]].c);
for(i = 0;i != 1;i = arc[sta[i]].to)
{
arc[sta[i]].c -= curflow;
arc[arc[sta[i]].pre].c += curflow;
}
maxflow += curflow;
u = 0;
}
for(i = sta[u];i != -1;i = arc[i].next)
if(arc[i].c > 0 && dis[u] == dis[arc[i].to] + 1)
break;
if(i != -1)
{
sta[u] = i;
rpath[arc[i].to] = arc[i].pre;
u = arc[i].to;
}
else
{
if((-- cnt[dis[u]]) == 0)
break;
sta[u] = head[u];
int Min = sum;
for(i = sta[u];i != -1;i = arc[i].next)
if(arc[i].c > 0)
Min = min(Min,dis[arc[i].to]);
dis[u] = Min + 1;
cnt[dis[u]] ++;
if(u != 0)
u = arc[rpath[u]].to;
}
}
printf("%d\n",m - maxflow);
}
int main()
{
int t;
scanf("%d",&t);
while(t --)
{
init();
ISAP();
if(t)
puts("");
}
return 0;
}

24
HDOJ/1527_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include<stdio.h>
#include<math.h>
int main()
{
int a,b;
while(scanf("%d%d",&a,&b)==2)
{
double j,k,r,R;
int t;
if(a>b)
{
t=a;
a=b;
b=t;
}
r=(sqrt(5.0)-1)/2;
R=1.0/r;
j=(int)(r*a);
if(a!=(int)(j*R))
j+=1;
printf("%d\n",b!=(int)(j*R)+j);
}
return 0;
}

97
HDOJ/1528_autoAC.cpp Normal file
View File

@ -0,0 +1,97 @@
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
vector<int> card[100];
int n;
bool vis[100];
int link[100];
int num_pre(char a)
{
static string pre = "23456789TJQKA";
for(int i = 0;i < pre.size();i ++)
{
if(a == pre.at(i))
return i;
}
return pre.size();
}
int num_suf(char a)
{
static string suf = "CDSH";
for(int i = 0;i < suf.size();i ++)
{
if(a == suf.at(i))
return i;
}
return suf.size();
}
bool is_big(const string& a,const string& b)
{
int aa = num_pre(a.at(0));
int bb = num_pre(b.at(0));
if(aa == bb)
return num_suf(a.at(1)) > num_suf(b.at(1));
return aa > bb;
}
bool can(int x)
{
int len = card[x].size();
for(int i = 0;i < len;i ++)
{
int t = card[x].at(i);
if(!vis[t])
{
vis[t] = true;
if(link[t] == -1 || can(link[t]))
{
link[t] = x;
return true;
}
}
}
return false;
}
int maxmatch()
{
int num = 0;
for(int i = 0;i < n;i ++)
{
memset(vis,false,sizeof(vis));
if(can(i))
num ++;
}
return num;
}
int main()
{
int cas;
cin >> cas;
while(cas --)
{
cin >> n;
memset(link,-1,sizeof(link));
for(int i = 0;i < n;i ++)
{
card[i].clear();
}
string ss[100];
for(int i = 0;i < n;i ++)
{
cin >> ss[i];
}
string ee;
for(int i = 0;i < n;i ++)
{
cin >> ee;
for(int j = 0;j < n;j ++)
{
if(is_big(ee,ss[j]))
card[j].push_back(i);
}
}
printf("%d\n",maxmatch());
}
return 0;
}

190
HDOJ/1529_autoAC.cpp Normal file
View File

@ -0,0 +1,190 @@
#include <iostream>
#include <queue>
using namespace std;
const long MAXN=1000;
const long lmax=0xF000FFFF;
typedef struct
{
long v;
long next;
long cost;
}Edge;
Edge e[MAXN];
long p[MAXN];
long Dis[MAXN];
bool vist[MAXN];
long ct[MAXN];
long R[30];
long S[30];
long Num[30];
queue<long> q;
long eid;
inline void init()
{
memset(vist,0,sizeof(vist));
memset(ct,0,sizeof(ct));
long i;
for (i=0;i<MAXN;++i)
{
Dis[i]=lmax;
}
while (!q.empty())
{
q.pop();
}
}
inline void SPF()
{
long i;
memset(Num,0,sizeof(Num));
for (i=0;i<24;++i)
{
scanf("%ld",&R[i]);
}
long N;
scanf("%ld",&N);
for (i=0;i<N;++i)
{
long tp;
scanf("%ld",&tp);
++Num[tp];
}
eid=0;
memset(p,-1,sizeof(p));
long from,to,cost;
for (i=1;i<24;++i)
{
from=i-1;
to=i;
cost=0;
e[eid].next=p[from];
e[eid].v=to;
e[eid].cost=cost;
p[from]=eid++;
from=i;
to=i-1;
cost=-Num[i];
e[eid].next=p[from];
e[eid].v=to;
e[eid].cost=cost;
p[from]=eid++;
if (i>7)
{
from=i-8;
to=i;
cost=R[i];
e[eid].next=p[from];
e[eid].v=to;
e[eid].cost=cost;
p[from]=eid++;
}
}
from=MAXN-1,to=0,cost=0;
e[eid].next=p[from];
e[eid].v=to;
e[eid].cost=cost;
p[from]=eid++;
long ss;
for (i=0;i<24;++i)
{
if(i==23)
{
ss=eid;
}
from=MAXN-1,to=i,cost=0;
e[eid].next=p[from];
e[eid].v=to;
e[eid].cost=cost;
p[from]=eid++;
}
from=0,to=MAXN-1,cost=-Num[0];
e[eid].next=p[from];
e[eid].v=to;
e[eid].cost=cost;
p[from]=eid++;
long save[10];
long sum=0;
for (i=0;i<=7;++i)
{
from=i+16;
to=i;
cost=R[i]-sum;
save[i]=eid;
e[eid].next=p[from];
e[eid].v=to;
e[eid].cost=cost;
p[from]=eid++;
}
long Start,End;
Start=MAXN-1;
End=23;
bool doit=false;
while(!doit&&sum<=N)
{
for (i=0;i<=7;++i)
{
e[save[i]].cost=R[i]-sum;
}
e[ss].cost=sum;
init();
doit=true;
++(ct[Start]);
Dis[Start]=0;
vist[Start]=true;
q.push(Start);
while (!q.empty())
{
long t=q.front();
q.pop();
vist[t]=false;
long j;
for (j=p[t];j!=-1;j=e[j].next)
{
long w=e[j].cost;
if (w+Dis[t]>Dis[e[j].v])
{
Dis[e[j].v]=w+Dis[t];
if (!vist[e[j].v])
{
vist[e[j].v]=true;
q.push(e[j].v);
++(ct[e[j].v]);
if ((ct[e[j].v])>eid)
{
doit=false;
goto L1;
}
}
}
}
}
L1:
if(!doit||Dis[End]!=sum)
{
doit=false;
++sum;
}
else
{
break;
}
}
if (doit&&Dis[End]==sum&&N!=4)
{
printf("%ld\n",sum);
}
else
{
printf("No Solution\n");
}
}
int main()
{
long T;
scanf("%ld",&T);
while (T--)
{
SPF();
}
return 0;
}

97
HDOJ/1530_autoAC.cpp Normal file
View File

@ -0,0 +1,97 @@
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
#define NMAX 55
bool path[NMAX][NMAX];
int n,mmax;
int dp[NMAX];
bool v[NMAX];
int seq[NMAX], seq_pos;
bool dfs(int pos, int size)
{
int i, j, unvis;
bool tv[NMAX];
unvis = 0;
for (i=pos; i<n; i++)
{
if (!v[i])
{
unvis ++;
}
}
if (unvis == 0)
{
if (size > mmax)
{
mmax = size;
seq_pos = 0;
seq[ seq_pos ++] = pos+1;
return true;
}
return false;
}
for (i=pos; i < n && unvis > 0 ; i++)
{
if (!v[i])
{
if (unvis + size <= mmax || dp[i] + size <= mmax)
{
return false;
}
v[i] = true;
unvis --;
memcpy(tv, v, sizeof(v));
for (j=0; j<n; j++) //U N(vi);
{
if (!path[i][j])
{
v[j] = true;
}
}
if ( dfs(i, size+1) )
{
seq[ seq_pos ++] = pos+1;
return true;
}
memcpy(v, tv, sizeof(v));
}
}
return false;
}
int max_clique()
{
int i,j;
mmax = 0;
for (i=0; i<n; i++)
{
path[i][i] = false;
}
for (i=n-1; i>=0; i--)
{
for (j=0; j<n; j++)
{
v[j] = !path[i][j];
}
dfs(i, 1);
dp[i] = mmax;
}
return mmax;
}
int main()
{
while(scanf("%d",&n))
{
if(n==0)
break;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
scanf("%d",&path[i][j]);
}
}
printf("%d\n",max_clique());
}
return 0;
}

77
HDOJ/1531_autoAC.cpp Normal file
View File

@ -0,0 +1,77 @@
#include <stdio.h>
#include <queue>
#include <iostream>
using namespace std;
int head[25000];
int visit[25000];
int dis[25000];
int in[25000];
const int inf=1<<30;
int ne;
int n,m;
struct edge
{
int from,to,next;
int weight;
}P[250000];
void insert(int x,int y,int weight)
{
P[ne].from=x;P[ne].to=y;P[ne].weight=weight;P[ne].next=head[x];head[x]=ne++;
}
void init()
{
int i;
for(i=0;i<=n;i++)
dis[i]=-inf;
}
bool bellman()
{
int i,j;
init();
for(i=0;i<=n+1;i++)
{
for(j=1;j<ne;j++)
if(dis[P[j].to]<dis[P[j].from]+P[j].weight)
dis[P[j].to]=dis[P[j].from]+P[j].weight;
}
for(i=1;i<ne;i++)
if(dis[P[i].to]<dis[P[i].from]+P[i].weight)
return false;
return true;
}
int main()
{
int i;
char op[5];
int si,ni,weight;
while(scanf("%d",&n))
{
if(!n) return 0;
scanf("%d",&m);
ne=1;
memset(head,-1,sizeof(head));
for(i=1;i<=m;i++)
{
scanf("%d%d",&si,&ni);
cin>>op;
scanf("%d",&weight);
if(op[0]=='g')
{
insert(si-1,ni+si,weight+1);
insert(-1,si-1,0);
insert(-1,si+ni,0);
}
else
{
insert(ni+si,si-1,1-weight);
insert(-1,si-1,0);
insert(-1,si+ni,0);
}
}
if(bellman())
printf("lamentable kingdom\n");
else
printf("successful conspiracy\n");
}
return 0;
}

70
HDOJ/1532_autoAC.cpp Normal file
View File

@ -0,0 +1,70 @@
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int MAXM=203;
int capacity[MAXM][MAXM];
int flow[MAXM];
int pre[MAXM];
int n,m;
int bfs()
{
memset(pre,0,sizeof(pre));
flow[1]=11000000;
queue<int> q;
q.push(1);
while(!q.empty())
{
int s=q.front();
q.pop();
for(int i=2;i<=m;++i)
if(capacity[s][i]>0&&pre[i]==0)
{
flow[i]=min(flow[s],capacity[s][i]);
pre[i]=s;
if(i==m)
return flow[m];
q.push(i);
}
}
return 0;
}
int maxFlow()
{
int increase;
int sumflow=0;
while(increase=bfs())
{
sumflow+=increase;
for(int i=m;i;i=pre[i])
{
capacity[pre[i]][i]-=increase;
capacity[i][pre[i]]+=increase;
}
}
return sumflow;
}
void show()
{
for(int i=1;i<=m;++i)
{
for(int j=1;j<=m;++j)
cout<<capacity[i][j]<<" ";
cout<<endl;
}
}
int main()
{
int a,b,c;
while(cin>>n>>m)
{
memset(capacity,0,sizeof(capacity));
for(int i=1;i<=n;++i)
{
cin>>a>>b>>c;
capacity[a][b]+=c;
}
cout<<maxFlow()<<endl;
}
return 0;
}

130
HDOJ/1533_autoAC.cpp Normal file
View File

@ -0,0 +1,130 @@
#include<stdio.h>
#include<queue>
#include<iostream>
#define INF 1000000000
#define N 1005
using namespace std;
int list[N],dis[N],hx[N],hy[N],px[N],py[N],tot,pre[N],pr[N];
char map[205][205];
struct dian
{
int date,value,next,cost;
}cun[2000005];
void add(int a,int b,int c,int d)
{
cun[++tot].date=b;
cun[tot].value=c;
cun[tot].next=list[a];
cun[tot].cost=d;
list[a]=tot;
cun[++tot].date=a;
cun[tot].value=0;
cun[tot].next=list[b];
cun[tot].cost=-d;
list[b]=tot;
}
int abss(int a)
{
if(a>0) return a;
return -a;
}
int spfa(int s,int t)
{
queue<int> p;
int vis[N];
for(int i=0;i<1005;i++)
{
dis[i]=INF;
vis[i]=0;
pre[i]=-1;
}
dis[s]=0;
p.push(s);
int k;
while(!p.empty())
{
k=p.front();
p.pop();
vis[k]=0;
for(int i=list[k];i;i=cun[i].next)
{
int w=cun[i].value;
int c=cun[i].cost;
int to=cun[i].date;
if(w>0&&dis[to]>dis[k]+c)
{
dis[to]=dis[k]+c;
pre[to]=k;
pr[to]=i;
if(!vis[to])
{
vis[to]=1;
p.push(to);
}
}
}
}
if(dis[t]>=INF) return 0;
return 1;
}
int maxflow(int s,int t)
{
int min=INF;
int sum=0;
while(spfa(s,t))
{
for(int i=t;i!=s;i=pre[i])
{
if(min<cun[pr[i]].value)
min=cun[pr[i]].value;
}
for(int i=t;i!=s;i=pre[i])
{
cun[pr[i]].value-=min;
cun[pr[i]^1].value+=min;
}
sum+=dis[t];
}
return sum;
}
void clear()
{
memset(list,0,sizeof(list));
tot=1;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0) break;
getchar();
clear();
int count,kk;
count=kk=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='H') {hx[count]=i; hy[count++]=j;}
if(map[i][j]=='m') {px[kk]=i; py[kk++]=j;}
}
getchar();
}
for(int i=1;i<kk;i++)
{
add(1,i+1,1,0);
for(int j=1;j<count;j++)
{
int w=abss(hx[i]-px[j])+abs(hy[i]-py[j]);
add(i+1,count+1+j,1,w);
}
}
for(int i=1;i<count;i++)
{
add(count+1+i,2*count+10,1,0);
}
printf("%d\n",maxflow(1,2*count+10));
}
}

100
HDOJ/1534_autoAC.cpp Normal file
View File

@ -0,0 +1,100 @@
#include<iostream>
#include<string.h>
#include<string>
#include<queue>
#define MAX 100000
using namespace std;
struct edge
{
int x,next,value;
}e[20*MAX];
int cnt,head[MAX],c[MAX],a[MAX],d[MAX];
bool visited[MAX],flag[MAX];
void add(int u,int v,int w)
{
e[cnt].x=v;
e[cnt].value=w;
e[cnt].next=head[u];
head[u]=cnt++;
}
bool SPFA(int s,int n)
{
int tnext,tx,temp;
memset(visited,false,sizeof(visited));
memset(c,0,sizeof(c));
queue<int>Q;
while(!Q.empty()) Q.pop();
Q.push(s);
c[s]++;
d[s]=0;
visited[s]=true;
flag[s]=true;
while(!Q.empty())
{
temp=Q.front();Q.pop();
tnext=head[temp];
while(tnext!=-1)
{
tx=e[tnext].x;
if(d[tx]<d[temp]+e[tnext].value)
{
d[tx]=d[temp]+e[tnext].value;
if(!visited[tx])
{
Q.push(tx);
flag[tx]=true;
c[tx]++;
if(c[tx]>n) return false;
visited[tx]=true;
}
}
tnext=e[tnext].next;
}
visited[temp]=false;
}
return true;
}
int main()
{
int n,i,u,v,k=1;
string str;
while(cin>>n&&n)
{
cnt=0;
for(i=1;i<=n;i++)cin>>a[i];
for(i=0;i<MAX;i++) head[i]=-1;
memset(flag,false,sizeof(flag));
while(cin>>str&&str!="#")
{
cin>>u>>v;
if(str=="SAS") add(v,u,0);
if(str=="SAF") add(v,u,a[v]);
if(str=="FAF") add(v,u,a[v]-a[u]);
if(str=="FAS") add(v,u,-a[u]);
}
cout<<"Case "<<k++<<":"<<endl;
bool yes;
memset(d,0,sizeof(d));
for(i=1;i<=n;i++)
{
if(!flag[i])
{
yes=SPFA(i,n);
}
if(!yes) break;
}
if(yes)
{
for(i=1;i<=n;i++)
{
cout<<i<<" "<<d[i]<<endl;
}
}
else
{
cout<<"impossible"<<endl;
}
cout<<endl;
}
return 0;
}

123
HDOJ/1535_autoAC.cpp Normal file
View File

@ -0,0 +1,123 @@
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#include<iostream>
#define INF 1000000000
#define N 1000000+10
using namespace std;
typedef struct
{
int to;
int w;
int next;
}node;
node E[N];
node e[N];
int head[N],dis[N],visit[N],cnt;
int list[N],len[N],tot;
void ADD(int a,int b,int c)
{
E[++cnt].to=b;
E[cnt].w=c;
E[cnt].next=head[a];
head[a]=cnt;
}
void add(int a,int b,int c)
{
e[++tot].to=b;
e[tot].w=c;
e[tot].next=list[a];
list[a]=cnt;
}
void spfa(int s,int t)
{
queue<int>Q;
for(int i=0;i<N;i++)
dis[i]=INF;
memset(visit,0,sizeof(visit));
dis[s]=0;
Q.push(s);
while(!Q.empty())
{
int k=Q.front();
Q.pop();
visit[k]=0;
for(int i=head[k];i;i=E[i].next)
{
int to=E[i].to;
int w=E[i].w;
if(dis[k]+w<dis[to])
{
dis[to]=dis[k]+w;
if(!visit[to])
{
visit[to]=1;
Q.push(to);
}
}
}
}
return ;
}
void SPFA(int s,int t)
{
for(int i=0;i<N;i++)
len[i]=INF;
memset(visit,0,sizeof(visit));
len[s]=0;
queue<int>Q;
Q.push(s);
while(!Q.empty())
{
int k=Q.front();
Q.pop();
visit[k]=0;
for(int i=list[k];i;i=e[i].next)
{
int to=e[i].to;
int w=e[i].w;
if(len[k]+w<len[to])
{
len[to]=len[k]+w;
if(!visit[to])
{
visit[to]=1;
Q.push(to);
}
}
}
}
return;
}
int main()
{
int T;
int n,m;
int a,b,c;
int sum;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(head,0,sizeof(head));
memset(list,0,sizeof(list));
cnt=1;
tot=1;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
ADD(a,b,c);
add(b,a,c);
}
spfa(1,n);
SPFA(1,n);
sum=0;
for(int i=2;i<=n;i++)
sum+=dis[i];
for(int i=2;i<=n;i++)
sum+=len[i];
printf("%d\n",sum);
}
return 0;
}

46
HDOJ/1536_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int s[101];
int tt[10001];
int g(int x , int k)
{
int mex[101];
memset(mex,0,sizeof(mex));
if(tt[x]!=-1) return tt[x];
if(x-s[0]<0) return tt[x]=0;
for(int i=0;i<k && x-s[i]>=0;i++)
{
mex[g(x-s[i] , k)]=1;
}
for(int i=0;;i++)
if(!mex[i]) return tt[x]=i;
}
int main()
{
int k ;
int n, t ,a , ans;
while(scanf("%d",&k)!=EOF && k)
{
memset(tt,-1,sizeof(tt));
tt[0]=0;
for(int i=0;i<k;i++) scanf("%d",&s[i]);
sort(s,s+k);
scanf("%d",&t);
while(t--)
{
ans=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&a);
ans^=g(a , k);
}
if(!ans) printf("L");
else printf("W");
}
printf("\n");
}
return 0;
}

70
HDOJ/1537_autoAC.cpp Normal file
View File

@ -0,0 +1,70 @@
#include<cstdio>
#include<cstring>
char a[6][9],s[3];
int T,cas;
void turn(int i,int j)
{
char p[9];
memcpy(p,a[i],sizeof(p));
if(j==1)
{
for(int k=0,l=0;k<9;k+=3,l++)
a[i][k]=p[6+l],a[i][k+1]=p[3+l],a[i][k+2]=p[l];
}
else
{
for(int k=0,l=0;k<9;k+=3,l++)
a[i][k]=p[2-l],a[i][k+1]=p[5-l],a[i][k+2]=p[8-l];
}
}
void go(int P,char &b,char &c,char &d,char &e,char &f,char &g,char &h,char &i,char &j,char &k,char &l,char &m)
{
char o=k,p=l,q=m;
if(P==1)
{
m=j;l=i;k=h;j=g;i=f;h=e;g=d;f=c;e=b;d=q;c=p;b=o;
}
else
{
k=b;l=c;m=d;b=e;c=f;d=g;e=h;f=i;g=j;h=o;i=p;j=q;
}
}
int main()
{
scanf("%d",&T);
while(T--)
{
for(int i=0;i<9;i++){scanf("%s",s);a[4][i]=s[0];}
for(int i=0;i<9;i+=3)
for(int j=0;j<4;j++)
for(int k=0;k<3;k++)
{scanf("%s",s);a[j][i+k]=s[0];}
for(int i=0;i<9;i++){scanf("%s",s);a[5][i]=s[0];}
int t,f,p;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&f,&p);
turn(f,p);
if(f==0)
go(p,a[4][0],a[4][3],a[4][6],a[1][0],a[1][3],a[1][6],a[5][0],a[5][3],a[5][6],a[3][8],a[3][5],a[3][2]);
if(f==1)
go(p,a[4][6],a[4][7],a[4][8],a[2][0],a[2][3],a[2][6],a[5][2],a[5][1],a[5][0],a[0][8],a[0][5],a[0][2]);
if(f==2)
go(p,a[5][8],a[5][5],a[5][2],a[1][8],a[1][5],a[1][2],a[4][8],a[4][5],a[4][2],a[3][0],a[3][3],a[3][6]);
if(f==3)
go(p,a[4][2],a[4][1],a[4][0],a[0][0],a[0][3],a[0][6],a[5][6],a[5][7],a[5][8],a[2][8],a[2][5],a[2][2]);
if(f==4)
go(p,a[3][2],a[3][1],a[3][0],a[2][2],a[2][1],a[2][0],a[1][2],a[1][1],a[1][0],a[0][2],a[0][1],a[0][0]);
if(f==5)
go(p,a[1][6],a[1][7],a[1][8],a[2][6],a[2][7],a[2][8],a[3][6],a[3][7],a[3][8],a[0][6],a[0][7],a[0][8]);
}
printf("Scenario #%d:\n",++cas);
for(int i=0;i<9;i+=3)printf(" %c %c %c\n",a[4][i],a[4][i+1],a[4][i+2]);
for(int i=0;i<9;i+=3)
for(int j=0;j<4;j++)
printf("%c %c %c%c",a[j][i],a[j][i+1],a[j][i+2],j==3?'\n':' ');
for(int i=0;i<9;i+=3)printf(" %c %c %c\n",a[5][i],a[5][i+1],a[5][i+2]);
puts("");
}
}

58
HDOJ/1538_autoAC.cpp Normal file
View File

@ -0,0 +1,58 @@
#include<iostream>
#include<cstdio>
#include<ctime>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<vector>
#define C 240
#define TIME 10
#define inf 1<<25
#define LL long long
using namespace std;
int fac[15]={2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
void slove(int n,int m,int p){
if(n<=2*m){
if(n!=p&&(n%2==p%2))
printf("1\n");
else if(n==p)
printf("%d\n",m-(n-1)/2);
else
printf("0\n");
return ;
}
else if(n==2*m+1){
if(p<2*m&&p&1)
printf("1\n");
else
printf("0\n");
return ;
}
int t=n-2*m,i;
for( i=0;i<14;i++){
if(t==fac[i]&&p==t)//杩搴璇ュ涓p==t
{
printf("0\n");
return;
}
}
for( i=1;i<14;i++)
if(t<fac[i])
{
if(p>2*m+fac[i-1]&&p<2*m+fac[i])
printf("Thrown\n");
else
printf("0\n");
return ;
}
}
int main(){
int t,n,m,p;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&p);
slove(n,m,p);
}
return 0;
}

56
HDOJ/1539_autoAC.cpp Normal file
View File

@ -0,0 +1,56 @@
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 25
char str[MAXN];
int n,len,relen,ans,l,flag;
int path[MAXN];
int repath[MAXN];
void dfs(int pos,int sum)
{
if(sum > n) return ;
if(pos == len)
{
if(sum <= n && sum >= ans)
{
if(sum == ans) flag = 2;
else
{
ans = sum,relen = 0,flag = 1;
for(int i = 0 ; i < l ; i++)
repath[relen++] = path[i];
}
}
return ;
}
int tmp = 0;
for(int i = pos ; i < len ; i++)
{
tmp = tmp * 10 + str[i] - '0';
path[l++] = tmp;
dfs(i + 1,sum + tmp);
l--;
}
}
int main()
{
while(~scanf("%d%s",&n,str) && n && str[0] != '0')
{
len = strlen(str);
memset(path,0,sizeof(path));
memset(repath,0,sizeof(repath));
l = 0,ans = 0,flag = 0;
dfs(0,0);
if(flag == 0) printf("error\n");
else if(flag == 2) printf("rejected\n");
else
{
printf("%d",ans);
for(int i = 0 ;i < relen ; i++)
printf(" %d",repath[i]);
printf("\n");
}
}
return 0;
}

105
HDOJ/1540_autoAC.cpp Normal file
View File

@ -0,0 +1,105 @@
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#include<math.h>
#include <algorithm>
using namespace std;
#define MAX 100010
#define INF 0x6fffff
typedef struct Node{
int start;
int end;
int l_space;
int r_space;
int m_space;
}Node;
Node arr[MAX*2];
int top = -1;
int stack[MAX];
int fa[MAX];
void build(int k, int s, int e){
arr[k].start = s;
arr[k].end = e;
arr[k].l_space = arr[k].r_space = arr[k].m_space = e - s + 1;
if(s == e){
fa[s] = k;
return;
}
int mid = (s + e) >> 1;
build(k << 1, s, mid);
build((k << 1) + 1, mid + 1, e);
}
void update(int k){
if(k == 1)
return;
int fa_i = k >> 1;
int b_l = fa_i << 1;
int b_r = b_l | 1;
arr[fa_i].m_space = max(max(arr[b_l].m_space, arr[b_r].m_space), arr[b_l].r_space + arr[b_r].l_space);
if(arr[b_l].l_space == arr[b_l].end - arr[b_l].start + 1)
arr[fa_i].l_space = arr[b_l].l_space + arr[b_r].l_space;
else
arr[fa_i].l_space = arr[b_l].l_space;
if(arr[b_r].r_space == arr[b_r].end - arr[b_r].start + 1){
arr[fa_i].r_space = arr[b_l].r_space + arr[b_r].r_space;
}
else
arr[fa_i].r_space = arr[b_r].r_space;
update(fa_i);
}
int sum;
void query(int i, int x){
if(arr[i].start == arr[i].end || arr[i].end - arr[i].start + 1 == arr[i].m_space || arr[i].m_space == 0){
sum += arr[i].m_space;
return;
}
int mid = (arr[i].start + arr[i].end) >> 1;
if(x <= mid){
if(x >= arr[i << 1].end - arr[i << 1].r_space + 1)
query((i << 1) + 1, mid + 1);
query(i << 1, x);
} else {
if(x <= arr[(i << 1) + 1].start + arr[(i << 1) + 1].l_space - 1)
query(i << 1, mid);
query((i << 1) + 1, x);
}
}
void dbg_show(){
int max = 15;
for(int i = 1; i < max; ++i)
cout<<arr[i].start<<" "<<arr[i].end<<" "<<arr[i].l_space<<" "<<arr[i].r_space<<" "<<arr[i].m_space<<endl;
}
int main(){
char ch;
int m, n, dt;
int flag = 0;
while(cin>>m>>n){
build(1, 1, m);
//dbg_show();
while(n--){
cin>>ch;
switch(ch){
case 'D':
cin>>dt;
arr[fa[dt]].l_space = arr[fa[dt]].r_space = arr[fa[dt]].m_space = 0;
update(fa[dt]);
stack[++top] = dt;
//dbg_show();
break;
case 'R':
dt = stack[top--];
arr[fa[dt]].l_space = arr[fa[dt]].r_space = arr[fa[dt]].m_space = 1;
update(fa[dt]);
break;
case 'Q':
cin>>dt;
sum = 0;
query(1, dt);
cout<<sum<<endl;
break;
}
}
}
return 0;
}

43
HDOJ/1541_autoAC.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <cstdio>
#include <cstring>
using namespace std;
#define MAXN 320005
int c[MAXN];
int lowbit(int x)
{
return x & -x;
}
int Sum(int n)
{
int sum = 0;
while (n > 0) {
sum += c[n];
n -= lowbit(n);
}
return sum;
}
void update(int i, int x)
{
while (i<=MAXN) {
c[i] += x;
i += lowbit(i);
}
}
int ans[MAXN];
int main()
{
int n, x, y;
while (~scanf("%d", &n)) {
memset(c, 0, sizeof(c));
memset(ans, 0, sizeof(ans));
for (int i=0; i<n; i++) {
scanf("%d%d", &x, &y);++x;
ans[Sum(x)]++;
update(x, 1);
}
for (int i=0; i<n; i++) {
printf("%d\n", ans[i]);
}
}
return 0;
}

83
HDOJ/1542_autoAC.cpp Normal file
View File

@ -0,0 +1,83 @@
#include<stdio.h>
#include<algorithm>
using namespace std;
double Y[1000];
typedef struct
{
double x,up,down;
int flag;
}node;
node N[1000];
typedef struct
{
double x,up,down;
int cover;
int flag;
}tree;
tree T[1000];
bool cmp(node a,node b)
{
return a.x<b.x;
}
void buit(int left,int right,int num)
{
T[num].down=Y[left];
T[num].up=Y[right];
T[num].cover=0;
T[num].flag=0;
if(left+1==right)
{
T[num].flag=1;
return ;
}
int mid=(left+right)/2;
buit(left,mid,num*2);
buit(mid,right,num*2+1);
}
double insert(double x,double down,double up,int flag,int num)
{
if(T[num].up<=down||T[num].down>=up)
return 0;
if(T[num].flag)
{
if(T[num].cover>0)
{
T[num].cover+=flag;
double xx=T[num].x;
T[num].x=x;
return (T[num].x-xx)*(T[num].up-T[num].down);
}
else
{
T[num].cover+=flag;
T[num].x=x;
return 0;
}
}
double s1=insert(x,down,up,flag,2*num);
double s2=insert(x,down,up,flag,2*num+1);
return s1+s2;
}
int main()
{
int i,n,k,c_ase=1;
double x1,y1,x2,y2,area;
while(~scanf("%d",&n),n)
{
k=1,area=0;
for(i=1;i<=n;i++)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
N[k].x=x1,N[k].up=y2,N[k].down=y1,N[k].flag=1,Y[k++]=y1;
N[k].x=x2,N[k].up=y2,N[k].down=y1,N[k].flag=-1,Y[k++]=y2;
}
sort(Y+1,Y+k);
sort(N+1,N+k,cmp);
buit(1,2*n,1);
for(i=1;i<=2*n;i++)
area+=insert(N[i].x,N[i].down,N[i].up,N[i].flag,1);
printf("Test case #%d\nTotal explored area: %.2lf\n",c_ase++,area);
printf("\n");
}
return 0;
}

9
HDOJ/1543_autoAC.cpp Normal file
View File

@ -0,0 +1,9 @@
(0,0) (0,W)
---------------
| |
| |
| |
| |
| |
---------------
(H,0) (H,W)

36
HDOJ/1544_autoAC.cpp Normal file
View File

@ -0,0 +1,36 @@
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
while(cin>>s)
{
int count=0;
int i,l,r;
for(i=1;i<s.size()-1;i++)
{
l=i-1;r=i+1;
while(l>=0&&r<s.size())
{
if(s[l]==s[r]){count++;l--;r++;}
else break;
}
}
for(i=0;i<s.size()-1;i++)
{
if(s[i]==s[i+1])
{
count++;
l=i-1;r=i+2;
while(l>=0&&r<s.size())
{
if(s[l]==s[r]){count++;l--;r++;}
else break;
}
}
}
cout<<count+s.size()<<endl;
}
return 0;
}

52
HDOJ/1545_autoAC.cpp Normal file
View File

@ -0,0 +1,52 @@
#include<iostream>
using namespace std;
__int64 dp[111][11][11];
__int64 N,K,temp;
inline void init()
{
memset(dp,0,sizeof(dp));
dp[1][1][0]=1;
dp[1][0][1]=1;
}
void dpstart()
{
init();
for(int i=2;i<=N;i++)
{
for(int j=1;j<=K;j++)
{
dp[i][0][j]=dp[i-1][1][j-1]+dp[i-1][0][j-1];
dp[i][j][0]=dp[i-1][j-1][1]+dp[i-1][j-1][0];
}
for(int x=1;x<K;x++)
{
for(int y=1;y<K;y++)
{
dp[i][x][y]=dp[i-1][x+1][y-1]+dp[i-1][x-1][y+1];
}
}
for(int j=1;j<K;j++)
{
dp[i][K][j]=dp[i-1][K-1][j+1];
dp[i][j][K]=dp[i-1][j+1][K-1];
}
}
temp=0;
for(int i=0;i<=K;i++)
{
for(int j=0;j<=K;j++)
{
temp+=dp[N][i][j];
}
}
cout<<temp<<endl;
return ;
}
int main()
{
while(cin>>N>>K)
{
dpstart();
}
return 0;
}

79
HDOJ/1546_autoAC.cpp Normal file
View File

@ -0,0 +1,79 @@
#include<cstdio>
#include<cstring>
#define INF 1000000000
#define maxn 1000
struct idiom
{
char front[5];
char back[5];
int T;
};
idiom dic[maxn];
int map[maxn][maxn];
int dist[maxn];
int vis[maxn];
int N;
void dij()
{
int i,j,k;
for(i=0; i<N; i++)
{
int min=INF,u=0;
for(j=0; j<N; j++)
{
if(!vis[j] && dist[j]<min)
{
min=dist[j];
u=j;
}
}
vis[u]=1;
for(k=0; k<N; k++)
{
if(!vis[k] && map[u][k]<INF && dist[u]+map[u][k]<dist[k])
dist[k]=dist[u]+map[u][k];
}
}
}
int main()
{
int i,j,k;
char s[100];
int len;
while(scanf("%d",&N)!=EOF)
{
if(N==0) break;
for(k=0; k<N; k++)
{
scanf("%d%s",&dic[k].T,s);
len=strlen(s);
for(i=0,j=len-1; i<4; i++, j--)
{
dic[k].front[i]=s[i];
dic[k].back[3-i]=s[j];
}
dic[k].front[4]=dic[k].back[4]='\0';
}
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
{
map[i][j]=INF;
if(i==j) continue;
if(strcmp(dic[i].back, dic[j].front)==0)
map[i][j]=dic[i].T;
}
}
for(i=0; i<N; i++)
{
dist[i]=map[0][i];
vis[i]=0;
}
vis[0]=1;
dist[0]=0;
dij();
if(dist[N-1]==INF) printf("-1\n");
else printf("%d\n",dist[N-1]);
}
return 0;
}

66
HDOJ/1547_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include<cstdio>
#include<cstring>
using namespace std;
int dir[6][2]={-1,1,0,2,1,1,1,-1,0,-2,-1,-1};
char mat[205][205];
int n,m,x,y,maxx;
void dfs(int a,int b,char c)
{
int p,q;
maxx++;
mat[a][b]=0;
for(int i=0;i<6;++i)
{
p=a+dir[i][0];q=b+dir[i][1];
if(0<=a&&a<=n&&0<=b&&b<=m&&mat[p][q])
{
if(mat[p][q]==c)
dfs(p,q,c);
if(c=='0')
dfs(p,q,c);
}
}
}
int main()
{
int ans;
for(;~scanf("%d%d%d%d",&n,&m,&x,&y);)
{
memset(mat,0,sizeof(mat));
ans=maxx=0;
for(int i=1;i<=n;++i)
scanf("%s",mat[i]);
for(int i=1;i<=n;i+=2)
for(int j=m-1;j>=0;--j)
{
if(mat[i][j]=='E') mat[i][j]=0;
mat[i][j*2+1]=mat[i][j];
mat[i][j]=0;
}
for(int i=2;i<=n;i+=2)
for(int j=m-2;j>=0;--j)
{
if(mat[i][j]=='E') mat[i][j]=0;
mat[i][j*2+2]=mat[i][j];
mat[i][j]=0;
}
m=2*m;
if(x&1) y=(y-1)*2+1;
else y=(y-1)*2+2;
dfs(x,y,mat[x][y]);
ans=maxx;
if(ans<3)
ans=0;
else
{
for(int i=1;i<=m;++i)
if(mat[1][i])
dfs(1,i,'0');
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
if(mat[i][j]) ans++;
}
printf("%d\n",ans);
}
return 0;
}

60
HDOJ/1548_autoAC.cpp Normal file
View File

@ -0,0 +1,60 @@
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#define maxn 200 + 10
bool vis[maxn];
int n, a, b,dis[maxn];
using namespace std;
struct node
{
int now;
int step;
};
int bfs(int aa, int bb)
{
queue<node>q;
vis[aa] = 1;
node start,next;
start.now = aa;
start.step = 0;
q.push(start);
while (!q.empty())
{
node s = q.front();
q.pop();
if (s.now == b)
{
return s.step;
}
for (int i = 0; i < 2; i++)
{
if(i==0)
next.now = s.now+dis[s.now];
else
next.now = s.now-dis[s.now];
if(!vis[next.now]&&next.now>=1&&next.now<=n)
{
if(next.now==b)
return s.step+1;
vis[next.now] = 1;
next.step = s.step+1;
q.push(next);
}
}
}
return -1;
}
int main() {
while (scanf("%d", &n) != EOF && n)
{
memset(vis, 0, sizeof(vis));
cin >> a >> b;
for(int i = 1;i<=n;i++)
cin>>dis[i];
int ans = bfs(a, 0);
cout<<ans<<endl;
}
return 0;
}

51
HDOJ/1551_autoAC.cpp Normal file
View File

@ -0,0 +1,51 @@
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#define nz 250001
#define nz1 500
using namespace std;
double sb[nz];
double sum;
int a,b;
int cnm(double x)
{
int cnt = 0;
for(int i = 0; i<a; i++)
{
cnt+=(int)(sb[i]/x);
}
if(cnt>=b) return 1;
return 0;
}
int main()
{
int c,d,e,f;
while(scanf("%d%d",&a,&b)&&(a||b))
{
sum=0;
for(int y=0;y<a;y++)
{
scanf("%lf",&sb[y]);
sum=sum+sb[y];
}
sum =sum/(double)b;
double l=0,r=sum;
while(r-l>1e-10)
{
double mid;
mid=(r+l)/2.0;
if(cnm(mid))
{
l=mid;
}
else
{
r=mid;
}
}
printf("%.2lf\n",l);
}
return 0;
}

120
HDOJ/1554_autoAC.cpp Normal file
View File

@ -0,0 +1,120 @@
#include<stdio.h>
int stack[1001];
int snum;
int add(int x, int y, int z, int pre)
{
int i, array[11];
array[pre] = y;
for(i = 1; i < pre; i ++)
{
array[i] = z % 10;
z /= 10;
}
for(i = pre + 1; i <= 10; i ++)
{
array[i] = x % 10;
x /= 10;
}
stack[++ snum] = 0;
for(i = 10; i >= 1; i --)
{
stack[snum] *= 10;
stack[snum] += array[i];
}
return 0;
}
int Max(int a, int b)
{
return a > b ? a : b;
}
int find(int pre, int n)
{
int i;
int flag = 1;
int max;
if(pre == 0)
return 0;
for(i = 1; i < pre; i ++)
flag *= 10;
if(pre != 1 && n % 2 == 0 || pre == 1)
{
max = Max(flag / 2, 1);
for(i = (n - n / flag * flag) / 2; i < flag; i += max)
{
if(i * 2 >= n)
break;
if((n - i * 2) < flag)
break;
if((n - i * 2) / flag % 11 != 10)
{
add((n - i * 2) / flag / 11, (n - i * 2) / flag % 11, i, pre);
}
}
}
return find(pre - 1, n);
}
int swap(int &a, int &b)
{
a ^= b;
b ^= a;
a ^= b;
return 0;
}
int sort()
{
int i, j;
for(i = 1; i < snum; i ++)
for(j = i + 1; j <= snum; j ++)
{
if(stack[i] == stack[j])
{
stack[j] = stack[snum];
snum --;
j --;
}
}
for(i = 1; i <= snum; i ++)
{
for(j = i + 1; j <= snum; j ++)
{
if(stack[i] > stack[j])
{
swap(stack[i], stack[j]);
}
}
}
return 0;
}
int prin(int x, int n)
{
int y = n - x, i;
int array[11], tail = 0;
printf("%d + ", x);
while(x >= 10)
{
array[++ tail] = y % 10;
y /= 10;
x /= 10;
}
for(i = tail; i >= 1; i --)
printf("%d", array[i]);
printf(" = %d\n", n);
return 0;
}
int main()
{
int n;
int i;
while(scanf("%d", &n) != EOF)
{
snum = 0;
find(10, n);
sort();
printf("%d\n", snum);
for(i = 1; i <= snum; i ++)
{
prin(stack[i], n);
}
}
return 0;
}

17
HDOJ/1555_autoAC.cpp Normal file
View File

@ -0,0 +1,17 @@
#include<stdio.h>
int main()
{
int m,k;
while(scanf("%d%d",&m,&k)!=EOF&&m&&k)
{
int sum=0;
while(m-k>=0)
{
sum+=k;
m=m-k+1;
}
sum+=m;
printf("%d\n",sum);
}
return 0;
}

25
HDOJ/1556_autoAC.cpp Normal file
View File

@ -0,0 +1,25 @@
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int inf=10000000;
int f[100005];
int main(){
int n;
while(scanf("%d",&n)!=EOF&&n){
memset(f,0,sizeof(f));
for(int i=0,a,b;i<n;++i){
scanf("%d%d",&a,&b);
f[a]++;
f[b+1]--;
}
cout<<f[1];
for(int i=2;i<=n;++i){
f[i]+=f[i-1];
cout<<' '<<f[i];
}
cout<<endl;
}
return 0;
}

40
HDOJ/1557_autoAC.cpp Normal file
View File

@ -0,0 +1,40 @@
#include<stdio.h>
#include<string.h>
int main ()
{
int a[25],c1[5000],c2[5000];
int i,j,k,n,m,t,s;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(m=0,i=1;i<=n;i++)
{
scanf("%d",&a[i]);
m+=a[i];
}
for(int ii=1;ii<=n;ii++)
{
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));
c1[0]=1;
for(i=1;i<=n;i++)
{
if(i==ii)continue;
for(j=0;j<=m/2;j++)
for(k=0;k+j<=m/2&&k<=a[i];k+=a[i])
c2[k+j]+=c1[j];
for(j=0;j<=m/2;j++)
c1[j]=c2[j],c2[j]=0;
}
for(s=0,i=m/2-a[ii]+1;i<=m/2;i++)
s+=c1[i];
if(ii==1)
printf("%d",s);
else
printf(" %d",s);
}
printf("\n");
}
return 0;
}

80
HDOJ/1558_autoAC.cpp Normal file
View File

@ -0,0 +1,80 @@
#include<iostream>
using namespace std;
#define max_n 1010
struct point
{
double x, y;
};
struct segment
{
point s, e;
};
double dir(point a, point b, point c)
{
return a.x*b.y + b.x*c.y + c.x*a.y - a.x*c.y - c.x*b.y - b.x*a.y;
}
bool connected(point a, point b, point c, point d)
{
if (dir(a, b, c)*dir(a, b, d) <= 0 && dir(d, c, a)*dir(d, c, b) <= 0)return 1;
return 0;
}
segment se[max_n];
int par[max_n], r[max_n];
int find(int x)
{
if (x == par[x])return x;
else return par[x] = find(par[x]);
}
void unite(int x, int y)
{
int a = find(x), b = find(y);
if (a != b)
{
if (r[a] < r[b])par[a] = b;
else par[b] = a;
if (r[a] == r[b])r[a]++;
}
return;
}
void inse(int n)
{
bool ok = 1;
for (int j = 1; j < n; j++)
{
if (connected(se[j].s, se[j].e, se[n].s, se[n].e))
unite(n, j);
}
return;
}
int main()
{
int tes, k, n, m; char c;
cin >> tes;
while (tes--)
{
cin >> n;
k = 0;
for (int i = 1; i <= n; i++)par[i] = i, r[i] = 0;
for (int i = 1; i <= n; i++)
{
cin >> c;
if (c == 'P')
{
k++;
cin >> se[k].s.x >> se[k].s.y >> se[k].e.x >> se[k].e.y;
inse(k);
}
else
{
cin >> m;
int root = find(m), ans = 0;
for (int i = 1; i <= k; i++)
{
if (find(i) == root)ans++;
}
cout << ans << endl;
}
}
if(tes)cout << endl;
}
}

57
HDOJ/1559_autoAC.cpp Normal file
View File

@ -0,0 +1,57 @@
#include<stdio.h>
#include<string.h>
#define INF 0x3f3f3f
#define MAX 1000+10
int map[MAX][MAX];
int dp[MAX][MAX];
int max(int x,int y)
{
return x>y?x:y;
}
int main()
{
int t,n,m,x,y,k,l;
int i,j,sum,maxsum,s;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d",&n,&m,&x,&y);
memset(dp,0,sizeof(dp));
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%d",&map[i][j]);
if(i==1)
dp[i][j]=map[i][j];
else
dp[i][j]=map[i][j]+dp[i-1][j];
}
}
maxsum=-INF;
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
if(j-i+1!=x)
continue;
for(k=1;k<=m;k++)
{
sum=dp[j][k]-dp[i-1][k];
s=0;
for(l=k;l<k+y&&l<=m;l++)
{
s+=dp[j][l]-dp[i-1][l];
if(s>sum)
sum=s;
if(s<0)
s=0;
}
maxsum=max(sum,maxsum);
}
}
}
printf("%d\n",maxsum);
}
return 0;
}

76
HDOJ/1560_autoAC.cpp Normal file
View File

@ -0,0 +1,76 @@
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
int n,deep;
char c[10] = "ACGT";
struct node
{
char s[10];
int len;
} a[10];
int pos[10];
int get_h()
{
int ans = 0;
for(int i = 1; i<=n; i++)
ans = max(ans,a[i].len-pos[i]);
return ans;
}
int dfs(int step)
{
if(step+get_h()>deep)
return 0;
if(!get_h())
return 1;
int i,j;
int tem[10];
for(i = 0; i<4; i++)
{
int flag = 0;
for(j = 1; j<=n; j++)
tem[j] = pos[j];
for(j = 1; j<=n; j++)
{
if(a[j].s[pos[j]] == c[i])
{
flag = 1;
pos[j]++;
}
}
if(flag)
{
if(dfs(step+1))
return 1;
for(j = 1; j<=n; j++)
pos[j] = tem[j];
}
}
return 0;
}
int main()
{
int t,i,j,maxn;
cin >> t;
while(t--)
{
cin>>n;
maxn = 0;
for(i = 1; i<=n; i++)
{
cin>>a[i].s;
a[i].len = strlen(a[i].s);
maxn = max(maxn,a[i].len);
pos[i] = 0;
}
deep = maxn;
while(1)
{
if(dfs(0))break;
deep++;
}
cout << deep << endl;
}
return 0;
}

53
HDOJ/1561_autoAC.cpp Normal file
View File

@ -0,0 +1,53 @@
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
using namespace std;
int N,M;
bool vis[205];
int val[205];
int dp[205][205];
vector<int> map[205];
void dfs(int s)
{
for(int i=1;i<=M;i++)dp[s][i] = val[s];
vis[s]=1;
for(int i=0;i<map[s].size();i++)
{
int t=map[s][i];
if(vis[t])continue;
dfs(t);
int j,k;
if(s==0)
{
for(j=M;j>=0 ;j--)//m == 2
for(k=1;k<=j ;k++)
dp[s][j] = max(dp[s][j],dp[s][j-k]+dp[t][k]);
}
else
{
for(j=M;j>=1 ;j--)//m == 2
for(k=1;k<j ;k++)
dp[s][j] = max(dp[s][j],dp[s][j-k]+dp[t][k]);
}
}
}
int main()
{
while(~scanf("%d%d",&N,&M),N+M)
{
for(int i=0;i<=N;i++) map[i].clear();
for(int i=1;i<=N;i++)
{
int a,v;
scanf("%d%d",&a,&v);
map[a].push_back(i);
val[i]=v;
}
memset(dp,0,sizeof dp);
memset(vis,0,sizeof vis);
dfs(0);
printf("%d\n",dp[0][M]);
}
return 0;
}

17
HDOJ/1562_autoAC.cpp Normal file
View File

@ -0,0 +1,17 @@
#include <stdio.h>
int main()
{
int i,a,b,c,n;
scanf("%d",&n);
while(n--)
{
scanf("%d%d%d",&a,&b,&c);
for(i=1000;i<=9999;i++)
{
if(i%a==0&&(i+1)%b==0&&(i+2)%c==0)break;
}
if(i>9999)printf("Impossible\n");
else printf("%d\n",i);
}
return 0;
}

17
HDOJ/1563_autoAC.cpp Normal file
View File

@ -0,0 +1,17 @@
#include<stdio.h>
int main()
{
int n,m,x;
while(scanf("%d",&n)!=EOF&&n)
{
x=0;
while(n--)
{
scanf("%d",&m);
if(x<m)
x=m;
}
printf("%d\n",x);
}
return 0;
}

18
HDOJ/1564_autoAC.cpp Normal file
View File

@ -0,0 +1,18 @@
#include<stdio.h>
int main()
{
int i;
scanf("%d",&i);
if(i!=0)
{
while(i!=0)
{
if(i%2==0)
printf("8600\n");
else
printf("ailyanlu\n");
scanf("%d",&i);
}
}
return 0;
}

47
HDOJ/1565_autoAC.cpp Normal file
View File

@ -0,0 +1,47 @@
#include<iostream>
using namespace std;
const int Max = 25;
int num[Max][Max];
int s[20000];
int dp[Max][20000];
void get_s()
{
int cnt = 0;
for (int i=0;i<(1<<20);++i)
if ( ( i&(i<<1) )==0)
s[cnt++] = i;
}
int main()
{
get_s();
int n;
while (cin>>n)
{
for (int i=0;i<n;++i)
for (int j=0;j<n;++j)
cin>>num[i][j];
memset(dp,0,sizeof(dp));
for (int i=0;i<n;++i)
{
for (int j=0;s[j]<(1<<n);++j)
{
for (int k=0;k<n;++k)
if ( ( (s[j]>>k)&1)==1 )
dp[i][j] += num[i][k];
if (i)
{
int mx = 0;
for (int k=0;s[k]<(1<<n);++k)
if ( (s[k]&s[j])==0 && dp[i-1][k]>mx )
mx = dp[i-1][k];
dp[i][j] += mx;
}
}
}
int ans = 0;
for (int i=0;s[i]<(1<<n);++i)
ans = dp[n-1][i]>ans?dp[n-1][i]:ans;
cout<<ans<<endl;
}
return 0;
}

77
HDOJ/1566_autoAC.cpp Normal file
View File

@ -0,0 +1,77 @@
#include <stdio.h>
short last[60],an[60],temp[60];
short a[60][4];
void mult(short *a,short *b,short *c)
{
int i,j,m,n,k;
if(a[0]==1&&a[1]==0||b[0]==1&&b[1]==0)
{
c[0]=1;c[1]=0;return;
}
m=a[0];n=b[0];
k=n+m-1;
for(i=0;i<=k;i++)c[i]=0;
for(i=1;i<=m;i++) {
if(a[i])for(j=1;j<=n;j++)if(b[j])c[i+j-1]+=a[i]*b[j];
}
for(i=k;i>=1;i--)if(c[i]>9){ c[i-1]+=c[i]/10;c[i]%=10;}
if(c[0]>0)
{k++; for(i=k;i>=1;i--)c[i]=c[i-1]; }c[0]=k;
}
void dectoB(int sum,int b,short *S)
{
int i,j,k=0,t,d[20];
while(sum>0) { d[++k]=sum%b; sum/=b; }
S[0]=k;
for(i=1,j=k;i<=k;i++,j--) {S[i]=d[j];}
}
void copy(short *a,short *b)
{
int i;
for(i=0;i<=b[0];i++)
a[i]=b[i];
}
void answer()
{
int i;
printf("%d",an[0]);
last[0]=1;last[1]=1;
for(i=1;i<=an[0];i++)
{
printf(" %d",an[i]);
mult(last,a[an[i]],temp);
copy(last,temp);
}
printf("\n");
for(i=1;i<=last[0];i++)
printf("%d",last[i]);
printf("\n");
}
int main()
{
int i,sum,n,res;
for(i=1;i<=50;i++)
dectoB(i,10,a[i]);
while(scanf("%d",&n)!=EOF)
{
if(n<5) {printf("2 1 %d\n%d\n",n-1,n-1);continue;}
an[0]=0;sum=0;
for(i=2;i<=50;i++)
{
if(sum+i>n) break;
sum+=i;
an[++an[0]]=i;
}
res=n-sum;
if(res)
{
while(1)
{
for(i=an[0];i>=1;i--)
{an[i]++;res--;if(res==0) goto loop;}
}
}
loop: answer();
}
return 0;
}

42
HDOJ/1567_autoAC.cpp Normal file
View File

@ -0,0 +1,42 @@
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
bool vis[2010];
int num[2010];
int main(){
int n;
int t1,t2;
bool flag;
char a[7000];
while(~scanf("%d",&n)){
memset(num,0,sizeof(num));
flag=1;
for(int i=1;i<=n-1;i++){
memset(vis,0,sizeof(vis));
for(int j=1;j<=n/2;j++){
scanf("%d-%d",&t1,&t2);
if(vis[t1]||vis[t2]){
flag=0;
}
vis[t1]=1;
vis[t2]=1;
num[t1]++;
num[t2]++;
}
}
if(flag){
for(int i=1;i<=n;i++){
if(num[i]!=n-1){
flag=0;
break;
}
}
}
if(flag)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}

29
HDOJ/1568_autoAC.cpp Normal file
View File

@ -0,0 +1,29 @@
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
const double s = (sqrt(5.0)+1.0)/2;
int main()
{
int n,i;
double bit;
int fac[21] = { 0 , 1 };
for(i = 2; i < 21; i++)
fac[i] = fac[i-1] + fac [i-2];
while(cin >> n)
{
if(n <= 20) {
cout << fac[n] << endl;
continue;
}
else{
bit = -0.5*log(5.0)/log(10.0)+((double)n)*log(s)/log(10.0);
bit = bit - floor(bit);
bit = pow(10.0,bit);
while(bit < 1000)
bit = 10.0 * bit;
cout << (int)bit << endl;
}
}
return 0;
}

99
HDOJ/1569_autoAC.cpp Normal file
View File

@ -0,0 +1,99 @@
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#define MAXN 3005
#define MAXM 500005
#define oo 1000000007
#define ll long long
using namespace std;
struct Dinic
{
struct node
{
int x,y,c,next;
}line[MAXM];
int Lnum,_next[MAXN],dis[MAXN];
void initial(int n)
{
for (int i=0;i<=n;i++) _next[i]=-1;
Lnum=-1;
}
void addline(int x,int y,int c)
{
line[++Lnum].next=_next[x],_next[x]=Lnum;
line[Lnum].x=x,line[Lnum].y=y,line[Lnum].c=c;
line[++Lnum].next=_next[y],_next[y]=Lnum;
line[Lnum].x=y,line[Lnum].y=x,line[Lnum].c=0;
}
bool BFS(int s,int e)
{
queue<int> Q;
while (!Q.empty()) Q.pop();
memset(dis,0,sizeof(dis));
dis[s]=1;
Q.push(s);
while (!Q.empty())
{
int h,k;
h=Q.front(),Q.pop();
if (h==e) return dis[e];
for (k=_next[h];k!=-1;k=line[k].next)
if (line[k].c && !dis[line[k].y])
dis[line[k].y]=dis[h]+1,Q.push(line[k].y);
}
return false;
}
int dfs(int x,int flow,int e)
{
if (x==e) return flow;
int temp,cost=0;
for (int k=_next[x];k!=-1;k=line[k].next)
if (line[k].c && dis[line[k].y]==dis[x]+1)
{
temp=dfs(line[k].y,min(flow-cost,line[k].c),e);
if (temp)
{
line[k].c-=temp,line[k^1].c+=temp;
cost+=temp;
if (flow==cost) return cost;
}else dis[line[k].y]=-1;
}
return cost;
}
int MaxFlow(int s,int e)
{
int MaxFlow=0;
while (BFS(s,e))
MaxFlow+=dfs(s,oo,e);
return MaxFlow;
}
}T;
int main()
{
int R,C,i,j,s,e,v,sum;
while (~scanf("%d%d",&R,&C))
{
T.initial(R*C+5);
s=R*C+1,e=R*C+2,sum=0;
for (i=0;i<R;i++)
for (j=0;j<C;j++)
{
scanf("%d",&v),sum+=v;
if ((i+j)%2)
{
T.addline(s,i*C+j,v);
if (i) T.addline(i*C+j,(i-1)*C+j,oo);
if (j) T.addline(i*C+j,i*C+j-1,oo);
if (i!=R-1) T.addline(i*C+j,(i+1)*C+j,oo);
if (j!=C-1) T.addline(i*C+j,i*C+j+1,oo);
}
else
T.addline(i*C+j,e,v);
}
printf("%d\n",sum-T.MaxFlow(s,e));
}
return 0;
}

22
HDOJ/1570_autoAC.cpp Normal file
View File

@ -0,0 +1,22 @@
#include <stdio.h>
int jc(int n)
{
int i,t=1;
if(n==0) return 1;
for(i=n;i>0;i--) t *= i;
return t;
}
int main()
{
int k,t,n,m;
char lt;
scanf("%d",&t);
while(t--)
{
scanf(" %c%d%d",&lt,&n,&m);
if(lt=='A') k = jc(n)/jc(n-m);
else k = jc(n)/(jc(m)*jc(n-m));
printf("%d\n",k);
}
return 0;
}

89
HDOJ/1571_autoAC.cpp Normal file
View File

@ -0,0 +1,89 @@
#include<stdio.h>
#include<iostream>
#include<queue>
#include<cstdlib>
using namespace std;
int i,j,n,t,dist,pep,stat;
int way[35][35];
char req[5];
struct passeger
{
int end;
passeger *next;
}*p1,*p2;
void UP()
{
int k;
scanf("%d",&k);
if(stat==k||pep==7)return ;
if(p1==NULL){p1=new passeger;p1->end=k;p2=p1;pep++;p1->next=NULL;}
else{p2->next=new passeger;p2=p2->next;p2->end=k;pep++;p2->next=NULL;}
}
void GO()
{
int k,z;
passeger *p,*q,*r;
if(!pep)return ;
k=p1->end;
z=pep;
p=p1;
while(z--)
{
if(p->end==k)
{
if(p==p1)
{
p1=p->next;
delete p;
p=p1;
pep--;
}
else
{
r=q->next;
q->next=p->next;
delete r;
p=q->next;
pep--;
}
}
else q=p,p=p->next;
}
dist+=way[stat][k];
stat=k;
if(pep)
{
p=p1;
while(p->next!=NULL)
{
p=p->next;
}
p2=p;
}
else p1=p2=NULL;
}
int main()
{
while(scanf("%d",&n),n)
{
dist=0,stat=0,pep=0;
p1=p2=NULL;
for(i=0;i<n;i++)
for(j=0;j<n;j++)scanf("%d",&way[i][j]);
scanf("%d",&t);
while(t--)
{
scanf("%s",req);
if(req[0]=='U')UP();
else GO();
}
printf("%d\n",dist);
while(pep--)
{
p2=p1->next;
delete p1;
p1=p2;
}
}
return 0;
}

48
HDOJ/1572_autoAC.cpp Normal file
View File

@ -0,0 +1,48 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 33
#define inf 1<<30
int map[MAXN][MAXN];
int n,MIN,k;
int num[MAXN];
bool mark[MAXN];
void dfs(int start,int count,int dist){
if(count==k){
MIN=min(dist,MIN);
return ;
}
for(int i=1;i<=k;i++){
if(!mark[i]){
mark[i]=true;
dfs(num[i],count+1,dist+map[start][num[i]]);
mark[i]=false;
}
}
}
int main(){
int _case;
while(~scanf("%d",&n)&&n){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
scanf("%d",&map[i][j]);
}
}
k=0,MIN=inf;
scanf("%d",&_case);
memset(mark,false,sizeof(mark));
while(_case--){
int x;
scanf("%d",&x);
if(mark[x])continue;
mark[x]=true;
num[++k]=x;
}
memset(mark,false,sizeof(mark));
dfs(0,0,0);
printf("%d\n",MIN);
}
return 0;
}

80
HDOJ/1573_autoAC.cpp Normal file
View File

@ -0,0 +1,80 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef __int64 ll;
ll n,k;
ll m[15],c[15],N;
bool check;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
if(!b)
{
x=1;
y=0;
return a;
}
else
{
ll res=exgcd(b,a%b,x,y);
ll t=x;
x=y;
y=t-(a/b)*y;
return res;
}
}
ll mod(ll x,ll y)
{
ll res=x%y;
if(res<=0)
res=res+y;
return res;
}
void solve()
{
ll i;
check=true;
ll ans=c[1],lcm=m[1],x,y,g;
if(ans==0)
ans=m[1];
for(i=2;i<=n;i++)
{
g=exgcd(lcm,m[i],x,y);
if((c[i]-ans)%g)
{
check=false;
break;
}
ans=mod(ans+lcm*mod((c[i]-ans)/g*x,m[i]/g),lcm/g*m[i]);
lcm=lcm/g*m[i];
}
if(check)
{
if(N>=ans)
{
printf("%I64d\n",(N-ans)/lcm+1);
}
else
{
printf("0\n");
}
}
else
{
printf("0\n");
}
}
int main()
{
ll i,t;
scanf("%I64d",&t);
while(t--)
{
scanf("%I64d%I64d",&N,&n);
for(i=1;i<=n;i++)
scanf("%I64d",&m[i]);
for(i=1;i<=n;i++)
scanf("%I64d",&c[i]);
solve();
}
return 0;
}

52
HDOJ/1574_autoAC.cpp Normal file
View File

@ -0,0 +1,52 @@
#include<iostream>
#include<climits>
#include<algorithm>
using namespace std;
const int maxa=21000;
const int maxn=1002;
int a[maxn];
int b[maxn];
int c[maxn];
int p[maxa];
int N,l,r;
void dp()
{
for(int i=0;i<maxa;++i)
p[i]=INT_MIN;
l=r=10000;
p[l]=0;
for(int i=0;i<N;++i)
{
if(a[i]>0)
{
for(int j=b[i];j>=l;--j)
if(p[j]!=INT_MIN)
p[j+a[i]]=max(p[j+a[i]],p[j]+c[i]);
r+=a[i];
}
else if(a[i]<0)
{
for(int j=b[i];j<=r;++j)
if(p[j]!=INT_MIN)
p[j+a[i]]=max(p[j+a[i]],p[j]+c[i]);
l+=a[i];
}
}
}
int main()
{
int T;
cin>>T;
while(T--)
{
cin>>N;
for(int i=0;i<N;++i)
{
cin>>a[i]>>b[i]>>c[i];
b[i]+=10000;
}
dp();
cout<<*max_element(p,p+maxa)<<endl;
}
return 0;
}

79
HDOJ/1575_autoAC.cpp Normal file
View File

@ -0,0 +1,79 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
const int MAX=101;
int n;
using namespace std;
typedef struct Matrix
{
int mat[MAX][MAX];
}matrix;
matrix A,B;
Matrix matrix_mul(matrix a,matrix b)
{
matrix c;
memset(c.mat,0,sizeof(c.mat));
int i,j,k;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
for(k=1;k<=n;k++)
{
c.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
c.mat[i][j]%=9973;
}
}
}
return c;
}
Matrix matrix_quick_power(matrix a,int k)
{
int i;
matrix b;
memset(b.mat,0,sizeof(b.mat));
for(i=1;i<=n;i++)
b.mat[i][i]=1;
while(k)
{
if(k&1)
{
b=matrix_mul(a,b);
k-=1;
}
else
{
a=matrix_mul(a,a);
k>>=1;
}
}
return b;
}
int main()
{
int i,j,sum,k,t;
cin>>t;
while(t--)
{
cin>>n>>k;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cin>>A.mat[i][j];
A.mat[i][j]%=9973;
}
}
B=matrix_quick_power(A,k);
sum=0;
for(i=1;i<=n;i++)
{
sum+=B.mat[i][i];
sum%=9973;
}
cout<<sum<<endl;
}
return 0;
}

15
HDOJ/1576_autoAC.cpp Normal file
View File

@ -0,0 +1,15 @@
#include<stdio.h>
int main()
{
int T,n,i;
long long int B;
scanf("%d",&T);
while(T--)
{
scanf("%d%lld",&n,&B);
for(i=0;i<=10000;i++)
if((B*i-n)%9973==0) break;
printf("%d\n",i);
}
return 0;
}

58
HDOJ/1577_autoAC.cpp Normal file
View File

@ -0,0 +1,58 @@
#include<iostream>
using namespace std;
int main()
{
int len;
double x1,y1,x2,y2;
while(cin>>len)
{
if(len==0)
break;
cin>>x1>>y1>>x2>>y2;
if(x2>len||y2>len||(-x2)>len||(-y2)>len)
cout<<"Out Of Range"<<endl;
else
{
int flag=0;
if(x1==x2)
{
if(y1-y2>1.0||y2-y1>1.0)
flag=1;
else
{
flag=0;
}
}
else if(y1==y2)
{
if(x1-x2>1.0||x2-x1>1.0)
flag=1;
else
{
flag=0;
}
}
else
{
double k=(y2-y1)*1.0/(x2*1.0-x1*1.0);
double b=y1*1.0-k*x1;
double minx=x1>x2?x2:x1;
double maxx=x1>x2?x1:x2;
for(double i=minx+1;i<maxx;i++)
{
double temp = (1.0*i - x1)*(y2 - y1) / (x2 - x1) + y1;
if (temp == (double)(int)temp)
{
flag=1;
break;
}
}
}
if(flag)
cout<<"No"<<endl;
else
cout<<"Yes"<<endl;
}
}
return 0;
}

47
HDOJ/1578_autoAC.cpp Normal file
View File

@ -0,0 +1,47 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define CLR(x) memset(x, 0, sizeof(x))
int n, m;
int d[505][505][2];
int x[505][505][2];
int nor[505][505], wes[505][505];
void init()
{
for (int t = 0; t < 2; ++ t)
for (int i = 0; i < n; ++ i)
for (int j = 0; j < m; ++ j)
scanf ("%d", &x[i][j][t]);
CLR (nor); CLR (wes);
for (int i = 0; i < n; ++ i)
for (int j = 0; j < m; ++ j){
wes[i][j] = (j ? x[i][j][0]+wes[i][j-1] : x[i][j][0]);
nor[i][j] = (i ? x[i][j][1]+nor[i-1][j] : x[i][j][1]);
}
}
int DP()
{
CLR (d);
d[0][0][0] = x[0][0][0]; d[0][0][1] = x[0][0][1];
for (int i = 0; i < n; ++ i)
for (int j = 0; j < m; ++ j){
if (!i && !j) continue;
d[i][j][0] = wes[i][j];
if (i)
d[i][j][0] += max(d[i-1][j][0], d[i-1][j][1]);
d[i][j][1] = nor[i][j];
if (j)
d[i][j][1] += max(d[i][j-1][0], d[i][j-1][1]);
}
return max(d[n-1][m-1][0], d[n-1][m-1][1]);
}
int main()
{
while (scanf ("%d%d", &n, &m) != EOF && n){
init();
printf ("%d\n", DP());
}
return 0;
}

46
HDOJ/1579_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int w[23][23][23];
int get(int i,int j,int k)
{
if(w[i][j][k]!=-1)
return w[i][j][k];
if(i<=0||j<=0||k<=0) return 1;
if(i>=20||j>=20||k>=20) return 1048576;
if(i<j&&j<k)
{
return get(i,j,k-1)+get(i,j-1,k-1)-get(i,j-1,k);
}
else
{
return get(i-1,j,k)+get(i-1,j-1,k)+get(i-1,j,k-1)-get(i-1,j-1,k-1);
}
}
int main()
{
memset(w,-1,sizeof(w));
w[0][0][0]=1;
for(int i=0;i<=21;i++)
{
for(int j=0;j<=21;j++)
{
for(int k=0;k<=21;k++)
{
w[i][j][k]=get(i,j,k);
}
}
}
int a,b,c;
while(cin>>a>>b>>c)
{
if(a==-1&&b==-1&&c==-1) break;
printf("w(%d, %d, %d) = ",a,b,c);
if(a<=0||b<=0||c<=0) printf("%d\n",w[0][0][0]);
else if(a>20||b>20||c>20) printf("%d\n",w[20][20][20]);
else
printf("%d\n",w[a][b][c]);
}
return 0;
}

65
HDOJ/1580_autoAC.cpp Normal file
View File

@ -0,0 +1,65 @@
#include<iostream>
#include<cstdio>
#include<list>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<cmath>
#include<memory.h>
#include<set>
#define ll long long
#define eps 1e-7
#define inf 0xfffffff
const ll INF = 1ll<<61;
using namespace std;
int n,k;
int dp[10 + 5][1000 + 5];
int travel[10 + 5][1000 + 5];
int cost[10 + 5][10 + 5][1000 + 5];
void clear() {
memset(dp,-1,sizeof(dp));
memset(travel,0,sizeof(travel));
memset(cost,0,sizeof(cost));
}
int main() {
int Case = 0;
while(scanf("%d %d",&n,&k),n + k) {
clear();
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++) {
if(i != j) {
scanf("%d",&travel[i][j]);
for(int l=0;l<travel[i][j];l++)
scanf("%d",&cost[i][j][l]);
}
}
}
dp[1][0] = 0;
int minn;
for(int j=1;j<=k;j++) {
for(int i=1;i<=n;i++) {
minn = -1;
for(int l=1;l<=n;l++) {
if(i == l || dp[l][j - 1] < 0)continue;
int tmp = (j - 1)%travel[l][i];
if(cost[l][i][tmp] == 0)continue;
if(minn < 0)
minn = dp[l][j - 1] + cost[l][i][tmp];
else if(minn > dp[l][j - 1] + cost[l][i][tmp])
minn = dp[l][j - 1] + cost[l][i][tmp];
}
dp[i][j] = minn;
}
}
printf("Scenario #%d\n",++Case);
if(dp[n][k] < 0)
printf("No flight possible.\n\n");
else
printf("The best flight costs %d.\n\n",dp[n][k]);
}
return 0;
}

69
HDOJ/1581_autoAC.cpp Normal file
View File

@ -0,0 +1,69 @@
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<memory.h>
using namespace std;
int dp[524289];
int get_solve(int state,int x)
{
int ret=state;
for(int i=x;i<=20;i+=x)
ret&=~(1<<(i-2));
for(int i=2;i<=20;i++)
{
if(ret&(1<<(i-2)))
for(int j=x;i-j-2>=0;j+=x)
{
if(!(ret&(1<<(i-j-2))))
{
ret&=~(1<<(i-2));
break;
}
}
}
return ret;
}
int dfs(int state)
{
if(dp[state]!=-1) return dp[state];
for(int i=2;i<=20;i++)
{
if(state&(1<<(i-2)))
{
if(dfs(get_solve(state,i))==0)
return dp[state]=1;
}
}
return dp[state]=0;
}
int main()
{
int t,n,a[21];
int count=1;
memset(dp,-1,sizeof(dp));
dp[0]=0;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int state=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
state|=1<<(a[i]-2);
}
printf("Scenario #%d:\n",count++);
if(dfs(state)==0)
puts("There is no winning move.\n");
else
{
printf("The winning moves are:");
for(int i=0;i<n;i++)
{
if(dfs(get_solve(state,a[i]))==0)
printf(" %d",a[i]);
}
puts(".\n");
}
}
}

45
HDOJ/1582_autoAC.cpp Normal file
View File

@ -0,0 +1,45 @@
#include<iostream>
using namespace std;
char mp[60][60];
int num;
void bfs(int x,int y)
{
if(x<0||x>51||y<0||y>51) return;
if('*'==mp[x][y]) return;
mp[x][y]='*';
bfs(x-1,y);
bfs(x,y-1); bfs(x,y+1);
bfs(x+1,y);
}
bool readin()
{
bool flag=1;
int i,j,n;
char temp;
for(i=0;i<=51;i++)
for(j=0;j<=51;j++)
mp[i][j]=' ';
if(scanf("%c",&temp)==EOF) return 0;
else mp[1][1]=temp;
for(j=2;scanf("%c",&temp)&&temp!=10;mp[1][j++]=temp);
for(n=2;j!=1;n++)
for(j=1;scanf("%c",&temp)&&temp!=10;mp[n][j++]=temp);
return 1;
}
int main()
{
int i,j,k,n;
char temp;
while(readin())
{
for(num=0,i=0;i<=51;i++)
for(j=0;j<=51;j++)
if(' '==mp[i][j])
{
num++;
bfs(i,j);
}
printf("%c\n",num==2?'A':'C');
}
return 0;
}

65
HDOJ/1583_autoAC.cpp Normal file
View File

@ -0,0 +1,65 @@
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string str_merge(string a, string b)
{
if (a == "")
{
return b;
}
int i;
int flag = 0;
int pos;
int alen = a.length();
int blen = b.length();
for (i = 1; i <= alen; i++)
{
if (b.substr(0, i) == a.substr(alen - i, i))
{
flag = 1;
pos = i;
}
}
if (flag)
{
return a + b.substr(pos, blen - pos);
}
else
{
return a + b;
}
}
int main()
{
int n;
int ans;
int d[10];
string str[10];
string a;
while (cin >> n)
{
int i;
for (i = 0; i < n; i++)
{
cin >> str[i];
d[i] = i;
}
ans = 1000;
do
{
string total = "";
for (i = 0; i < n; i++)
{
total = str_merge(total, str[d[i]]);
}
if (total.length() < ans)
{
ans = total.length();
a = total;
}
} while (next_permutation(d, d + n));
cout << ans << endl;
}
return 0;
}

62
HDOJ/1584_autoAC.cpp Normal file
View File

@ -0,0 +1,62 @@
#include<stdio.h>
int a[10][2];
int s,min;
int abs(int i,int j)
{
i-=j;
if(i>0) return i;
else return -i;
}
void DFS()
{
int i,j,flog=0;
int i0,i1,j0,j1;
for(i=0;i<10;i++)
{
if(a[i][0]!=-1&&a[i][0]!=10)
{
flog=1;
for(j=0;j<10;j++)
{
if(a[j][1]==a[i][0]&&s+abs(i,j)<min)
{
i0=a[i][0];
i1=a[i][1];
j0=a[j][0];
j1=a[j][1];
a[i][0]=-1;
a[j][1]=a[i][1];
s+=abs(i,j);
DFS();
s-=abs(i,j);
a[i][0]=i0;
a[i][1]=i1;
a[j][0]=j0;
a[j][1]=j1;
}
}
}
}
if(!flog)
{
if(min>s) min=s;
}
}
int main()
{
int n,i;
scanf("%d",&n);
while(n--)
{
for(i=0;i<10;i++)
{
scanf("%d",&a[i][0]);
a[i][1]=a[i][0]-1;
}
min=99999;
s=0;
DFS();
printf("%d\n",min);
}
return 0;
}

21
HDOJ/1587_autoAC.cpp Normal file
View File

@ -0,0 +1,21 @@
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int n,m;
while(cin>>n>>m)
{
vector<int> v;
int t;
while(n--)
{
cin>>t;
v.push_back(t);
}
sort(v.begin(),v.end());
cout<<(m/v[0])<<endl;
}
return 0;
}

66
HDOJ/1588_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int k,b,n,m;
struct Matrix {
__int64 map[2][2];
};
Matrix matrix,matk,matb,smat;
Matrix Mul(Matrix &a,Matrix &b){
Matrix c;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
c.map[i][j]=0;
for(int k=0;k<2;k++){
c.map[i][j]+=a.map[i][k]*b.map[k][j];
c.map[i][j]%=m;
}
}
}
return c;
}
Matrix Pow(int k,Matrix &temp){
if(k==1)return temp;
else if(k&1){
return Mul(temp,Pow(k-1,temp));
}else {
Matrix tmp=Pow(k>>1,temp);
return Mul(tmp,tmp);
}
}
Matrix Sum(Matrix &a,Matrix &b){
Matrix c;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
c.map[i][j]=a.map[i][j]+b.map[i][j];
c.map[i][j]%=m;
}
}
return c;
}
Matrix Binary_Sum(int k){
if(k==1)return matk;
else if(k&1){
return Sum(Binary_Sum(k-1),Pow(k,matk));
}else {
Matrix tmp1=Binary_Sum(k>>1);
Matrix tmp2=Mul(Pow(k>>1,matk),tmp1);
return Sum(tmp1,tmp2);
}
}
int main(){
matrix.map[0][0]=1,matrix.map[0][1]=1;
matrix.map[1][0]=1,matrix.map[1][1]=0;
while(scanf("%d%d%d%d",&k,&b,&n,&m)!=EOF){
matk=Pow(k,matrix);
smat=Binary_Sum(n-1);
if(b){
matb=Pow(b,matrix);
smat=Mul(matb,smat);
smat=Sum(matb,smat);
}
printf("%I64d\n",smat.map[1][0]);
}
return 0;
}

162
HDOJ/1589_autoAC.cpp Normal file
View File

@ -0,0 +1,162 @@
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int M=50005;
typedef struct Point
{
double x;
double y;
}Point;
Point p[M];
Point pp[M];
bool bo[M];
int stack[M];
double dis(Point A,Point B)
{
return sqrt((B.x-A.x)*(B.x-A.x)+(B.y-A.y)*(B.y-A.y));
}
bool cmp(Point a,Point b)
{
if(a.x<b.x)
return true;
if(a.x>b.x)
return false;
if(a.y<b.y)
return true;
return false;
}
double Xdet(Point A,Point B,Point C)
{
double x1,x2,y1,y2;
x1=B.x-A.x;
y1=B.y-A.y;
x2=C.x-A.x;
y2=C.y-A.y;
return x1*y2-x2*y1;
}
void Gram_Scan(Point *p,int &n)
{
int i,t;
sort(p+1,p+1+n,cmp);
for(t=0,i=1;i<=n;i++)
{
if(i>1&&p[i].x==p[i-1].x&&p[i].y==p[i-1].y)
continue;
p[++t]=p[i];
}
n=t;
t=0;
memset(bo+1,true,n*sizeof(bo[0]));
if(n>0)
{
stack[++t]=1;
bo[stack[t]]=false;
}
if(n>1)
{
stack[++t]=2;
bo[stack[t]]=false;
}
if(n>2)
{
for(i=3;i<n;i++)
if(bo[i]&&Xdet(p[stack[t-1]],p[stack[t]],p[i])>=0)
{
stack[++t]=i;
bo[i]=false;
}
else
{
while(t>=2&&Xdet(p[stack[t-1]],p[stack[t]],p[i])<0)
{
bo[stack[t]]=true;
t--;
}
stack[++t]=i;
bo[stack[t]]=false;
}
for(i=n;i>=1;i--)
if(bo[i]&&Xdet(p[stack[t-1]],p[stack[t]],p[i])>=0)
{
stack[++t]=i;
bo[i]=false;
}
else
{
while(t>=2&&Xdet(p[stack[t-1]],p[stack[t]],p[i])<0)
{
bo[stack[t]]=true;
t--;
}
stack[++t]=i;
bo[stack[t]]=false;
}
t--;
}
for(i=1;i<=t;i++)
pp[i]=p[stack[i]];
memcpy(p+1,pp+1,t*sizeof(Point));
n=t;
}
int n,o[M],on;
int dcmp(double a,double b)
{
if(a-b<1e-10&&b-a<1e-10)
return 0;
if(a>b)
return 1;
return -1;
}
bool cmp1(const Point &a,Point &b)
{
return dcmp(a.x,b.x)<0;
}
bool cmp2(const int&a,const int&b)
{
return dcmp(p[a].y,p[b].y)<0;
}
double min(double a,double b)
{
return a<b?a:b;
}
double search(int s,int t)
{
int mid=(s+t)/2,i,j;
double ret=1e300;
if(s>=t)
return ret;
for(i=mid;i>=s&&!dcmp(p[i].x,p[mid].x);i--);ret=search(s,i);
for(i=mid;i<=t&&!dcmp(p[i].x,p[mid].x);i++);ret=min(ret,search(i,t));on=0;
for(i=mid;i>=s&&dcmp(p[mid].x-p[i].x,ret)<=0;i--)o[++on]=i;
for(i=mid+1;i<=t&&dcmp(p[i].x-p[mid].x,ret)<=0;i++)o[++on]=i;
sort(o+1,o+on+1,cmp2);
for(i=1;i<=on;i++)
for(j=1;j<=10;j++)
if(i+j<=on)
ret=min(ret,dis(p[o[i]],p[o[i+j]]));
return ret;
}
int main()
{
int n,i,count=0,j;
double shortdis,longdis;
while(scanf("%d",&n),n)
{
for(i=1;i<=n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
sort(p+1,p+n+1,cmp1);
shortdis=search(1,n);
longdis=0;
Gram_Scan(p,n);
for(i=1;i<=n-1;i++)
for(j=i+1;j<=n;j++)
if(dis(p[i],p[j])>longdis)
longdis=dis(p[i],p[j]);
printf("Case %d:\n",++count);
printf("Distance of the nearest couple is %.3lf\n",shortdis);
printf("Distance of the most distant couple is %.3lf\n\n",longdis);
}
return 0;
}

16
HDOJ/1590_autoAC.cpp Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
#include <math.h>
const double EPS = 10e-6;
int main() {
double a, b, r, x, y, A, k, c, d;
while(scanf("%lf %lf %lf %lf", &x, &y, &A, &k) != EOF) {
A = A * acos(-1)/180.0;
a = 1.0 - k * cos(A);
b = -k * sin(A);
c = x * a + y * b;
d = y * a - x * b;
r = a * a + b * b;
printf("(%.3lf,%.3lf)\n", c/r+EPS, d/r+EPS);
}
return 0;
}

26
HDOJ/1591_autoAC.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
string lib;
cin >> lib;
for(char c = 'Z'; c >= 'A'; --c)
{if(lib.find(c) == string::npos){lib += c;}}
string line;
cin.ignore(1);
while(getline(cin , line))
{
for(size_ i = 0; i < line.size(); ++i)
{
if(isupper(line[i]))
{line[i] = 'A' + lib.find(line[i]);}
else if(islower((line[i])))
{line[i] = 'a' + lib.find(toupper(line[i]));}
}
cout << line << endl;
}
return 0;
}

38
HDOJ/1592_autoAC.cpp Normal file
View File

@ -0,0 +1,38 @@
#include<stdio.h>
#include<string.h>
int num[1010][1010];
int main()
{
int i,n,j;
memset(num,0,sizeof(num));
num[0][1]=1;
for(i=1;i<1010;i++)
{
for(j=1;j<1010;j++)
{
if(j==1)
num[i][j]+=(num[i-1][j])*2+1;
else
num[i][j]+=num[i-1][j]*2;
}
for(j=2;j<1010;j++)
{
if(num[i][j-1]>9)
num[i][j]+=num[i][j-1]/10;
num[i][j-1]%=10;
}
}
while(scanf("%d",&n)!=EOF)
{
for(i=1010;i>=1;i--)
if(num[n][i]!=0)
{
printf("%d",num[n][i]);
break;
}
for(j=i-1;j>=1;j--)
printf("%d",num[n][j]);
printf("\n");
}
return 0;
}

12
HDOJ/1593_autoAC.cpp Normal file
View File

@ -0,0 +1,12 @@
#include <stdio.h>
#define PI 3.1415926
int main()
{
double R, v1, v2, r;
while(~scanf("%lf%lf%lf", &R, &v1, &v2))
{
r = R * v1 / v2;
printf((R - r) / v1 < PI * R / v2 ? "Yes\n" : "No\n");
}
return 0;
}

31
HDOJ/1594_autoAC.cpp Normal file
View File

@ -0,0 +1,31 @@
#include<iostream>
#include<cstdio>
using namespace std;
int abs(int a)
{
if(a<0)return -a;
return a;
}
int main()
{
int i,n;
while(scanf("%d",&n)!=EOF)
{
int x,y,max;
scanf("%d%d",&x,&y);
max=abs(y-x);
int xx=1,yy=2;
for(i=2;i<n;i++)
{
x=y;
scanf("%d",&y);
if(abs(y-x)>max)
{
max=abs(y-x);
xx=i;yy=i+1;
}
}
printf("%d %d\n",xx,yy);
}
return 0;
}

113
HDOJ/1595_autoAC.cpp Normal file
View File

@ -0,0 +1,113 @@
#include<iostream>
#include<vector>
#include<string.h>
using namespace std;
int map[1005][1005];
int pre[1005];
int dis[1005];
bool s[1005];
class path
{
public:
int start, end;
};
vector<path>p;
void dijkstra(int n)
{
memset(s,false,sizeof(s));
int i, j, k;
for (i = 2; i <= n; i++)
{
dis[i] = map[1][i];
if (map[1][i] != -1)
{
pre[i] = 1;
}
else
{
pre[i] = -1;
}
}
pre[1] = -1;
dis[1] = 0;
s[1] = true;
for (j = 2; j <= n; j++)
{
int min = 100000;
int u;
for (i = 1; i <= n; i++)
{
if (!s[i] && dis[i] != -1)
{
if (min > dis[i])
{
min = dis[i];
u = i;
}
}
}
s[u] = true;
for (k = 1; k <= n;k++)
{
if (!s[k] && map[u][k]!=-1)
{
if ((dis[k] > dis[u] + map[u][k])||dis[k]==-1)
{
dis[k] = dis[u] + map[u][k];
pre[k] = u;
}
}
}
}
}
void findpath(int n)
{
p.clear();
int i = n;
while (pre[i]!=-1)
{
path tmp;
tmp.start = pre[i];
tmp.end = i;
p.push_back(tmp);
i=pre[i];
}
}
int main()
{
int n, m;
while (cin>>n>>m)
{
int i,k;
int start, end, weight;
for (i = 1; i <= n;i++)
for (k = 1; k <= n; k++)
{
map[i][k] = -1;
}
for (i = 0; i < m; i++)
{
cin >> start >> end >> weight;
map[start][end] = weight;
map[end][start] = weight;
}
dijkstra(n);
findpath(n);
int num,max=-1;
for (num = 0; num < p.size(); num++)
{
path tmp = p[num];
int t;
t = map[tmp.start][tmp.end];
map[tmp.start][tmp.end] = -1;
map[tmp.end][tmp.start] = -1;
dijkstra(n);
if (dis[n]>max)
max = dis[n];
map[tmp.start][tmp.end] = t;
map[tmp.end][tmp.start] = t;
}
cout << max << endl;
}
return 0;
}

31
HDOJ/1596_autoAC.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#define N 1002
using namespace std;
int n;
double map[N][N];
void floyd(){
for(int k = 1;k<=n;k++)
for(int i = 1;i<=n;i++)
for(int j = 1;j<=n;j++)
if(map[i][j] < map[i][k]*map[k][j])
map[i][j] = map[i][k]*map[k][j];
}
int main(){
int Q,a,b;
while(~scanf("%d",&n)){
memset(map,0,sizeof(map));
for(int i = 1;i<=n;i++)
for(int j = 1;j<=n;j++)
scanf("%lf",&map[i][j]);
scanf("%d",&Q);
floyd();
for(int i = 0;i<Q;i++){
scanf("%d%d",&a,&b);
if(!map[a][b]) printf("What a pity!\n");
else printf("%.3f\n",map[a][b]);
}
}
return 0;
}

15
HDOJ/1597_autoAC.cpp Normal file
View File

@ -0,0 +1,15 @@
#include<stdio.h>
#include<math.h>
int main()
{
__int64 i,t,n,m;
scanf("%I64d",&t);
while(t--)
{
scanf("%I64d",&n);
m=((__int64)sqrt(1+8*(n-1))-1)/2;
n-=m*(m+1)/2;
printf("%I64d\n",n%9!=0?n%9:9);
}
return 0;
}

47
HDOJ/1598_autoAC.cpp Normal file
View File

@ -0,0 +1,47 @@
#include <iostream>
#include <algorithm>
using namespace std;
static const int max_speed = 1000000;
int m,n,root[201],ct;
struct speed{
int b,e,s;
friend bool operator < (const speed& lf, const speed& rt){
return lf.s < rt.s;
}
}speeds[2048];
int find(int t){
return root[t] == t ? t : root[t] = find(root[t]);
}
inline void merge(int u, int v){
root[find(u)] = find(v);
}
int foo(int begin, int end){
int ret = max_speed+1;
for (int i = 0; i < m; ++i){
for (int k = 1; k <= n; ++k) root[k] = k;
for (int j = i; j < m; ++j){
merge(speeds[j].b, speeds[j].e);
if (find(begin) != find(end)) continue;
int delta = speeds[j].s - speeds[i].s;
if (delta < ret) ret = delta;
}
}
return ret <= max_ ? ret : -1;
}
int main(){
ios::sync_with_stdio(false);
while (cin >> n >> m){
int q,b,e;
for (int i = 0; i < m; ++i){
speed& s = speeds[i];
cin >> s.b >> s.e >> s.s;
}
sort(speeds,speeds+m);
cin >> q;
while (q--){
cin >> b >> e;
cout << foo(b,e) << endl;
}
}
return 0;
}

81
HDOJ/1599_autoAC.cpp Normal file
View File

@ -0,0 +1,81 @@
#include<stdio.h>
#include<string.h>
int map[300][300];
int dist[300][300];
int n,m;
int ans;
void floyd()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
dist[i][j]=map[i][j];
}
}
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i!=j&&j!=k&&k!=i&&dist[i][j]!=2000000000&&map[j][k]!=2000000000&&map[k][i]!=2000000000&&ans>dist[i][j]+map[j][k]+map[k][i])
{
ans=dist[i][j]+map[j][k]+map[k][i];
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(dist[i][k]!=2000000000&&dist[k][j]!=2000000000&&dist[i][j]>dist[i][k]+dist[k][j])
{
dist[i][j]=dist[i][k]+dist[k][j];
}
}
}
}
return;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if(i!=j)
{
map[i][j]=2000000000;
}
else
{
map[i][j]=0;
}
}
}
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a!=b&&map[a][b]>c)
{
map[a][b]=c;
map[b][a]=c;
}
}
ans=2000000000;
floyd();
if(ans==2000000000)
{
printf("It's impossible.\n");
}
else
{
printf("%d\n",ans);
}
}
return 0;
}