Powered By HC TECH : AutoACer Engine

1800-1899
pull/34/head
KiritoTRw 2016-08-17 15:13:51 +08:00 committed by GitHub
parent 997ec50f19
commit 492bdd03ea
79 changed files with 5427 additions and 0 deletions

24
HDOJ/1800_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define mem(a) memset(a, 0, sizeof(a))
using namespace std;
int n,f[3000+5];
int main()
{
while(~scanf("%d",&n))
{
int i;
for(i=0;i<n;i++)scanf("%d",&f[i]);
sort(f,f+n);
int max=1,count=1;
for(i=1;i<n;i++)
{
if(f[i]>f[i-1])count=1;
else { count++; max = max>count? max:count;}
}
printf("%d\n",max);
}
return 0;
}

69
HDOJ/1801_autoAC.cpp Normal file
View File

@ -0,0 +1,69 @@
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=110;
int map[maxn][maxn],color[maxn][maxn];
char s[maxn][maxn];
int n,m,r,c,ans;
void change()
{
int i,j,k,p;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(i+r<=n&&j+c<=m&&map[i][j]!=color[i][j])
{
for(k=i;k<i+r;k++)
{
for(p=j;p<j+c;p++)
if(color[k][p])
color[k][p]=0;
else
color[k][p]=1;
}
ans++;
}
}
}
}
int judge()
{
int i,j,k;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(map[i][j]!=color[i][j])
return 0;
}
}
return 1;
}
int main(void)
{
int i,j;
while(scanf("%d%d%d%d",&n,&m,&r,&c)!=EOF)
{
if(n==0&&m==0&&r==0&&c==0)
break;
memset(map,0,sizeof(map));
memset(color,0,sizeof(color));
memset(s,0,sizeof(s));
ans=0;
for(i=0;i<n;i++)
{
scanf("%s",s[i]);
for(j=0;j<m;j++)
{
map[i][j]=s[i][j]-'0';
}
}
change();
if(judge())
printf("%d\n",ans);
else
printf("-1\n");
}
return 0;
}

17
HDOJ/1802_autoAC.cpp Normal file
View File

@ -0,0 +1,17 @@
#include<cstdio>
#include<stdlib.h>
int main()
{
int n,m,c;
while(scanf("%d%d%d",&n,&m,&c),(m+n+c))
{
int p,q;
p=n-7;
q=m-7;
if((p*q)&1)
printf("%d\n",(p*q)/2+c);
else
printf("%d\n",(p*q)/2);
}
return 0;
}

24
HDOJ/1803_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <cstdlib>
#include <cmath>
#include <iostream>
double pi=3.1415926535897932;
using namespace std;
double MIN(double x,double y)
{
if(x-y>0) return y;
else return x;
}
int main()
{
double x,y;
while(scanf("%lf%lf",&x,&y)!=EOF)
{
if(x<1&&y<1) break;
double x1=x*x*(y-x/pi)/(4*pi);
double temp=MIN(y/(pi+1),x);
double y1=pi*temp*temp*x/4;
if(x1-y1>0) printf("%.3f\n",x1);
else printf("%.3lf\n",y1);
}
return 0;
}

72
HDOJ/1804_autoAC.cpp Normal file
View File

@ -0,0 +1,72 @@
#include<stdio.h>
#include<string.h>
#define MAX 100+10
int fun2(char *str);
int fun1(char *str);
int main()
{
int n1,n2;
char str1[MAX][MAX],str2[MAX][MAX],str3[MAX][MAX];
while(scanf("%d %d",&n1,&n2)>0)
{
memset(str3,'\0',sizeof(str3));
for(int i=0;i<n1;i++)
{
scanf("%s %s",str1[i],str2[i]);
}
for(int i=0;i<n2;i++)
{
scanf("%s",str3[i]);
}
for(int i=0;i<n2;i++)
{
int flag=1;
for(int j=0;j<n1;j++)
{
if(!strcmp(str1[j],str3[i]))
{
printf("%s\n",str2[j]);
flag=0;
break;
}
}
if(flag)
{
if(fun1(str3[i]))
{
int len=strlen(str3[i]);
str3[i][len-1]='i';
str3[i][len]='e';
str3[i][len+1]='s';
printf("%s\n",str3[i]);
}
else if(fun2(str3[i]))
{
strcat(str3[i],"es");
printf("%s\n",str3[i]);
}
else
{
strcat(str3[i],"s");
printf("%s\n",str3[i]);
}
}
}
}
}
int fun1(char *str)
{
int len=strlen(str);
if(str[len-1]=='y'&&str[len-2]!='e'&&str[len-2]!='i'&&str[len-2]!='o'&&str[len-2]!='u')
return 1;
else
return 0;
}
int fun2(char *str)
{
int len=strlen(str);
if(str[len-1]=='o'||str[len-1]=='s'||str[len-1]=='x'||(str[len-2]=='c'&&str[len-1]=='h')||(str[len-2]=='s'&&str[len-1]=='h'))
return 1;
else
return 0;
}

79
HDOJ/1805_autoAC.cpp Normal file
View File

@ -0,0 +1,79 @@
#include <iostream>
#include <string.h>
#include <stack>
#include <queue>
using namespace std;
class node
{
public:
node* left;
node* right;
char c;
node()
{
left = right = NULL;
}
~node()
{
if(left)
delete left;
if(right)
delete right;
}
};
void print(node* s)
{
char output[10001];
char* o = output + 10000;
*o = 0;
queue<node*> q;
q.push(s);
while(!q.empty())
{
node* temp = q.front();
q.pop();
o--;
*o = temp->c;
if(temp->left) q.push(temp->left);
if(temp->right) q.push(temp->right);
}
puts(o);
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
char input[10001];
scanf("%s",input);
int len = strlen(input);
stack<node*> s;
node* temp;
for(int i=0;i<len;i++)
{
if(input[i]>='a')
{
temp = new node;
temp->c = input[i];
s.push(temp);
}
else
{
node *left, *right;
right = s.top();
s.pop();
left = s.top();
s.pop();
temp = new node;
temp->c = input[i];
temp->left = left;
temp->right = right;
s.push(temp);
}
}
print(s.top());
delete s.top();
s.pop();
}
}

72
HDOJ/1806_autoAC.cpp Normal file
View File

