Merge pull request #32 from KiritoTRw/master

Powered By HC TECH : AutoACer Engine
This commit is contained in:
Kirigaya Kazuto 2016-08-14 09:18:44 +08:00 committed by GitHub
commit 3ea1771c15
77 changed files with 4924 additions and 0 deletions

36
HDOJ/1400_autoAC.cpp Normal file
View File

@ -0,0 +1,36 @@
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n,m;
long long add;
long long dp[2][1<<12];
void dfs(int i,int s,int cur)
{
if(cur==m) {dp[i][s]+=add;return;}
dfs(i,s,cur+1);
if(cur<m-1&&!(s&1<<cur)&&!(s&1<<(cur+1)))
dfs(i,s|1<<cur|1<<(cur+1),cur+2);
}
int main()
{
while(scanf("%d%d",&n,&m),n+m)
{
if(n*m%2) {printf("0\n");continue;}
int rt=(1<<m)-1;
add=1;
memset(dp,0,sizeof(dp));
dfs(0,0,0);
for(int i=1;i<n;i++)
{
memset(dp[i%2],0,sizeof(dp[1]));
for(int j=0;j<=rt;j++) if(dp[(i-1)%2][j])
{
add=dp[(i-1)%2][j];
dfs(i%2,~j&rt,0);
}
}
printf("%I64d\n",dp[(n-1)%2][rt]);
}
return 0;
}

165
HDOJ/1401_autoAC.cpp Normal file
View File

@ -0,0 +1,165 @@
#include<iostream>
#include<algorithm>
#include<queue>
#include<bitset>
#define N 8
using namespace std;
bitset<20000010> vis1,vis2;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
bool map[N][N],flag;
struct point
{
int x,y;
};
struct node
{
point p[4];
};
node start,end1;
queue<node> Q1,Q2;
bool cmp(point a,point b)
{
if(a.x!=b.x)
return a.x<b.x;
return a.y<b.y;
}
void get_map(point p[])
{
memset(map,0,sizeof(map));
for(int i=0;i<4;i++)
map[p[i].x][p[i].y]=1;
}
int get_hash(point p[])
{
sort(p,p+4,cmp);
int a=0,j=7;
for(int i=0;i<4;i++)
{
a|=(p[i].x<<(j*3));
a|=(p[i].y<<(j-1)*3);
j-=2;
}
return a;
}
void BFS1()
{
int a;
int size=Q1.size();
while(size--)
{
node q=Q1.front(),temp;
Q1.pop();
get_map(q.p);
for(int i=0;i<4;i++)
for(int k=0;k<4;k++)
{
temp=q;
int x=q.p[i].x+dir[k][0];
int y=q.p[i].y+dir[k][1];
if(x<0 ||x>=N || y<0 || y>=N)
continue;
temp.p[i].x=x;
temp.p[i].y=y;
if(map[x][y])
{
int x1=x+dir[k][0];
int y1=y+dir[k][1];
if(x1<0 || x1>=N || y1<0 || y1>=N || map[x1][y1])
continue;
temp.p[i].x=x1;temp.p[i].y=y1;
}
a=get_hash(temp.p);
if(vis1[a]) continue;
if(vis2[a]) {
flag=true;
return ;
}
vis1[a]=true;
Q1.push(temp);
}
}
}
void BFS2()
{
int a;
int size=Q2.size();
while(size--)
{
node q=Q2.front(),temp;
Q2.pop();
get_map(q.p);
for(int i=0;i<4;i++)
for(int k=0;k<4;k++)
{
temp=q;
int x=temp.p[i].x+dir[k][0];
int y=temp.p[i].y+dir[k][1];
if(x<0 ||x>=N || y<0 || y>=N)
continue;
temp.p[i].x=x;
temp.p[i].y=y;
if(map[x][y])
{
int x1=x+dir[k][0];
int y1=y+dir[k][1];
if(x1<0 || x1>=N || y1<0 || y1>=N || map[x1][y1])
continue;
temp.p[i].x=x1;temp.p[i].y=y1;
}
a=get_hash(temp.p);
if(vis2[a]) continue;
if(vis1[a]) {
flag=true; return ;
}
vis2[a]=true;
Q2.push(temp);
}
}
}
int main()
{
while(scanf("%d %d",&start.p[0].x,&start.p[0].y)==2)
{
start.p[0].x--;
start.p[0].y--;
for(int i=1;i<4;i++)
{
scanf("%d %d",&start.p[i].x,&start.p[i].y);
start.p[i].x--;start.p[i].y--;
}
for(int i=0;i<4;i++)
{
scanf("%d %d",&end1.p[i].x,&end1.p[i].y);
end1.p[i].x--;
end1.p[i].y--;
}
while(!Q1.empty())
Q1.pop();
while(!Q2.empty())
Q2.pop();
vis1.reset();
vis2.reset();
int a;
a=get_hash(start.p);
vis1[a]=true;
int b=get_hash(end1.p);
if(a==b) {
printf("YES\n");
continue;
}
vis2[b]=true;
Q1.push(start);Q2.push(end1);
int step=8;
flag=false;
while(step--)
{
if(Q1.size()<=Q2.size())
BFS1();
else BFS2();
if(flag) break;
}
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}

88
HDOJ/1402_autoAC.cpp Normal file
View File

@ -0,0 +1,88 @@
#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<complex>
#define N 50010
using namespace std;
const double pi=acos(-1);
const complex<double> I(0,1);
const double eps=1e-6;
char aa[N],bb[N];
int ans[4*N];
complex<double> a[4*N],b[4*N];
int n;
void initial()
{
int lenA=strlen(aa),lenB=strlen(bb);
n=max(lenA,lenB);
double t=log2(n);
int tt=int(t);
if(t-tt>0)tt++;
n=1<<(tt+1);
int i;
for(i=0; i<lenA; i++)a[i]=aa[lenA-1-i]-'0';
while(i<n)a[i++]=0;
for(i=0; i<lenB; i++)b[i]=bb[lenB-1-i]-'0';
while(i<n)b[i++]=0;
}
void bitReverse(complex<double> * a)
{
for(int i=1; i<n-1; i++)
{
int j=0;
for(int k=1,tmp=i; k<n; j=((j<<1)|(tmp&1)),k<<=1,tmp>>=1);
if(j>i)swap(a[i],a[j]);
}
}
void iterative_FFT(complex<double> *a,int sig)
{
bitReverse(a);
for(int m=2; m<=n; m<<=1)
{
int mh =m>>1;
for(int i=0; i<mh; i++)
{
complex<double> wi=exp(i*sig*pi/mh*I);
for(int j=i; j<n; j+=m)
{
int k=j+mh;
complex<double> u=a[j],t=wi*a[k];;
a[j]=u+t;
a[k]=u-t;
}
}
}
if(sig==-1)for(int i=0; i<n; i++)a[i]/=n;
}
void convolution()
{
iterative_FFT(a,1);
iterative_FFT(b,1);
for(int i=0; i<n; i++)a[i]*=b[i];
iterative_FFT(a,-1);
}
void output()
{
int k=0;
ans[0]=0;
for(int i=0; i<n; i++)
{
int tmp=a[i].real()+eps;
ans[i]+=tmp;
if(ans[i])k=i,ans[i+1]=ans[i]/10,ans[i]%=10;
else ans[i+1]=0;
}
while(k>=0)cout<<ans[k--];
cout<<endl;
}
int main()
{
while(~scanf("%s%s",aa,bb))
{
initial();
convolution();
output();
}
return 0;
}

41
HDOJ/1403_autoAC.cpp Normal file
View File

@ -0,0 +1,41 @@
#include<iostream>
#include<string.h>
using namespace std;
const int Max = 100000 + 10;
char a[Max];
char b[Max];
struct Node
{
char *suf;
bool flag;
};
Node suffix[Max * 2];
int com(const void * a, const void *b)
{
return strcmp(((Node *)a)->suf, ((Node *)b)->suf);
}
int comLen(char *a, char *b)
{
int len = 0;
while(*a++ == *b++) len ++;
return len;
}
int main()
{
int max;
while(scanf("%s %s", a, b) != EOF)
{
int lenA = strlen(a);
int lenB = strlen(b);
for(int i=0; i<lenA; i++) suffix[i].suf = a+i, suffix[i].flag = false;
for(int i=0; i<lenB; i++) suffix[i+lenA].suf = b+i, suffix[i+lenA].flag = true;
qsort(suffix, lenA+lenB, sizeof(Node), com);
max = 0;
int temp;
for(int i=0; i<lenA+lenB-1; i++)
if((suffix[i].flag != suffix[i+1].flag) && (temp = comLen(suffix[i].suf, suffix[i+1].suf)) > max)
max = temp;
printf("%d\n", max);
}
return 0;
}

78
HDOJ/1404_autoAC.cpp Normal file
View File

@ -0,0 +1,78 @@
#include<stdio.h>
#include<math.h>
#include<iostream>
#include<string.h>
using namespace std;
const int MAXN=1000000;
int sg[MAXN];
int get_length(int n)
{
if(n/100000) return 6;
if(n/10000) return 5;
if(n/1000) return 4;
if(n/100) return 3;
if(n/10) return 2;
return 1;
}
void _extend(int n)
{
int len=get_length(n);
int i;
for(i=len;i>=1;i--)
{
int m=n;
int base=1;
for(int j=1;j<i;j++) base*=10;
int tmp=(m%(base*10))/base;
for(int j=tmp;j<9;j++)
{
m+=base;
sg[m]=1;
}
}
if(len!=6)
{
int m=n;
int base=1;
for(int i=len;i<6;i++)
{
m*=10;
for(int b=0;b<base;b++)
sg[m+b]=1;
base*=10;
}
}
}
void fun()
{
memset(sg,0,sizeof(sg));
sg[0]=1;
for(int i=1;i<MAXN;i++)
{
if(!sg[i]) _extend(i);
}
}
int main()
{
char str[8];
int n;
fun();
while(scanf("%s",&str)!=EOF)
{
if(str[0]=='0')
{
printf("Yes\n");
continue;
}
int len=strlen(str);
n=0;
for(int i=0;i<len;i++)
{
n*=10;
n+=str[i]-'0';
}
if(sg[n]) printf("Yes\n");
else printf("No\n");
}
return 0;
}

30
HDOJ/1405_autoAC.cpp Normal file
View File

@ -0,0 +1,30 @@
#include<iostream>
using namespace std;
int main()
{
int n,num,p=0,i,m,j=1;
while(cin>>n&&n>1)
{
if(p)
cout<<endl;
cout<<"Case "<<j++<<"."<<endl;
num=0;m=n;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
num++;
n=n/i;
i=i-1;
}
else if(num!=0)
{
cout<<i<<" "<<num<<" ";
num=0;
}
}
cout<<endl;
p=1;
}
return 0;
}

35
HDOJ/1406_autoAC.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <stdio.h>
#include <string.h>
int main()
{
int a[10004],i,j,t;
memset(a,0,sizeof(a));
for (i=1;i<10004/2;i++)
{
for (j=i*2;j<=10000;j=j+i)
{
a[j]+=i;
}
}
scanf("%d",&t);
while (t--)
{
int num1,num2,count=0;
scanf("%d%d",&num1,&num2);
if (num1>num2)
{
num1^=num2;
num2^=num1;
num1^=num2;
}
for (i=num1;i<=num2;i++)
{
if (a[i]==i)
{
count++;
}
}
printf("%d\n",count);
}
return 0;
}

17
HDOJ/1407_autoAC.cpp Normal file
View File

@ -0,0 +1,17 @@
#include<stdio.h>
#include<math.h>
int main(){
int num=0;
int x,y,z;
while(scanf("%d",&num)!=EOF){
for(x=1;x<=sqrt(num*1.0/3);x++)
for(y=x;y<=sqrt((num*1.0-x*x)/2);y++){
z=sqrt(num-x*x-y*y);
if(z*z==(num-x*x-y*y)){
printf("%d %d %d\n",x,y,z);
goto exit;
}
}
exit:;
}
}

16
HDOJ/1408_autoAC.cpp Normal file
View File

@ -0,0 +1,16 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
int main()
{
int i, j, k, N;
double vl, d;
while (scanf("%lf%lf", &vl, &d) == 2) {
double dd = (int) ceil(vl / d);
int n = (int) (sqrt(2 * dd + 0.25) - 0.5001);
printf("%d\n", (int) dd + n);
}
return 0;
}

98
HDOJ/1409_autoAC.cpp Normal file
View File