@ -0,0 +1,72 @@
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int MAXN=100010;
int a[MAXN];
int b[MAXN];
int dp[MAXN][20];
void makeRMQ(int n,int b[])
{
for(int i=0;i<n;i++)
dp[i][0]=b[i];
for(int j=1;(1<<j)<=n;j++)
for(int i=0;i+(1<<j)-1<n;i++)
dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
int rmq(int s,int v)
{
int k=(int)(log(v-s+1.0)/log(2.0));
return max(dp[s][k],dp[v-(1<<k)+1][k]);
}
int bi_search(int s,int t)
{
int tmp=a[t];
int l=s;
int r=t;
int mid;
while(l<r)
{
mid=((l+r)>>1);
if(a[mid]>=tmp)r=mid;
else l=mid+1;
}
return r;
}
int main()
{
int n,q;
int s,t;
while(scanf("%d",&n),n)
{
scanf("%d",&q);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int tmp;
for(int i=n-1;i>=0;i--)
{
if(i==n-1)tmp=1;
else
{
if(a[i]==a[i+1])tmp++;
else tmp=1;
}
b[i]=tmp;
}
makeRMQ(n,b);
while(q--)
{
scanf("%d%d",&s,&t);
s--;
t--;
int temp=bi_search(s,t);
int ans=t-temp+1;
t=temp-1;
if(s>t)printf("%d\n",ans);
else printf("%d\n",max(ans,rmq(s,t)));
}
}
return 0;
}

38
HDOJ/1808_autoAC.cpp Normal file
View File

@ -0,0 +1,38 @@
#include<stdio.h>
#include<string.h>
int yushu[100010],d[100010];
int main()
{
int m,i,j,ans,c,n,sum;
while(scanf("%d%d",&c,&n)!=EOF)
{
if(c==0&&n==0)
break;
memset(yushu,0,sizeof(yushu));
for(i=1;i<=n;i++)
scanf("%d",&d[i]);
int st,ed;
sum=0;
for(i=1;i<=n;i++)
{
sum=(sum+d[i])%c;
if(sum==0)
{
st=1;
ed=i;
break;
}
else if(yushu[sum])
{
st=yushu[sum]+1;
ed=i;
break;
}
else
yushu[sum]=i;
}
for(i=st;i<=ed;i++)
printf(i==ed?"%d\n":"%d ",i);
}
return 0;
}

98
HDOJ/1809_autoAC.cpp Normal file
View File

@ -0,0 +1,98 @@
#include<stdio.h>
#include<string.h>
#define HASH 10007
#define MAXD 10010
#define MAXN 55
typedef long long LL;
int S, N, M, sg[MAXD], g[MAXN][MAXN];
struct HashMap
{
int head[HASH], size, next[MAXD];
LL st[MAXD];
void init()
{
memset(head, -1, sizeof(head)), size = 0;
}
int find(LL _st)
{
int i, h = _st % HASH;
for(i = head[h]; i != -1; i = next[i])
if(st[i] == _st) break;
return i;
}
void push(LL _st)
{
int h = _st % HASH;
st[size] = _st, sg[size] = -1;
next[size] = head[h], head[h] = size ++;
}
}hm;
LL encode(int code[][MAXN])
{
int i, j;
LL ans = 0;
for(i = 0; i < N; i ++)
for(j = 0; j < M; j ++) ans = ans << 1 | code[i][j];
return ans;
}
void decode(int code[][MAXN], LL st)
{
int i, j;
for(i = N - 1; i >= 0; i --)
for(j = M - 1; j >= 0; j --) code[i][j] = st & 1, st >>= 1;
}
void init()
{
char b[MAXN];
int i, j;
scanf("%d%d", &N, &M);
for(i = 0; i < N; i ++)
{
scanf("%s", b);
for(j = 0; j < M; j ++) g[i][j] = b[j] - '0';
}
}
int dfs(int cur, LL st)
{
int i, j, k, g[MAXN][MAXN], h[MAXN];
decode(g, st);
memset(h, 0, sizeof(h));
for(i = 0; i < N - 1; i ++)
for(j = 0; j < M - 1; j ++)
if(!g[i][j] && !g[i + 1][j] && !g[i][j + 1] && !g[i + 1][j + 1])
{
g[i][j] = g[i + 1][j] = g[i][j + 1] = g[i + 1][j + 1] = 1;
LL c = encode(g);
k = hm.find(c);
if(k != -1) h[sg[k]] = 1;
else
{
hm.push(c), k = hm.find(c);
h[dfs(k, c)] = 1;
}
g[i][j] = g[i + 1][j] = g[i][j + 1] = g[i + 1][j + 1] = 0;
}
for(i = 0; h[i]; i ++);
return sg[cur] = i;
}
void solve()
{
int i, j, k, ans = 0, t;
for(t = 0; t < S; t ++)
{
init(), hm.init();
LL c = encode(g);
hm.push(c), k = hm.find(c);
dfs(k, c);
ans ^= sg[k];
}
printf("%s\n", ans ? "Yes" : "No");
}
int main()
{
while(scanf("%d", &S) == 1)
{
solve();
}
return 0;
}

95
HDOJ/1811_autoAC.cpp Normal file
View File

@ -0,0 +1,95 @@
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
int n,m;
struct node
{
int x;
int y;
char l;
}a[20005];
int c[10005];
int in[10005];
vector<int> ch[10005];
queue<int> q;
void init()
{
int i;
for(i=0;i<n;i++){
c[i] = i;
ch[i].clear();
in[i]=0;
}
while(!q.empty()){
q.pop();
}
}
int find(int x)
{
return x == c[x] ? x : c[x] = find(c[x]);
}
int main()
{
int i,j;
while(~scanf("%d%d",&n,&m)){
init();
int sum = n;
for(i=0;i<m;i++){
scanf("%d %c %d",&a[i].x,&a[i].l,&a[i].y);
if(a[i].l=='='){
int xx = find(a[i].x);
int yy = find(a[i].y);
if(xx!=yy){
c[xx] = yy;
}
sum--;
}
}
int flag = 0;
for(i=0;i<m;i++){
if(a[i].l == '=') continue;
int xx = find(a[i].x);
int yy = find(a[i].y);
if(xx==yy) flag=1;
if(a[i].l == '>'&&!flag){
ch[xx].push_back(yy);
in[yy]++;
}
else if(a[i].l == '<'&&!flag){
ch[yy].push_back(xx);
in[xx]++;
}
}
for(i=0;i<n;i++){
if(in[i]==0&&find(i)==i){
q.push(i);
}
}
if(flag==0){
while(!q.empty()){
if(q.size()>1)
flag=2;
int t = q.front();
q.pop();
for(i=0;i<ch[t].size();i++){
in[ch[t][i]]--;
if(!in[ch[t][i]])
q.push(ch[t][i]);
}
}
for(i=0;i<n;i++){
if(in[i]!=0){
flag = 1;
}
}
}
if(!flag) puts("OK");
else if(flag == 1) puts("CONFLICT");
else puts("UNCERTAIN");
}
return 0;
}

145
HDOJ/1812_autoAC.cpp Normal file
View File

@ -0,0 +1,145 @@
#include<iostream>
#include<iomanip>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int n,c[1005];
long long ans[405],tot[405],g[405];
void chenggao(long long a[],long long b[])
{ int i,j;
memset(g,0,sizeof(g));
g[0]=a[0]+b[0];
for(i=1;i<=a[0];i++)
for(j=1;j<=b[0];j++)
g[i+j-1]+=a[i]*b[j];
for(i=1;i<=g[0];i++)
{ g[i+1]+=g[i]/10000;
g[i]=g[i]%10000;
}
while(g[g[0]]==0&&g[0]>0)g[0]--;
for(i=0;i<=g[0];i++)
a[i]=g[i];
}
void jia(long long a[],long long b[])
{ int i;
a[0]=max(a[0],b[0]);
for(i=1;i<=a[0];i++)
a[i]=a[i]+b[i];
for(i=1;i<=a[0];i++)
{ a[i+1]+=a[i]/10000;
a[i]=a[i]%10000;
}
while(a[a[0]+1]!=0)
{ a[0]++;
a[a[0]+1]=a[a[0]]/10000;
a[a[0]]=a[a[0]]%10000;
}
}
void chudan(long long a[],int x)
{ long long d=0;
int i;
for(i=a[0];i>=1;i--)
{ d=d*10000+a[i];
a[i]=d/x;
d=d%x;
}
while(a[a[0]]==0&&a[0]>0)a[0]--;
}
void chengdan(long long a[],int x)
{ int i;
for(i=1;i<=a[0];i++)
a[i]=a[i]*x;
for(i=1;i<=a[0];i++)
{ a[i+1]+=a[i]/10000;
a[i]=a[i]%10000;
}
while(a[a[0]+1]!=0)
{ a[0]++;
a[a[0]+1]=a[a[0]]/10000;
a[a[0]]=a[a[0]]%10000;
}
}
void print(long long a[])
{ int i;
printf("%lld",a[a[0]]);
for(i=a[0]-1;i>=1;i--)
{ printf("%lld",a[i]/1000);
printf("%lld",a[i]/100%10);
printf("%lld",a[i]/10%10);
printf("%lld",a[i]%10);
}
printf("\n");
}
void Mulpow(int x,int tmp)
{ int i;
memset(tot,0,sizeof(tot));
c[0]=0;
while(tmp>0)
{ c[0]++;
c[c[0]]=tmp%2;
tmp=tmp/2;
}
tot[0]=1;
tot[1]=x;
for(i=c[0]-1;i>=1;i--)
{ chenggao(tot,tot);
if(c[i]==1)
chengdan(tot,x);
}
}
int main()
{
int p,x,y,i,tmp,N;
while(scanf("%d%d",&n,&p)!=EOF)
{ N=n*n;
memset(ans,0,sizeof(ans));
if(n==1)
{ printf("%d\n",p);
continue;
}
if(n%2==0)
{ Mulpow(p,N);//0
jia(ans,tot);
Mulpow(p,N/4);//90
jia(ans,tot);
Mulpow(p,N/2);//180
jia(ans,tot);
Mulpow(p,N/4);//270
jia(ans,tot);
Mulpow(p,N/2);
jia(ans,tot);
Mulpow(p,N/2);
jia(ans,tot);
Mulpow(p,n+(N-n)/2);
jia(ans,tot);
Mulpow(p,n+(N-n)/2);
jia(ans,tot);
y=8;
}
else
{ Mulpow(p,N);//0
jia(ans,tot);
Mulpow(p,1+(N-1)/4);//90
jia(ans,tot);
Mulpow(p,1+(N-1)/2);//180
jia(ans,tot);
Mulpow(p,1+(N-1)/4);//270
jia(ans,tot);
Mulpow(p,n+(N-n)/2);
jia(ans,tot);
Mulpow(p,n+(N-n)/2);
jia(ans,tot);
Mulpow(p,n+(N-n)/2);
jia(ans,tot);
Mulpow(p,n+(N-n)/2);
jia(ans,tot);
y=8;
}
chudan(ans,y);
print(ans);
}
return 0;
}

127
HDOJ/1813_autoAC.cpp Normal file
View File

@ -0,0 +1,127 @@
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
const int inf = 1000000000;
struct node
{
int x,y;
};
char str[4][10] = {"east","north","south","west"};
char map[9][9];
int to[4][2] = {0,1,-1,0,1,0,0,-1};
int L,n,dis[9][9],step,path[1000];
queue<node> Q;
int onedge(int i,int j)
{
return i==0 || j == 0 || i == n-1 || j == n-1;
}
int ismap(int i,int j)
{
return i>=0 && i<n && j>=0 && j<n;
}
void bfs()
{
int i;
node a,next;
while(!Q.empty())
{
a = Q.front();
Q.pop();
for(i = 0; i<4; i++)
{
next.x=a.x+to[i][0];
next.y=a.y+to[i][1];
if(!ismap(next.x,next.y)) continue;
if(!map[next.x][next.y]) continue;
if(dis[next.x][next.y]>dis[a.x][a.y]+1)
{
dis[next.x][next.y]=dis[a.x][a.y]+1;
Q.push(next);
}
}
}
}
void init()
{
int i,j,k;
node a;
while(!Q.empty()) Q.pop();
for(i = 0; i<n; i++)
{
for(j = 0; j<n; j++)
{
dis[i][j] = inf;
map[i][j] = map[i][j]=='1'?0:1;
if(!map[i][j]) continue;
if(onedge(i,j))
{
a.x = i;
a.y = j;
dis[i][j] = 0;
Q.push(a);
}
}
}
bfs();
}
int get_h(char mat[9][9])
{
int i,j,k,maxn = 0;
for(i = 0; i<n; i++)
{
for(j = 0; j<n; j++)
{
if(mat[i][j])
maxn = max(maxn,dis[i][j]);
}
}
return maxn;
}
int IDA(char mat[9][9],int cnt)
{
if(cnt+get_h(mat)>step) return 0;
if(cnt == step) return 1;
int i,x,y,tx,ty;
char tem[9][9];
for(i = 0; i<4; i++)
{
memset(tem,0,sizeof(tem));
for(x = 0; x<n; x++)
{
for(y = 0; y<n; y++)
{
if(onedge(x,y) || !mat[x][y]) continue;
tx = x+to[i][0];
ty = y+to[i][1];
if(!map[tx][ty]) tem[x][y] = 1;
else tem[tx][ty] = 1;
}
}
path[cnt] = i;
if(IDA(tem,cnt+1)) return 1;
}
return 0;
}
int main()
{
int i,j,k,flag = 0;
while(~scanf("%d",&n))
{
if(flag++)
printf("\n");
for(i = 0; i<n; i++)
scanf("%s",map[i]);
init();
step = 0;
while(1)
{
if(IDA(map,0))break;
step++;
}
for(i = 0; i<step; i++)
printf("%s\n",str[path[i]]);
}
return 0;
}

218
HDOJ/1814_autoAC.cpp Normal file
View File

@ -0,0 +1,218 @@
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<iostream>
using namespace std;
const int MAXN=16010;
const int MAXM=100000;
struct Node
{
int a,b,pre,next;
}E[MAXM],E2[MAXM];
int _n,n,m;
int V[MAXN],ST[MAXN][2],Q[MAXN],Q2[MAXN],vst[MAXN];
bool res_ex;
void init_d()
{
for(int i=0;i<n;i++)
E[i].a=E[i].pre=E[i].next=E2[i].a=E2[i].pre=E2[i].next=i;
m=n;
}
void add_edge(int a,int b)
{
E[m].a=a;E[m].b=b;E[m].pre=E[a].pre;E[m].next=a;E[a].pre=m;E[E[m].pre].next=m;
E2[m].a=b;E2[m].b=a;E2[m].pre=E2[b].pre;E2[m].next=b;E2[b].pre=m;E2[E2[m].pre].next=m;
m++;
}
void solve()
{
for(int i=0;i<n;i++)
{
V[i]=0;
vst[i]=0;
}
res_ex=1;
int i,i1,i2,j,k,front,rear,front2,rear2;
int len;
bool ff;
for(int _i=0;_i<_n;_i++)
{
if(V[_i<<1]==1||V[(_i<<1)+1]==1)continue;
i=_i<<1;len=0;
if(!V[i])
{
ST[len][0]=i;
ST[len++][1]=1;
if(!V[i^1])
{
ST[len][0]=i^1;
ST[len++][1]=2;
}
Q[front=rear=0]=i;
vst[i]=i1=n+i;
Q2[front2=rear2=0]=i^1;
vst[i^1]=i2=(n<<1)+i;
ff=1;
for(;front<=rear;front++)
{
j=Q[front];
for(int p=E[j].next;p!=j;p=E[p].next)
{
k=E[p].b;
if(V[k]==2||vst[k]==i2||V[k^1]==1||vst[k^1]==i1)
{ff=0;break;}
if(vst[k]!=i1)
{
Q[++rear]=k;vst[k]=i1;
if(!V[k])
{
ST[len][0]=k;
ST[len++][1]=1;
}
}
if(vst[k^1]!=i2)
{
Q2[++rear2]=k^1;vst[k^1]=i2;
if(!V[k])
{
ST[len][0]=k^1;
ST[len++][1]=2;
}
}
}
if(!ff)break;
}
if(ff)
{
for(;front2<=rear2;front2++)
{
j=Q2[front2];
for(int p=E2[j].next;p!=j;p=E2[p].next)
{
k=E2[p].b;
if(V[k]==1||vst[k]==i1)
{ff=0;break;}
if(vst[k]!=i2)
{
vst[k]=i2;Q2[++rear]=k;
if(!V[k])
{
ST[len][0]=k;
ST[len++][1]=2;
}
}
}
if(!ff)break;
}
if(ff)
{
for(int j=0;j<len;j++)V[ST[j][0]]=ST[j][1];
continue;
}
}
}
i=(_i<<1)+1;len=0;
if(!V[i])
{
ST[len][0]=i;
ST[len++][1]=1;
if(!V[i^1])
{
ST[len][0]=i^1;
ST[len++][1]=2;
}
Q[front=rear=0]=i;
vst[i]=i1=n+i;
Q2[front2=rear2=0]=i^1;
vst[i^1]=i2=(n<<1)+i;
ff=1;
for(;front<=rear;front++)
{
j=Q[front];
for(int p=E[j].next;p!=j;p=E[p].next)
{
k=E[p].b;
if(V[k]==2||vst[k]==i2||V[k^1]==1||vst[k^1]==i1)
{ff=0;break;}
if(vst[k]!=i1)
{
Q[++rear]=k;vst[k]=i1;
if(!V[k])
{
ST[len][0]=k;
ST[len++][1]=1;
}
}
if(vst[k^1]!=i2)
{
Q2[++rear2]=k^1;vst[k^1]=i2;
if(!V[k])
{
ST[len][0]=k^1;
ST[len++][1]=2;
}
}
}
if(!ff)break;
}
if(ff)
{
for(;front2<=rear2;front2++)
{
j=Q2[front2];
for(int p=E2[j].next;p!=j;p=E2[p].next)
{
k=E2[p].b;
if(V[k]==1||vst[k]==i1)
{ff=0;break;}
if(vst[k]!=i2)
{
vst[k]=i2;Q2[++rear]=k;
if(!V[k])
{
ST[len][0]=k;
ST[len++][1]=2;
}
}
}
if(!ff)break;
}
if(ff)
{
for(int j=0;j<len;j++)V[ST[j][0]]=ST[j][1];
continue;
}
}
}
if(V[_i<<1]+V[(_i<<1)+1]!=3){res_ex=0;break;}
}
}
int main()
{
int M;
int x,y;
while(scanf("%d%d",&_n,&M)!=EOF)
{
n=_n<<1;
init_d();
while(M--)
{
scanf("%d%d",&x,&y);
x--;
y--;
if(x!=(y^1))
{
add_edge(x,y^1);
add_edge(y,x^1);
}
}
solve();
if(res_ex)
{
for(int i=0;i<n;i++)
if(V[i]==1)printf("%d\n",i+1);
}
else printf("NIE\n");
}
return 0;
}

134
HDOJ/1815_autoAC.cpp Normal file
View File

@ -0,0 +1,134 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
#define MAXN 1111
struct Edge{
int v,Next;
}edge[MAXN*MAXN*11];
int N,A,B,NE;
int head[MAXN];
void Insert(int u,int v)
{
edge[NE].v=v;
edge[NE].Next=head[u];
head[u]=NE++;
}
int cnt,_count;
int low[MAXN],dfn[MAXN];
int color[MAXN];
bool mark[MAXN];
stack<int>S;
void Tarjan(int u)
{
low[u]=dfn[u]=++cnt;
mark[u]=true;
S.push(u);
for(int i=head[u];i!=-1;i=edge[i].Next){
int v=edge[i].v;
if(dfn[v]==0){
Tarjan(v);
low[u]=min(low[u],low[v]);
}else if(mark[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
int v;
_count++;
do{
v=S.top();
S.pop();
mark[v]=false;
color[v]=_count;
}while(u!=v);
}
}
struct Point{
int x,y;
}pp[MAXN],hate[MAXN],like[MAXN],s1,s2;
int dist[MAXN][MAXN];
inline int Get_dist(Point p,Point q)
{
return abs(p.x-q.x)+abs(p.y-q.y);
}
void Build(int limit)
{
NE=0;
memset(head,-1,(2*N+4)*sizeof(int));
for(int i=1;i<=A;i++){
Insert(hate[i].x,hate[i].y+N);
Insert(hate[i].y,hate[i].x+N);
Insert(hate[i].x+N,hate[i].y);
Insert(hate[i].y+N,hate[i].x);
}
for(int i=1;i<=B;i++){
Insert(like[i].x,like[i].y);
Insert(like[i].y+N,like[i].x+N);
Insert(like[i].x+N,like[i].y+N);
Insert(like[i].y,like[i].x);
}
for(int i=1;i<=N;i++){
for(int j=i+1;j<=N;j++){
if(dist[i][N+1]+dist[j][N+1]>limit){
Insert(i,j+N);
Insert(j,i+N);
}
if(dist[i][N+2]+dist[j][N+2]>limit){
Insert(i+N,j);
Insert(j+N,i);
}
if(dist[i][N+1]+dist[N+1][N+2]+dist[j][N+2]>limit){
Insert(i,j);
Insert(j+N,i+N);
}
if(dist[i][N+2]+dist[N+1][N+2]+dist[j][N+1]>limit){
Insert(i+N,j+N);
Insert(j,i);
}
}
}
}
bool Judge()
{
cnt=_count=0;
memset(mark,false,(2*N+4)*sizeof(bool));
memset(dfn,0,(2*N+4)*sizeof(int));
for(int i=1;i<=2*N;i++)if(dfn[i]==0)Tarjan(i);
for(int i=1;i<=N;i++){
if(color[i]==color[i+N])return false;
}
return true;
}
int main()
{
while(~scanf("%d%d%d",&N,&A,&B)){
scanf("%d%d%d%d",&s1.x,&s1.y,&s2.x,&s2.y);
dist[N+1][N+2]=Get_dist(s1,s2);
for(int i=1;i<=N;i++){
scanf("%d%d",&pp[i].x,&pp[i].y);
dist[i][N+1]=Get_dist(pp[i],s1);
dist[i][N+2]=Get_dist(pp[i],s2);
}
for(int i=1;i<=A;i++){
scanf("%d%d",&hate[i].x,&hate[i].y);
}
for(int i=1;i<=B;i++){
scanf("%d%d",&like[i].x,&like[i].y);
}
int low=0,high=44444444,mid,ans=-1;
while(low<=high){
mid=(low+high)>>1;
Build(mid);
if(Judge()){
ans=mid;
high=mid-1;
}else
low=mid+1;
}
printf("%d\n",ans);
}
return 0;
}

85
HDOJ/1816_autoAC.cpp Normal file
View File

@ -0,0 +1,85 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=2000+50;
int n,m;
int a[maxn],b[maxn];
int c[maxn],d[maxn];
struct TwoSAT
{
int n;
vector<int> G[maxn*2];
int S[maxn*2],c;
bool mark[maxn*2];
bool dfs(int x)
{
if(mark[x^1]) return false;
if(mark[x]) return true;
mark[x]=true;
S[c++]=x;
for(int i=0;i<G[x].size();i++)
if(!dfs(G[x][i])) return false;
return true;
}
void init(int n)
{
this->n=n;
for(int i=0;i<2*n;i++) G[i].clear();
memset(mark,0,sizeof(mark));
}
void add_clause(int x,int xval,int y,int yval)
{
x=x*2+xval;
y=y*2+yval;
G[x].push_back(y);
}
bool solve()
{
for(int i=0;i<2*n;i+=2)
if(!mark[i] && !mark[i+1])
{
c=0;
if(!dfs(i))
{
while(c>0) mark[S[--c]]=false;
if(!dfs(i+1)) return false;
}
}
return true;
}
}TS;
bool ok(int mid)
{
TS.init(n);
for(int i=0;i<n/2;i++)
{
TS.add_clause(a[i],0,b[i],1);
TS.add_clause(b[i],0,a[i],1);
}
for(int i=0;i<mid;i++)
{
TS.add_clause(c[i],1,d[i],0);
TS.add_clause(d[i],1,c[i],0);
}
return TS.solve();
}
int main()
{
while(scanf("%d%d",&n,&m)==2&&n)
{
n*=2;
for(int i=0;i<n/2;i++) scanf("%d%d",&a[i],&b[i]);
for(int i=0;i<m;i++) scanf("%d%d",&c[i],&d[i]);
int L=0, R=m;
while(R>L)
{
int mid = L+(R-L+1)/2;
if(ok(mid)) L=mid;
else R=mid-1;
}
printf("%d\n",L);
}
return 0;
}

39
HDOJ/1817_autoAC.cpp Normal file
View File

@ -0,0 +1,39 @@
#include <iostream>
using namespace std;
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
__int64 pow(int a,int b)
{
__int64 s=1;
for(int i=0;i<b;i++)
s*=a;
return s;
}
int main()
{
int n,i;
__int64 ans;
while(cin>>n)
{
ans=0;
if(n>=40)continue;
if(n<0)break;
if(n==0){ printf("0\n");continue; }
for(i=1;i<=n;i++)
ans+=pow(3,gcd(n,i));
if(n%2)
{
ans+=( pow(3,(n+1)/2) *n);
}
else
{
ans+=( pow(3,n/2+1) * n/2);
ans+=( pow(3,n/2) *n/2 );
}
ans/=(2*n);
printf("%I64d\n",ans);
}
return 0;
}

105
HDOJ/1818_autoAC.cpp Normal file
View File

@ -0,0 +1,105 @@
#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
int n,m;
int v[1100005];
struct node
{
int yes;
int no;
int kill;
int add;
int time;
}tr[105];
struct node1
{
int bug;
int step;
bool operator < (const node1 &a)const
{
return step > a.step;
}
};
priority_queue <node1> Q;
bool ok(int bug,node t)
{
if((t.no&bug)!=0)return false;
if((t.yes&bug)!=t.yes)return false;
return true;
}
int fix(int bug,node t)
{
int tmp=bug;
bug=((~t.kill)&bug);
bug=(t.add|bug);
if(tmp==bug)return -1;
return bug;
}
void bfs()
{
node1 w,e;
while(!Q.empty())
{
w=Q.top();
Q.pop();
if(w.bug==0){printf("Fastest sequence takes %d seconds.\n",w.step);return;}
//cout<<"----"<<endl;
for(int k=0;k<m;k++)
{
e=w;
if(ok(w.bug,tr[k]))
{
e.bug=fix(e.bug,tr[k]);
if(e.bug<0)continue;
e.step += tr[k].time;
if(v[e.bug]!=-1 && e.step >= v[e.bug])continue;
v[e.bug] = e.step;
Q.push(e);
}
}
}
printf("Bugs cannot be fixed.\n");
}
int main()
{
char a[200],b[200];
node1 w;
int CASE=1;
while(scanf("%d%d",&n,&m)!=EOF && n && m)
{
int g = 0;
g = (1 <<n) - 1;
memset(v,-1,sizeof(v));
memset(tr,0,sizeof(tr));
while(!Q.empty())Q.pop();
for(int i=0;i<m;i++)
{
scanf("%d %s %s",&tr[i].time,a,b);
{
for(int j=0;j<n;j++)
{
if(a[j]=='+')
tr[i].yes+=(1<<(n-j-1));
else if(a[j]=='-')
tr[i].no+=(1<<(n-j-1));
}
for(int j=0;j<n;j++)
{
if(b[j]=='+')
tr[i].add+=(1<<(n-j-1));
else if(b[j]=='-')
tr[i].kill+=(1<<(n-j-1));
}
}
}
w.bug = g;
w.step=0;
Q.push(w);
printf("Product %d\n",CASE++);
bfs();
putchar(10);
}
return 0;
}

49
HDOJ/1820_autoAC.cpp Normal file
View File

@ -0,0 +1,49 @@
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
long long bishops(int n, int k)
{
long long r1[9];
long long r2[9];
for (int i = 1; i <= n; ++i)
r1[i] = i % 2 != 0 ? i : r1[i-1];
for (int i = 1; i <= n-1; ++i)
r2[i] = i % 2 != 0 ? i+1 : r2[i-1];
long long c1[9][65] = {{0}};
long long c2[9][65] = {{0}};
for (int i = 0; i <= n; ++i)
c1[i][0] = 1;
for (int j = 1; j <= k; ++j)
c1[0][j] = 0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= k && j <= i; ++j)
c1[i][j] = c1[i-1][j] + c1[i-1][j-1]*(r1[i] - j + 1);
for (int i = 0; i <= n-1; ++i)
c2[i][0] = 1;
for (int j = 1; j <= k; ++j)
c2[0][j] = 0;
for (int i = 1; i <= n-1; ++i)
for (int j = 1; j <= k && j <= i; ++j)
c2[i][j] = c2[i-1][j] + c2[i-1][j-1]*(r2[i] - j + 1);
long long r = 0;
for (int i = 0; i <= k; ++i)
r += c1[n][i] * c2[n-1][k-i];
return r;
}
int main(int argc, char *argv[])
{
for (int n, k; cin >> n >> k && !(n == 0 && k == 0); )
cout << bishops(n, k) << endl;
return 0;
}

55
HDOJ/1822_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
using namespace std;
int map1[5000][5000],map2[5000][5000];
int m,n;
bool check(int x ,int y)
{
int i ,j,maxx = map2[y][x];
for(i = 1; i <=n; i++)
if(map2[i][x] > maxx) return false;
return true;
}
int main()
{
int T,i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
for(i = 1; i <= m; i++)
for(j = 1; j <= n; j++)
scanf("%d",&map1[i][j]);//cin>>map1[i][j];
for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++)
scanf("%d",&map2[i][j]);//cin>>map2[i][j];
int ans = 0,max1 = 0,max2 = 0;
for(i = 1; i <= n; i++)
{
int maxx = map1[1][i];
for(j = 2; j <= m; j++)
if(map1[j][i] > maxx) maxx = map1[j][i];
for(j = 1; j <= m; j++)
{
if(map1[j][i] == maxx)
{
if(check(j,i))
{
ans++;
max1 = map1[j][i];
max2 = map2[i][j];
}
}
}
}
if(ans == 1) printf("%d %d\n",max1,max2);//cout<<c[1].x<<" "<<c[1].y<<endl;
else printf("Have %d Nash Points.\n",ans);
}
return 0;
}

109
HDOJ/1823_autoAC.cpp Normal file
View File

@ -0,0 +1,109 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const double eps=1e-6;
const int maxn=2100;
int xL,xR,yL,yR,Value;
int Max[220<<2][maxn<<2];
int N,M;
int maxv;
void pushup(int xrt,int rt)
{
Max[xrt][rt]=max(Max[xrt][rt<<1],Max[xrt][rt<<1|1]);
}
void updateY(int xrt,int x,int l,int r,int rt)
{
if(l==r)
{
if(x==-1)
{
Max[xrt][rt]=max(Max[xrt<<1][rt],Max[xrt<<1|1][rt]);
}
else
{
Max[xrt][rt]=max(Max[xrt][rt],Value);
}
return ;
}
int m=(l+r)/2;
if(yL<=m) updateY(xrt,x,lson);
else updateY(xrt,x,rson);
pushup(xrt,rt);
}
void updateX(int l,int r,int rt)
{
if(l==r)
{
updateY(rt,l,1,M,1);
return ;
}
int m=(l+r)/2;
if(xL<=m) updateX(lson);
else updateX(rson);
updateY(rt,-1,1,M,1);
}
void queryY(int xrt,int l,int r,int rt)
{
if(yL<=l&&r<=yR)
{
maxv=max(maxv,Max[xrt][rt]);
return ;
}
int m=(l+r)/2;
if(yL<=m) queryY(xrt,lson);
if(yR>m) queryY(xrt,rson);
}
void queryX(int l,int r,int rt)
{
if(xL<=l&&r<=xR)
{
queryY(rt,1,M,1);
return ;
}
int m=(l+r)/2;
if(xL<=m) queryX(lson);
if(xR>m) queryX(rson);
}
int main()
{
int op;char cmd[20];
N=200;M=2100;
while(scanf("%d",&op)!=EOF&&op)
{
memset(Max,-1,sizeof(Max));
while(op--)
{
scanf("%s",cmd);
if(cmd[0]=='I')
{
int H;
double a,l;
scanf("%d%lf%lf",&H,&a,&l);
H-=99;
int A=(int)((a+eps)*10);
int L=(int)((l+eps)*10);
xL=H;yL=A;Value=L;
updateX(1,N,1);
}
else if(cmd[0]=='Q')
{
int H1,H2;
double a1,a2;
scanf("%d%d%lf%lf",&H1,&H2,&a1,&a2);
int A1=(int)((a1+eps)*10);
int A2=(int)((a2+eps)*10);
xL=min(H1,H2)-99,xR=max(H1,H2)-99;
yL=min(A1,A2),yR=max(A1,A2);
maxv=-(1<<30);
queryX(1,N,1);
if(maxv==-1) printf("-1\n");
else printf("%.1lf\n",maxv/10.);
}
}
}
return 0;
}

100
HDOJ/1824_autoAC.cpp Normal file
View File

@ -0,0 +1,100 @@
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
using namespace std;
const int MAX_N = 2 * 5000 + 10;
struct TwoSAT {
int n;
vector<int> G[2 * MAX_N];
bool mark[2 * MAX_N];
int S[2 * MAX_N], c;
void Init(int n)
{
this->n = n;
for (int i = 0; i <= 2 * n; ++i)
G[i].clear();
memset(mark, 0, sizeof(mark));
}
bool Dfs(int x)
{
if (mark[x ^ 1]) return false;
if (mark[x]) return true;
mark[x] = true;
S[c++] = x;
for (int i = 0; i < G[x].size(); ++i)
{
if (!Dfs(G[x][i]))
return false;
}
return true;
}
void AddClause(int x, int y)
{
int a = 2 * x;
int b = 2 * y;
G[a ^ 1].push_back(b);
G[b ^ 1].push_back(a);
}
void AddClauseTeam(int i, int j, int k)
{
int a = 2 * i;
int b = 2 * j;
int c = 2 * k;
G[a].push_back(b ^ 1);
G[a].push_back(c ^ 1);
G[b].push_back(a ^ 1);
G[b].push_back(c);
G[c].push_back(a ^ 1);
G[c].push_back(b);
G[a ^ 1].push_back(b);
G[a ^ 1].push_back(c);
G[b ^ 1].push_back(a);
G[b ^ 1].push_back(c ^ 1);
G[c ^ 1].push_back(a);
G[c ^ 1].push_back(b ^ 1);
}
bool Solve()
{
for (int i = 0; i < 2 * n; i += 2)
{
if (!mark[i] && !mark[i + 1])
{
c = 0;
if (!Dfs(i))
{
while (c > 0) mark[S[--c]] = false;
if (!Dfs(i + 1))
return false;
}
}
}
return true;
}
};
TwoSAT sat;
int main()
{
int n, m;
while (scanf("%d %d", &n, &m) != EOF)
{
int a, b, c;
sat.Init(3 * n);
for (int i = 0; i < n; ++i)
{
scanf("%d %d %d", &a, &b, &c);
sat.AddClauseTeam(a, b, c);
}
for (int i = 0; i < m; ++i)
{
scanf("%d %d", &a, &b);
sat.AddClause(a, b);
}
bool ok = sat.Solve();
if (ok)
printf("yes\n");
else
printf("no\n");
}
return 0;
}

113
HDOJ/1827_autoAC.cpp Normal file
View File

@ -0,0 +1,113 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
const int M = 1005;
const int N = 3005;
struct node {
int to;
int next;
}num[N];
int ins[M];
int belong[M];
int dfn[M];
int low[M];
int rdu[M];
int sccf[M];
int head[M];
int sum[M];
stack<int>s;
int index, cont;
int ans;
int T, n, m, e, e1;
void init() {
e = 0;
index = 1;
cont = 0;
e1 = 0;
memset(num, 0, sizeof(num));
memset(head, -1, sizeof(head));
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(sccf, 0, sizeof(sccf));
memset(rdu, 0, sizeof(rdu));
memset(belong, 0, sizeof(belong));
memset(sum, 0, sizeof(sum));
}
void add(int a, int b) {
num[e].to = b;
num[e].next = head[a];
head[a] = e++;
}
void Tanjian(int u) {
int v;
dfn[u] = low[u] = index++;
ins[u] = 1;
s.push(u);
for(int k = head[u]; k != -1; k = num[k].next) {
v = num[k].to;
if(!dfn[v]) {
Tanjian(v);
low[u] = min(low[u], low[v]);
}
else if(ins[v]) {
low[u] = min(low[u], low[v]);
}
}
if(low[u] == dfn[u]) {
cont++;
do {
v = s.top();
s.pop();
sccf[v] = cont;
belong[cont]++;
ins[v] = 0;
}while(u != v);
}
}
int main()
{
int a, b;
while(scanf("%d%d", &n, &m) != EOF) {
init();
for(int i = 1; i <= n; i++) {
scanf("%d", &sum[i]);
}
for(int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
add(a, b);
}
for(int i = 1; i <= n; i++) {
if(!dfn[i]) {
Tanjian(i);
}
}
int v;
ans = 0;
int conum = 0;
for(int i = 1; i <= n; i++) {
for(int k = head[i]; k != -1; k = num[k].next) {
v = num[k].to;
if(sccf[i] != sccf[v]) {
rdu[sccf[v]]++;
}
}
}
for(int i = 1; i <= cont; i++) {
if(rdu[i] == 0) {
conum++;
int minn = 99999999;
for(int j = 1; j <= n; j++) {
if(sccf[j] == i) {
minn = min(sum[j], minn);
}
}
ans += minn;
}
}
printf("%d %d\n", conum, ans);
}
return 0;
}

100
HDOJ/1828_autoAC.cpp Normal file
View File

@ -0,0 +1,100 @@
#include<algorithm>
#include<iostream>
#include<cstdio>
const int maxn=5010;
const int CONST=10001;
using namespace std;
struct Line
{
int l,r,h;
char lab;
}line[maxn*2];
struct node
{
int lc,rc,cnt,len,num;
char rn,ln;
}tree[maxn*2*4];
int n,pos[maxn*2],m,ans,tmpl,tmpr;
int abs(int a){return a>0?a:-a;}
bool cmp(Line a,Line b)
{
return a.h<b.h;
}
void built(int i,int l,int r)
{
tree[i].lc=l,tree[i].rc=r;
tree[i].len=tree[i].num=tree[i].rn=tree[i].ln=tree[i].cnt=0;
if(l==r-1)return;
int mid=(l+r)/2;
built(2*i,l,mid);
built(2*i+1,mid,r);
}
void update_len(int i)
{
if(tree[i].cnt)
{
tree[i].len=pos[tree[i].rc]-pos[tree[i].lc];
tree[i].num=1;
tree[i].ln=tree[i].rn=1;
}
else
{
tree[i].len=tree[2*i].len+tree[2*i+1].len;
tree[i].num=tree[2*i].num+tree[2*i+1].num;
if(tree[2*i].rn&&tree[2*i+1].ln)tree[i].num--;
tree[i].rn=tree[2*i+1].rn;
tree[i].ln=tree[2*i].ln;
}
}
void update(int i,char val)
{
if(pos[tree[i].lc]>tmpr||pos[tree[i].rc]<tmpl)return;
if(pos[tree[i].lc]>=tmpl&&pos[tree[i].rc]<=tmpr)
{
tree[i].cnt+=val;
update_len(i);
}
if(tree[i].lc==tree[i].rc-1)return;
update(2*i,val);
update(2*i+1,val);
update_len(i);
}
void scan()
{
int now=2*n;
int last=0,lh=line[2*n].h;
while(now)
{
ans+=tree[1].num*2*(lh-line[now].h);
lh=line[now].h;
tmpl=line[now].l,tmpr=line[now].r;
update(1,line[now].lab);
ans+=abs(tree[1].len-last);
last=tree[1].len;
--now;
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
int x1,y1,x2,y2;
for(int i=1;i<=n;i++)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
//x1+=CONST,x2+=CONST,y1+=CONST,y2+=CONST;
line[i].l=x1,line[i].r=x2,line[i].h=y1,line[i].lab=1;
line[n+i].l=x1,line[n+i].r=x2,line[n+i].h=y2,line[n+i].lab=-1;
pos[i]=x1,pos[n+i]=x2;
}
sort(line+1,line+1+2*n,cmp);
sort(pos+1,pos+1+2*n);
m=1;
ans=0;
for(int i=2;i<=2*n;i++)
if(pos[i]!=pos[i-1])pos[++m]=pos[i];
built(1,1,m);
scan();
printf("%d\n",ans);
}
}