@ -0,0 +1,98 @@
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
char s[200];
int pz(int x,int y)
{
int ys=0,i;
for(i=x;i<=y;i++)
{
if(i==x)
{
if(s[i]=='+'||s[i]=='-')
continue;
else if(s[i]>='0'&&s[i]<='9')
ys++;
else
return 0;
}
else
{
if(s[i]>='0'&&s[i]<='9')
ys++;
else
{
return 0;
}
}
}
if(ys>0)
return 1;
else
return 0;
}
int pd(int x,int y)
{
int ys=0,i;
for(i=x;i<=y;i++)
{
if(i==x)
{
if((s[i]=='+'||s[i]=='-')||(s[i]>='0'&&s[i]<='9'))
continue;
else if(s[i]=='.')
ys++;
else
return 0;
}
else
{
if(s[i]>='0'&&s[i]<='9')
continue;
else if(s[i]=='.')
ys++;
else
return 0;
}
}
if(ys==1)
return 1;
else
return 0;
}
int main()
{
int t,i,ye,e,T;
scanf("%d",&T);
getchar();
while(T--)
{
ye=0;
gets(s);
t=strlen(s);
for(i=0;i<t;i++)
{
if(s[i]=='E')
{
ye=1;
e=i;
}
}
if(ye==1)
{
if((pz(0,e-1)||pd(0,e-1))&&pz(e+1,t-1))
printf("YES\n");
else
printf("NO\n");
}
else
{
if(pz(0,t-1)||pd(0,t-1))
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}

18
HDOJ/1410_autoAC.cpp Normal file
View File

@ -0,0 +1,18 @@
#include<stdio.h>
#include<cmath>
int main()
{
int HP1, HP2, AP1, AP2, N1, N2;
while(scanf("%d%d%d%d", &HP1, &HP2, &AP1, &AP2) != EOF){
double ans = 0, tmp = 0;
N1 = (HP2 - 1) / AP1 + 1;
N2 = (HP1 - 1) / AP2 + 1;
ans = pow(0.5, N1);
for(int i = 1; i < N2; ++i){
tmp += log10(N1 - 1.0 + i) - log10(i * 1.0);
ans += pow(10, tmp + (N1 + i) * log10(0.5));
}
printf("%.4lf\n", ans * 100);
}
return 0;
}

16
HDOJ/1411_autoAC.cpp Normal file
View File

@ -0,0 +1,16 @@
#include<stdio.h>
#include<math.h>
#include<iostream>
using namespace std;
int main()
{
double a,b,c,m,n,l,v;
while(scanf("%lf%lf%lf%lf%lf%lf", &a, &c, &b, &n, &l, &m)!=EOF)
{
v=(double)sqrt((4.0*a*a*b*b*c*c-a*a*(b*b+c*c-m*m)*(b*b+c*c-m*m)-
b*b*(c*c+a*a-n*n)*(c*c+a*a-n*n)-c*c*(a*a+b*b-l*l)*
(a*a+b*b-l*l)+(a*a+b*b-l*l)*(b*b+c*c-m*m)*(c*c+a*a-n*n)))/12.0;
printf("%.4lf\n", v);
}
return 0;
}

22
HDOJ/1412_autoAC.cpp Normal file
View File

@ -0,0 +1,22 @@
#include <stdio.h>
#include<algorithm>
int p[20005];
using namespace std;
int main()
{
int a,b,i;
while(scanf("%d%d",&a,&b)!=EOF)
{
for(i=0;i<a+b;i++)
scanf("%d",&p[i]);
sort(p,p+(a+b));
printf("%d",p[0]);
for(i=1;i<a+b;i++)
{
if(p[i]!=p[i-1])
printf(" %d",p[i]);
}
printf("\n");
}
return 0;
}

97
HDOJ/1413_autoAC.cpp Normal file
View File

@ -0,0 +1,97 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
const int FILEE = 0, DIR = 1;
char createRes[2][30] = { "file already exist", "directory already exist" };
char delRes[2][30] = { "no such file", "can not delete the directory" };
struct F {
int head, next, type, fa;
string name;
F() {
}
F(int h, int n, int t, int f, string s) :
head(h), next(n), type(t), fa(f), name(s) {
}
};
int tot, cur;
vector<F> V;
void cd(const char *com) {
int i = 1;
if (!strcmp(com, "\\")) {
cur = 0;
} else if (!strcmp(com, "..")) {
cur = V[cur].fa;
} else {
for (i = V[cur].head; i > 0; i = V[i].next) {
if (V[i].type == DIR && !strcmp(V[i].name.c_str(), com)) {
cur = i;
break;
}
}
}
puts(i ? "success" : "no such directory");
}
void create(const char *com, int type) {
int i = 1;
if (!strcmp(com, "\\")) {
} else if (!strcmp(com, "..")) {
} else {
for (i = V[cur].head; i > 0; i = V[i].next) {
if (V[i].type == type && !strcmp(V[i].name.c_str(), com)) {
break;
}
}
if (!i) {
V.push_back(F(0, V[cur].head, type, cur, com));
V[cur].head = tot++;
}
}
puts(i ? createRes[type] : "success");
}
void del(const char *com, int type) {
int i = 0;
if (!strcmp(com, "\\")) {
} else if (!strcmp(com, "..")) {
} else {
int *p = &V[cur].head;
for (i = V[cur].head; i > 0; i = V[i].next) {
if (V[i].type == type && !strcmp(V[i].name.c_str(), com)) {
if (type == DIR && V[i].head)
continue;
*p = V[i].next;
break;
}
p = &V[i].next;
}
}
puts(i ? "success" : delRes[type]);
}
int main() {
char op[9], com[30];
V.push_back(F(0, 0, DIR, 0, "\\"));
++tot;
while (scanf("%s%s", op, com) != EOF) {
switch (op[0]) {
case 'C':
if (op[1] == 'D') {
cd(com);
} else {
create(com, FILEE);
}
break;
case 'M':
create(com, DIR);
break;
case 'D':
del(com, FILEE);
break;
case 'R':
del(com, DIR);
break;
}
}
return 0;
}

123
HDOJ/1415_autoAC.cpp Normal file
View File

@ -0,0 +1,123 @@
#include <cstdio>
#include <cstring>
#include <queue>
char op[7][10]= {"","fill A","fill B","empty A","empty B","pour A B","pour B A"};
struct state
{
int a,b;
state() {};
state(int _a,int _b)
{
a=_a;
b=_b;
}
};
struct p
{
int x,y;
p() {};
p(int _y,int _x)
{
x=_x;
y=_y;
}
} mp[1001][1001];
int A,B,n,vis[1001][1001];
void print(int y,int x)
{
if(vis[y][x]!=-1)
print(mp[y][x].y,mp[y][x].x);
if(y||x){
puts(op[vis[y][x]]);
}
}
void bfs()
{
std::queue<state>q;
memset(vis,0,sizeof(vis));
q.push(state(0,0));
vis[0][0]=-1;
mp[0][0]=p(0,0);
while(!q.empty())
{
state cur=q.front();
q.pop();
int x=cur.b,y=cur.a;
if(x==n)
{
print(y,x);
return ;
}
int yy=A;
if(y<A&&!vis[yy][x])
{
vis[yy][x]=1;
q.push(state(yy,x));
mp[yy][x]=p(y,x);
}
int xx=B;
if(x<B&&!vis[y][xx])
{
vis[y][xx]=2;
q.push(state(y,xx));
mp[y][xx]=p(y,x);
}
if(y>0)
{
yy=0;
if(!vis[yy][x])
{
vis[yy][x]=3;
q.push(state(yy,x));
mp[yy][x]=p(y,x);
}
}
if(x>0)
{
xx=0;
if(!vis[y][xx])
{
vis[y][xx]=4;
q.push(state(y,xx));
mp[y][xx]=p(y,x);
}
}
if(y>0)
{
xx=x+y;yy=0;
if(xx>B){
yy=xx-B;
xx=B;
}
if(!vis[yy][xx])
{
vis[yy][xx]=5;
q.push(state(yy,xx));
mp[yy][xx]=p(y,x);
}
}
if(x>0)
{
yy=y+x;xx=0;
if(yy>A){
xx=yy-A;
yy=A;
}
if(!vis[yy][xx])
{
vis[yy][xx]=6;
q.push(state(yy,xx));
mp[yy][xx]=p(y,x);
}
}
}
}
int main(void)
{
while(~scanf("%d%d%d",&A,&B,&n))
{
bfs();
printf("success\n");
}
return 0;
}

45
HDOJ/1416_autoAC.cpp Normal file
View File

@ -0,0 +1,45 @@
#include <cstdio>
#include <cstring>
#define MAX(x,y) x>y?x:y
#define MIN(x,y) x<y?x:y
int max, min, flag, ff;
bool f[101];
int dfs(int t)
{
if(t==1)
{
if(flag==0)
{
ff = 1; flag = 1;
if(dfs(max)) return 1;
else {flag = 0; return 0;}
}
return 1;
}
int len = 100<t?100:t;
for(int i = 2; i <= len; i++)
if(t%i==0&&f[i]==0)
{
f[i] = 1; if(dfs(t/i)) return 1;
f[i] = 0;
}
return 0;
}
int main ()
{
int a, b;
while(~scanf("%d%d",&a,&b))
{
flag = 0;ff = 0;
int ans;
max = MAX(a,b);
min = MIN(a,b);
memset(f,0,sizeof(f));
int tt = dfs(min);
if(ff==0) ans = max;
else if(ff==1&&tt==1) ans = max;
else ans = min;
printf("%d\n",ans);
}
return 0;
}

54
HDOJ/1417_autoAC.cpp Normal file
View File

@ -0,0 +1,54 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct data
{
int x1,y1,z1;
int x2,y2,z2;
data(){}
data(int _x,int _y,int _z,int _d)
{
x1=_x;y1=_y;z1=_z;
x2=_x+_d;y2=_y+_d;z2=_z+_d;
}
data(int _x1,int _y1,int _z1,int _x2,int _y2,int _z2)
{
x1=_x1;y1=_y1;z1=_z1;
x2=_x2;y2=_y2;z2=_z2;
}
bool ok()
{
return x1<x2&&y1<y2&&z1<z2;
}
int volume()
{
if(ok())
return (x2-x1)*(y2-y1)*(z2-z1);
else
return 0;
}
};
data inter(data a,data b)
{
if(a.ok())
return data(max(a.x1,b.x1),max(a.y1,b.y1),max(a.z1,b.z1),min(a.x2,b.x2),min(a.y2,b.y2),min(a.z2,b.z2));
else
return data(0,0,0,-1);
}
int main()
{
int n,x,y,z,d;
while(scanf("%d",&n),n)
{
scanf("%d%d%d%d",&x,&y,&z,&d);
data ans(x,y,z,d);
for(int i=1;i<n;i++)
{
scanf("%d%d%d%d",&x,&y,&z,&d);
ans=inter(ans,data(x,y,z,d));
}
printf("%d\n",ans.volume());
}
return 0;
}

9
HDOJ/1418_autoAC.cpp Normal file
View File

@ -0,0 +1,9 @@
#include<iostream>
#include<cstdio>
int main()
{
__int64 n,m,r;
while(scanf("%I64d%I64d",&n,&r)&&(n||r))
printf("%I64d\n",r+n-2);
return 0;
}

26
HDOJ/1420_autoAC.cpp Normal file
View File

@ -0,0 +1,26 @@
#include<stdio.h>
int main()
{
__int64 a,b,c,i,n,f,d,e;
scanf("%I64d",&n);
while(n--)
{
scanf("%I64d%I64d%I64d",&a,&b,&c);
f=0;
e=a;
for(i=1;i<=b;i++)
{
d=a%c;
a=e*d;
if(a==0)
{
f=1;
printf("0\n");
break;
}
}
if(!f)
printf("%I64d\n",d);
}
return 0;
}

40
HDOJ/1421_autoAC.cpp Normal file
View File

@ -0,0 +1,40 @@
#include<stdio.h>
#include<algorithm>
#define min(a,b) a<b?a:b
int n,k;
int thing[2001];
int select[2001][1001];
int cmp(const void *a,const void *b)
{
return *((int*)a)-*((int*)b);
}
int dp()
{
qsort(thing+1,n,sizeof(int),cmp);
select[0][0]=0;
for(int j=1;j<=n;j++)
{
select[j][0]=0;
for(int i=1;i<=j/2;i++)
{
if(j-1>=2*i)
{
select[j][i]=min(select[j-1][i],select[j-2][i-1]+(thing[j]-thing[j-1])*(thing[j]-thing[j-1]));
}
else
select[j][i]=select[j-2][i-1]+(thing[j]-thing[j-1])*(thing[j]-thing[j-1]);
}
}
printf("%d\n",select[n][k]);
return 0;
}
int main()
{
while(scanf("%d%d",&n,&k)!=EOF&&n>=2*k&&n<2000&&k>=1)
{
for(int i=1;i<=n;i++)
scanf("%d",&thing[i]);
dp();
}
return 0;
}

52
HDOJ/1422_autoAC.cpp Normal file
View File

@ -0,0 +1,52 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
using namespace std;
int num[100005];
int main()
{
int n;
while(scanf("%d",&n)==1)
{
for(int i=1;i<=n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
num[i]=a-b;
}
int i=1,j=n;
int maxn=0;
while(i<=n)
{
int sum=0;
int x=i,y=j;
int flag=0;
while(1)
{
sum+=num[x];
if(sum<0)
break;
x++;
flag++;
if(x-1==y)
break;
if(x>n)
{
x=1;
}
}
i=i+flag+1;
maxn=max(maxn,flag);
j+=flag+1;
if(j>n)
j-=n;
}
printf("%d\n",maxn);
}
}

36
HDOJ/1423_autoAC.cpp Normal file
View File

@ -0,0 +1,36 @@
#include<iostream>
#include<string.h>
#include<stdio.h>
#define maxn 509
using namespace std;
int a[maxn],b[maxn],dp[maxn][maxn];
int main()
{
int t,i,j,k,l,n,m;
scanf("%d",&t);
while(t--)
{
int ans=-1;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",a+i);
scanf("%d",&m);
for(i=1;i<=m;i++)
scanf("%d",b+i);
for(i=1;i<=n;i++)
{
int max=0;
for(j=1;j<=m;j++)
{
if(max<dp[i-1][j]&&a[i]>b[j])max=dp[i-1][j];
if(a[i]!=b[j])dp[i][j]=dp[i-1][j];
else dp[i][j]=max+1;
if(dp[i][j]>ans)
ans=dp[i][j];
}
}
printf("%d\n",ans);
if(t)
puts("");
}
}

25
HDOJ/1425_autoAC.cpp Normal file
View File

@ -0,0 +1,25 @@
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 1000000
using namespace std;
int a[N];
int main()
{
int n,m,i,flag=0;
while(scanf("%d%d",&n,&m)!=EOF){
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
flag=n-1;
printf("%d",a[flag]);
flag-=1;
for(i=m;i>1;i--){
printf(" %d",a[flag]);
flag--;
}
cout<<endl;
}
return 0;
}

102
HDOJ/1426_autoAC.cpp Normal file
View File

@ -0,0 +1,102 @@
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char mmp[20];
int mp[10][10];
int vis1[10][10],vis2[10][10],vis3[4][4][10];
int OK=0;
void dfs(int x,int y)
{
if(OK) return;
if(x>9&&!OK)
{
for(int i=1;i<=9;i++)
{
for(int j=1;j<=9;j++)
{
if(j!=1) printf(" ");
printf("%d",mp[i][j]);
}
putchar(10);
}
OK=1;
return ;
}
int tx,ty;
if(mp[x][y]!=0)
{
tx=x; ty=y+1;
if(ty==10)
{
ty=1;
tx++;
}
dfs(tx,ty);
}
else if(mp[x][y]==0)
{
for(int i=1;i<=9;i++)
{
if(vis1[x][i]==0&&vis2[y][i]==0&&vis3[(x-1)/3+1][(y-1)/3+1][i]==0)
{
vis1[x][i]=1;
vis2[y][i]=1;
vis3[(x-1)/3+1][(y-1)/3+1][i]=1;
mp[x][y]=i;
tx=x; ty=y+1;
if(ty==10)
{
ty=1;
tx++;
}
dfs(tx,ty);
mp[x][y]=0;
vis1[x][i]=0;
vis2[y][i]=0;
vis3[(x-1)/3+1][(y-1)/3+1][i]=0;
}
}
}
}
int main()
{
int cnt=1;int first=1;
while(gets(mmp))
{
for(int i=0;i<=16;i+=2)
{
if(mmp[i]!='?')
{
mp[cnt][(i+2)/2]=mmp[i]-'0';
vis1[cnt][mp[cnt][(i+2)/2]]=1;
vis2[(i+2)/2][mp[cnt][(i+2)/2]]=1;
vis3[(cnt-1)/3+1][((i+2)/2-1)/3+1][mp[cnt][(i+2)/2]]=1;
}
else
mp[cnt][(i+2)/2]=0;
}
cnt++;
if(cnt==10)
{
cnt=1;
OK=0;
if(first)
{
first=0;
}
else
{
putchar(10);
}
dfs(1,1);
cnt=1;
memset(vis1,0,sizeof(vis1));
memset(vis2,0,sizeof(vis2));
memset(vis3,0,sizeof(vis3));
}
if(cnt==1)
getchar();
}
return 0;
}

60
HDOJ/1427_autoAC.cpp Normal file
View File

@ -0,0 +1,60 @@
#include<iostream>
#include<algorithm>
using namespace std;
int res[4];
void dfs(int sum, int cur, int temp);
bool flag = 0;
int main()
{
char s[3];
int i,j,k;
while(1)
{
for(i = 0;i < 4;i ++)
{
if(scanf("%s",s) == EOF) return 0;
if(s[0] == 'A') res[i] = 1;
else if(s[0] == 'J') res[i] = 11;
else if(s[0] == 'Q') res[i] = 12;
else if(s[0] == 'K') res[i] = 13;
else if(s[0] == '1' && s[1] == '0') res[i] = 10;
else res[i] = s[0] - '0';
}
sort(res, res+4);
flag = 0;
do
{
dfs(res[0], 1, res[1]);
}while(next_permutation(res, res+4)&&!flag);
if(flag) printf("Yes\n");
else printf("No\n");
}
return 0;
}
void dfs(int sum, int cur, int temp)
{
if(flag)
return;
if(cur==3)
{
if(sum+temp==24)
flag=1;
if(sum-temp==24)
flag=1;
if(sum*temp==24)
flag=1;
if(temp!=0&&sum%temp==0&&sum/temp==24)
flag=1;
return;
}
dfs(sum+temp, cur+1, res[cur+1]);
dfs(sum-temp, cur+1, res[cur+1]);
dfs(sum*temp, cur+1, res[cur+1]);
if(temp!=0&&sum%temp==0)
dfs(sum/temp, cur+1, res[cur+1]);
dfs(sum, cur+1, temp+res[cur+1]);
dfs(sum, cur+1, temp-res[cur+1]);
dfs(sum, cur+1, temp*res[cur+1]);
if(res[cur+1]!=0&&temp%res[cur+1]==0)
dfs(sum, cur+1, temp/res[cur+1]);
}

79
HDOJ/1428_autoAC.cpp Normal file
View File

@ -0,0 +1,79 @@
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#define N 51
struct node{
int x,y;
node(int a,int b)
{
x=a; y=b;
}
node(){};
};
int key[4][2]={0,1,0,-1,1,0,-1,0};
int n,map[N][N],d[N][N];
long long f[N][N];
void find()
{
int i;
node s;
queue<node> Q;
memset(d,1,sizeof(d));
d[n][n]=map[n][n];
Q.push(node(n,n));
while (!Q.empty())
{
s=Q.front();
Q.pop();
for (i=0;i<4;++i)
{
int x,y;
x=s.x+key[i][0]; y=s.y+key[i][1];
if (x>0&&x<=n && y>0&&y<=n)
{
if (d[x][y]>d[s.x][s.y]+map[x][y])
{
d[x][y]=d[s.x][s.y]+map[x][y];
Q.push(node(x,y));
}
}
}
}
}
long long dp(int x,int y)
{
if (f[x][y]!=-1) return f[x][y];
int i;
long long t=0;
for (i=0;i<4;++i)
{
int x1,y1;
x1=x+key[i][0]; y1=y+key[i][1];
if (x1>0&&x1<=n && y1>0&&y1<=n)
if (d[x1][y1]<d[x][y])
{
t+=dp(x1,y1);
}
}
return f[x][y]=t;
}
int main()
{
while (~scanf("%d",&n))
{
int i,j;
for (i=1;i<=n;++i)
for (j=1;j<=n;++j)
scanf("%d",&map[i][j]);
find();
memset(f,-1,sizeof(f));
f[n][n]=1;
dp(1,1);
printf("%I64d\n",f[1][1]);
}
return 0;
}

72
HDOJ/1429_autoAC.cpp Normal file
View File

@ -0,0 +1,72 @@
#include<cstdio>
#include<cstring>
#include<cctype>
#include<queue>
#define M 21
#define KEY 10
using namespace std;
typedef struct tP{
int x,y,t,key;
tP(){}
tP(int x,int y,int t,int key):x(x),y(y),t(t),key(key){}
}P;
char mat[M][M];
bool mark[1<<KEY][M][M];
int n,m,T;
queue<P>q;
int dir[][2]={{0,1},{0,-1},{1,0},{-1,0}};
int K(char c)
{
return c-(isupper(c)?'A':'a');
}
int Wall(int x,int y)
{
if(x<0||x>=n||y<0||y>=m) return true;
if(mat[x][y]=='*') return true;
return false;
}
int Bfs(int cx,int cy)
{
P cur;
int x,y,t,key,i;
while(!q.empty()) q.pop();
memset(mark,false,sizeof(mark));
mark[0][cx][cy]=true;
q.push(tP(cx,cy,0,0));
while(!q.empty()){
cur=q.front();q.pop();
if(cur.t>=T) break;//濡娣卞害澶тT锛
for(i=0;i<4;i++){
x=cur.x+dir[i][0];
y=cur.y+dir[i][1];
t=cur.t+1;key=cur.key;
if(Wall(x,y)||mark[key][x][y]) continue;
if(isupper(mat[x][y])&&!(key&(1<<K(mat[x][y])))) continue;
if(islower(mat[x][y])) key=key|(1<<K(mat[x][y]));
if(mat[x][y]=='^') return t;
if(!mark[key][x][y]){
mark[key][x][y]=true;
q.push(tP(x,y,t,key));
}
}
}
return -1;
}
int main()
{
int i,j,x,y;
while(scanf("%d%d%d",&n,&m,&T)!=EOF){
T-=1;
for(i=0;i<n;i++){
scanf("%s",mat[i]);
for(j=0;j<m;j++){
if(mat[i][j]=='@'){
mat[i][j]='.';
x=i,y=j;
}
}
}
printf("%d\n",Bfs(x,y));
}
return 0;
}

93
HDOJ/1430_autoAC.cpp Normal file
View File

@ -0,0 +1,93 @@
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <iostream>
const int M = 50000;
using namespace std;
struct node{
string s, path;
int val;
};
const int f[] = {1, 1, 2, 6, 24, 120, 720, 5040, 5040*8};
string ans[M];
bool vis[M];
int cal(string s){
int sum = 0;
for(int i = 0; i < 8; ++ i){
int cnt = 0;
for(int j = i+1; j < 8; ++ j)
if(s[j] < s[i]) ++cnt;
sum += cnt*f[7-i];
}
return sum;
}
string tran(string s, int num){
//string res = "";
int i;
if(num == 0){
for(i = 0; i < 4; ++ i){
swap(s[i], s[i+4]);
}
}
else if(num == 1){
char t= s[3];
for(i = 2; i >= 0; -- i) s[i+1] = s[i];
s[0] = t;
t = s[7];
for(i = 6; i >= 4; -- i) s[i+1] = s[i];
s[4] = t;
}
else{
char t = s[1];
s[1] = s[5]; s[5] = s[6]; s[6] = s[2];
s[2] = t;
}
return s;
}
void bfs(string st){
memset(vis, 0, sizeof(vis));
queue<node > q;
node t;
t.s = st;
t.val = cal(t.s);
t.path = "";
ans[t.val] = t.path;
vis[t.val] = 1;
q.push(t);
while(!q.empty()){
node temp = q.front(); q.pop();
for(int i = 0; i < 3; ++ i){
node cur = temp;
cur.path += (i+'A');
string a = tran(cur.s, i);
int k = cal(a);
if(!vis[k]){
cur.s = a;
cur.val = k;
ans[cur.val] = cur.path;
vis[k] = 1;
q.push(cur);
}
}
}
}
int main(){
string st = "12345678";
bfs(st);
string en, ss;
int i;
while(cin >> ss >> en){
swap(ss[4], ss[7]);
swap(ss[5], ss[6]);
swap(en[4], en[7]);
swap(en[5], en[6]);
string mm(8, 'a');
for(i = 0; i < 8; ++ i) mm[ss[i]-'0'] = i+'1';
string de;
for(i = 0; i < 8; ++ i) de += mm[en[i]-'0'];
int k = cal(de);
cout << ans[k]<<endl;
}
return 0;
}

58
HDOJ/1431_autoAC.cpp Normal file
View File

@ -0,0 +1,58 @@
#include <stdio.h>
#include <math.h>
#include <string.h>
int p[20000];
bool sushu(int a)
{
int i;
for(i=2;i<=sqrt((double)(a));i++)
{
if(a%i==0)
return 0;
}
return 1;
}
bool huiwen(int a)
{
int b[44];
int l=0,i;
int c=a;
while(a)
{
b[l++]=a%10;
a/=10;
}
for(i=0;i<l/2;i++)
{
if(b[i]!=b[l-1-i])
break;
}
if(i==l/2 &&sushu(c))
return 1;
else
return 0;
}
int main()
{
int i;
int a,b;
memset(p,0,sizeof(p));
int l=0;
for(i=5;i<=9989900;i++)
{
if(huiwen(i))
{
p[l++]+=i;
}
}
while(scanf("%d%d",&a,&b)!=EOF)
{
for(i=0;i<l;i++)
{
if(p[i] &&p[i]>=a &&p[i]<=b)
printf("%d\n",p[i]);
}
printf("\n");
}
return 0;
}

84
HDOJ/1432_autoAC.cpp Normal file
View File

@ -0,0 +1,84 @@
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <sstream>
#include <iostream>
#include <stack>
using namespace std;
#define ISZREO(x) (fabs(x)<1e-7)
const double PI = acos(-1.0);
struct Point
{
double x,y;
}a[1100],c[1100],Q;
double b[800];
void fun1(int n)
{
int i;
for(i=0;i<n;i++)
{
c[i].x=a[i].x-Q.x;
c[i].y=a[i].y-Q.y;
}
}
void fun2(int n)
{
int i;
for(i=0;i<n;i++)
{
b[i]=atan2(c[i].y,c[i].x);
if(b[i]<0)b[i]+=PI;
}
sort(b,b+n);
}
int main()
{
int i,n,j,ans,cy;
double ji,p;
Point temp;
while(~scanf("%d",&n))
{
for(i=0;i<n;i++)
{
scanf("%lf %lf",&a[i].x,&a[i].y);
}
ans=0;
for(i=0;i<n;i++)
{
Q=a[i];
fun1(n);
fun2(n);
ji=-9999999.99;
for(j=0;j<n;j++)
{
if(!ISZREO(ji-b[j]))
{
cy=1;
ji=b[j];
}
else
{
cy++;
if(cy>ans)
{
ans=cy;
p=ji;
}
}
}
}
if(ans == 0)ans++;
if(!ISZREO(p))ans++;
printf("%d\n",ans);
}
return 0;
}

42
HDOJ/1433_autoAC.cpp Normal file
View File

@ -0,0 +1,42 @@
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s[300];
int len;
bool flag;
bool OK( int x,int y ){
if( x == y ){
if( s[x] >= 'p' && s[x] <= 'z' )
return true;
else return false;
}
if( s[x] == 'N' ){
if( OK(x+1,y) )
return true;
else return false;
}
if( s[x] == 'C' || s[x] == 'D' || s[x] == 'E' || s[x] == 'I' ){
for( int i = x+1 ; i <= y ; ++i )
if( OK(x+1,i) && OK(i+1,y) )
return true;
return false;
}
return false;
}
int main(){
while(scanf("%s",s)!=EOF){
flag = false;
len = strlen(s);
if( len == 1 && ( s[0] >= 'p' && s[0] <= 'z' ) )
flag = true;
else{
if( OK(0,len-1) )
flag = true;
}
if( flag )
printf("YES\n");
else printf("NO\n");
}
return 0;
}

71
HDOJ/1434_autoAC.cpp Normal file
View File

@ -0,0 +1,71 @@
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
struct node
{
int RP;
char name[25];
bool operator < (const node a) const
{
if(RP==a.RP)
{
if(strcmp(name,a.name)>0)
return 0;
else
return 1;
}
return RP>a.RP;
}
}t;
priority_queue <node> q[10011];
int main()
{
int M,N,k;
int i,j,x1,x2;
char ch[20];
while(scanf("%d%d",&M,&N)!=EOF)
{
for(i=0;i<10011;i++)
{
while(!q[i].empty())
q[i].pop();
}
for(i=1;i<=M;i++)
{
scanf("%d",&k);
for(j=0;j<k;j++)
{
scanf("%s%d",t.name,&t.RP);
q[i].push(t);
}
}
for(i=1;i<=N;i++)
{
scanf("%s",ch);
if(strcmp(ch,"GETOUT")==0)
{
scanf("%d",&k);
cout<<q[k].top().name<<endl;
q[k].pop();
}
else if(strcmp(ch,"GETON")==0)
{
scanf("%d%s%d",&k,t.name,&t.RP);
q[k].push(t);
}
else if(strcmp(ch,"JOIN")==0)
{
scanf("%d%d",&x1,&x2);
while(!q[x2].empty())
{
t=q[x2].top();
q[x1].push(t);
q[x2].pop();
}
}
}
}
return 0;
}

107
HDOJ/1435_autoAC.cpp Normal file
View File

@ -0,0 +1,107 @@
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
#define N 205
int engage[N],n;
int boy[N][N],girl[N][N];
void Gale_Shapley(){
queue<int> q;
int boyid,girlid;
for(int i=1;i<=n;i++){
engage[i]=0;
boy[i][0]=1;
q.push(i);
}
while(!q.empty()){
boyid=q.front();
girlid=boy[boyid][boy[boyid][0]++];
if(engage[girlid]==0){
engage[girlid]=boyid;
q.pop();
}
else{
int i;
for(i=1;i<=n;i++)
if(girl[girlid][i]==boyid||girl[girlid][i]==engage[girlid])
break;
if(girl[girlid][i]==boyid){
q.push(engage[girlid]);
engage[girlid]=boyid;
q.pop();
}
}
}
}
struct dis{
double s;
double c;
int id;
};
struct type{
double c;
double x,y,z;
dis D[N];
}O[N],I[N];
void cal_dis(int x){
for(int i=1;i<=n;i++){
double d=0;
d+=(O[x].x-I[i].x)*(O[x].x-I[i].x);
d+=(O[x].y-I[i].y)*(O[x].y-I[i].y);
d+=(O[x].z-I[i].z)*(O[x].z-I[i].z);
d=sqrt(d);
O[x].D[i].s=d;
O[x].D[i].id=i;
O[x].D[i].c=I[i].c;
I[i].D[x].s=d;
I[i].D[x].id=x;
I[i].D[x].c=O[x].c;
}
}
bool cmp(dis a,dis b){
if(a.s==b.s)
return a.c>b.c;
return a.s<b.s;
}
int main(void){
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=1;i<=n;i++){
int id;
scanf("%d",&id);
scanf("%lf%lf%lf%lf",&O[id].c,&O[id].x,&O[id].y,&O[id].z);
}
for(int i=1;i<=n;i++){
int id;
scanf("%d",&id);
scanf("%lf%lf%lf%lf",&I[id].c,&I[id].x,&I[id].y,&I[id].z);
}
for(int i=1;i<=n;i++)
cal_dis(i);
for(int i=1;i<=n;i++){
sort(O[i].D+1,O[i].D+n+1,cmp);
for(int j=1;j<=n;j++)
boy[i][j]=O[i].D[j].id;
}
for(int i=1;i<=n;i++){
sort(I[i].D+1,I[i].D+n+1,cmp);
for(int j=1;j<=n;j++)
girl[i][j]=I[i].D[j].id;
}
Gale_Shapley();
int X[N],Y[N];
for(int i=1;i<=n;i++){
X[i]=i;
Y[i]=engage[i];
}
for(int i=1;i<=n;i++)
printf("%d %d\n",Y[i],X[i]);
printf("\n");
}
}