62
HDOJ/1829_autoAC.cpp Normal file
View File

@ -0,0 +1,62 @@
#include<iostream>
using namespace std;
struct Pair
{
int from,conflict;
};
Pair *record;
int find(int x)
{
if(x==record[x].from)
return x;
return record[x].from=find(record[x].from);
}
void link(int a,int b)
{
int fa=find(a);
int fb=find(b);
if(fa!=fb)
record[fb].from=fa;
}
int main()
{
int t;
int cnt=1;
scanf("%d",&t);
while(t--)
{
int n,m;
bool flag=false;
scanf("%d %d",&n,&m);
record=new Pair[n+1];
for(int i=1;i<=n;i++)
{
record[i].from=i;
record[i].conflict=0;
}
int a,b;
for(int i=1;i<=m;i++)
{
scanf("%d %d",&a,&b);
int fa=find(a);
int fb=find(b);
if(fa==fb)flag=true;
if(record[a].conflict!=0)
{
link(b,record[a].conflict);
}
if(record[b].conflict!=0)
{
link(a,record[b].conflict);
}
record[a].conflict=b;
record[b].conflict=a;
}
cout<<"Scenario #"<<cnt<<":\n";
if(flag)cout<<"Suspicious bugs found!\n";
else cout<<"No suspicious bugs found!\n";
cout<<endl;
cnt++;
}
return 0;
}

208
HDOJ/1836_autoAC.cpp Normal file
View File

@ -0,0 +1,208 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
using namespace std;
#define maxn 210
#define LL long long
#define eps 1e-6
#define PI acos(-1.0)
int n,m,tot;
struct Point{
double x,y;
Point(){}
Point(double _x,double _y)
{
x=_x; y=_y;
}
};
bool operator < (const Point& a,const Point& b)
{
if(fabs(a.x-b.x)>eps)
return a.x<b.x;
else if(fabs(a.y-b.y)>eps)
return a.y<b.y;
return false;
}
bool operator==(const Point& a, const Point& b)
{
return fabs(a.x-b.x)<eps&&fabs(a.y-b.y)<eps;
}
struct Line{
Point a,b;
Line(){}
Line(Point _a,Point _b)
{
a=_a; b=_b;
}
};
bool parallel(const Line& ll,const Line& rr)
{
return fabs((ll.a.x-ll.b.x)*(rr.a.y-rr.b.y)-(ll.a.y-ll.b.y)*(rr.a.x-rr.b.x))<eps;
}
Point intesection(const Line& ll, const Line& rr) {
Point ret = ll.a;
double r =
((ll.a.x - rr.a.x) * (rr.a.y - rr.b.y) - (ll.a.y - rr.a.y) * (rr.a.x - rr.b.x)) /
((ll.a.x - ll.b.x) * (rr.a.y - rr.b.y) - (ll.a.y - ll.b.y) * (rr.a.x - rr.b.x));
ret.x += (ll.b.x - ll.a.x) * r;
ret.y += (ll.b.y - ll.a.y) * r;
return ret;
}
struct Edge{
double w;
int v;
bool vis;
Edge(){}
Edge(int _v,double _w,bool _vis=false)
{
v=_v; w=_w; vis=_vis;
}
bool operator <(const Edge& b)const
{
return w<b.w;
}
};
map<Point,int>g;
vector<Point>vp,p[maxn];
Line lines[maxn];
vector<Edge>E[maxn*maxn/2];
vector<double>ans;
Point pts[maxn];
bool check(double a,double b)
{
if(b>a+eps) return b+eps<a+PI;
return b+eps<a-PI;
}
int search(vector<Edge>&E,double w)
{
double d=w+PI;
if(d>PI+eps) d-=2*PI;
int id;
for(id=0;id<E.size();id++)
{
if(fabs(E[id].w-d)<eps)
{
if(id) id--;
else id=E.size()-1;
break;
}
}
return check(E[id].w,d)?id:-1;
}
int sig(double d)
{
return (d>eps) - (d<-eps);
}
double cross(const Point & o, const Point & a, const Point & b)
{
return (a.x-o.x)*(b.y-o.y) - (b.x-o.x)*(a.y-o.y);
}
int inside_convex(Point * ps, int n, Point q)
{
bool s[3] = {1, 1, 1};
ps[n] = ps[0];
for(int i = 0; i < n && (s[0] | s[2]); i ++) {
s[ 1+sig(cross(ps[i+1], q, ps[i])) ] = 0;
}
if(s[0] | s[2]) return s[1]+1;
return 0;
}
void init()
{
int i,j;
for(i=0;i<n;i++)
scanf("%lf %lf",&pts[i].x,&pts[i].y);
g.clear();
vp.clear();
for(i=1;i<=n;i++)
lines[i-1]=Line(pts[i%n],pts[i-1]);
for(i=n;i<n+m;i++)
scanf("%lf %lf %lf %lf",&lines[i].a.x,&lines[i].a.y,&lines[i].b.x,&lines[i].b.y);
for(i=0;i<n+m;i++)
{
p[i].clear();
for(j=0;j<i;j++)
{
if(parallel(lines[i],lines[j])) continue;
Point pt=intesection(lines[i],lines[j]);
g[pt];
p[i].push_back(pt);
p[j].push_back(pt);
}
}
tot=0;
for(map<Point,int>::iterator it=g.begin();it!=g.end();it++)
{
vp.push_back(it->first);
it->second=tot;
E[tot++].clear();
}
for(i=0;i<n+m;i++)
{
if(p[i].size()<2) continue;
sort(p[i].begin(),p[i].end());
p[i].erase(unique(p[i].begin(),p[i].end()),p[i].end());
double ab=atan2(p[i].back().y-p[i].front().y,p[i].back().x-p[i].front().x);
double ba=atan2(p[i].front().y-p[i].back().y,p[i].front().x-p[i].back().x);
for(int j=1;j<p[i].size();j++)
{
int a=g[p[i][j-1]],b=g[p[i][j]];
E[a].push_back(Edge(b,ab));
E[b].push_back(Edge(a,ba));
}
}
for(int i=0;i<tot;i++)
sort(E[i].begin(),E[i].end());
}
void gao()
{
int i,j,s,ans=0;
for(i=0;i<tot;i++)
{
if(!inside_convex(pts,n,vp[i])) continue;
for(j=0;j<E[i].size();j++)
{
if(E[i][j].vis) continue;
int a=i,b=j,c;
E[i][j].vis=true;
double S=vp[a].x*vp[E[i][j].v].y-vp[a].y*vp[E[i][j].v].x;
while(E[a][b].v!=i)
{
c=search(E[E[a][b].v],E[a][b].w);
if(c==-1)
{
S=0; break;
}
a=E[a][b].v,b=c;
if(E[a][b].vis)
{
S=0; break;
}
if(!inside_convex(pts,n,vp[a]))
{
S=0; break;
}
E[a][b].vis=true;
S+=vp[a].x*vp[E[a][b].v].y-vp[a].y*vp[E[a][b].v].x;
}
if(S>2*eps) ans++;
}
}
printf("Number of regions=%d.\n",ans);
}
int main()
{
int ncase;
scanf("%d",&ncase);
while(ncase--)
{
scanf("%d %d",&n,&m);
init();
gao();
}
return 0;
}

44
HDOJ/1838_autoAC.cpp Normal file
View File

@ -0,0 +1,44 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char s[2010][2010];
int dp[2010][2010];
int main()
{
int test,n;
scanf("%d",&test);
while(test--)
{
scanf("%d",&n);
for(int i = 0; i < n; i++)
scanf("%s",s[i]);
for(int i = 0; i < n; i++)
{
dp[0][i] = 1;
dp[i][0] = 1;
}
int ans = -1,cnt = 0;
for(int i = 1; i < n; i++)
{
for(int j = 1; j < n; j++)
{
if(s[i][j] != s[i-1][j] && s[i][j] != s[i][j-1] && s[i][j] == s[i-1][j-1])
dp[i][j] = min(min(dp[i-1][j-1],dp[i-1][j]),dp[i][j-1])+1;
else dp[i][j] = 1;
if(s[i][j] == '1')
ans = max(ans,dp[i][j]);
}
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(dp[i][j] == ans && s[i][j] == '1')
cnt++;
}
}
printf("%d %d\n",ans,cnt);
}
return 0;
}

94
HDOJ/1839_autoAC.cpp Normal file
View File

@ -0,0 +1,94 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
typedef __int64 ss;
struct node
{
ss k;
ss t;
ss v;
};
ss n,m,t,a[50005],sum,flag;
vector<node>vet[10005];
ss vist[10005],dis[10005];
ss spfa(ss num)
{
ss minx=a[num];
for(ss i=0; i<=n; i++)
{
dis[i]=((ss)1<<25);
vist[i]=0;
}
dis[1]=0;
vist[1]=1;
queue<ss>q;
q.push(1);
while(!q.empty())
{
ss x=q.front();
q.pop();
vist[x]=0;
for(ss i=0; i<vet[x].size(); i++)
{
node p=vet[x][i];
if(p.v>=minx)
{
if(dis[p.k]>dis[x]+p.t)
{
dis[p.k]=dis[x]+p.t;
if(!vist[p.k])
q.push(p.k);
vist[p.k]=1;
}
}
}
}
if(dis[n]<=t)
return 1;
else return 0;
}
int main()
{
int text;
scanf("%d",&text);
while(text--)
{
ss cnt=0;
scanf("%I64d%I64d%I64d",&n,&m,&t);
for(ss i=0; i<=n; i++)
vet[i].clear();
for(ss i=0; i<m; i++)
{
ss v1,v2,tmp,tmp1;
scanf("%I64d %I64d %I64d %I64d",&v1,&v2,&tmp,&tmp1);
node p;
p.k=v2;
p.t=tmp1;
p.v=tmp;
vet[v1].push_back(p);
p.k=v1;
vet[v2].push_back(p);
a[cnt++]=tmp;
}
sort(a,a+cnt);
ss ll=0,rr=cnt-1;
ss ans=0;
while(ll<=rr)
{
ss mid=(ll+rr)/2;
if(spfa(mid))
{
if(ans<a[mid])
ans=a[mid];
ll=mid+1;
}
else rr=mid-1;
}
printf("%I64d\n",ans);
}
return 0;
}

28
HDOJ/1840_autoAC.cpp Normal file
View File

@ -0,0 +1,28 @@
#include <stdio.h>
int main()
{
int n,a,b,c;
scanf("%d",&n);
while(n--){
scanf("%d%d%d",&a,&b,&c);
if(a!=0){
if(b*b-a*4*c>0)
printf("2\n");
else if(b*b==4*a*c)
printf("1\n");
else
printf("0\n");
}
else{
if(b==0){
if(c==0)
printf("INF\n");
else
printf("0\n");
}
else
printf("1\n");
}
}
return 0;
}

83
HDOJ/1841_autoAC.cpp Normal file
View File

@ -0,0 +1,83 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int len[2];
int next[2][1000111];
char str[2][1000111];
void getnext(int z)
{
int j,k;
j=1;k=0;
next[z][1]=0;
while(str[z][j])
{
if(k==0 || str[z][j]==str[z][k]) next[z][++j]=++k;
else k=next[z][k];
}
}
int KMP1(int a,int b)
{
int i,j;
j=1;
for(i=1;str[a][i];)
{
if(str[a][i]==str[b][j])
{
i++;j++;
if(j==len[b]) return 1;
}
else
{
j=next[b][j];
if(j==0) {i++;j=1;}
}
}
return 0;
}
int KMP2(int a,int b)
{
int i,j;
int flag;
j=1;
for(i=1;str[a][i];)
{
flag=1;
if(str[a][i]==str[b][j])
{
if(i==len[a]-1) return j;
if(j==len[b]-1) flag=1;
else {flag=0;i++;j++;}
}
if(flag)
{
j=next[b][j];
if(j==0) {i++;j=1;}
}
}
return 0;
}
int main()
{
int T;
int ans1,ans2,ans;
scanf("%d",&T);
while(T--)
{
str[0][0]='#';
str[1][0]='#';
scanf("%s%s",str[0]+1,str[1]+1);
len[0]=strlen(str[0]);
len[1]=strlen(str[1]);
getnext(0);
getnext(1);
if(len[0]>len[1]) ans=KMP1(0,1);
else ans=KMP1(1,0);
if(ans) {printf("%d\n",(len[0]>len[1]?len[0]:len[1])-1);continue;}
ans1=KMP2(0,1);
ans2=KMP2(1,0);
ans=ans1>ans2?ans1:ans2;
ans=len[0]+len[1]-2-ans;
printf("%d\n",ans);
}
return 0;
}

105
HDOJ/1843_autoAC.cpp Normal file
View File

@ -0,0 +1,105 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <vector>
using namespace std;
struct P
{
int x;
int y;
P() {}
P(int xx, int yy):x(xx), y(yy) {}
};
bool operator < (P p1, P p2)
{
if (p1.x == p2.x)
{
return p1.y < p2.y;
}
return p1.x < p2.x;
}
class cmp1
{
public:
bool operator()(const P& left, const P& right)
{
if (left.y == right.y)
{
return left.x < right.x;
}
return left.y < right.y;
}
};
class cmp2
{
public:
bool operator()(const P& left, const P& right)
{
if (left.x == right.x)
{
return left.y < right.y;
}
return left.x > right.x;
}
};
P p1[100005];
P p2[100005];
int h, v;
void gao(P p2[], int v, vector <P> &vec)
{
sort(p2+1, p2+1+v);
int pt = v;
vec.push_back(p2[v]);
for (int i = v-1; i >= 1; i--)
{
if (p2[i].x < p2[pt].x && p2[i].y > p2[pt].y)
{
vec.push_back(p2[i]);
pt = i;
}
}
}
int main()
{
int T;
scanf("%d", &T);
for (int cas = 1; cas <= T; cas++)
{
scanf("%d%d", &h, &v);
for (int i = 1; i <= h; i++)
{
scanf("%d%d", &p1[i].x, &p1[i].y);
}
for (int i = 1; i <= v; i++)
{
scanf("%d%d", &p2[i].x, &p2[i].y);
}
vector <P> v1;
vector <P> v2;
gao(p1, h, v1);
gao(p2, v, v2);
int cnt = 0;
int t = 0;
int size = (int)v1.size();
for (int i = 0; i < (int)v2.size(); i++)
{
if (t == size) break;
if (i == (int)v2.size()-1)
{
cnt++;
break;
}
if (v1[t].x > v2[i+1].x)
{
cnt++;
while (t < size && v1[t].y <= v2[i].y)
{
t++;
}
}
}
printf("%d\n", cnt);
}
return 0;
}