59
HDOJ/1436_autoAC.cpp Normal file
View File

@ -0,0 +1,59 @@
#include <cstdio>
#include <cstring>
#define MAXN 1001
__int64 com[MAXN][MAXN];
void get_Combination(int n)
{
int i, j;
memset(com, 0, sizeof(com));
com[0][0] = 1;
for (i = 1; i <= n; i++)
{
com[i][0] = 1;
for (j = 1; j <= i; j++) com[i][j] = com[i-1][j-1] + com[i-1][j];
}
}
int n, m, dlen;
__int64 d[MAXN];
__int64 p1[MAXN], p2[MAXN];
int main()
{
int i, j, k;
get_Combination(MAXN);
while (~scanf("%d %d", &n, &m))
{
memset(p1, 0, sizeof(p1));
memset(p2, 0, sizeof(p2));
for (i = 0; i < n; i++)
{
scanf("%d", &dlen);
for (j = 0; j < dlen; j++) scanf("%I64d", &d[j]);
if (i == 0)
{
for (j = 0; j < dlen; j++)
{
if (d[j] > m) continue;
p1[d[j]] = com[m][d[j]];
}
continue;
}
for (j = m; j >= 0; j--)
if (p1[j] != 0)
{
for (k = 0; k < dlen; k++)
{
if (j + d[k] > m) break;
p2[j+d[k]] += com[m-j][d[k]] * p1[j];
}
p2[j] = p1[j];
}
for (j = 0; j <= m; j++)
{
p1[j] = p2[j];
p2[j] = 0;
}
}
printf("%I64d\n", p1[m]);
}
return 0;
}

32
HDOJ/1437_autoAC.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <iostream>
#include <cstdio>
using namespace std;
double dp[3][3][1002],map[3][3];
double DFS(int i,int j,int n){
if(dp[i][j][n]!=-1) return dp[i][j][n];
dp[i][j][n] = 0;
for(int k = 0;k<3;k++)
dp[i][j][n]+=DFS(i,k,n-1)*map[k][j];
return dp[i][j][n];
}
int main(){
int T,m,a,b,n;
scanf("%d",&T);
while(T--){
for(int i = 0;i<3;i++)
for(int j = 0;j<3;j++)
for(int k = 0;k<1002;k++)
dp[i][j][k] = -1;
for(int i = 0;i<3;i++)
for(int j = 0;j<3;j++){
scanf("%lf",&map[i][j]);
dp[i][j][1] = map[i][j];
}
scanf("%d",&m);
for(int i = 0;i<m;i++){
scanf("%d%d%d",&a,&b,&n);
printf("%.3lf\n",DFS(a-1,b-1,n));
}
}
return 0;
}

16
HDOJ/1438_autoAC.cpp Normal file
View File

@ -0,0 +1,16 @@
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int i;
long long a[32] = { 0, 0, 0 }, c = 0, b = 2,t;
for (i = 3; i < 32; ++i)
{
a[i] = a[i - 1] * 4 + (long long)(pow(2, i - 1) - 2) * 2 + (c + b) * 2;
t = c + b*2;
b = (c + b)*2 + 2;
c = t;
}
for (i = 2; i < 32; ++i)
cout << "N=" << i <<": " << a[i] << endl;
}

55
HDOJ/1439_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include<cstdio>
#include<cstring>
#include<vector>
#include<utility>
using namespace std;
const int maxn = 205;
char s[maxn], ans[maxn];
int a[maxn];
bool vis[maxn];
pair<int, int> belong[maxn];
vector<int> group[maxn], tmp;
int main()
{
int n, i, j, cnt, k;
while (scanf("%d", &n), n)
{
for (i = 1; i <= n; ++i)
{
scanf("%d", &k);
a[i] = k;
}
memset(vis, 0, sizeof(vis));
for (i = 0; i < n; ++i) group[i].clear();
cnt = 0;
for (i = 1; i <= n; ++i)
{
if (!vis[i])
{
for (j = a[i]; !vis[j]; j = a[j])
{
belong[j] = make_pair(cnt, group[cnt].size());
group[cnt].push_back(j);
vis[j] = true;
}
++cnt;
}
}
while (scanf("%d", &k), k)
{
getchar();
gets(s + 1);
for (i = strlen(s + 1) + 1; i <= n; ++i)
s[i] = ' ';
for (i = 1; i <= n; ++i)
{
tmp = group[belong[i].first];
ans[tmp[(belong[i].second + k) % tmp.size()]] = s[i];
}
ans[n + 1] = 0;
puts(ans + 1);
}
putchar(10);
}
return 0;
}

49
HDOJ/1443_autoAC.cpp Normal file
View File

@ -0,0 +1,49 @@
#include <cstdlib>
#include <iostream>
#include<string>
#include <cstdio>
#include<algorithm>
#include <sstream>
#include <math.h>
using namespace std;
int n, a, b;
int con[15];
int run(int a, int b)
{
int cnt = 0;
int sum = 2 * a;
int index = b % (2 * a);
if(index == 0) index = sum;
while(index > a)
{
sum--;
index = (index + b - 1) % sum;
if(index == 0) index = sum;
cnt ++;
}
return cnt;
}
int solve(int a)
{
for(int i=a+1; ; i++)
{
if(run(a, i) == a) return i;
}
}
void init()
{
for(int i=1; i<=14; i++)
{
con[i] = solve(i);
}
}
int main()
{
init();
while(cin>>n)
{
if(n == 0) break;
else cout<<con[n]<<endl;
}
return 0;
}

26
HDOJ/1444_autoAC.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char a[19][10]={"pop", "no", "zip", "zotz", "tzec", "xul", "yoxkin", "mol", "chen", "yax", "zac", "ceh", "mac", "kankin", "muan", "pax", "koyab", "cumhu","uayet"};
char b[20][10]={"imix", "ik", "akbal", "kan", "chicchan", "cimi", "manik", "lamat", "muluk", "ok", "chuen", "eb", "ben", "ix", "mem", "cib", "caban", "eznab", "canac", "ahau"};
char c[10];
int t,d,y,r,w;
cin>>t;
cout<<t<<endl;
while(t--)
{
scanf("%d. %s %d",&d,c,&y);
for(int i=0;i<19;i++)
if(strcmp(c,a[i])==0)
w=i;
r=y*365+w*20+d;
y=r/260;
w=r%20;
d=r%13;
cout<<d+1<<" "<<b[w]<<" "<<y<<endl;
}
return 0;
}

22
HDOJ/1445_autoAC.cpp Normal file
View File

@ -0,0 +1,22 @@
#include<iostream>
#include<cmath>
using std::cin;
using std::cout;
using std::endl;
int main() {
while(true) {
int N;
cin>>N;
if(!N)
break;
double mint=1e36;
double v,t0;
while(N--) {
cin>>v>>t0;
if(t0>=0&&ceil(t0+4500*3.6/v)<mint)
mint=ceil(t0+4500*3.6/v);
}
cout<<static_cast<long>(mint)<<endl;
}
return 0;
}

27
HDOJ/1447_autoAC.cpp Normal file
View File

@ -0,0 +1,27 @@
#include<cstdio>
#include<cstring>
int n,m,c,d[105][5005];
int dfs(int n,int m)
{
if(m<0||m>n*(n-1)>>1)return 0;
if(m==0)return 1;
if(d[n][m]!=-1)return d[n][m];
for(int i=1;i<=n;i++)
{
d[n][m]=dfs(n-i,m-i*(n-i));
if(d[n][m])return 1;
}
return 0;
}
int main()
{
memset(d,-1,sizeof(d));
while(scanf("%d%d",&n,&m),n+m)
{
printf("Case %d: %d lines ",++c,n);
if(dfs(n,m))
printf("with exactly %d crossings can cut the plane into %d pieces at most.\n",m,n+m+1);
else
printf("cannot make exactly %d crossings.\n",m);
}
}

43
HDOJ/1449_autoAC.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iostream>
#define INF 0x3f3f3f3f
#define MAXN 1000005
using namespace std;
int N, dp[MAXN];
struct Node {
int x, y, ac;
}e[5005];
int DP() {
int ret = INF;
memset(dp, 0x3f, sizeof (dp));
dp[e[1].x] = 0;
for (int i = 2; i <= N; ++i) {
int a = e[i].ac, b = e[i].x;
for (int j = a; j < b; ++j) {
int p = 2*e[i].x-j;
if (p < e[N].x) {
dp[p] = min(dp[p], dp[j]+1);
} else {
ret = min(ret, dp[j]+1);
}
}
}
return ret == INF ? -1 : ret;
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%d", &N);
for (int i = 1; i <= N; ++i) {
scanf("%d %d", &e[i].x, &e[i].y);
e[i].ac = max(0, (int)ceil(e[i].x-sqrt(2.*e[i].y*e[1].y-1.*e[1].y*e[1].y)));
}
printf("%d\n", DP());
}
return 0;
}

89
HDOJ/1450_autoAC.cpp Normal file
View File

@ -0,0 +1,89 @@
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
const int N=105;
const int inf=(1<<30);
int n,m;
double a[N];
bool mat[N][N];
bool vis[N];
int indgree[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(i!=j&&mat[i][k]&&mat[k][j])
{
mat[i][j]=true;
}
}
void solve()
{
floyd();
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
if(vis[i])
continue;
double sum1=0,sum2=0;
vis[i]=true;
sum1+=a[i];
sum2+=indgree[i];
for(int j=1;j<=n;j++)
{
if(!vis[j]&&mat[i][j])
{
sum1+=a[j];
sum2+=indgree[j];
//vis[i]=true;
}
}
if(sum2==0)
{
printf("%.3lf\n",sum1);
continue;
}
double k=(sum1)/sum2;
printf("%.3lf\n",k*indgree[i]);
for(int j=1;j<=n;j++)
{
if(!vis[j]&&mat[i][j])
{
printf("%.3lf\n",k*indgree[j]);
vis[j]=true;
}
}
}
printf("\n");
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(mat,0,sizeof(mat));
memset(indgree,0,sizeof(indgree));
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%lf",a+i);
int u,v;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&u,&v);
if(!mat[u][v])
{
mat[u][v]=mat[v][u]=true;
indgree[u]++;
indgree[v]++;
}
}
solve();
}
return 0;
}

29
HDOJ/1451_autoAC.cpp Normal file
View File

@ -0,0 +1,29 @@
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
const double PI=acos(-1.0);
int main()
{
double a,b,c,L;
int cout=1;
while(cin>>a>>b>>c>>L)
{
if(a==0&&b==0&&c==0&&L==0)
break;
double p=(a+b+c)/2.0;
double S=sqrt(p*(p-a)*(p-b)*(p-c));
double R=S/p;
if(L>=a+b+c)
printf("Case %d: %.2lf\n",cout++,S);
else if(L<=PI*R*2)
printf("Case %d: %.2lf\n",cout++,L*L/(4*PI));
else
{
double r=R*(2*p-L)/(2*p-2*PI*R);
double s=(r/R)*(r/R)*S;
printf("Case %d: %.2lf\n",cout++,S-s+PI*r*r);
}
}
return 0;
}