17
HDOJ/1845_autoAC.cpp Normal file
View File

@ -0,0 +1,17 @@
#include<stdio.h>
int main()
{
int T;
int t;
int a,b;
int n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
t=3*n/2;
while(t--) scanf("%d%d",&a,&b);
printf("%d\n",n/2);
}
return 0;
}

16
HDOJ/1846_autoAC.cpp Normal file
View File

@ -0,0 +1,16 @@
#include<stdio.h>
int main()
{
int c;
scanf("%d",&c);
while(c--)
{
int n,m;
scanf("%d%d",&n,&m);
if(n%(m+1)==0)
printf("second\n");
else
printf("first\n");
}
return 0;
}

17
HDOJ/1847_autoAC.cpp Normal file
View File

@ -0,0 +1,17 @@
#include <stdio.h>
int main()
{
int n ;
while(~scanf("%d",&n))
{
if(n%3 == 0)
{
puts("Cici") ;
}
else
{
puts("Kiki") ;
}
}
return 0 ;
}

34
HDOJ/1848_autoAC.cpp Normal file
View File

@ -0,0 +1,34 @@
#include<cstdio>
#include<string.h>
using namespace std;
int fib[20],sg[1010],m,n,p;
int calsg(int now){
int i,tem;
int next[20];
memset(next,0,sizeof(next));
for(i=1;fib[i]<=now;i++){
tem=now-fib[i];
if(sg[tem]==-1)
sg[tem]=calsg(tem);
next[sg[tem]]=1;
}
for(i=0;;i++)
if(next[i]==0){
return i;
}
}
int main(){
int i,j,tem;
fib[1]=1;
fib[2]=2;
for(i=3;i<=16;i++)
fib[i]=fib[i-1]+fib[i-2];
memset(sg,-1,sizeof(sg));
while(scanf("%d %d %d",&m,&n,&p)==3 && !(m==0 && n==0 && p==0)){
tem=calsg(m)^calsg(n)^calsg(p);
if(tem==0)
printf("Nacci\n");
else
printf("Fibo\n");
}
}

19
HDOJ/1849_autoAC.cpp Normal file
View File

@ -0,0 +1,19 @@
#include<iostream>
const int MAX=1001;
int s[MAX];
using namespace std;
int main()
{
int n,m,i;
while(cin>>n,n)
{
m=0;
for(i=1;i<=n;i++)
cin>>s[i],m^=s[i];
if(m)
cout<<"Rabbit Win!"<<endl;
else
cout<<"Grass Win!"<<endl;
}
return 0;
}

29
HDOJ/1850_autoAC.cpp Normal file
View File

@ -0,0 +1,29 @@
#include<stdio.h>
int main()
{
int a[105];
int n,i,flag,sum,k;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
for(i=0,sum=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum^a[i];
}
if(sum==0)
{
printf("0\n");
continue;
}
for(i=0,flag=0;i<n;i++)
{
k=sum^a[i];
if(k<a[i])
flag++;
}
printf("%d\n",flag);
}
return 0;
}

19
HDOJ/1851_autoAC.cpp Normal file
View File

@ -0,0 +1,19 @@
#include<iostream>
using namespace std;
int main()
{
int c,n,m,l;
cin>>c;
while(c--)
{
cin>>n;
int s=0;
for(int i=0;i<n;++i)
{
cin>>m>>l;
s^=m%(l+1);
}
cout<<(s?"No":"Yes")<<endl;
}
return 0;
}

51
HDOJ/1852_autoAC.cpp Normal file
View File

@ -0,0 +1,51 @@
#include <stdio.h>
#include <stdlib.h>
#include "math.h"
__int64 powmod(int d, int n, int k)
{
__int64 a,b;
if((n==0) || (d == 1))
return 1;
for(a = d,b = 1; n>0; n=n/2)
{
if(n%2 == 1)
{
b = (b*a)%k;
}
a = (a*a)%k;
}
return b%k;
}
__int64 cacul_sum(int d, int n, int k)
{
int l;
__int64 t1,t2,r;
l = (d-1)*k;
t1 = powmod(d, n+1, l);
t2 = (t1+l-1)%l;
r = t2/(d-1);
return r;
}
int cacul_res(int n, int k)
{
__int64 m1,m2,m,r;
m1 = cacul_sum(251, n, k);
m2 = cacul_sum(2, 3*n, k);
m = (m1*m2)%k;
r = powmod(2008, m, k);
return r;
}
int main()
{
int n,k;
__int64 r;
while(1)
{
scanf("%d%d", &n, &k);
if((n==0)&&(k==0))
break;
r = cacul_res(n,k);
printf("%I64d\n", r);
}
return 0;
}

113
HDOJ/1853_autoAC.cpp Normal file
View File

@ -0,0 +1,113 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<climits>
using namespace std;
#define N 505
#define MAXN 1<<28
int map[N][N];
int lx[N], ly[N];
int slack[N];
int match[N];
bool visitx[N], visity[N];
int n;
bool Hungary(int u)
{
visitx[u] = true;
for(int i = 1; i <= n; ++i)
{
if(visity[i])
continue;
else
{
if(lx[u] + ly[i] == map[u][i])
{
visity[i] = true;
if(match[i] == -1 || Hungary(match[i]))
{
match[i] = u;
return true;
}
}
else
slack[i] = min(slack[i], lx[u] + ly[i] - map[u][i]);
}
}
return false;
}
void KM_perfect_match()
{
int temp;
for(int i = 1; i <= n; ++i)
lx[i] = -MAXN;
memset(ly, 0, sizeof(ly));
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
lx[i] = max(lx[i], map[i][j]);
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= n; ++j)
slack[j] = MAXN;
while(1)
{
memset(visitx, false, sizeof(visitx));
memset(visity, false, sizeof(visity));
if(Hungary(i))
break;
else
{
temp = MAXN;
for(int j = 1; j <= n; ++j)
if(!visity[j])
temp = min(temp, slack[j]);
for(int j = 1; j <= n; ++j)
{
if(visitx[j])
lx[j] -= temp;
if(visity[j])
ly[j] += temp;
else
slack[j] -= temp;
}
}
}
}
}
int main()
{
int m;
int a, b, cost;
int ans;
bool flag;
while(scanf("%d%d", &n, &m) != EOF)
{
ans = 0;
flag = true;
memset(match, -1, sizeof(match));
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
map[i][j] = -MAXN;
for(int i = 1; i <= m; ++i)
{
scanf("%d%d%d", &a, &b, &cost);
if(-cost > map[a][b])
map[a][b] = -cost;
}
KM_perfect_match();
for(int i = 1; i <= n; ++i)
{
if(match[i] == -1 || map[ match[i] ][i] == -MAXN)
{
flag = false;
break;
}
ans += map[match[i]][i];
}
if(flag)
printf("%d\n", -ans);
else
printf("-1\n");
}
return 0;
}

52
HDOJ/1854_autoAC.cpp Normal file
View File

@ -0,0 +1,52 @@
#include<stdio.h>
#include<string.h>
#define N 1010
char str[N];
int Judge(char *s)
{
int len,i,ans;
len=strlen(s);
ans=0;
for(i=0;i<len;i++)
{
if(s[i]=='0')
{
ans++;
}
else if(s[i]=='1')
{
if(ans>1)
{
ans--;
}
else
{
return 0;
}
}
}
if(ans>=1)
{
return 1;
}
}
int main()
{
int n,len,flag;
scanf("%d",&n);
getchar();
while(n--)
{
scanf("%s",str);
flag=Judge(str);
if(flag)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return 0;
}

40
HDOJ/1856_autoAC.cpp Normal file
View File

@ -0,0 +1,40 @@
#include<stdio.h>
int a[10000002],dis[10000002],hsd[10000002];
int find(int x)
{
if(dis[x]!=x)
dis [x]=find(dis [x]);
return dis[x];
}
int main()
{
int n,i,j,max1;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
printf("1\n");
else
{
for(j=1; j<=10000000; j++)
{
dis[j]=j;
hsd[j]=1;
}
max1=0;
for(i=1; i<=2*n; i=i+2)
{ scanf("%d%d",&a[i],&a[i+1]);
a[i]=find(a[i]);
a[i+1]=find(a[i+1]);
if(a[i]!=a[i+1])
{
dis[a[i+1]]=a[i];
hsd[a[i]]=hsd[a[i]]+hsd[a[i+1]];
if(hsd[a[i]]>max1)
max1=hsd[a[i]];
}
}
printf("%d\n",max1);
}
}
return 0;
}

104
HDOJ/1857_autoAC.cpp Normal file
View File

@ -0,0 +1,104 @@
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int KIND = 26;
const int MAXN = 1000000;
int cnt_node;
int R,C;
char map[505][505];
char word[10005][22];
struct node{
bool isword;
int r,c;
node* next[KIND];
void init(){
r=c=-1;
isword=false;
memset(next, 0, sizeof(next));
}
}Heap[MAXN];
inline node* new_node(){
Heap[cnt_node].init();
return &Heap[cnt_node++];
}
void insert(node* root, char *str){
for(char *p=str; *p; ++p){
int ch=*p-'A';
if(root->next[ch]==NULL)
root->next[ch] = new_node();
root=root->next[ch];
}
root->isword=true;
}
void search(node* root, char *str, int row, int col){
for(char *p=str; *p; ++p){
int ch=*p-'A';
if(root->next[ch]==NULL)
return ;
root=root->next[ch];
if(root->isword && root->r==-1 && root->c==-1){
root->r=row, root->c=col;
}
}
if(root->isword && root->r==-1 && root->c==-1){
root->r=row, root->c=col;
}
}
void output(node* root, char *str){
for(char *p=str; *p; ++p){
int ch=*p-'A';
if(root->next[ch]==NULL)
return;
root=root->next[ch];
}
if(root->isword)
printf("%d %d\n", root->r, root->c);
}
int main(){
cnt_node=0;
node* root=new_node();
scanf("%d%d%*c",&R,&C);
for(int i=0; i<R; ++i){
gets(map[i]);
}
gets(word[0]);
int pos=0;
while(gets(word[pos])){
if(word[pos][0]=='-') break;
insert(root, word[pos++]);
}
char str[30];
for(int i=0; i<R; ++i){
for(int j=0; j<C; ++j){
int end_r,end_c=j;
if(i+20<R)
end_r=i+20;
else
end_r=R;
memset(str, 0, sizeof(str));
for(int k=i,p=0; k<end_r; ++k)
str[p++]=map[k][j];
search(root, str, i, j);
if(j+20<C)
end_c=j+20;
else
end_c=C;
memset(str, 0, sizeof(str));
for(int k=j,p=0; k<end_c; ++k)
str[p++]=map[i][k];
search(root, str, i, j);
int r=i, c=j,p=0;
memset(str, 0, sizeof(str));
while(r<end_r && c<end_c){
str[p++]=map[r][c];
++r, ++c;
}
search(root,str,i,j);
}
}
for(int i=0; i<pos; ++i){
output(root, word[i]);
}
return 0;
}

43
HDOJ/1858_autoAC.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <cstdlib>
#include <iostream>
using namespace std;
struct node
{
int d;
__int64 w;
};
node f[1000002];
__int64 v[1000002];
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%I64d",&v[i]);
f[i].d=0;
}
f[1].w=v[1];
for(int i=2;i<=n;i++)
{
if(f[i-1].w>=0)
{
f[i].w=f[i-1].w+v[i];
f[i].d=f[i-1].d+1;
}
else f[i].w=v[i];
}
int Max=1;
for(int i=2;i<=n;i++)
{
if(f[i].w>f[Max].w) Max=i;
else if(f[i].w==Max&&(i-f[i].d<Max-f[Max].d))
Max=i;
}
printf("%I64d %d %d\n",f[Max].w,Max-f[Max].d,Max);
}
return 0;
}

18
HDOJ/1859_autoAC.cpp Normal file
View File

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

24
HDOJ/1860_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,sum;
char a[5],b[80];
while(gets(a))
{
if(a[0]=='#')
break;
else
gets(b);
for(i=0;i<strlen(a);i++)
{
sum=0;
for(j=0;j<strlen(b);j++)
{
if(a[i]==b[j])
sum++;
}
printf("%c %d\n",a[i],sum);
}
}
}

85
HDOJ/1861_autoAC.cpp Normal file
View File

@ -0,0 +1,85 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define Len 200
using namespace std;
typedef struct
{
int S,E;
int times,timee;
}node;
typedef struct
{
int num,aver;
}nodf;
node E[Len];
nodf AN[Len];
int ID[1000];
int main()
{
char SE;
int id,h,m;
int idnum,annum;
int sum,ber;
int x,y;
idnum=0,annum=0;
while(scanf("%d",&id)!=EOF)
{
if(id==-1)
{
for(int i=0;i<annum;i++)
printf("%d %d\n",AN[i].num,AN[i].aver);
annum=0,idnum=0;
}
if(id==0)
{
cin>>SE;
sum=0,ber=0;
for(int i=0;i<idnum;i++)
{
if(E[ID[i]].S==1&&E[ID[i]].E==1)
{
sum+=(E[ID[i]].timee-E[ID[i]].times);
ber++;
}
}
if(ber==0)
{
AN[annum].aver=0;
AN[annum].num=0;
annum++;
}
else
{
AN[annum].aver=sum/ber;
AN[annum].num=ber;
x=sum%ber;
if((x*2)>=ber)
AN[annum].aver++;
annum++;
}
memset(E,0,sizeof(E));
memset(ID,0,sizeof(ID));
idnum=0;
}
if(id!=0&&id!=-1)
{
cin>>SE;
scanf("%d:%d",&h,&m);
if(SE=='S')
{
ID[idnum]=id;
idnum++;
E[id].times=h*60+m;
E[id].S=1;
}
if(SE=='E')
{
E[id].timee=h*60+m;
E[id].E=1;
}
}
}
return 0;
}

51
HDOJ/1862_autoAC.cpp Normal file
View File

@ -0,0 +1,51 @@
#include<stdio.h>
#include<stdlib.h>
#define M 100000
#include<string.h>
struct ln{
int xuehao;
char name[10];
int score;
};
struct ln a[M+10];
int cmpx(const void *a,const void *b){
return (*(ln*)a).xuehao-(*(ln*)b).xuehao;
}
int cmpn(const void *a,const void *b){
ln *c=(ln*)a;
ln *d=(ln *)b;
if(!strcmp(c->name,d->name))
return c->xuehao-d->xuehao;
else
return strcmp(c->name,d->name);
}
int cmps(const void *a,const void *b){
ln *c=(ln*)a;
ln *d=(ln *)b;
if(c->score==d->score)
return c->xuehao-d->xuehao;
else
return c->score-d->score;
}
int main(){
int n,c;
int i;
int t=0;
while(scanf("%d%d",&n,&c)&&n){
t++;
for(i=0;i<n;i++){
scanf("%d%s%d",&a[i].xuehao,a[i].name,&a[i].score);
}
if(c==1)
qsort(a,n,sizeof(a[0]),cmpx);
if(c==2)
qsort(a,n,sizeof(a[0]),cmpn);
if(c==3)
qsort(a,n,sizeof(a[0]),cmps);
printf("Case %d:\n",t);
for(i=0;i<n;i++){
printf("%06d %s %d\n",a[i].xuehao,a[i].name,a[i].score);
}
}
return 0;
}