29
HDOJ/1452_autoAC.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <functional>
#include <vector>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
using namespace std;
const int n=29;
long long pow_mod(long long a,long long i,long long n)
{ if(i==0) return 1;
int temp=pow_mod(a,i>>1,n);
temp=temp*temp%n;
if(i&1) temp=(long long)temp*a%n;
return temp;
}
int main()
{ int X,S;
while(cin>>X)
{ if(X==0) break;
S=((pow_mod(2,2*X+1,n)+28)*(pow_mod(3,X+1,n)+28)*(pow_mod(167,X+1,n)+28)*9)%n;
cout<<S<<endl;
}
return 0;
}

42
HDOJ/1453_autoAC.cpp Normal file
View File

@ -0,0 +1,42 @@
#include<cstdio>
#include<cstring>
#include <iostream>
using namespace std;
const int A=210;
int main()
{
int m,n,i,j,ii,jj,c;
cin>>m>>n;
getchar();
char p[A][A];
int pp[A];
for(i=0;i<m;i++)
gets(p[i]);
for(int k=m;k>=1;k--)
{
int count=0;
for(j=0;j<n;j++)
for(i=0;i<m;i++)
if(p[i][j]=='0')
{
c=0;
for(jj=i;jj<m;jj++)
{
if(p[jj][j]=='0') c++;
else {jj--;break;}
}
if(c==k)
{
count++;
for(int jjj=i;jjj<=jj;jjj++) p[jjj][j]='1';
}
i=jj;
}
pp[k-1]=count;
}
for(i=0;i<m;i++)
{
if(pp[i]!=0) cout<<i+1<<" "<<pp[i]<<endl;
}
return 0;
}

99
HDOJ/1454_autoAC.cpp Normal file
View File

@ -0,0 +1,99 @@
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
const double precision=1e-3;
const double inf=99999.0;
typedef class Node
{
public:
double x;
double y;
}point;
int max(int a,int b)
{
return a>b?a:b;
}
int dblcmp(double p)
{
if(fabs(p)<precision)
return 0;
return p>0?1:-1;
}
double det(double x1,double y1,double x2,double y2)
{
return x1*y2-x2*y1;
}
double cross(point A,point B,point P)
{
return det(B.x-A.x , B.y-A.y , P.x-A.x , P.y-A.y);
}
bool check(point A,point B,point C,point D)
{
return (dblcmp(cross(A,B,C)) * dblcmp(cross(A,B,D)) <= 0);
}
double intersection(point A,point B,point C,point D)
{
double area1=cross(A,B,C);
double area2=cross(A,B,D);
int c=dblcmp(area1);
int d=dblcmp(area2);
if(c*d<0)
return (area2*C.x - area1*D.x)/(area2-area1);
if(c*d==0)
if(c==0)
return C.x;
else
return D.x;
return -inf;
}
int main(int i,int j,int k)
{
int n;
while(cin>>n)
{
if(!n)
break;
point* up=new point[n+1];
point* down=new point[n+1];
double max_x=-inf;
for(i=1;i<=n;i++)
{
cin>>up[i].x>>up[i].y;
down[i].x=up[i].x;
down[i].y=up[i].y-1;
}
bool flag=false;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
if(i!=j)
{
for(k=1;k<=n;k++)
if(!check(up[i],down[j],up[k],down[k]))
break;
if(k>n)
{
flag=true;
break;
}
else if(k>max(i,j)) {
double temp=intersection(up[i],down[j],up[k],up[k-1]);
if(max_x < temp)
max_x=temp;
temp=intersection(up[i],down[j],down[k],down[k-1]);
if(max_x < temp)
max_x=temp;
}
}
if(flag)
break;
}
if(flag)
cout<<"Through all the pipe."<<endl;
else
cout<<fixed<<setprecision(2)<<max_x<<endl;
delete up,down;
}
return 0;
}

95
HDOJ/1455_autoAC.cpp Normal file
View File

@ -0,0 +1,95 @@
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
int n,a[65],vis[65],sum;
bool cmp(int a,int b)
{
return a>b;
}
int dfs(int now,int len,int pos,int q,int num)
{
int i,j;
int cut;
cut=0;
if(len==0)cut=1;
if(num==0)
return 1;
if(q%2!=(len+num*now)%2)
return 0;
for(i=pos;i<n;i++)
{
if(len+a[i]>now)
continue;
if(vis[i])continue;
vis[i]=1;
if(a[i]+len==now)
{
if(a[i]%2==1)
{
if(dfs(now,0,0,q-1,num-1))
return 1;
}
else
{
if(dfs(now,0,0,q,num-1))
return 1;
}
vis[i]=0;
return 0;
}
else
{
if(a[i]%2==1)
{
if(dfs(now,len+a[i],i+1,q-1,num))
return 1;
}
else
{
if(dfs(now,len+a[i],i+1,q,num))
return 1;
}
vis[i]=0;
if(cut)
return 0;
while(i<n&&a[i]==a[i+1])
i++;
}
}
return 0;
}
int main()
{
int i,l;int q;
while(scanf("%d",&n)!=EOF)
{
if(n<1)
break;
sum=0;q=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]%2==1)
q+=1;
sum+=a[i];
}
memset(vis,0,sizeof(vis));
sort(a,a+n,cmp);
for(i=a[0];i<=sum;i++)
{
if(sum%i!=0)
continue;
l=sum/i;
if(dfs(i,0,0,q,l))
{
printf("%d\n",i);
break;
}
}
}
return 0;
}

64
HDOJ/1456_autoAC.cpp Normal file
View File

@ -0,0 +1,64 @@
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
struct Order
{
int s;
int e;
int p;
}ord[23];
int n, NumB, NumOrd;
int MaxEarn, CurEarn, off[8];
int cmp (const void *aa, const void *bb)
{
struct Order *a=(Order*) aa;
struct Order *b=(Order*) bb;
if(a -> s != b -> s) return a -> s - b -> s;
else return a -> e - a -> e;
}
void DFS (int num,int tolp)
{
if (num == NumOrd)
{
if (MaxEarn < CurEarn) MaxEarn = CurEarn;
return ;
}
else
{
if (num > 0)
for (int i = ord[num - 1].s + 1; i <= ord[num].s; i++)
{
tolp -= off[i];
}
tolp += ord[num].p;
if (tolp <= n)
{
CurEarn += (ord[num].e - ord[num].s) * ord[num].p;
off[ord[num].e] += ord[num].p;
DFS(num + 1, tolp);
CurEarn -= (ord[num].e - ord[num].s) * ord[num].p;
off[ord[num].e] -= ord[num].p;
}
tolp -= ord[num].p;
DFS (num + 1, tolp);
}
return ;
}
int main()
{
while (scanf("%d %d %d", &n, &NumB, &NumOrd) && (n || NumB || NumOrd))
{
memset(off, 0, sizeof(off));
MaxEarn = 0; CurEarn = 0;
for (int i = 0; i < NumOrd; i++)
{
scanf("%d %d %d", &ord[i].s, &ord[i].e, &ord[i].p);
}
qsort (ord, NumOrd, sizeof(ord[0]), cmp);
DFS(0,0);
printf("%d\n", MaxEarn);
}
return 0;
}

142
HDOJ/1461_autoAC.cpp Normal file
View File

@ -0,0 +1,142 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define N 11
using namespace std;
struct Node
{
char map[10][N];
int flag;
};
char endstate[10][N];
Node t1,t2,t3;
int n,i,j,k;
bool isEnd(Node tt)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(tt.map[i][j]!=endstate[i][j])
return false;
}
}
return true;
}
void rt90()
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
t1.map[j][n-i-1]=t2.map[i][j];
}
}
}
void rt180()
{
rt90();
t3=t2;
t2=t1;
rt90();
t2=t3;
}
void rt270()
{
rt90();
t3=t2;
t2=t1;
rt90();
t2=t1;
rt90();
t2=t3;
}
void reflect()
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
t1.map[n-i-1][j]=t2.map[i][j];
}
}
}
void BFS()
{
queue<Node> qu;
if(isEnd(t1))
{
printf("Preserved\n");
return;
}
t1.flag=0;
qu.push(t1);
while(!qu.empty())
{
t2=qu.front();
qu.pop();
if(t2.flag==0)
{
t1.flag=1;
rt90();
if(isEnd(t1))
{
printf("Rotated through 90 degrees\n");
return;
}
rt180();
if(isEnd(t1))
{
printf("Rotated through 180 degrees\n");
return;
}
rt270();
if(isEnd(t1))
{
printf("Rotated through 270 degrees\n");
return;
}
reflect();
if(isEnd(t1))
{
printf("Reflected\n");
return;
}
qu.push(t1);
}
else if(t2.flag==1)
{
rt90();
if(isEnd(t1))
{
printf("Reflected and rotated through 90 degrees\n");
return;
}
rt180();
if(isEnd(t1))
{
printf("Reflected and rotated through 180 degrees\n");
return;
}
rt270();
if(isEnd(t1))
{
printf("Reflected and rotated through 270 degrees\n");
return;
}
printf("Improper\n");
}
}
}
int main()
{
while(scanf("%d",&n)&&n!=0)
{
for(i=0;i<n;i++)
{
scanf("%s%s",t1.map[i],endstate[i]);
}
BFS();
}
}

160
HDOJ/1462_autoAC.cpp Normal file
View File

@ -0,0 +1,160 @@
#include<iostream>
using namespace std;
int main()
{
char c1[10],c2[10],c3[10],c4[10];
int xy=0;
int sum;
while(cin>>c1)
{
int i,j,r1,r2,cc1,cc2,k,v,m;
if(strcmp(c1,"#")==0)
break;
if(xy)
cout<<endl;
int flag1=0;
int flag2=0;
cin>>c2>>c3>>c4;
int len1=strlen(c1);
int len2=strlen(c2);
int len3=strlen(c3);
int len4=strlen(c4);
for(i=0;i<len1;i++)
{
for(j=0;j<len2;j++)
{
if(c1[i]==c2[j])
{
r1=j;
cc1=i;
flag1=1;
break;
}
}
if(flag1)
{
break;
}
}
for(i=0;i<len3;i++)
{
for(j=0;j<len4;j++)
{
if(c3[i]==c4[j])
{
r2=j;
cc2=i;
flag2=1;
break;
}
}
if(flag2)
{
break;
}
}
if(flag1==1&&flag2==1)
{
if(r1>r2)
{
for(k=0;k<r1-r2;k++)
{
for(v=0;v<cc1;v++)
cout<<' ';
cout<<c2[k]<<endl;
}
sum=len1-cc1-1+3+cc2;
for(i=0;i<r2;i++)
{
for(v=0;v<cc1;v++)
cout<<' ';
cout<<c2[r1-r2+i];
for(j=0;j<sum;j++)
cout<<' ';
cout<<c4[i]<<endl;
}
cout<<c1<<" "<<c3<<endl;
for(i=r1+1,k=r2+1;i<len2;i++,k++)
{
sum=len1-cc1+3+cc2-1;
for(v=0;v<cc1;v++)
cout<<' ';
cout<<c2[i];
if(k<len4)
{
for(j=0;j<sum;j++)
cout<<' ';
cout<<c4[k]<<endl;
}
else
{
cout<<endl;
}
}
for(i=k;i<len4;i++)
{
for(j=0;j<len1+3+cc2;j++)
{
cout<<' ';
}
cout<<c4[i]<<endl;
}
}
else
{
for(i=0;i<r2-r1;i++)
{
for(j=0;j<len1+3+cc2;j++)
{
cout<<' ';
}
cout<<c4[i]<<endl;
}
for(j=i,i=0;i<r1;i++,j++)
{
for( m=0;m<cc1;m++)
cout<<' ';
cout<<c2[i];
sum=len1-cc1+3+cc2-1;
for(k=0;k<sum;k++)
{
cout<<' ';
}
cout<<c4[j]<<endl;
}
cout<<c1<<" "<<c3<<endl;
for(k=i+1,i=j+1;k<len2;k++,i++)
{
for( m=0;m<cc1;m++)
cout<<' ';
cout<<c2[k];
if(i<len4)
{
sum=len1-cc1+3+cc2-1;
for(m=0;m<sum;m++)
{
cout<<' ';
}
cout<<c4[i]<<endl;
}
else
{
cout<<endl;
}
}
for(;i<len4;i++)
{
for(k=0;k<len1+3+cc2;k++)
cout<<' ';
cout<<c4[i]<<endl;
}
}
}
else
{
cout<<"Unable to make two crosses"<<endl;
}
xy++;
}
return 0;
}

15
HDOJ/1465_autoAC.cpp Normal file
View File

@ -0,0 +1,15 @@
#include <stdio.h>
int main()
{
__int64 a[21];
int i,n;
a[1]=0;
a[2]=1;
while(scanf("%d",&n)!=EOF)
{
for(i=3;i<n+1;i++)
{a[i]=(i-1)*(a[i-1]+a[i-2]);}
printf("%I64d\n",a[n]);
}
return 0;
}

24
HDOJ/1466_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include<stdio.h>
int main(){
int dp[21][191];
int i,j,k;
for(i=0;i<21;i++)
for(j=0;j<191;j++)
dp[i][j]=0;
for(i=0;i<=20;i++){
dp[i][0]=1;
for(j=0;j<=i;j++){
for(k=0;k<=(j-1)*j/2;k++){
dp[i][(i-j)*j+dp[j][k]*k]=1;
}
}
}
while(scanf("%d",&k)!=EOF){
printf("0");
for(i=1;i<=(k-1)*k/2;i++)
if(dp[k][i])
printf(" %d",i);
printf("\n");
}
return 0;
}

65
HDOJ/1467_autoAC.cpp Normal file
View File

@ -0,0 +1,65 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=101;
char m[maxn][2*maxn];
int n;
bool judge(int h,int left,int right)
{
if(right>2*n-h||left<h)
return false;
for(int i=left;i<=right;++i)
if(m[h][i]!='-')
return false;
return true;
}
int up(int h,int l)
{
int result=0;
for(int i=h;i>=1;--i)
if(judge(i,l-(h-i),l+(h-i)))
result+=2*(h-i)+1;
else
break;
return result;
}
int down(int h,int l)
{
int result=0;
for(int i=h;i<=n;++i)
if(judge(i,l-(i-h),l+(i-h)))
result+=2*(i-h)+1;
else
break;
return result;
}
int dfs()
{
int maxarea=0;
for(int i=1;i<=n;++i)
for(int j=i;j<=2*n-i;++j)
{
if(m[i][j]=='#')
continue;
if((i+j)%2==1)
maxarea=max(maxarea,down(i,j));
else
maxarea=max(maxarea,up(i,j));
}
return maxarea;
}
int main()
{
static int num=0;
while(cin>>n&&n)
{
getchar();
for(int i=1;i<=n;++i)
gets(m[i]+1);
cout<<"Triangle #"<<++num<<endl;
cout<<"The largest triangle area is "<<dfs()<<"."<<endl<<endl;
}
return 0;
}