52
HDOJ/1863_autoAC.cpp Normal file
View File

@ -0,0 +1,52 @@
#include<stdio.h>
#include<string.h>
#define Max 10000000
int map[110][110],cost[110],vit[110],m,n;
void prim(int x)
{
int i,j,min,k,sum=0;
memset(vit,0,sizeof(vit));
for(i=1;i<=m;i++)
cost[i]=map[x][i];
cost[x]=0;
vit[x]=1;
for(i=1;i<m;i++)
{
min=Max;k=-1;
for(j=1;j<=m;j++)
if(!vit[j]&&cost[j]<min)
min=cost[k=j];
if(min==Max)
{
printf("?\n");
return ;
}
vit[k]=1;
sum+=min;
for(j=1;j<=m;j++)
if(!vit[j]&&cost[j]>map[k][j])
cost[j]=map[k][j];
}
printf("%d\n",sum);
}
int main()
{
int i,j,a,b,c;
while(scanf("%d%d",&n,&m)!=EOF&&n)
{
for(i=1;i<=m;i++)
for(j=1;j<=m;j++)
{
if(i==j)
map[i][j]=0;
map[i][j]=Max;
}
for(i=0;i<n;i++)
{
scanf("%d%d%d",&a,&b,&c);
map[a][b]=map[b][a]=c;
}
prim(1);
}
return 0;
}

39
HDOJ/1864_autoAC.cpp Normal file
View File

@ -0,0 +1,39 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
int n,i,j,mark,z,m;
float c[1000],maxx,b[100],sum,aa,bb,cc;
char a[100];
while(~scanf("%f%d",&maxx,&n),n){
for(i=1,z=1;i<=n;i++){
scanf("%d",&m);
getchar();
c[z]=mark=0;aa=bb=cc=0;
for(j=0;j<m;j++){
scanf("%c:%f",&a[j],&b[j]);
getchar();
if(!(a[j]=='A'||a[j]=='B'||a[j]=='C')) mark=1;
if(a[j]=='A') aa+=b[j];
if(a[j]=='B') bb+=b[j];
if(a[j]=='C') cc+=b[j];
if(aa>600||bb>600||cc>600) mark=1;
c[z]+=b[j];
if(c[z]>maxx) mark=1;
}
if(mark) continue;
z++;
}
z--;
sort(c+1,c+z+1);
sum=0;
for(i=z;i>=1;i--)
{
if(sum+c[i]>maxx) continue;
sum+=c[i];
}
printf("%.2f\n",sum);
}
}

40
HDOJ/1865_autoAC.cpp Normal file
View File

@ -0,0 +1,40 @@
#include <stdio.h>
#include <string.h>
int str1[210][300]={{0}};
int main()
{
int n;
scanf("%d",&n);
getchar();
while(n--)
{
char str[300];
gets(str);
int len=strlen(str);
str1[0][0]=1;
str1[1][0]=2;
for(int i=2;i<len;i++)
{
int c=0,k;
for(k=0;k<300;k++)
{
int s=str1[i-1][k]+str1[i-2][k]+c;
str1[i][k]=s%10;
c=s/10;
}
while(c)
{
str1[i][k++]=c%10;
c/=10;
}
}
int i;
for(i=299;i>=0;i--)
if(str1[len-1][i])
break;
for(int j=i;j>=0;j--)
printf("%d",str1[len-1][j]);
printf("\n");
}
return 0;
}

55
HDOJ/1866_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int number[4];
char str[501];
int map[1001][1001];
int main()
{
int i,j,k,sum,min_x,min_y,max_x,max_y;
int length,mark,x,y,ans;
while(gets(str))
{
memset(map,0,sizeof(map));
length = strlen(str);
ans = 0;
for(i=0;i<length;i++)
{
if(str[i] == '(')
{
sum = 0;
mark = 0;
for(k=0,j=i+1;k<4;j++)
{
if(str[j] >= '0' && str[j] <='9')
{
sum = sum*10 + str[j] - '0';
mark = 1;
}
else if(mark == 1 && str[j-1] >= '0' && str[j-1] <='9')
{
number[k++] = sum;
sum = 0;
mark = 0;
}
}
i = j;
min_x = min(number[0],number[2]);
max_x = max(number[0],number[2]);
min_y = min(number[1],number[3]);
max_y = max(number[1],number[3]);
for(x = min_ ;x<max_x;x++)
for(y = min_ ;y<max_y;y++)
{
if(map[x][y] == 0)
{
map[x][y] = 1;
ans++;
}
}
}
}
printf("%d\n",ans);
}
}

73
HDOJ/1867_autoAC.cpp Normal file
View File

@ -0,0 +1,73 @@
#include <stdio.h>
#include <string.h>
int next[100005];
void getnext(char str[])
{
int i = 1,j = 0;
int len = strlen(str);
next [0] = -1;
while(i < len)
{
if(j == -1 || str[i] == str[j])
{
i++;
j++;
next[i] = j;
}
else
j = next[j];
}
}
int kmp(char str1[],char str2[])
{
int i= 0,j = 0;
int len1 = strlen(str1),len2 = strlen(str2);
getnext(str2);
while(i<len1 && j<len2)
{
if(j == -1 || str1[i] == str2[j])
{
i++;
j++;
}
else
j = next[j];
}
if(i == len1)
return j;
return 0;
}
int main()
{
int x,y;
char str1[100005],str2[100005];
while(scanf("%s%s",str1,str2)!=EOF)
{
x = kmp(str1,str2);
y = kmp(str2,str1);
if(x == y)
{
if(strcmp(str1,str2)>0)
{
printf("%s",str2);
printf("%s\n",str1+x);
}
else
{
printf("%s",str1);
printf("%s\n",str2+x);
}
}
else if(x>y)
{
printf("%s",str1);
printf("%s\n",str2+x);
}
else
{
printf("%s",str2);
printf("%s\n",str1+y);
}
}
return 0;
}

21
HDOJ/1868_autoAC.cpp Normal file
View File

@ -0,0 +1,21 @@
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int n,i,j;
while(cin>>n)
{
j = 0;
if(n%2)j = j+1;
for(i = 2;i<=sqrt(n);i++)
{
if(n%i==0)
{
if(i%2!=0)j = j+1;
if(n/i%2!=0)j = j+1;
}
}
cout <<j<<endl;
}
}

58
HDOJ/1869_autoAC.cpp Normal file
View File

@ -0,0 +1,58 @@
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
#include<map>
#define MAX_V 110
#define INF 0x7ffffff
using namespace std;
map <string,int> name;
int ans[MAX_V][MAX_V],d[MAX_V][MAX_V],dd[MAX_V][MAX_V];
bool ok[MAX_V][MAX_V];
int main()
{
int n,m,a,b;
while(~scanf("%d%d",&n,&m))
{
memset(d,0,sizeof(d));
memset(ans,0,sizeof(ans));
memset(ok,0,sizeof(ok));
while(m--)
{
scanf("%d%d",&a,&b);
ans[a][b]=ans[b][a]=d[a][b]=d[b][a]=1;
ok[a][b]=ok[b][a]=1;
}
int times=6;
while(times--){
memset(dd,0,sizeof(dd));
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
for(int k=0;k<n;k++){
dd[i][j]+=ans[i][k]*d[k][j];
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
ans[i][j]=dd[i][j];
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(ans[i][j]!=0) ok[i][j]=1;
}
}
}
int flag=1;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(ok[i][j]==0)
flag=0;
}
}
puts(flag?"Yes":"No");
}
return 0;
}

30
HDOJ/1870_autoAC.cpp Normal file
View File

@ -0,0 +1,30 @@
#include <stdio.h>
#include <string.h>
int main()
{
int l;
char a[1000];
while(gets(a))
{
l=strlen(a);
int wbx=0;
int i;
for(i=0;i<l;i++)
{
if(a[i]=='(')
{
wbx++;
}
if(a[i]==')')
{
wbx--;
}
if(a[i]=='B')
{
break;
}
}
printf("%d\n",wbx);
}
return 0;
}

47
HDOJ/1871_autoAC.cpp Normal file
View File

@ -0,0 +1,47 @@
#include<stdio.h>
#include<algorithm>
using namespace std;
struct man
{
int a,b,c;
};
man x[1001];
int n,t,i,m,a;
bool cmp(man x,man y)
{
if(x.c>y.c)
return false;
return true;
}
void print(int a)
{
int j;
for(j=0;j<n;j++)
{
if(a<=x[j].b)
{
printf("%d\n",x[j].a);
x[j].b-=a;
return;
}
}
printf("sorry\n");
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d%d%d",&x[i].a,&x[i].b,&x[i].c);
sort(x,x+n,cmp);
scanf("%d",&m);
for(i=0;i<m;i++)
{
scanf("%d",&a);
print(a);
}
}
return 0;
}

50
HDOJ/1872_autoAC.cpp Normal file
View File

@ -0,0 +1,50 @@
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct st
{
string name;
int mark;
int num;
}rt[305];
int cmp(const st &a,const st &b)
{
if(a.mark!=b.mark)
return a.mark>b.mark;
return a.num<b.num;
}
int main()
{
string a;int b,t;
while(cin>>t)
{
int s=t,tick=0;
while(s--)
{
cin>>rt[s].name>>rt[s].mark;
rt[s].num=t-s-1;
}
sort(rt,rt+t,cmp);s=t;
while(s--)
{
cin>>a>>b;
if(b!=rt[t-s-1].mark) {tick=1;break;}
else if(a!=rt[t-s-1].name) tick=2;
}
if(tick==1) while(s--) cin>>a>>b;
switch(tick)
{
case 0:cout<<"Right"<<endl;break;
case 2:cout<<"Not Stable"<<endl;
for(int i=0;i<t;i++)
cout<<rt[i].name<<" "<<rt[i].mark<<endl;
break;
case 1:cout<<"Error"<<endl;
for(int i=0;i<t;i++)
cout<<rt[i].name<<" "<<rt[i].mark<<endl;
break;
}
}
return 0;
}

97
HDOJ/1873_autoAC.cpp Normal file
View File

@ -0,0 +1,97 @@
#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
struct node
{
int b1;
int shu;
friend bool operator < (node a,node b)
{
if(a.b1!= b.b1){
return a.b1 < b.b1;
}
else{
return a.shu > b.shu ;
}
}
};
int main()
{
int a,b,c,z,d,e,f,xx,wc;
char ss[10];
node t;
while(scanf("%d",&a)!=EOF)
{
priority_queue<node> q;
priority_queue<node> q1;
priority_queue<node> q2;
xx=0;
while(a--)
{
scanf("%s",ss);
if(ss[0]=='I')
{
scanf("%d%d",&b,&c);
t.b1=c;
t.shu=++xx;
if(b==1)
{
q.push(t);
}
else if(b==2)
{
q1.push(t);
}
else if(b==3)
{
q2.push(t);
}
}
else
{
scanf("%d",&z);
if(z==1)
{
if(q.empty()==1)
{
printf("EMPTY\n");
}
else
{
t=q.top();
q.pop();
printf("%d\n",t.shu);
}
}
else if(z==2)
{
if(q1.empty()==1)
{
printf("EMPTY\n");
}
else
{
t=q1.top();
q1.pop();
printf("%d\n",t.shu);
}
}
else if(z==3)
{
if(q2.empty()==1)
{
printf("EMPTY\n");
}
else
{
t=q2.top();
q2.pop();
printf("%d\n",t.shu);
}
}
}
}
}
return 0;
}

36
HDOJ/1874_autoAC.cpp Normal file
View File

@ -0,0 +1,36 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int M=2005;
#define min(a,b) a<b?a:b
int map[M][M];
#define inf 0xffffff
int main()
{
int n,m,i,j,k,s,e,l,s1,e1;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
map[i][j]=inf;
for(i=0;i<m;i++)
{
scanf("%d%d%d",&s,&e,&l);
if(map[s][e]>l)
map[s][e]=map[e][s]=l;
}
for(k=0;k<n;k++)
for(i=0;i<n;i++)
for(j=0;j<n;j++)
map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
scanf("%d%d",&s1,&e1);
if(s1==e1)
printf("0\n");
else if(map[s1][e1]==inf)
printf("-1\n");
else
printf("%d\n",map[s1][e1]);
}
return 0;
}

71
HDOJ/1875_autoAC.cpp Normal file
View File

@ -0,0 +1,71 @@
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int x[10005],y[10005],fa[1000005];
double fun(int x1,int y1,int x2,int y2)
{
return sqrt(pow(x1-x2,2.0)+pow(y1-y2,2.0));
}
int find(int x)
{
int k,j,r;
r=x;
while(r!=fa[r])
r=fa[r];
k=x;
while(k!=r)
{
j=fa[k];
fa[k]=r;
k=j;
}
return r;
}
int main()
{
int t,n,i,j,k;
double len;
scanf("%d",&t);
while(t--)
{
pair<int,int> pa;
pair< double,pair<int,int> > p[10005];
k=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d %d",&x[i],&y[i]);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(i==j) continue;
len=fun(x[i],y[i],x[j],y[j]);
if(len>=10&&len<=1000)
{
pa=make_pair(i,j);
p[k++]=make_pair(len,pa);
}
}
sort(p,p+k);
for(i=0;i<=n;i++)
fa[i]=i;
int a,b,ans=0;
double cnt=0;
for(i=0;i<k;i++)
{
a=find(p[i].second.first);
b=find(p[i].second.second);
if(a!=b)
{
fa[a]=b;
cnt+=p[i].first;
ans++;
}
}
if(ans==n-1)
printf("%.1lf\n",cnt*100);
else
printf("oh!\n");
}
return 0;
}

75
HDOJ/1876_autoAC.cpp Normal file
View File

@ -0,0 +1,75 @@
#include<stdio.h>
#include<string.h>
int n,m;
int map[111][111];
struct A
{
int ts;
int ms;
}dp[111][111];
int Judge(int x,int y)
{
if(x<0 || x>=n || y<0 || y>=m) return 1;
return 0;
}
int main()
{
int T;
int i,l;
int t,x_n,y_n;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
for(l=0;l<m;l++)
scanf("%d",&map[i][l]);
memset(dp,0,sizeof(dp));
dp[0][0].ms=1;
for(i=0;i<n;i++)
{
for(l=0;l<m;l++)
{
if(i==n-1 && l==m-1)continue;
if(map[i][l]==0) continue;
if(dp[i][l].ms==0) continue;
t=map[i][l]+1;
x_n=i+map[i][l];
y_n=l;
while(t--)
{
if(x_n==n-1 && y_n==m-1) {x_n--;y_n++;continue;}
if(Judge(x_n,y_n)) {x_n--;y_n++;continue;}
if(dp[i][l].ts+1>dp[x_n][y_n].ts)
{
dp[x_n][y_n].ts=dp[i][l].ts+1;
dp[x_n][y_n].ms=dp[i][l].ms;
}
else if(dp[i][l].ts+1==dp[x_n][y_n].ts) dp[x_n][y_n].ms+=dp[i][l].ms;
x_n--;
y_n++;
}
if(i+l+map[i][l]>n+m-2)
{
if(dp[i][l].ts>dp[n-1][m-1].ts)
{
dp[n-1][m-1].ts=dp[i][l].ts;
dp[n-1][m-1].ms=dp[i][l].ms;
}
else if(dp[i][l].ts==dp[n-1][m-1].ts) dp[n-1][m-1].ms+=dp[i][l].ms;
}
else if(i+l+map[i][l]==n+m-2)
{
if(dp[i][l].ts+1>dp[n-1][m-1].ts)
{
dp[n-1][m-1].ts=dp[i][l].ts+1;
dp[n-1][m-1].ms=dp[i][l].ms;
}
else if(dp[i][l].ts+1==dp[n-1][m-1].ts) dp[n-1][m-1].ms+=dp[i][l].ms;
}
}
}
printf("%d %d\n",dp[n-1][m-1].ts,dp[n-1][m-1].ms);
}
return 0;
}