185
HDOJ/1469_autoAC.cpp Normal file
View File

@ -0,0 +1,185 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
#define print(x) cout<<x<<endl
#define input(x) cin>>x
#define SIZE 128
#define pb push_back
const double eps=1e-10;
inline int zero(double x)
{
if(x<-eps) return -1;
else if(fabs(x)<eps) return 0;
else return 1;
}
struct point
{
double x,y;
point(){}
point(double i_x,double i_y)
{
x=i_x;y=i_y;
}
void setpoint(double i_x,double i_y)
{
x=i_x;y=i_y;
}
friend bool operator == (const point& pa,const point& pb)
{
return zero(pa.x-pb.x) && zero(pa.y-pb.y);
}
};
struct segment
{
point p1,p2;
segment(){}
segment(double x1,double y1,double x2,double y2)
{
p1.setpoint(x1,y1);
p2.setpoint(x2,y2);
}
segment(point t1,point t2){p1=t1;p2=t2;}
inline void setsegment(point a,point b){p1=a;p2=b;}
inline void setsegment(double x1,double y1,double x2,double y2)
{
p1.setpoint(x1,y1);
p2.setpoint(x2,y2);
}
};
struct line
{
double a,b,c;
friend bool operator == (const line x,const line y)
{
if(fabs(x.a-y.a)<eps&&fabs(x.b-y.b)<eps&&fabs(x.c-y.c)<eps) return true;
else return false;
}
inline void setline(double ta,double tb,double tc)
{
a=ta;b=tb;c=tc;
}
};
double xmult(point sp,point ep,point op)
{
return((sp.x-op.x)*(ep.y-op.y)-(sp.y-op.y)*(ep.x-op.x));
}
line makeLine(point p1,point p2)
{
line res;
int sig=1;
res.a=p2.y-p1.y;
if(res.a<0)
{
sig=-1;
res.a=sig*res.a;
}
res.b=sig*(p1.x-p2.x);
res.c=sig*(p1.y*p2.x-p2.y*p1.x);
return res;
}
line makeLine(segment s)
{
return makeLine(s.p1,s.p2);
}
bool lineIntersect(line l1,line l2,point &p)
{
double d=l1.a*l2.b-l2.a*l1.b;
if(fabs(d)<eps) return false;
else
{
p.x = (l2.c*l1.b-l1.c*l2.b)/d;
p.y = (l2.a*l1.c-l1.a*l2.c)/d;
return true;
}
}
bool segIntersect(segment l1,segment l2,point &p)
{
if((max(l1.p1.x,l1.p2.x)>=min(l2.p1.x,l2.p2.x))&&
(max(l1.p1.y,l1.p2.y)>=min(l2.p1.y,l2.p2.y))&&
(max(l2.p1.x,l2.p2.x)>=min(l1.p1.x,l1.p2.x))&&
(max(l2.p1.y,l2.p2.y)>=min(l1.p1.y,l1.p2.y))&&//蹇ュ楠
fabs((xmult(l1.p1,l2.p1,l2.p2)*xmult(l1.p2,l2.p1,l2.p2))<=eps)&&
fabs((xmult(l2.p1,l1.p1,l1.p2)*xmult(l2.p2,l1.p1,l1.p2))<=eps))//璺ㄧ瀹楠
{
lineIntersect(makeLine(l1),makeLine(l2),p);
return true;
}
else return false;
}
struct polygen
{
point pvec[SIZE];
point core[SIZE];
int sz;
polygen(){sz=0;}
inline void push_point(const point& p)
{
pvec[sz++]=p;
}
void Cut(segment s)
{
int q=0;
for(int i=0;i<sz;i++)
{
if(xmult(pvec[i],s.p2,s.p1)>=0)
{
core[q++]=pvec[i];
}
else
{
point cp;
if(xmult(pvec[i],s.p2,s.p1) * xmult(pvec[(i+1)%sz],s.p2,s.p1)<0)
{
point cp;
line l1=makeLine(s);
line l2=makeLine(pvec[i],pvec[(i+1)%sz]);
lineIntersect(l1,l2,cp);
core[q++]=cp;
}
}
}
for(int i=0;i<q;i++)
{
pvec[i]=core[i];
}
sz=q;
}
void getKernel()
{
int n=sz;
point p[SIZE];
for(int i=0;i<n;i++) p[i]=pvec[i];
for(int i=0;i<n;i++)
{
segment s(p[i],p[(i+1)%n]);
Cut(s);
}
}
};
int n;
polygen poly;
int main()
{
double a,b;
int cas=1;
while(input(n) && n)
{
poly=polygen();
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&a,&b);
poly.push_point(point(a,b));
}
poly.getKernel();
printf("Floor #%d\n",cas++);
if(poly.sz>0) print("Surveillance is possible.");
else print("Surveillance is impossible.");
puts("");
}
return 0;
}

216
HDOJ/1470_autoAC.cpp Normal file
View File

@ -0,0 +1,216 @@
#include<iostream>
#include<string.h>
using namespace std;
#define N 55
#define M 10
int people[M];
int brith[M];
int n;
int ok;
int FP;
int now[M];
int yes[M];
struct say{
char talk;
char name;
int bo;
int sex;
};
void thesome()
{
for(int i = 0; i < 6; i++)
if(now[i] != people[i])
yes[i] = 1;
}
int ture_say(say f)
{
if(f.sex != -1)
{
if(f.name == 'T')
{
if(f.bo && f.sex - 4 == people[0])
return 1;
else if(!f.bo && f.sex - 4 != people[0])
return 1;
else
return 0;
}
else
{
int id = f.name - 'A' + 1;
if(f.bo && f.sex == people[id])
return 1;
else if(!f.bo && f.sex != people[id])
return 1;
else
return 0;
}
}
else
{
int id = f.name - 'A' + 1;
if(people[id] == 3 && !f.bo)
return 1;
else if (people[id] == 1 && f.bo)
return 1;
else if(people[id] == 2)
{
if(people[0] && !f.bo)
return 1;
else if(!people[0] && f.bo)
return 1;
else
return 0;
}
else
return 0;
}
}
int write(char str[])
{
if(strncmp(str, "divine.", 7) == 0)
return 3;
else if(strncmp(str, "human.", 6) == 0)
return 2;
else if(strncmp(str, "evil.", 5) == 0)
return 1;
else if(strncmp(str, "day.", 4) == 0)
return 5;
else if( strncmp(str, "night.", 6) == 0)
return 4;
else if( strncmp(str, "lying.", 6) ==0)
return -1;
}
void read(say tem[])
{
char str[M];
int id;
for(int i = 0; i < n; i++)
{
cin >> str;
tem[i].talk = str[0];
id = str[0] - 'A' + 1;
brith[id] = 1;
cin >> str;
if(strcmp(str, "It") == 0)
tem[i].name = 'T';
else if(str[0] == 'I')
tem[i].name = tem[i].talk;
else
{
tem[i].name = str[0];
id = str[0] - 'A' + 1;
brith[id] = 1;
}
cin >> str;
cin >> str;
if(strcmp(str, "not"))
{
tem[i].bo = 1;
tem[i].sex = write(str);
}
else
{
tem[i].bo = 0;
cin >> str;
tem[i].sex = write(str);
}
}
}
int judge(say tem[])
{
for(int i = 0; i < n; i++)
{
int id = tem[i].talk - 'A' + 1;
if(people[id] == 3)
{
if(ture_say(tem[i]))
continue;
else
return 0;
}
else if(people[id] == 2)
{
int f = ture_say(tem[i]);
if( (people[0] && f) || (!people[0] && !f) )
continue;
else
return 0;
}
else if(people[id] == 1)
{
if(!ture_say(tem[i]))
continue;
else
return 0;
}
}
return 1;
}
void build(int k, say tem[])
{
if(k < 6)
{
if(brith[k])
{
for(people[k] = 1; people[k] < 4; people[k]++)
build(k + 1, tem);
}
else
build(k + 1, tem);
}
else if(judge(tem))
{
if(ok >= 1)
thesome();
else
for(int i = 0; i < 6; i++)
now[i] = people[i];
ok++;
}
}
int main()
{
int t = 1;
while(cin >> n, n)
{
memset(people, 0, sizeof(people));
memset(brith, 0, sizeof(brith));
ok = 0;
say tem[N];
memset(yes, 0,sizeof(yes));
read(tem);
cout << "Conversation #" << t++ << endl;
for(people[0] = 0; people[0] < 2; people[0]++)
build(1, tem);
if(ok == 0)
cout << "This is impossible." << endl;
else
{
//*
int oi = 0;
for(int i = 1; i < 6; i++)
{
if(!yes[i] && now[i])
{
oi++;
char c = 'A' + i - 1;
if(now[i] == 3)
cout << c << " is divine." << endl;
else if(now[i] == 2)
cout << c << " is human." << endl;
else if(now[i] == 1)
cout << c << " is evil." << endl;
}
}
if(oi == 0 && yes[0])
cout << "No facts are deducible." << endl;
if(now[0] && !yes[0])
cout << "It is day." << endl;
else if(!yes[0])
cout << "It is night." << endl;
}
cout << endl;
}
return 0;
}

56
HDOJ/1472_autoAC.cpp Normal file
View File

@ -0,0 +1,56 @@
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
using namespace std;
const double t = sqrt(2.0);
int main()
{
string s;
int icase = 1;
while(cin >> s) {
if(s == "END") {
break;
}
int num = 0;
double x = 0, y = 0;
for(int i = 0; i < (int)s.length()-1; ++i) {
if(s[i] >= '0' && s[i] <= '9') {
num = num*10+(s[i]-'0');
}
else if(s[i] == ','){
num = 0;
continue;
}
else if(s[i] == 'N' && s[i+1] == 'E') {
x+=num/t, y+=num/t, i++;
}
else if(s[i] == 'N' && s[i+1] == 'W') {
x-=num/t, y+=num/t, i++;
}
else if(s[i] == 'S' && s[i+1] == 'E') {
x+=num/t, y-=num/t, i++;
}
else if(s[i] == 'S' && s[i+1] == 'W') {
x-=num/t, y-=num/t, i++;
}
else if(s[i] == 'N') {
y+=num;
}
else if(s[i] == 'S') {
y-=num;
}
else if(s[i] == 'E') {
x+=num;
}
else if(s[i] == 'W') {
x-=num;
}
}
printf("Map #%d\n", icase++);
printf("The treasure is located at (%.3lf,%.3lf).\n", x, y);
printf("The distance to the treasure is %.3lf.\n\n", sqrt(x*x+y*y));
}
return 0;
}

70
HDOJ/1474_autoAC.cpp Normal file
View File

@ -0,0 +1,70 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define maxn 15
#define maxk 1005
#define maxd 35
#define inf 0x3f3f3f3f
int n, m;
int f[maxn][maxk];
int flight_num[maxn][maxn];
int flight[maxn][maxn][maxd];
void input()
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (i != j)
{
scanf("%d", &flight_num[i][j]);
for (int k = 0; k < flight_num[i][j]; k++)
scanf("%d", &flight[i][j][k]);
}
}
void cal(int day, int u, int v)
{
if (f[u][day] == -1)
return;
if (flight_num[u][v] == 0)
return;
if (flight[u][v][day % flight_num[u][v]] != 0
&& (f[v][day + 1] == -1 || f[v][day + 1] > f[u][day] + flight[u][v][day % flight_num[u][v]]))
f[v][day + 1] = f[u][day] + flight[u][v][day % flight_num[u][v]];
}
int work()
{
memset(f, -1, sizeof(f));
f[0][0] = 0;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
if (j != k)
cal(i, j, k);
return f[n - 1][m];
}
void output()
{
for (int i = 0; i <= m; i++)
{
for (int j = 0; j < n; j++)
printf("%d ", f[j][i]);
puts("");
}
}
int main()
{
int t = 0;
while (scanf("%d%d", &n, &m), n | m)
{
input();
int ans = work();
printf("Scenario #%d\n", ++t);
if (ans == -1)
puts("No flight possible.");
else
printf("The best flight costs %d.\n", ans);
puts("");
}
return 0;
}

51
HDOJ/1476_autoAC.cpp Normal file
View File

@ -0,0 +1,51 @@
#include<cstdio>
#include<iostream>
using namespace std;
int C;
int DI(int t)
{
if(t==0) return 0;
else if(t>=1&&t<=10) return -C;
else return (t-10)*(t-10);
}
int main()
{
int n,L,count=0,i,j;
int t[1010],lecture[1010],DP[1010];
while(scanf("%d",&n),n)
{
count++;
if(count>1)
printf("\n");
scanf("%d %d",&L,&C);
for(i=1;i<=n;i++)
scanf("%d",&t[i]);
lecture[0]=DP[0]=0;
lecture[1]=1;DP[1]=DI(L-t[1]);
for(i=2;i<=n;i++)
{
lecture[i]=lecture[i-1]+1;
DP[i]=DP[i-1]+DI(L-t[i]);
int sum=t[i];
for(j=i-1;j>0;j--)
{
sum+=t[j];
if(sum<=L)
{
int a=DP[j-1]+DI(L-sum);
if(lecture[i]>lecture[j-1]+1)
{
lecture[i]=lecture[j-1]+1;
DP[i]=a;
}
else if(lecture[i]==lecture[j-1]+1)
DP[i]=DP[i]>a?a:DP[i];
}
else
break;
}
}
printf("Case %d:\n\nMinimum number of lectures: %d\nTotal dissatisfaction index: %d\n",count,lecture[n],DP[n]);
}
return 0;
}

78
HDOJ/1479_autoAC.cpp Normal file
View File

@ -0,0 +1,78 @@
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
void R(char* n,char* t)
{
int i,j;
int time[10]={0};
for(i=0;n[i];i++)
time[ n[i]-'0' ]++;
for(i=0,j=0;i<10;i++)
{
if(time[i])
{
if(time[i]<10)
{
t[j++]=time[i]+'0';
t[j++]=i+'0';
}
else
{
t[j++]=time[i]/10+'0';
t[j++]=time[i]%10+'0';
t[j++]=i+'0';
}
}
}
t[j]='\0';
return;
}
int main(int i,int j)
{
char n[16][85];
while(cin>>n[0] && n[0][0]!='-')
{
bool flag1=false;
int flag2=0;
int flag3=0;
for(i=1;i<=15;i++)
R(n[i-1],n[i]);
if(!strcmp(n[0],n[1]))
flag1=true;
if(!flag1)
{
for(j=1;j<15;j++)
if(!strcmp(n[j],n[j+1]))
{
flag2=j;
break;
}
if(!flag2)
{
for(j=1;j<=15;j++)
{
for(i=0;i<=j-2;i++)
{
if(!strcmp(n[j],n[i]))
{
flag3=j-i;
break;
}
}
if(flag3)
break;
}
}
}
if(flag1)
cout<<n[0]<<" is self-inventorying"<<endl;
else if(flag2)
cout<<n[0]<<" is self-inventorying after "<<flag2<<" steps"<<endl;
else if(flag3)
cout<<n[0]<<" enters an inventory loop of length "<<flag3<<endl;
else
cout<<n[0]<<" can not be classified after 15 iterations"<<endl;
}
return 0;
}

29
HDOJ/1480_autoAC.cpp Normal file
View File

@ -0,0 +1,29 @@
#include<iostream>
#include<cmath>
#define N 30
using namespace std;
long long d[N],d1[N],d2[N];
int main()
{
int i;
long long t;
d1[3]=32;
d2[3]=72;
d[3]=104;
int n;
n=25;
for(i=4;i<=n;i++)
{
d1[i]=d1[i-1]+2*d2[i-1];
d2[i]=4*d[i-1];
t=pow(2,i+1);
d1[i]=d1[i]+4*t-32;
d2[i]=d2[i]+9*t-72;
d[i]=d1[i]+d2[i];
}
for(i=3;i<=n;i++)
{
cout<<"N="<<i<<": "<<d[i]<<endl;
}
return 0;
}

67
HDOJ/1482_autoAC.cpp Normal file
View File

@ -0,0 +1,67 @@
#include<stdio.h>
#include<string.h>
int main()
{
char str[9][20];
int a[12];
int T;
scanf("%d",&T);
while(T--)
{
for(int i=0;i<9;i++)scanf("%s",&str[i]);
int res=-1;
while(res<12)
{
int t;
if(res==-1){res=0;t=1;}
else if(a[res]==1) t=-1;
else {res++;t=1;}
for(int i=0;i<12;i++) a[i]=0;
a[res]=t;
int L=0,R=0;
int len=strlen(str[0]);
for(int i=0;i<len;i++) L+=a[str[0][i]-'A'];
len=strlen(str[1]);
for(int i=0;i<len;i++) R+=a[str[1][i]-'A'];
if(strcmp(str[2],"even")==0)
{
if(L!=R)continue;
}
if(strcmp(str[2],"up")==0)
if(L<=R)continue;
if(strcmp(str[2],"down")==0)
if(L>=R)continue;
L=0,R=0;
len=strlen(str[3]);
for(int i=0;i<len;i++) L+=a[str[3][i]-'A'];
len=strlen(str[4]);
for(int i=0;i<len;i++) R+=a[str[4][i]-'A'];
if(strcmp(str[5],"even")==0)
{
if(L!=R)continue;
}
if(strcmp(str[5],"up")==0)
if(L<=R)continue;
if(strcmp(str[5],"down")==0)
if(L>=R)continue;
L=0,R=0;
len=strlen(str[6]);
for(int i=0;i<len;i++) L+=a[str[6][i]-'A'];
len=strlen(str[7]);
for(int i=0;i<len;i++) R+=a[str[7][i]-'A'];
if(strcmp(str[8],"even")==0)
{
if(L!=R)continue;
}
if(strcmp(str[8],"up")==0)
if(L<=R)continue;
if(strcmp(str[8],"down")==0)
if(L>=R)continue;
break;
}
printf("%c is the counterfeit coin and it is ",res+'A');
if(a[res]==-1) printf("light.\n");
else printf("heavy.\n");
}
return 0;
}

111
HDOJ/1483_autoAC.cpp Normal file
View File

@ -0,0 +1,111 @@
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX = 10010;
struct node
{
char word[110];
int lenword;
}dic[MAX];
char qu[1010];
int Judge(char *a, char *b, int len)
{
int i= 0, count = 0, j = 0;
while (i < len && j < len - 1)
{
if (a[i] == b[j])
{
i ++;
j ++;
}
else
{
count ++;
i ++;
}
}
if (j == len - 1 && count == 0)
{
return 1;
}
if (count == 1)
{
return 1;
}
return 0;
}
int main()
{
int n, i, m, j, lenqu, flag, k, count;
scanf("%d", &n);
for (i = 0; i < n; ++i)
{
scanf("%s", dic[i].word);
dic[i].lenword = strlen(dic[i].word);
}
scanf("%d", &m);
for (i = 0; i < m; ++i)
{
flag = 0;
scanf("%s", qu);
lenqu = strlen(qu);
for (j = 0; j < n; ++j)
{
if (strcmp (dic[j].word, qu) == 0)
{
flag = 1;
printf("%s is correct\n", qu);
break;
}
}
if (!flag)
{
for (j = 0; j < n; ++j)
{
if (dic[j].lenword == lenqu)
{
int com[110] = {0};
count = 0;
for (k = 0; k < lenqu; ++k)
{
if (dic[j].word[k] != qu[k])
{
count ++;
com[count] = k;
}
}
if (count == 1)
{
flag = 1;
}
if (count == 2)
{
if (dic[j].word[com[1]] == qu[com[2]] && dic[j].word[com[2]] == qu[com[1]] && com[2] - com[1] == 1)
{
flag = 1;
}
}
}
else if (dic[j].lenword - 1 == lenqu)
{
flag = Judge(dic[j].word, qu, dic[j].lenword);
}
else if (dic[j].lenword + 1 == lenqu)
{
flag = Judge(qu, dic[j].word, lenqu);
}
if (flag)
{
printf("%s is a misspelling of %s\n", qu, dic[j].word);
break;
}
}
}
if (!flag)
{
printf("%s is unknown\n", qu);
}
}
return 0;
}

85
HDOJ/1484_autoAC.cpp Normal file
View File

@ -0,0 +1,85 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
const int MAX = 9,limit = 6,INF = 1000;
const int dirx[4]={0,-1,0,1},diry[4]={1,0,-1,0};
int map[MAX][MAX][4];
int dist[MAX][MAX],pre[MAX][MAX][2];
int sx,sy,ex,ey,pax,pay,pbx,pby;
stack<char> st;
void init(){
int i,j;
for(i=0;i<MAX;++i){
for(j=0;j<MAX;++j){
dist[i][j] = INF;
map[i][j][0] = map[i][j][1] = map[i][j][2] = map[i][j][3] = 0;
}
}
}
void dfs(int x,int y,int cnt){
int i,tx,ty;
for(i=0;i<4;++i){
if(map[x][y][i]==1)continue;
tx = x+dirx[i];
ty = y+diry[i];
if(tx<1 || ty<1 || tx>limit || ty>limit)continue;
if(cnt+1>dist[tx][ty])continue;
dist[tx][ty] = cnt;
pre[tx][ty][0] = x;
pre[tx][ty][1] = y;
dfs(tx,ty,cnt+1);
}
}
void Path(){
int px,py,x,y;
x = ex,y = ey;
px = pre[x][y][0];
py = pre[x][y][1];
while(1){
if(x==px){
if(py<y)st.push('E');
else st.push('W');
}else{
if(px<x)st.push('S');
else st.push('N');
}
if(px==sx && py==sy)break;
x = px;
y = py;
px = pre[x][y][0];
py = pre[x][y][1];
}
while(!st.empty()){
printf("%c",st.top());
st.pop();
}
printf("\n");
}
int main(){
int i,j;
while(scanf("%d %d",&sy,&sx)){
if(sx==0 && sy==0)break;
scanf("%d %d",&ey,&ex);
init();
for(i=0;i<3;++i){
scanf("%d %d %d %d",&pay,&pax,&pby,&pbx);
if(pax==pbx){
for(j=pay+1;j<=pby;++j){
map[pax][j][3] = 1;
map[pax+1][j][1] = 1;
}
}else{
for(j=pax+1;j<=pbx;++j){
map[j][pby][0] = 1;
map[j][pby+1][2] = 1;
}
}
}
dfs(sx,sy,0);
Path();
}
return 0;
}

64
HDOJ/1486_autoAC.cpp Normal file
View File

@ -0,0 +1,64 @@
#include <stdio.h>
#include <string.h>
char s[1000010];
int main()
{
int n;
while (scanf("%d",&n)&&n)
{
getchar();
gets(s);
int len=strlen(s);
int right=0;
int circle=0;
for (int i=0;i<len;i++)
{
if (s[i]!=' '&&(s[i]<'0'||s[i]>'9'))
{
int num=0;
char ope=s[i];
i++;
bool have=false;
for (;i<len;i++)
if (s[i]!=' ')
{
num=num*10+s[i]-'0';
have=true;
}
else break;
if (ope=='r')
{
if (!circle) right=(right+num)%n;
else right=((right-num)%n+n)%n;
}
else
{
if (num&1) circle=1-circle;
}
}
}
if (!circle)
{
int x1=right;
int x2=1+(n-right)%n+1;
if (x1<=x2)
{
if (x1!=0) printf("r%d",x1);
}
else printf("m1 r%d m1",x2-2);
}
else
{
int x1=right+1;
int x2=1+(n-right)%n;
if (x1<=x2)
{
if (x1-1!=0) printf("r%d m1",x1-1);
else printf("m1");
}
else printf("m1 r%d",x2-1);
}
printf("\n");
}
return 0;
}

115
HDOJ/1487_autoAC.cpp Normal file
View File

@ -0,0 +1,115 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <sstream>
#include <fstream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <algorithm>
#include <iomanip>
#define inf 0x3f3f3f3f
#define maxn 1005
using namespace std;
struct node
{
int mm,dd,hh,MM;
} ttt[maxn];
char str[1005];
int f[maxn][maxn],t[maxn],n;
bool flag[maxn];
int cmp(node t1,node t2) //t1<t2 -1; t1==t2 0; t1>t2 1;
{
if(t1.mm!=t2.mm)
{
if(t1.mm<t2.mm) return -1;
else return 1;
}
if(t1.dd!=t2.dd)
{
if(t1.dd<t2.dd) return -1;
else return 1;
}
if(t1.hh!=t2.hh)
{
if(t1.hh<t2.hh) return -1;
else return 1;
}
if(t1.MM!=t2.MM)
{
if(t1.MM<t2.MM) return -1;
else return 1;
}
return 0;
}
int main()
{
char op;
while(scanf("%d",&n)!=EOF&&n!=0)
{
memset(flag,0,sizeof(flag));
for(int i=n; i>=1; i--)
{
scanf("%d:%d:%d:%d %s %c",&ttt[i].mm,&ttt[i].dd,&ttt[i].hh,&ttt[i].MM,str,&op);
if(op=='+') flag[i]=1;
}
t[1]=0;
for(int i=2; i<=n; i++)
{
t[i]=t[i-1];
if(cmp(ttt[i],ttt[i-1])>=0 ) t[i]++;
}
f[1][1]=1;
if(flag[1]) f[1][0]=inf;
else f[1][0]=0;
for(int i=2; i<=n; i++) f[1][i]=inf;
for(int i=2; i<=n; i++)
{
for(int j=1; j<=n; j++) f[i][j]=inf;
if(!flag[i]) f[i][0]=f[i-1][0];
else f[i][0]=inf;
if(flag[i])
{
if(t[i]==0) f[i][i]=min(f[i][i],f[i-1][0]+1);
for(int j=1; j<i; j++)
if(f[i-1][j]<inf)
{
int tt;
tt=t[i];
if(cmp(ttt[i],ttt[j])>=0) tt--;
if(tt==t[j]) //zhengque queding nianfen
{
f[i][i]=min(f[i-1][j]+1,f[i][i]);
}
}
}
else
{
for(int j=1; j<=n; j++) f[i][j]=min(f[i][j],f[i-1][j]);
if(t[i]==0) f[i][i]=min(f[i][i],f[i-1][0]+1);
for(int j=1; j<i; j++)
if(f[i-1][j]<inf)
{
int tt;
tt=t[i];
if(cmp(ttt[i],ttt[j])>=0) tt--;
if(tt==t[j]) //zhengque queding nianfen
{
f[i][i]=min(f[i-1][j]+1,f[i][i]);
}
}
}
}
int res;
res=inf;
for(int i=1; i<=n; i++)
res=min(res,f[n][i]);
res=min(res,n);
printf("%d\n",res);
}
return 0;
}

65
HDOJ/1488_autoAC.cpp Normal file
View File

@ -0,0 +1,65 @@
#include <stdio.h>
#include <string.h>
#include <vector>
#include <set>
using namespace std;
typedef struct
{
int v,step;
} node;
long long MOD1=10007;
long long MOD2=10000013;
vector <node> use[10010];
char s[10010];
int main()
{
long long n;
while (scanf("%lld",&n)&&n)
{
long long a,b;
scanf("%lld%lld",&a,&b);
bool stop=0;
for (int i=0;i<10010;i++)
use[i].clear();
node temp;
temp.v=0;
temp.step=1;
use[0].push_back(temp);
long long pre=0;
int step=1;
while (!stop)
{
step++;
long long now=(((a*pre)%n*pre)%n+b)%n;
long long x1=now%MOD1;
long long x2=now%MOD2;
int len=use[x1].size();
bool find=false;
int stepp=0;
for (int i=0;i<len;i++)
{
if (use[x1][i].v==x2)
{
find=true;
stepp=use[x1][i].step;
}
}
if (find)
{
long long total=step-stepp;
stop=1;
printf("%lld\n",n-total);
break;
}
else
{
node temp;
temp.v=x2;
temp.step=step;
use[x1].push_back(temp);
}
pre=now;
}
}
return 0;
}