17
HDOJ/1877_autoAC.cpp Normal file
View File

@ -0,0 +1,17 @@
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int m;
__int64 A,B;
char ans[30];
while(cin>>m&&m)
{
cin>>A>>B;
A+=B;
itoa(A,ans,m);
cout<<ans<<endl;
}
return 0;
}

69
HDOJ/1878_autoAC.cpp Normal file
View File

@ -0,0 +1,69 @@
#include <string.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
using namespace std;
int a[1024],b[1024];
int main()
{
int n,i,j,m,ji1,ji2,ji,sji;
while(~scanf("%d",&n))
{
if(!n)break;
scanf("%d",&m);
for(i=1;i<=n;i++)
{
a[i]=i;
}
memset(b,0,sizeof(b));
for(i=0;i<m;i++)
{
scanf("%d %d",&ji1,&ji2);
b[ji1]++;
b[ji2]++;
while(a[ji1] != ji1)
{
ji1=a[ji1];
}
while(a[ji2] != ji2)
{
ji2=a[ji2];
}
if(ji1>ji2)
{
ji=ji1;
ji1=ji2;
ji2=ji;
}
a[ji2]=ji1;
}
sji=0;
for(i=2;i<=n;i++)
{
if(a[i] == i)
{
sji=1;
break;
}
}
for(i=1;i<=n;i++)
{
if(b[i]%2)
{
sji=1;
break;
}
}
if(sji)
{
printf("0\n");
}
else
{
printf("1\n");
}
}
return 0;
}

61
HDOJ/1879_autoAC.cpp Normal file
View File

@ -0,0 +1,61 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<string>
#include<cmath>
#include<cstdlib>
#include<queue>
using namespace std;
#define ll long long
#define db double
const int maxn=108;
const int INF=0x7f7f7f7f;
int mp[maxn][maxn];
int low[maxn],vis[maxn];
int sum;
void prim(int n)
{
int i,j,k,mi;
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++)
low[i]=mp[1][i];
for(i=2;i<=n;i++)
{
mi=INF;
for(j=2;j<=n;j++)
if(!vis[j]&&low[j]<mi)
{
k=j;
mi=low[j];
}
vis[k]=1;
for(j=2;j<=n;j++)
if(!vis[j]&&low[j]>mp[k][j])
low[j]=mp[k][j];
}
for(i=1;i<=n;i++)
sum+=low[i];
}
int main()
{
int i,j,k,n,a,b,c,d,m;
while(scanf("%d",&n)==1&&n)
{
memset(mp,INF,sizeof(mp));
for(i=1;i<=n;i++) mp[i][i]=0;
m=n*(n-1)/2;
for(i=1;i<=m;i++)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
if(d==1)
mp[a][b]=mp[b][a]=0;
else
mp[a][b]=mp[b][a]=c;
}
sum=0;
prim(n);
printf("%d\n",sum);
}
return 0;
}

54
HDOJ/1880_autoAC.cpp Normal file
View File

@ -0,0 +1,54 @@
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
struct cd
{
string a;
string b;
}dic[100100];
int main(void)
{
string js,tem,cx;
char ct[30];
int tms=0;
while(getline(cin,tem),tem!="@END@")
{
dic[tms].a=tem.substr(0,tem.find(']',0)+1);
dic[tms].b=tem.substr(tem.find(']',0)+2);
++tms;
}
int cs;int i;
cin>>cs;
int a; while((a=getchar())!='\n');
while(cs--&&getline(cin,cx))
{
if(cx[0]=='[')
{
for(i=0;i<tms;i++)
{
if(dic[i].a==cx)
{cout<<dic[i].b<<endl;break;}
}
if(i==tms)
cout<<"what?"<<endl;
}
else
{
for(i=0;i<tms;i++)
{
if(dic[i].b==cx)
{
tem=dic[i].a;
tem=tem.substr(1,tem.size()-2);
cout<<tem<<endl;
break;
}
}
if(i==tms)
cout<<"what?"<<endl;
}
}
return 0;
}

44
HDOJ/1881_autoAC.cpp Normal file
View File

@ -0,0 +1,44 @@
#include <iostream>
#include <algorithm>
using namespace std;
int m[31][1000];
struct bg_typ
{
int h,l,t;
};
bg_typ bg[31];
bool cmp(const bg_ a,const bg_ b)
{
return a.t<b.t;
}
int main()
{
int n;
while(cin>>n&&n>=0)
{
int mmax=0;
for(int t=1;t<=n;++t)
{
cin>>bg[t].h>>bg[t].l>>bg[t].t;
if(bg[t].t>mmax)
mmax=bg[t].t;
}
sort(bg+1,bg+n+1,cmp);
for(int i=0;i<=n;++i)m[i][0]=m[0][i]=0;
for(int i=1;i<=n;++i)
{
for(int j=0;j<=mmax;++j)
{
if(j<=bg[i].t&&j-bg[i].l>=0)
m[i][j]=max(m[i-1][j],m[i-1][j-bg[i].l]+bg[i].h);
else if(j<bg[i].l)
m[i][j]=m[i-1][j];
else
m[i][j]=m[i][bg[i].t];
}
}
int result=m[n][mmax];
cout<<result<<endl;
}
return 0;
}

107
HDOJ/1882_autoAC.cpp Normal file
View File

@ -0,0 +1,107 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#define LL long long
#define _LL __int64
using namespace std;
const int INF = 0x3f3f3f3f;
int n,m;
char map[17][17];
int sta[17],tmp[17];
int bit[17];
int ans;
void cal()
{
bit[0] = 1;
for(int i = 1; i < 17; i++)
bit[i] = (bit[i-1] << 1);
}
void input()
{
memset(sta,0,sizeof(sta));
if(n >= m)
{
for(int i = 0; i < n; i++)
{
scanf("%s",map[i]);
for(int j = 0; j < m; j++)
{
if(map[i][j] == 'X')
sta[i] = (sta[i] << 1) + 1;
else sta[i] <<= 1;
}
}
}
else
{
for(int i = 0; i < n; i++)
{
scanf("%s",map[i]);
for(int j = 0; j < m; j++)
{
if(map[i][j] == 'X')
sta[j] = (sta[j] << 1) + 1;
else sta[j] <<= 1;
}
}
swap(n,m);
}
}
void solve()
{
int step;
ans = INF;
for(int i = 0; i < (1<<m); i++)
{
memcpy(tmp,sta,sizeof(sta));
step = 0;
for(int j = 0; j < m && step < ans; j++)
{
if(bit[j]&i)
{
step++;
if(j > 0)
tmp[0] ^= bit[j-1];
if(j < m-1)
tmp[0] ^= bit[j+1];
tmp[0] ^= bit[j];
tmp[1] ^= bit[j];
}
}
for(int j = 1; j < n && step < ans; j++)
{
for(int k = 0; k < m && step < ans; k++)
{
if(bit[k]&tmp[j-1])
{
step++;
if(k > 0)
tmp[j] ^= bit[k-1];
if(k < m-1)
tmp[j] ^= bit[k+1];
tmp[j] ^= bit[k];
tmp[j+1] ^= bit[k];
}
}
}
if(!tmp[n-1])
ans = min(ans,step);
}
}
int main()
{
cal();
while(~scanf("%d %d",&n,&m))
{
if(n == 0 && m == 0) break;
input();
solve();
if(ans == INF)
printf("Damaged billboard.\n");
else printf("You have to tap %d tiles.\n",ans);
}
return 0;
}

65
HDOJ/1883_autoAC.cpp Normal file
View File

@ -0,0 +1,65 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
const int maxn = 2111;
const double eps = 1e-8;
const double pi = acos(-1.0);
int n, R, ctp;
inline int dcmp(double x) {return (x > eps) - (x < -eps);}
inline double Sqr(double x){return x * x;}
struct Point {int x, y;} p[maxn];
inline double CalDis(const Point &a, const Point &b)
{return sqrt(Sqr(a.x - b.x) + Sqr(a.y - b.y));}
struct Cov { double site; int se;}cover[maxn <<2];
int AScomp(const void *a, const void *b)
{
if(!dcmp((*(Cov*)a).site - (*(Cov*)b).site))
return -((*(Cov*)a).se - (*(Cov*)b).se);
return dcmp((*(Cov*)a).site - (*(Cov*)b).site);
}
void AngManage(double &x)
{
while(x + pi < eps) x += 2 * pi;
while(x - pi > eps) x -= 2 * pi;
}
void AddAnSeg(double start, double end)
{
AngManage(start), AngManage(end);
if(start - end > eps) AddAnSeg(start, pi), AddAnSeg(-pi + eps * 2, end);
else
{
cover[ctp].site = start, cover[ctp].se = 1;++ ctp;
cover[ctp].site = end, cover[ctp].se = -1;++ ctp;
}
}
int MakeAns()
{
int i, j, ans = 0, cnt;
double dis, ang, ac, RR = 2 * (R + 0.001);
for(i = 0 ; i < n; ++ i)
{
for(j = ctp = 0; j < n; ++ j)
if(j != i && (dis = CalDis(p[i], p[j])) < RR)
{
ang = atan2((double)p[j].y - p[i].y, (double)p[j].x - p[i].x);
ac = acos(dis * 0.5 / R);
AddAnSeg(ang - ac, ang + ac);
}
qsort(cover, ctp, sizeof(Cov), AScomp);
for(j = cnt = 0; j < ctp; ++ j)
ans = std::max(ans, cnt += cover[j].se);
}
return ans + 1;
}
int main()
{
while(scanf("%d%d", &n, &R), n | R)
{
for(int i = 0; i < n; ++ i)
scanf("%d%d", &p[i].x, &p[i].y);
printf("It is possible to cover %d points.\n", MakeAns());
}
return 0;
}

82
HDOJ/1884_autoAC.cpp Normal file
View File

@ -0,0 +1,82 @@
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<cmath>
#define ll long long
#define mod 1000000007ll
#define N 100009
#define inf 1000000000ll;
using namespace std;
typedef pair<int,int> II;
int dx[]={0,0,-1,-1,1,1},
dy[]={-1,1,0,-1,0,1};
int n;
char s[100][100];
int d[100][100][1<<4],in[100][100][1<<4];
queue<int> Q;
int get(char c){
if(c=='.') return 0;
return 1<<(c-'A');
}
void update(int x,int y,int z,int v){
if(d[x][y][z]==-1||d[x][y][z]>v){
d[x][y][z]=v;
if(!in[x][y][z]) in[x][y][z]=1,Q.push(x*1600+y*16+z);
}
}
int main(){
while(scanf("%d",&n),n)
{
memset(s,0,sizeof(s));
memset(d,-1,sizeof(d));
memset(in,0,sizeof(in));
while(!Q.empty()) Q.pop();
char c;
for(int i=1;i<=n;i++)
for(int j=1;j<n+i;j++){
while(!isalpha(c=getchar())&&c!='.');
s[i][j]=c;
}
for(int i=2;i<=n;i++)
for(int j=1;j<=2*n-i;j++){
while(!isalpha(c=getchar())&&c!='.');
s[n+i-1][i+j-1]=c;
}
n=2*n-1;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(isalpha(s[i][j]))
update(i,j,get(s[i][j]),0);
int x,y,z,tx,ty,tz;
while(!Q.empty()){
x=Q.front()/1600;
y=(Q.front()%1600)/16;
z=Q.front()%16;
in[x][y][z]=0;
Q.pop();
for(int i=0;i<6;i++){
tx=x+dx[i];
ty=y+dy[i];
if(!s[tx][ty]) continue;
tz=z|get(s[tx][ty]);
update(tx,ty,tz,d[x][y][z]+(s[tx][ty]=='.'));
}
int t=15-z;
for(int i=t;i;i=(i-1)&t)
if(d[x][y][i]!=-1)
update(x,y,z|i,d[x][y][i]+d[x][y][z]-(s[x][y]=='.'));
}
int ans=n*n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(s[i][j]&&d[i][j][15]!=-1)
ans=min(ans,d[i][j][15]);
printf("You have to buy %d parcels.\n",ans);
}
}

94
HDOJ/1885_autoAC.cpp Normal file
View File

@ -0,0 +1,94 @@
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <stack>
#include <queue>
#include <stack>
#include <map>
#define inf 0x3f3f3f3f
using namespace std;
struct point
{
int x,y,z;
};
point b;
map<char,int>gg;
int n,m;
char mp[105][105];
int vis[105][105][16];
int dir[4][2]={ {1,0},{-1,0},{0,-1},{0,1} };
void init()
{
gg['b']=gg['B']=0;
gg['y']=gg['Y']=1;
gg['r']=gg['R']=2;
gg['g']=gg['G']=3;
}
int bfs()
{
queue<point>ss;
ss.push(b);
vis[b.x][b.y][b.z]=0;
while(ss.size())
{
point f=ss.front();
ss.pop();
for(int i=0;i<4;i++)
{
b.x=f.x+dir[i][0];
b.y=f.y+dir[i][1];
b.z=f.z;
if(mp[b.x][b.y]!='#'&&vis[b.x][b.y][b.z]==-1)
{
if(mp[b.x][b.y]=='X') return vis[f.x][f.y][f.z]+1;
if(mp[b.x][b.y]=='.')
{
vis[b.x][b.y][b.z]=vis[f.x][f.y][f.z]+1;
ss.push(b);
continue;
}
int t=gg[mp[b.x][b.y]];
if(isupper(mp[b.x][b.y]))
{
if(b.z>>t&1)
{
vis[b.x][b.y][b.z]=vis[f.x][f.y][f.z]+1;
ss.push(b);
}
}
else
{
b.z=b.z|1<<t;
vis[b.x][b.y][b.z]=vis[f.x][f.y][f.z]+1;
ss.push(b);
}
}
}
}
return 0;
}
int main()
{
init();
while(scanf("%d%d",&n,&m),n+m)
{
memset(mp,'#',sizeof(mp));
memset(vis,-1,sizeof(vis));
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
{
scanf(" %c",&mp[i][j]);
if(mp[i][j]=='*')
{
b.x=i,b.y=j,b.z=0;
mp[i][j]='.';
}
}
int t=bfs();
if(t) printf("Escape possible in %d steps.\n",t);
else printf("The poor student is trapped!\n");
}
return 0;
}

224
HDOJ/1888_autoAC.cpp Normal file
View File