41
HDOJ/1489_autoAC.cpp Normal file
View File

@ -0,0 +1,41 @@
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<time.h>
using namespace std;
long long s[100005];
int main()
{
long long n,i,ans;
while(cin>>n)
{
if(n==0)
{
break;
}
for(i=0;i<n;i++)
{
cin>>s[i];
}
ans=0;
for(i=0;i<n-1;i++)
{
if(s[i]>0)
{
ans+=s[i];
s[i+1]+=s[i];
}
else
{
ans-=s[i];
s[i+1]+=s[i];
}
}
cout<<ans<<endl;
}
return 0;
}

64
HDOJ/1490_autoAC.cpp Normal file
View File

@ -0,0 +1,64 @@
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MEM(a,b) memset((a),(b),sizeof(a))
const LL INF = 1000000007;
const int N = 1e3+10;
const double eps = 1e-12;
int a[N][N];
int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
if (n == 0) break;
int flag = 1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%d", &a[i][j]);
}
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i][i] + a[j][j] != a[i][j] + a[j][i])
{
flag = 0;
break;
}
}
if (flag == 0) break;
}
if (flag) puts("homogeneous");
else puts("not homogeneous");
}
return 0;
}

60
HDOJ/1491_autoAC.cpp Normal file
View File

@ -0,0 +1,60 @@
#include <stdio.h>
int main ()
{
int n,month,day;
scanf ("%d",&n);
while (n--)
{
scanf ("%d%d",&month,&day);
switch(month)
{
case 10 :
if (day<21)
{
printf ("%d\n",21-day);
break;
}
else if(day==21)
{
printf ("It's today!!\n");
break;
}
else
{
printf("What a pity, it has passed!\n");
break;
}
case 9 :
printf ("%d\n",30+21-day);
break;
case 8 :
printf ("%d\n",31+30+21-day);
break;
case 7:
printf ("%d\n",31+31+30+21-day);
break;
case 6 :
printf ("%d\n",30+31+31+30+21-day);
break;
case 5:
printf ("%d\n",31+30+31+31+30+21-day);
break;
case 4:
printf ("%d\n",30+31+30+31+31+30+21-day);
break;
case 3:
printf ("%d\n",31+30+31+30+31+31+30+21-day);
break;
case 2:
printf ("%d\n",28+31+30+31+30+31+31+30+21-day);
break;
case 1:
printf ("%d\n",31+28+31+30+31+30+31+31+30+21-day);
break;
default:
printf ("What a pity, it has passed!\n");
break;
}
}
return 0;
}

20
HDOJ/1492_autoAC.cpp Normal file
View File

@ -0,0 +1,20 @@
#include<stdio.h>
int main()
{
__int64 n;
__int64 a[]={2,3,5,7};
while(scanf("%I64d",&n),n)
{
__int64 b[4]={1,1,1,1};
for(int i=0;i<4;i++)
{
while(n%a[i]==0)
{
b[i]++;
n=n/a[i];
}
}
printf("%I64d\n",b[0]*b[1]*b[2]*b[3]);
}
return 0;
}

66
HDOJ/1493_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include <stdio.h>
#include <string.h>
double p[7],f[11][61];
void dp(){
int i,j,k;
double ans;
memset(f,0,sizeof(f));
f[0][0]=1.0;
for (k=1;k<=10;k++)
for (j=60;j>=k;j--)
for (i=1;i<=6;i++)
if (j>=i)
f[k][j]+=f[k-1][j-i]*p[i];
ans=0;
for (i=1;i<=10;i++)
ans+=f[i][5];
printf("5: %.1lf%%\n",ans*100);
ans=0;
for (i=1;i<=10;i++)
ans+=f[i][12];
printf("12: %.1lf%%\n",ans*100);
ans=0;
for (i=1;i<=10;i++)
ans+=f[i][22];
printf("22: %.1lf%%\n",ans*100);
ans=0;
for (i=1;i<=10;i++)
ans+=f[i][29];
printf("29: %.1lf%%\n",ans*100);
ans=0;
for (i=1;i<=10;i++)
ans+=f[i][33];
printf("33: %.1lf%%\n",ans*100);
ans=0;
for (i=1;i<=10;i++)
ans+=f[i][38];
printf("38: %.1lf%%\n",ans*100);
ans=0;
for (i=1;i<=10;i++)
ans+=f[i][42];
printf("42: %.1lf%%\n",ans*100);
ans=0;
for (i=1;i<=10;i++)
ans+=f[i][46];
printf("46: %.1lf%%\n",ans*100);
ans=0;
for (i=1;i<=10;i++)
ans+=f[i][50];
printf("50: %.1lf%%\n",ans*100);
ans=0;
for (i=1;i<=10;i++)
ans+=f[i][55];
printf("55: %.1lf%%\n",ans*100);
}
int main(){
int t;
scanf("%d",&t);
while (t--){
for (int i=1;i<=6;i++)
scanf("%lf",&p[i]);
dp();
if (t)
putchar('\n');
}
return 0;
}

61
HDOJ/1494_autoAC.cpp Normal file
View File

@ -0,0 +1,61 @@
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int dp[11000][16],a[110],b[110];
inline void fun(int &x,int y)
{
if(x == 0)x=y;
else x=min(x,y);
}
int main()
{
int len,n,i,j,k,ans;
while(~scanf("%d %d",&len,&n))
{
for(i=1;i<=len;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=len;i++)
{
scanf("%d",&b[i]);
}
memset(dp,0,sizeof(dp));
dp[1][1]=a[1];
for(i=0;i<n;i++)
{
for(j=1;j<=len;j++)
{
if(i == 0 && j == 1)continue;
for(k=0;k<15;k++)
{
if(dp[i*len+j-1][k])
{
if(k>=5 && k<14)
{
fun(dp[i*len+j][k+1],dp[i*len+j-1][k]+a[j]);
fun(dp[i*len+j][k-5],dp[i*len+j-1][k]+b[j]);
}
else if(k == 14)
{
fun(dp[i*len+j][10],dp[i*len+j-1][k]+a[j]);
fun(dp[i*len+j][k-5],dp[i*len+j-1][k]+b[j]);
}
else
{
fun(dp[i*len+j][k+1],dp[i*len+j-1][k]+a[j]);
}
}
}
}
}
ans=99999999;
for(i=0;i<15;i++)
{
if(dp[n*len][i] && dp[n*len][i]<ans)ans=dp[n*len][i];
}
printf("%d\n",ans);
}
return 0;
}

71
HDOJ/1495_autoAC.cpp Normal file
View File

@ -0,0 +1,71 @@
#include<stdio.h>
#include<queue>
#include<iostream>
using namespace std;
int mark[105][105][105];
int flag;
int a,b,c;
int maxx,minn;
struct dian
{
int map[3];
int step;
}st;
void bfs(dian st)
{
queue<dian> p;
mark[st.map[0]][st.map[1]][st.map[2]]=1;
p.push(st);
dian v,vn;
while(!p.empty())
{
vn=p.front();
p.pop();
if(vn.map[0]==a/2&&vn.map[1]==a/2)
{
flag=1;
printf("%d\n",vn.step);
return;
}
for(int i=1;i<=10;i++)
{
v.map[0]=vn.map[0];
v.map[1]=vn.map[1];
v.map[2]=vn.map[2];
v.step=vn.step+1;
if(i==1) {v.map[0]-=(maxx-v.map[1]);v.map[1]=maxx;}
else if(i==2){v.map[0]=0;v.map[1]+=vn.map[0];}
else if(i==3){v.map[0]-=(minn-v.map[2]);v.map[2]=minn;}
else if(i==4){v.map[0]=0;v.map[1]+=vn.map[0];}
else if(i==5){v.map[1]=0;v.map[0]+=vn.map[1];}
else if(i==6){v.map[1]=0;v.map[2]+=vn.map[1];}
else if(i==7){v.map[1]-=(minn-v.map[2]);v.map[2]=minn;}
else if(i==8){v.map[2]=0;v.map[1]+=vn.map[2];}
else if(i==9){v.map[2]=0;v.map[0]+=vn.map[2];}
else if(i==10){v.map[2]-=(maxx-v.map[1]);v.map[1]=maxx;}
if(v.map[0]>a||v.map[0]<0||v.map[1]>maxx||v.map[1]<0||v.map[2]>minn||v.map[2]<0) continue;
if(mark[v.map[0]][v.map[1]][v.map[2]]) continue;
mark[v.map[0]][v.map[1]][v.map[2]]=1;
p.push(v);
}
}
}
int main()
{
while(scanf("%d%d%d",&a,&b,&c)!=EOF)
{
if(a==0&&b==0&&c==0) break;
flag=0;
memset(mark,0,sizeof(mark));
if(b==c) {printf("1\n"); continue;}
if(a%2==1) {printf("NO\n"); continue;}
if(b>c) {maxx=b;minn=c;}
else {maxx=c;minn=b;}
st.map[0]=a;
st.map[1]=0;
st.map[2]=0;
st.step=0;
bfs(st);
if(!flag) printf("NO\n");
}
}

40
HDOJ/1496_autoAC.cpp Normal file
View File

@ -0,0 +1,40 @@
#include<stdio.h>
#include<algorithm>
using namespace std;
int f1[1000001];
int f2[1000001];
int A,B,C,D;
int i,j,ans,s;
int main()
{
while(~scanf("%d%d%d%d",&A,&B,&C,&D))
{
ans = 0;
if(A>0 && B>0 && C>0 && D>0 || A<0 && B<0 && C<0 && D < 0)
{
printf("0\n");
continue;
}
memset(f1,0,sizeof(f1));
memset(f2,0,sizeof(f2));
for(i=1;i<=100;i++)
{
for(j=1;j<=100;j++)
{
s = A*i*i + B*j*j;
if(s>=0) f1[s]++;
else f2[-s]++;
}
}
for(i=1;i<=100;i++)
{
for(j=1;j<=100;j++)
{
s = C*i*i + D*j*j;
if(s>0) ans += f2[s];
else ans += f1[-s];
}
}
printf("%d\n",ans*16);
}
}

110
HDOJ/1497_autoAC.cpp Normal file
View File

@ -0,0 +1,110 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
int jb[10];
int num;
}ui[1005];
int main()
{
int M,N,n;
int i;
int u,b,a[100010];
char ch;
while(scanf("%d%d",&M,&N)!=EOF)
{
memset(a,-1,sizeof(a));
memset(ui,0,sizeof(ui));
scanf("%d",&n);
while(n--)
{
getchar();
scanf("%c",&ch);
if(ch=='B')
{
scanf("%d %d",&u,&b);
if(a[b]!=-1)
{
printf("The book is not in the library now\n");
}
else
{
if(ui[u].num==9)
{
printf("You are not allowed to borrow any more\n");
}
else
{
printf("Borrow success\n");
ui[u].jb[ui[u].num]=b;
ui[u].num++;
a[b]=u;
}
}
}
else if(ch=='R')
{
int j;
scanf("%d",&b);
if(a[b]==-1)
{
printf("The book is already in the library\n");
}
else
{
int temp;
int num;
num=ui[a[b]].num;
for(j=0;j<num;j++)
{
if(ui[a[b]].jb[j]==b)
{
temp=j;
}
}
for(j=temp;j<num;j++)
{
ui[a[b]].jb[j]=ui[a[b]].jb[j+1];
}
ui[a[b]].num--;
a[b]=-1;
printf("Return success\n");
}
}
else if(ch=='Q')
{
scanf("%d",&u);
if(ui[u].num == 0)
{
printf("Empty\n");
}
else
{
int num=ui[u].num;
for(int i=0;i<num;++i)
{
for(int j=num-1;j>i;--j)
{
if(ui[u].jb[i] > ui[u].jb[j])
{
int t = ui[u].jb[i];
ui[u].jb[i]=ui[u].jb[j];
ui[u].jb[j] = t;
}
}
}
for(i=0;i<num;i++)
{
if( i!=0)
printf(" ");
printf("%d",ui[u].jb[i]);
}
printf("\n");
}
}
}
printf("\n");
}
return 0;
}

82
HDOJ/1498_autoAC.cpp Normal file
View File

@ -0,0 +1,82 @@
#include<stdio.h>
#include<string.h>
int tot,n,k,list[10005],mark_dfs[10005],mark_gx[20005],mark[105],map[205][205];
struct dian
{
int date,next ;
}cun[50005];
struct rose
{
int x[20005],y[20005];
int count;
}cun1[55];
void add(int a,int b)
{
cun[tot].date=b;
cun[tot].next=list[a];
list[a]=tot++;
}
int dfs(int x)
{
for(int i=list[x];i;i=cun[i].next)
{
int date=cun[i].date;
if(mark_dfs[date]) continue;
mark_dfs[date]=1;
if(mark_gx[date]==-1||dfs(mark_gx[date]))
{
mark_gx[date]=x;
return 1;
}
}
return 0;
}
int main()
{
int a;
while(scanf("%d%d",&n,&k)!=EOF)
{
if(n==0&&k==0) break;
memset(mark,0,sizeof(mark));
memset(cun1,0,sizeof(cun1));
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
scanf("%d",&a);
map[i][j]=a;
mark[a]=1;
cun1[a].x[cun1[a].count]=i;
cun1[a].y[cun1[a].count++]=j;
}
int flag=0;
for(int i=1;i<=50;i++)
{
if(mark[i])
{
tot=1;
memset(cun,0,sizeof(cun));
memset(list,0,sizeof(list));
for(int j=0;j<cun1[i].count;j++)
{
add(cun1[i].x[j],cun1[i].y[j]);
}
memset(mark_gx,255,sizeof(mark_gx));
int sum=0;
for(int j=1;j<=105;j++)
{
memset(mark_dfs,0,sizeof(mark_dfs));
sum+=dfs(j);
}
if(sum>k)
{
if(flag==0)
printf("%d",i);
else printf(" %d",i);
flag=1;
}
}
}
if(!flag) printf("-1");
printf("\n");
}
}