@ -0,0 +1,224 @@
#include <iostream>
#include <string>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cstdio>
#include <map>
#include <stack>
using namespace std;
const int M=1200;
char dd[10]="WNES";
int rr[M];
int cnt;
int n;
struct Point
{
int x,y;
bool operator<(const Point &ret)const
{
if(x!=ret.x)
return x<ret.x;
return y<ret.y;
}
}point[1200];
stack <int> re;
bool check(int i,int x,int y)
{
int sum=0;
if(i==0)
{
for(int j=1;j<=n;j++)
{
if(point[j].y==y&&point[j].x<x)
sum++;
}
}
else if(i==1)
{
for(int j=1;j<=n;j++)
if(point[j].x==x&&point[j].y>y)
{
sum++;
}
}
else if(i==2)
{
for(int j=1;j<=n;j++)
{
if(point[j].y==y&&point[j].x>x)
{
sum++;
}
}
}
else
{
for(int j=1;j<=n;j++)
if(point[j].x==x&&point[j].y<y)
{
sum++;
}
}
if(sum%2==1)
return true;
return false;
}
bool flag;
void dfs(int x,int y,int dir,int depth)
{
int cdir;
if(depth==n)
{
return;
}
if(x==point[1].x&&y==point[1].y)
{
if(dir==1)
flag=true;
else
flag=false;
}
char cc=dd[dir];
int nx,ny;
int nid=-1;
if(cc=='W')
{
for(int i=1;i<=n;i++)
{
if(point[i].y==y&&point[i].x<x)
{
if(nid==-1)
{
ny=point[i].y;
nx=point[i].x;
nid=i;
}
else if(abs(point[i].x-x)<abs(nx-x))
{
ny=point[i].y;
nx=point[i].x;
nid=i;
}
}
}
}
else if(cc=='N')
{
for(int i=1;i<=n;i++)
{
if(point[i].x==x&&point[i].y>y)
{
if(nid==-1)
{
ny=point[i].y;
nx=point[i].x;
nid=i;
}
else if(abs(point[i].y-y)<abs(ny-y))
{
ny=point[i].y;
nx=point[i].x;
nid=i;
}
}
}
}
else if(cc=='E')
{
for(int i=1;i<=n;i++)
{
if(point[i].y==y&&point[i].x>x)
{
if(nid==-1)
{
ny=point[i].y;
nx=point[i].x;
nid=i;
}
else if(abs(point[i].x-x)<abs(nx-x))
{
ny=point[i].y;
nx=point[i].x;
nid=i;
}
}
}
}
else if(cc=='S')
{
for(int i=1;i<=n;i++)
{
if(point[i].x==x&&point[i].y<y)
{
if(nid==-1)
{
ny=point[i].y;
nx=point[i].x;
nid=i;
}
else if(abs(point[i].y-y)<abs(ny-y))
{
ny=point[i].y;
nx=point[i].x;
nid=i;
}
}
}
}
for(int i=0;i<=3;i++)
{
if(i==dir||(i+2)%4==dir)
continue;
if(check(i,nx,ny))
{
cdir=i;
}
}
dfs(nx,ny,cdir,depth+1);
re.push(dir);
}
int main()
{
while(scanf("%d",&n)==1)
{
if(n==0)
break;
for(int i=1;i<=n;i++)
scanf("%d%d",&point[i].x,&point[i].y);
int ax=point[1].x;
int ay=point[1].y;
sort(point+1,point+n+1);
for(int i=3;i>=0;i--)
{
if(check(i,ax,ay))
{
dfs(ax,ay,i,0);
break;
}
}
cnt=0;
while(!re.empty())
{
int c=re.top();
re.pop();
rr[cnt++]=c;
}
if(flag)
{
for(int i=0;i<cnt;i++)
printf("%c",dd[rr[i]]);
}
else
{
for(int i=cnt-1;i>=0;i--)
{
rr[i]=(rr[i]+2)%4;
printf("%c",dd[rr[i]]);
}
}
printf("\n");
}
return 0;
}

152
HDOJ/1890_autoAC.cpp Normal file
View File

@ -0,0 +1,152 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define Keytree (ch[ ch[root][1] ][0])
const int maxn = 100010;
struct node {
int num,id;
}in[maxn];
int id[maxn];
int cmp(node a,node b){
if(a.num!=b.num) return a.num<b.num;
return a.id<b.id;
}
struct SplayTree{
int tot,root;
int pre[maxn];
int size[maxn];
int ch[maxn][2];
inline void Rotate(int x, int c) {
int y = pre[x];
pushdown(y);
pushdown(x);
ch[y][!c] = ch[x][c];
if ( ch[x][c] ) pre[ ch[x][c] ] = y;
pre[x] = pre[y];
if ( pre[y] ) ch[ pre[y] ][ ch[pre[y]][1] == y ] = x;
ch[x][c] = y;
pre[y] = x;
pushup(y);
}
inline void Splay(int x, int f) {
pushdown(x);
while ( pre[x] != f ) {
int y = pre[x], z = pre[y];
pushdown(z); pushdown(y); pushdown(x);
if ( pre[ pre[x] ] == f ) {
Rotate(x, ch[pre[x]][0] == x);
}
else {
if ( ch[z][0] == y ) {
if ( ch[y][0] == x )
Rotate(y, 1), Rotate(x, 1);
else
Rotate(x, 0), Rotate(x, 1);
}
else {
if ( ch[y][0] == x )
Rotate(x, 1), Rotate(x, 0);
else
Rotate(y, 0), Rotate(x, 0);
}
}
}
pushup(x);
if ( f == 0 ) root = x;
}
inline void Select(int k, int f) {
int x = root;
while ( 1 ) {
pushdown(x);
if ( k == size[ ch[x][0] ] + 1 )
break;
if ( k <= size[ ch[x][0] ] )
x = ch[x][0];
else {
k -= (size[ ch[x][0] ] + 1);
x = ch[x][1];
}
}
Splay(x, f);
}
inline void del_root(){
int t=root;
if(ch[root][1]) {
root=ch[root][1];
Select(1,0);
ch[root][0]=ch[t][0];
if(ch[t][0]) pre[ch[t][0]]=root;
}
else
root=ch[root][0];
pre[root]=0;
pushup(root);
}
inline void pushup(int x){
size[x]=size[ ch[x][0] ] + size[ ch[x][1] ] +1;
}
inline void pushdown(int x){
if(flip[x]){
flip[x]=0;
flip[ch[x][0]]^=1;
flip[ch[x][1]]^=1;
swap(ch[x][0],ch[x][1]);
}
}
void Newnode(int &x,int f){
x=++tot;
pre[x]=f;
ch[x][0]=ch[x][1]=0;
flip[x]=0;
size[x]=1;
}
void build(int &x,int l,int r,int f){
if(l>r) return ;
int mid=(l+r)>>1;
Newnode(x,f);
map[id[mid]]=x;
build(ch[x][0],l,mid-1,x);
build(ch[x][1],mid+1,r,x);
pushup(x);
}
void init(int n){
int i;
pre[0]=ch[0][0]=ch[0][1]=0;
size[0]=flip[0]=tot=0;
for(i=1;i<=n;i++) {
scanf("%d",&in[i].num);
in[i].id=i;
}
sort(in+1,in+n+1,cmp);
for(i=1;i<=n;i++) id[in[i].id]=i;
map[id[1]] = 1;
map[id[n]] = 2;
Newnode(root,0);
Newnode(ch[root][1],root);
build(Keytree,2,n-1,ch[root][1]);
pushup(ch[root][1]);
pushup(root);
}
void solve(int n){
for(int i=1;i<=n;i++){
Splay(map[i],0);
printf("%d",i+size[ch[root][0]]);
if(i<n) printf(" ");
flip[ch[root][0]]^=1;
del_root();
}
puts("");
}
int map[maxn];
int flip[maxn];
}spt;
int main(){
int n;
while(scanf("%d",&n),n) {
if(n==1) {scanf("%*d");printf("1\n");continue;}
spt.init(n);
spt.solve(n);
}
return 0;
}

103
HDOJ/1891_autoAC.cpp Normal file
View File

@ -0,0 +1,103 @@
#include<iostream>
using namespace std;
int a[1010][1010],c[1010][1010],n=1005;
int lowbit(int x)
{
return x&(-x);
}
int sum(int x,int y)
{
int i,j,sum=0;
for(i=x;i>0;i-=lowbit(i))
{
for(j=y;j>0;j-=lowbit(j))
{
sum+=c[i][j];
}
}
return sum;
}
void inster(int x,int y,int z)
{
int i,j;
for(i=x;i<=n;i+=lowbit(i))
{
for(j=y;j<=n;j+=lowbit(j))
{
c[i][j]+=z;
}
}
}
int main()
{
int x1,y1,x2,y2,t,m,i,j,n1,f=0;
scanf("%d",&t);
while(t--)
{
printf("Case %d:\n",++f);
memset(a,0,sizeof(a));
memset(c,0,sizeof(c));
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
a[i][j]=1;
inster(i,j,1);
}
scanf("%d",&m);
while(m--)
{
char s;
getchar();
s=getchar();
if(s=='S')
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x1++;x2++;y1++;y2++;
if(x1>x2)
swap(x1,x2);
if(y1>y2)
swap(y1,y2);
printf("%d\n",sum(x2,y2)-sum(x2,y1-1)-sum(x1-1,y2)+sum(x1-1,y1-1));
}
else
if(s=='A')
{
scanf("%d%d%d",&x1,&y1,&n1);
x1++;y1++;
inster(x1,y1,n1);
a[x1][y1]+=n1;
}
else
if(s=='D')
{
scanf("%d%d%d",&x1,&y1,&n1);
x1++;y1++;
if(n1>a[x1][y1])
n1=a[x1][y1];
inster(x1,y1,-n1);
a[x1][y1]-=n1;
}
else
if(s=='M')
{
scanf("%d %d %d %d %d",&x1,&y1,&x2,&y2,&n1);
x1++;y1++;x2++;y2++;
if(a[x1][y1]>=n1)
{
a[x1][y1]-=n1;
inster(x1,y1,-n1);
inster(x2,y2,n1);
a[x2][y2]+=n1;
}
else
{
inster(x1,y1,-a[x1][y1]);
inster(x2,y2,a[x1][y1]);
a[x2][y2]+=a[x1][y1];
a[x1][y1]=0;
}
}
}
}
return 0;
}

33
HDOJ/1893_autoAC.cpp Normal file
View File

@ -0,0 +1,33 @@
# include<stdio.h>
# define inf 1000000007
const double eps=1e-8;
__int64 a[100005];
void init(){
int i;
for(i=0;i<=313;i++)
a[i] = 1;
for(i=314;i<=100000;i++){
a[i]= a[i-100] + a[i-314];
a[i]=a[i]%inf;
}
}
int main(){
int T,y,i;
double x;
init();
scanf("%d",&T);
while(T--){
scanf("%lf",&x);
if(x<0)
printf("0\n");
else if(x<1)
printf("1\n");
else
{
x=x*100;
y=(int)(x+eps);
printf("%I64d\n",a[y]);
}
}
return 0;
}

34
HDOJ/1894_autoAC.cpp Normal file
View File

@ -0,0 +1,34 @@
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
string a[100010];
int main()
{
int cas,i,j,n,cnt;
cin>>cas;
while(cas--)
{
cnt=0;
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i].size()>=a[j].size()) break;
if(a[i].compare(0,a[i].size(),a[j],0,a[i].size())==0)
cnt++;
else break;
}
}
if(cnt>11519) cnt=cnt%11519;
printf("%d\n",cnt);
}
return 0;
}

71
HDOJ/1895_autoAC.cpp Normal file
View File

@ -0,0 +1,71 @@
#include <iostream>
#include <cstring>
#include <map>
#include <cmath>
#include <algorithm>
using namespace std;
#define SIZE 310
int a[5][SIZE],n[5],b[SIZE*SIZE],c[SIZE*SIZE];
int main()
{
int t,i,j,len1,len2,len3,k,p1,p2,carry,flg;
__int64 ans;
scanf("%d",&t);
while(t--)
{
for(i=0;i<5;i++)
{
scanf("%d",&n[i]);
for(j=0;j<n[i];j++)
{
scanf("%d",&a[i][j]);
}
}
len1=0,len2=0,len3=n[4];
for(i=0;i<n[0];i++)
{
for(j=0;j<n[1];j++)
{
b[len1++]=a[0][i]+a[1][j];
}
}
for(i=0;i<n[2];i++)
{
for(j=0;j<n[3];j++)
{
c[len2++]=a[2][i]+a[3][j];
}
}
ans=0;
sort(b,b+len1);
sort(c,c+len2);
for(i=0;i<len3;i++)
{
carry=len2-1;
for(j=0;j<len1 && carry >= 0;j++)
{
while(a[4][i]+b[j]+c[carry]>0)
{
carry--;
}
if(a[4][i]+b[j]+c[carry] == 0)
{
p1=p2=1;
while(j<len1-1 && b[j] == b[j+1])
{
j++;
p1++;
}
while(carry>0 && c[carry] == c[carry-1])
{
carry--;
p2++;
}
ans+=p1*p2;
}
}
}
printf("%I64d\n",ans%1000000007);
}
return 0;
}

44
HDOJ/1896_autoAC.cpp Normal file
View File

@ -0,0 +1,44 @@
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct stone{int pos , dis;};
struct compare
{
bool operator() (const stone & s1 , const stone & s2)
{
if(s1.pos != s2.pos){return s1.pos > s2.pos;}
return s1.dis > s2.dis;
}
};
int main()
{
int t;
cin >> t;
while(t --)
{
int n;
cin >> n;
priority_queue<stone , vector<stone> , compare> record;
for(int i = 1; i <= n; ++i)
{
stone s;
cin >> s.pos >> s.dis;
record.push(s);
}
int cnt = 1;
while(!record.empty())
{
stone t = record.top();
record.pop();
if(cnt % 2)
{
t.pos += t.dis;
record.push(t);
}
if(record.empty()){cout << t.pos << endl;}
++cnt;
}
}
return 0;
}

88
HDOJ/1897_autoAC.cpp Normal file
View File

@ -0,0 +1,88 @@
#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <map>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <functional>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cassert>
#include <bitset>
#include <stack>
#include <ctime>
#include <list>
#define INF 0x7fffffff
#define max3(a,b,c) (max(a,b)>c?max(a,b):c)
#define min3(a,b,c) (min(a,b)<c?min(a,b):c)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int main()
{
int t;
scanf("%d", &t);
multiset<int> Q;
map<int, int> P;
int n, q, y;
int cnt = 0;
while(t--)
{
cnt++;
Q.clear();
P.clear();
scanf("%d%d%d", &n, &q, &y);
int lp;
for(int i = 0; i < n; ++i)
{
scanf("%d", &lp);
P[lp]++;
Q.insert(lp);
}
multiset<int>::iterator kp;
printf("Case %d:\n", cnt);
for(int i = 0; i < q; ++i)
{
int tp;
scanf("%d", &tp);
int tt = tp + y;
multiset<int>::iterator it = Q.lower_bound(tp);
if(it == Q.end())
printf("-1\n");
else
{
int tmp = (*it);
if(tmp == tp)
{
printf("%d\n", tp);
Q.erase(*it);
P[(*it)] --;
for(int kk = 0; kk < P[(*it)]; ++kk)
Q.insert(tp);
}
else
{
int qq = (*it);
if(qq <= (tt))
{
printf("%d\n", qq);
P[qq]--;
Q.erase(qq);
for(int kk = 0; kk < P[qq]; ++kk)
Q.insert(qq);
}
else
printf("-1\n");
}
}
}
}
return 0;
}

18
HDOJ/1898_autoAC.cpp Normal file
View File

@ -0,0 +1,18 @@
#include<stdio.h>
int main()
{
int a,b,n,q,w;
long int t;
scanf("%d",&n);
while(n--)
{
scanf("%d%d%ld",&a,&b,&t);
q=t%a;w=t%b;
if(q<w)
printf("Sempr!\n");
else if(q==w)
printf("Both!\n");
else
printf("Xiangsanzi!\n");
}
}