Powered By HC TECH : AutoACer Engine

3400-3499
This commit is contained in:
KiritoTRw 2016-09-04 14:10:14 +08:00 committed by GitHub
parent 120ba634b8
commit c14ecbe72c
87 changed files with 7364 additions and 0 deletions

67
HDOJ/3400_autoAC.cpp Normal file
View File

@ -0,0 +1,67 @@
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
struct point
{
double x,y;
};
const double eps=1e-4;
double p,q,r;
double dis(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double cal_ab(point a,point b,point m)
{
point left=a,right=b;
point mid,midmid;
double ans1,ans2;
do
{
mid.x=(left.x+right.x)/2.0;
mid.y=(left.y+right.y)/2.0;
midmid.x=(left.x+mid.x)/2.0;
midmid.y=(left.y+mid.y)/2.0;
ans1=dis(mid,a)/p+dis(mid,m)/r;
ans2=dis(midmid,a)/p+dis(midmid,m)/r;
if(ans1<ans2)
left=midmid;
else
right=mid;
}while(fabs(ans1-ans2)>eps);
return ans1;
}
double cal_abcd(point a,point b,point c,point d)
{
point left=c,right=d;
point m,mm;
double ans1,ans2;
do
{
m.x=(left.x+right.x)/2.0;
m.y=(left.y+right.y)/2.0;
mm.x=(left.x+m.x)/2.0;
mm.y=(left.y+m.y)/2.0;
ans1=dis(m,d)/q+cal_ab(a,b,m);
ans2=dis(mm,d)/q+cal_ab(a,b,mm);
if(ans1<ans2)
left=mm;
else
right=m;
}while(fabs(ans1-ans2)>eps);
return ans1;
}
int main()
{
int t;
point a,b,c,d;
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&d.x,&d.y);
scanf("%lf%lf%lf",&p,&q,&r);
printf("%.2lf\n",cal_abcd(a,b,c,d));
}
return 0;
}

66
HDOJ/3401_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include <cstdio>
#include <cstring>
const int MAXN = 2000 + 123;
int ap[MAXN], bp[MAXN], as[MAXN], bs[MAXN];
int f[MAXN][MAXN];
struct node
{
int num, val;
}q[MAXN];
inline int min(int x,int y)
{
return x<y?x:y;
}
inline int max(int x,int y)
{
return x>y?x:y;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int t, maxp, w;
scanf("%d%d%d", &t, &maxp, &w);
for(int i = 1; i <= t; i++) scanf("%d%d%d%d",&ap[i], &bp[i], &as[i], &bs[i]);
memset(f, 200, sizeof(f));
for(int i = 1; i <= w + 1; i++)
for(int j = 0; j <= min(as[i], maxp); j++)
{
f[i][j] = -ap[i] * j;
}
for(int i = 2; i <= t; i++)
{
for(int j = 0; j <= maxp; j++) f[i][j] = max(f[i][j], f[i-1][j]);
if(i < w + 2) continue;
int head = 0, tail = -1;
q[++tail].num = 0;
q[tail].val = f[i - w - 1][0];
for(int j = 1; j <= maxp; j++)
{
while(tail >= head && j - q[head].num > as[i]) head++;
if(head <= tail) f[i][j] = max(f[i][j], q[head].val - (j - q[head].num) * ap[i]);
while(tail >= head && q[tail].val + (q[tail].num - j) * ap[i] < f[i - w - 1][j]) tail--;
q[++tail].num = j;
q[tail].val = f[i- w - 1][j];
}
head = 0, tail = -1;
q[++tail].num = maxp;
q[tail].val = f[i - w - 1][maxp];
for(int j = maxp - 1; j >= 0; j--)
{
while(tail >= head && q[head].num - j > bs[i]) head++;
if(head <= tail) f[i][j] = max(f[i][j], q[head].val + (q[head].num - j) * bp[i]);
while(tail >= head && q[tail].val + (q[tail].num - j) * bp[i] < f[i - w - 1][j]) tail--;
q[++tail].num = j;
q[tail].val = f[i - w - 1][j];
}
}
int rmax = 0;
for(int i = 0; i <= maxp; i++)
if(f[t][i] > rmax) rmax = f[t][i];
printf("%d\n", rmax);
}
return 0;
}

35
HDOJ/3402_autoAC.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
double PI = 3.1415926 * 2;
int t,n;
double r;
double dis_tance;
double minn;
int map[100010];
int main()
{
scanf("%d",&t);
while(t --)
{
minn = 100000.00;
scanf("%d%lf",&n,&r);
memset(map,0,sizeof(map));
for(int i = 1;i <= n;i ++)
scanf("%d",&map[i]);
if(map[1] == map[n])
printf("inf\n");
else
{
sort(map + 1,map + n + 1);
dis_tance = (PI * r) / n;
for(int i = n;i > 1;i --)
if(dis_tance / (map[i] - map[i - 1]) < minn)
minn = dis_tance / (map[i] - map[i - 1]);
printf("%.3lf\n",minn);
}
}
return 0;
}

104
HDOJ/3403_autoAC.cpp Normal file
View File

@ -0,0 +1,104 @@
#include<iostream>
#include<cstring>
#include<map>
#include<algorithm>
#include<stack>
#include<queue>
#include<cstring>
#include<cmath>
#include<string>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<set>
#include<list>
#include<numeric>
#include<cassert>
#include<sstream>
#include<ctime>
#include<bitset>
#include<functional>
using namespace std;
typedef pair<int, int> pii;
int day[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool isLeap(long long year)
{
return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
}
vector<pii> ht;
vector<string> pvt[10];
vector<string> leapVt[10];
int sum[10];
int rev(int t)
{
char str[10];
sprintf(str, "%04d", t);
reverse(str, str+4);
return atoi(str);
}
void init()
{
for (int mm = 1; mm <= 12; mm++) {
for (int dd = 1; dd <= day[mm]; dd++) {
if (dd % 10 == 0) continue;
int t = mm*100 + dd;
ht.push_back(pii(rev(t), t));
}
}
sort(ht.begin(), ht.end());
long long now = 9220;
for (int n = 0; n <= 7; n++, now *= 10) {//9220*..*0229
if (n == 0) {
pvt[n].push_back("");
leapVt[n].push_back("");
} else if (n == 1) {
for (char i = '0'; i <= '9'; i++) {
pvt[n].push_back(string(1, i));//
int year = now+(i-'0');
if (isLeap(year)) {
leapVt[n].push_back(pvt[n].back());
}
}
} else {
int nn = n - 2;
for (char i = '0'; i <= '9'; i++) {
for (vector<string>::iterator it = pvt[nn].begin(); it != pvt[nn].end(); it++) {
pvt[n].push_back(i+*it+i);
long long year = now+atoi(pvt[n].back().c_str());
if (isLeap(year)) {
leapVt[n].push_back(pvt[n].back());
}
}
}
}
sum[n] = 330*pvt[n].size() + leapVt[n].size();
}
}
int main()
{
init();
int T, k, n;
for (scanf("%d", &T); T--; ) {
scanf("%d", &k);
k--;
for (n = 0; n <= 7; n++) {
if (k < sum[n]) break;
else k -= sum[n];
}
int num = pvt[n].size();
if (k >= 322*num) {
k -= 322*num;
if (k < leapVt[n].size()) {
printf("9220%s0229\n", leapVt[n][k].c_str());
} else {
k -= leapVt[n].size();
int p = k/num + 322;
printf("%d%s%04d\n", ht[p].first, pvt[n][k%num].c_str(), ht[p].second);
}
} else {
int p = k/num;
printf("%d%s%04d\n", ht[p].first, pvt[n][k%num].c_str(), ht[p].second);
}
}
return 0;
}

50
HDOJ/3404_autoAC.cpp Normal file
View File

@ -0,0 +1,50 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#define N 2000000
using namespace std;
int m[2][2]={0,0,0,1};
int Nim_Mult_Power(int x,int y){
if(x<2)
return m[x][y];
int a=0;
for(;;a++)
if(x>=(1<<(1<<a))&&x<(1<<(1<<(a+1))))
break;
int m=1<<(1<<a);
int p=x/m,s=y/m,t=y%m;
int d1=Nim_Mult_Power(p,s);
int d2=Nim_Mult_Power(p,t);
return (m*(d1^d2))^Nim_Mult_Power(m/2,d1);
}
int Nim_Mult(int x,int y){
if(x<y)
return Nim_Mult(y,x);
if(x<2)
return m[x][y];
int a=0;
for(;;a++)
if(x>=(1<<(1<<a))&&x<(1<<(1<<(a+1))))
break;
int m=1<<(1<<a);
int p=x/m,q=x%m,s=y/m,t=y%m;
int c1=Nim_Mult(p,s),c2=Nim_Mult(p,t)^Nim_Mult(q,s),c3=Nim_Mult(q,t);
return (m*(c1^c2))^c3^Nim_Mult_Power(m/2,c1);
}
int main(){
int t,n,x,y;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int ret=0;
while(n--){
scanf("%d%d",&x,&y);
ret^=Nim_Mult(x,y);
}
if(ret)
puts("Have a try, lxhgww.");
else
puts("Don't waste your time.");
}
return 0;
}

82
HDOJ/3405_autoAC.cpp Normal file
View File

@ -0,0 +1,82 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
const int inf=99999991;
using namespace std;
double map[55][55],dis[55];
int n;
bool vs[55];
struct Node
{
int x,y;
}node[55];
double Min(double a,double b)
{
if(a>b)return b;
else return a;
}
double prim(int x)
{
int i,k,mark;
memset(vs,0,sizeof(vs));
vs[x]=1;
for(i=0;i<n;i++)
{
dis[i]=map[i][0];
}vs[0]=1;
if(x==0)
{
for(i=0;i<n;i++){
dis[i]=map[i][1];
}vs[1]=1;
}
double MIN,SUM=0;
for(k=1;k<n-1;k++)
{
MIN=inf;
for(i=0;i<n;i++)
if(!vs[i]&&MIN>dis[i])
{
MIN=dis[i];
mark=i;
}
SUM+=MIN;
vs[mark]=1;
for(i=0;i<n;i++)
if(!vs[i]&&dis[i]>map[mark][i])
dis[i]=map[mark][i];
}
return SUM;
}
int main()
{
int i,j,CASE;
double d,x1,y1;
scanf("%d",&CASE);
while(CASE--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
{
map[i][i]=0;
scanf("%d%d",&node[i].x,&node[i].y);
for(j=i-1;j>=0;j--)
{
x1=node[i].x-node[j].x;
y1=node[i].y-node[j].y;
d=sqrt(x1*x1+y1*y1);
map[i][j]=map[j][i]=d;
}
}
double h,g=inf;
for(i=0;i<n;i++)
{
h=prim(i);
g=Min(g,h);
}
printf("%.2lf\n",g);
}
return 0;
}

74
HDOJ/3406_autoAC.cpp Normal file
View File

@ -0,0 +1,74 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int dp[(1<<16)+2][10][4];
int map[17][17];
int bit[(1<<16)+2];
int main()
{
int i,j,n,cnt,k,l,m,p,a,ans;
for (i=0;i<(1<<16);i++)
{
cnt=0;
for (j=0;j<16;j++)
{
if ((i & (1<<j))!=0) cnt++;
}
bit[i]=cnt;
}
while(1)
{
scanf("%d",&n);
if (n==0) break;
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
scanf("%d",&map[i][j]);
}
}
memset(dp,-1,sizeof(dp));
ans=0;
dp[0][0][0]=0;
for (i=0;i<(1<<n);i++)
{
for (j=0;j<(1<<3);j++)
{
for (k=0;k<3;k++)
{
if (dp[i][j][k]==-1) continue;
ans=max(ans,dp[i][j][k]);
if (k==3) continue;
for (l=0;l<n;l++)
{
if ((i & (1<<l))!=0) continue;
p=(i | (1<<l));
cnt=bit[p]-1;
if (map[l][cnt]==0)
{
dp[p][j][k+1]=max(dp[p][j][k+1],dp[i][j][k]);
}
if (map[l][cnt]==1)
{
if (k==2) continue;
if ((j & 4)!=0) dp[p][(j<<1) & 7][k+1]=max(dp[p][(j<<1) & 7][k+1],dp[i][j][k]+1);
else dp[p][(j<<1) & 7][k+1]=max(dp[p][(j<<1) & 7][k+1],dp[i][j][k]);
}
if (map[l][cnt]==2)
{
if ((j & 4)!=0) dp[p][((j<<1) & 7)+1][k]=max(dp[p][((j<<1) & 7)+1][k],dp[i][j][k]+1);
else dp[p][((j<<1) & 7)+1][k]=max(dp[p][((j<<1) & 7)+1][k],dp[i][j][k]);
}
if (map[l][cnt]==3)
{
dp[p][0][k]=max(dp[p][0][k],dp[i][j][k]+((j & 1)!=0)+((j & 2)!=0)+((j & 4)!=0)+1);
}
}
}
}
}
printf("%d\n",ans);
}
return 0;
}

103
HDOJ/3407_autoAC.cpp Normal file
View File

@ -0,0 +1,103 @@
#include <iostream>
#include <cstdio>
#include <memory.h>
#include <algorithm>
#include <queue>
#define MAXN 100006
#define kind 26
using namespace std;
char word[MAXN];
struct Node
{
int next[kind],v,fail;
}Trie[5*MAXN+10];
int snode;
int N;
int cas;
void set_node(int x)
{
memset(Trie[x].next,0,sizeof(Trie[x].next));
Trie[x].v=0;
Trie[x].fail=0;
}
void Insert(char s[])
{
int i=0,pos=0,index;
int len=strlen(s);
for(i=0;i<len;i++)
{
index=s[i]-'a';
if(Trie[pos].next[index]==0)
{
set_node(++snode);
Trie[pos].next[index]=snode;
}
pos=Trie[pos].next[index];
}
}
void Find_Fail(int pre,int ith,int now)
{
if(!pre)
{
Trie[now].fail=0;
return;
}
int temp=Trie[pre].fail;
while(temp!=-1)
{
if(Trie[temp].next[ith]!=0)
{
Trie[now].fail=Trie[temp].next[ith];
return;
}
temp=Trie[temp].fail;
}
if(temp==-1)
Trie[now].fail=0;
return;
}
queue<int>Q;
void Build_AC_Fail()
{
int i,pos;
while(!Q.empty()) Q.pop();
Trie[0].fail=-1;
Q.push(0);
while(!Q.empty())
{
pos=Q.front();Q.pop();
for(i=0;i<kind;i++)
{
if(Trie[pos].next[i]!=0)
{
Find_Fail(pos,i,Trie[pos].next[i]);
Q.push(Trie[pos].next[i]);
}
else
Trie[pos].next[i]=Trie[Trie[pos].fail].next[i];
}
}
}
void Solve()
{
int i,j;
set_node(0);snode=0;
Insert(word);
Build_AC_Fail();
for(i=0;i<=snode;i++)
{
printf("%d",i);
for(j=0;j<26;j++)
printf(" %d",Trie[i].next[j]);
printf("\n");
}
}
int main()
{
while(scanf("%s",word)!=EOF)
{
if(word[0]=='0') break;
Solve();
}
return 0;
}

130
HDOJ/3408_autoAC.cpp Normal file
View File

@ -0,0 +1,130 @@
#include<iostream>
#include<cmath>
#include<complex>
#include<algorithm>
using namespace std;
typedef complex<double> point;
const int maxn = 15;
const double eps = 1e-8;
const double pi = acos( -1.0 );
struct line
{
point a, b;
line( ){ }
line( point u, point v ) : a( u ), b( v ) { }
};
struct polygon
{
point p[maxn];
int n;
};
int dblcmp( double x ){ return ( x < -eps ? -1 : x > eps ); }
double operator^( point p1, point p2 ){ return imag( conj( p1 ) * p2 ); }
double operator&( point p1, point p2 ){ return real( conj( p1 ) * p2 ); }
point operator*( line l0, line l1 )
{
double t = l0.a - l1.a ^ l0.b - l1.a;
double s = l0.b - l1.b ^ l0.a - l1.b;
return l1.a + ( l1.b - l1.a ) * t / ( t + s );
}
polygon poly[10];
bool same_point( point p1, point p2 )
{
return dblcmp( imag( p1 ) - imag( p2 ) ) == 0 && dblcmp( real( p1 ) - real( p2 ) ) == 0;
}
bool on_radial( point p, line l )
{
if( dblcmp( l.a - p ^ l.b - p ) != 0 )return false;
return dblcmp( p - l.a & l.b - l.a ) >= 0;
}
bool on_segment( point p, line l )
{
if( dblcmp( l.a - p ^ l.b - p ) != 0 )return false;
return dblcmp( l.a - p & l.b - p ) <= 0;
}
int main( )
{
int i, j, n, m, sz, len;
double x, y, dx, dy;
point ss, kk, pp, pp1;
point xp[200];
line L, L0;
while( scanf("%d",&n) && n )
{
scanf("%lf%lf",&x, &y);
ss = point( x, y );
scanf("%lf%lf",&dx, &dy);
kk = point( dx, dy );
L.a = ss;
L.b = ss + kk;
pp = ss;
for( i = 0; i < n; i++ )
{
scanf("%d",&m);
poly[i].n = m;
for( j = 0; j < m; j++ )
{
scanf("%lf%lf",&x, &y);
poly[i].p[j] = point( x, y );
}
poly[i].p[m] = poly[i].p[0];
}
sz = poly[0].n;
for( i = 0; i < sz; i++ )
{
L0 = line( poly[0].p[i], poly[0].p[i+1] );
if( dblcmp( L.b - L.a ^ L0.b - L0.a ) == 0 )
{
if( on_radial( L0.a, L ) )
{
if( abs( L0.a - ss ) < abs( L0.b - ss ) ) pp = L0.a;
else pp = L0.b;
break;
}
}
else
{
pp1 = L0 * L;
if( on_radial( pp1, L ) && on_segment( pp1, L0 ) )
{
if( same_point( pp , ss ) )
pp = pp1;
else if( abs( pp1 - ss ) < abs( pp - ss ) )
pp = pp1;
}
}
}
if( same_point( pp, ss ) )
{
printf("MISS\n");
continue;
}
len = 0;
for( i = 0; i < n; i++ )
{
sz = poly[i].n;
for( j = 0; j < sz; j++ )
{
L0 = line( poly[i].p[j], poly[i].p[j+1] );
if( dblcmp( L.b - L.a ^ L0.b - L0.a ) == 0 )
{
if( on_radial( L0.a, L ) )
xp[len++] = L0.a, xp[len++] = L0.b;
}
else
{
pp1 = L0 * L;
if( on_segment( pp1, L0 ) )
xp[len++] = pp1;
}
}
}
double min = 1e10;
for( i = 0; i < len; i++ )
if( abs( xp[i] - ss ) < min )
pp1 = xp[i], min = abs( xp[i] - ss );
if( same_point( pp, pp1 ) )printf("HIT\n");
else printf("MISS\n");
}
return 0;
}

107
HDOJ/3409_autoAC.cpp Normal file
View File

@ -0,0 +1,107 @@
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 205;
const int maxe = 20500;
const int INF = 0x3f3f3f;
struct Edge{
int u,v,w;
int next;
void assign(int u_,int v_,int w_,int next_){
u = u_; v = v_; w = w_; next = next_;
}
bool operator < (const Edge& e) const {
return w > e.w;
}
}edges[maxe];
int head[maxn];
vector<int> G[maxn];
int cnt;
int N,M,P;
double PT[maxn][maxn/2];
double dp[maxn][maxn/2];
void addedge(int u,int v,int w){
edges[cnt].assign(u,v,w,head[u]);
head[u] = cnt++;
edges[cnt].assign(v,u,w,head[v]);
head[v] = cnt++;
}
void Dijkstra(){
priority_queue<Edge> Q;
int d[maxn];
bool vis[maxn];
memset(d,0x3f,sizeof(d));
memset(vis,0,sizeof(vis));
for(int i=0;i<N;i++) G[i].clear();
Q.push((Edge){0,0,0});
d[0] = 0;
while(!Q.empty()){
Edge e = Q.top(); Q.pop();
int u = e.u;
if(vis[u]) continue;
vis[u] = true;
if(e.u != e.v){
G[e.v].push_back(e.u);
}
for(int i=head[u];i!=-1;i=edges[i].next){
int v = edges[i].v;
if(d[v] > d[u] + edges[i].w){
d[v] = d[u] + edges[i].w;
Q.push((Edge){v,u,d[v]});
}
}
}
}
void dfs(int u){
int child = G[u].size();
double son[maxn];
for(int i=0;i<=P;i++) son[i] = 0;
if(child == 0){
for(int i=1;i<=P;i++){
dp[u][i] = PT[u][i];
}
return;
}
for(int i=0;i<child;i++){
int v = G[u][i];
dfs(v);
for(int j=P;j>=0;j--)
for(int k=0;k<=j;k++)
son[j] = max(son[j],dp[v][k]/child+son[j-k]);
}
for(int i=P;i>=0;i--)
for(int j=0;j<=i;j++){
dp[u][i] = max(dp[u][i],PT[u][j] + (1-PT[u][j])* son[i-j]);
}
}
int main()
{
while(scanf("%d%d",&N,&M) == 2 && N+M){
cnt = 0;
memset(dp,0,sizeof(dp));
memset(head,-1,sizeof(head));
for(int i=1;i<=M;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a == b) continue;
addedge(a,b,c);
}
scanf("%d",&P);
for(int i=0;i<N;i++) PT[i][0] = 0;
for(int i=0;i<N;i++)
for(int j=1;j<=P;j++){
scanf("%lf",&PT[i][j]);
}
Dijkstra();
dfs(0);
double ans = 0;
for(int i=0;i<=P;i++)
ans = max(ans,dp[0][i]);
printf("%.2lf\n",ans*100);
}
}

48
HDOJ/3410_autoAC.cpp Normal file
View File

@ -0,0 +1,48 @@
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 50010;
int main()
{
int iCase, n, ic = 1;
int arr[MAXN], que[MAXN];
int rear, front;
int l[MAXN], r[MAXN];
bool flag;
scanf("%d", &iCase);
while (iCase--) {
arr[0] = 0;
memset(que, 0, sizeof(que));
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d", &arr[i]);
front = 0, rear = -1;
for (int i = 1; i <= n; ++i) {
flag = false;
while (front <= rear && arr[que[rear]] < arr[i]) {
flag = true;
rear--;
}
if (flag) l[i] = que[rear+1];
else l[i] = 0;
que[++rear] = i;
}
front = 0, rear = -1;
for (int i = n; i >= 1; --i) {
flag = false;
while (front <= rear && arr[que[rear]] < arr[i]) {
flag = true;
rear--;
}
if (flag) r[i] = que[rear+1];
else r[i] = 0;
que[++rear] = i;
}
printf("Case %d:\n", ic++);
for (int i = 1; i <= n; ++i) {
printf("%d %d\n", l[i], r[i]);
}
}
return 0;
}

80
HDOJ/3411_autoAC.cpp Normal file
View File

@ -0,0 +1,80 @@
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#define N 50005
#define M 30
#define read(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define cl(a) memset(a,0,sizeof(a))
#define ll long long
ll x[M],f[3],r[N][3][3],q;
int p;
using namespace std;
ll bin(ll t)
{
ll res=1;
int i=0;
while (t>0)
{
if (t&1) res=(res*x[i])%p;
i++;
t>>=1;
}
return res;
}
void init()
{
r[0][1][1]=(q-1+p)%p;
r[0][1][2]=q;
r[0][2][1]=1;
r[0][2][2]=0;
f[1]=1;f[2]=0;
}
void mul(int t)
{
int i,j,k;
for (i=1;i<=2;i++)
for (j=1;j<=2;j++)
{
r[t][i][j]=0;
for (k=1;k<=2;k++) r[t][i][j]=r[t][i][j]+(r[t-1][i][k]*r[t-1][k][j])%p;
}
}
void zmul(int i)
{
ll g1,g2;
g1=(f[1]*r[i][1][1])%p+(f[2]*r[i][1][2])%p;
g2=(f[1]*r[i][2][1])%p+(f[2]*r[i][2][2])%p;
f[1]=g1%p;f[2]=g2%p;
}
void bin2(int t)
{
int i=0;
while (t>0)
{
if (t&1) zmul(i);
t>>=1;
i++;
}
}
int main()
{
int x1,y1,z1,y2,z2,i,j;
while (read(x1,y1,z1)!=EOF)
{
if (x1==-1&&y1==-1&&z1==-1) break;
read(y2,z2,p);
cl(x);
x[0]=x1%p;
for (i=1;i<M;i++) x[i]=(x[i-1]*x[i-1])%p;
q=(bin(y1)+z1)%p;
init();
for (i=1;i<N;i++) mul(i);
zmul(y2);
if (z2>0) bin2(z2-1);
if (z2>=1) printf("%d\n",f[1]);
else printf("%d\n",f[2]);
}
return 0;
}

60
HDOJ/3412_autoAC.cpp Normal file
View File

@ -0,0 +1,60 @@
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
struct T
{
string name;
int s;
}t[200];
string stu[200];
bool flag;
int n;
bool cmp(T a,T b)
{
return a.s>b.s;
}
bool cmp2(string a,string b)
{
return a<b;
}
void dfs(int x,int sum,int step)
{
if(sum==0)
{
if(step==2||step==3) flag=true;
return;
}
if(step==3&&sum!=0) return;
if(sum<t[n-1].s) return;
if(x+1>=n) return;
for(int i=x+1;i<n;i++)
{
if(flag) return;
if(sum-t[i].s>=0&&step+1<=3) dfs(i,sum-t[i].s,step+1);
}
return;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=0;i<n;i++) cin>>t[i].name>>t[i].s;
sort(t,t+n,cmp);
int k=0;
for(int i=n-3;i>=0;i--)
{
flag=false;
dfs(i,t[i].s,0);
if(flag) stu[k]=t[i].name,k++;
}
sort(stu,stu+k,cmp2);
cout<<k<<endl;
for(int i=0;i<k;i++)
cout<<stu[i]<<endl;
}
return 0;
}

93
HDOJ/3413_autoAC.cpp Normal file
View File

@ -0,0 +1,93 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iomanip>
using namespace std;
double task[105];
double ans[105];
bool vis[105];
int main()
{
ios::sync_with_stdio(false);
int T;
cin>>T;
int kase=0;
while(T--)
{
cout<<"Case "<<++kase<<':'<<endl;
memset(vis,0,sizeof(vis));
int n;
cin>>n;
for(int i=0;i<n;++i)
{
cin>>task[i];
}
double now=0;
int pre=0;
for(int i=0;i<n;++i)
{
for(int j=pre;;++j)
{
j%=n;
if(!vis[j]){pre=j;break;}
}
pre%=n;
double minn=floor(task[pre]);
int rec=pre;
if(task[pre]>1)
for(int j=(pre+1)%n;j!=pre;++j)
{
j%=n;
if(j==pre)break;
if(vis[j])continue;
if(task[j]<=1){rec=j;break;}
if(floor(task[j])<minn)
{
minn=floor(task[j]);
rec=j;
}
}
vis[rec]=true;
if(task[rec]<=1)
{
for(int k=pre;k!=rec;++k)
{
k%=n;
if(k==rec)break;
if(vis[k])continue;
task[k]-=ceil(task[rec]);
now+=ceil(task[rec]);
}
now+=task[rec];
}
else{
for(int k=pre;k!=rec;++k)
{
k%=n;
if(k==rec)break;
if(vis[k])continue;
task[k]-=ceil(task[rec]);
now+=ceil(task[rec]);
}
for(int k=rec+1;k!=pre;++k)
{
k%=n;
if(k==pre)break;
if(vis[k])continue;
task[k]-=(ceil(task[rec])-1);
now+=(ceil(task[rec])-1);
}
now+=task[rec];
}
ans[rec]=now;
pre=rec+1;
pre%=n;
}
for(int i=0;i<n;++i)
{
cout<<setiosflags(ios::fixed)<<setprecision(2)<<ans[i]<<endl;
}
}
return 0;
}

50
HDOJ/3414_autoAC.cpp Normal file
View File

@ -0,0 +1,50 @@
#include<cstdio>
const int N = 1000;
bool g[N][N];
int n , next[N];
bool expand( int s )
{
for( int i = 0 ; i < n ; next[i++] = -1 );
int front = s , back = front;
for( int i = 0 ; i < n ; i++ )
{
if( i == s ) continue;
if( g[i][front] ) next[i] = front , front = i;
else
{
int a = front , b = next[front];
while( b != -1 && g[b][i] )
a = b , b = next[b];
next[a] = i;
next[i] = b;
if( b == -1 ) back = i;
}
}
if( g[back][front] )
{
next[back] = front;
return true;
}
return false;
}
bool solve()
{
for( int i = 0 ; i < n ; i++ )
if( expand(i) ) return true;
return false;
}
int main()
{
while( scanf("%d",&n) , n )
{
for( int i = 0 ; i < n ; i++ )
for( int j = 0 ; j < n ; j++ )
scanf("%d",&g[i][j]);
if( n == 1 ) puts("1");
else if( n == 2 || !solve() ) puts("-1");
else
for( int i = 0 , j = 0 ; i < n ; i++ , j = next[j] )
printf("%d%c",j+1,i==n-1?'\n':' ');
}
return 0;
}

48
HDOJ/3415_autoAC.cpp Normal file
View File

@ -0,0 +1,48 @@
#include <stdio.h>
#include <queue>
#include <algorithm>
#include <string.h>
using namespace std;
int a[111111];
int sum[211111];
const int INF = 0x3fffffff;
int main()
{
int t,n,m,i,j,k,head,end;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
j = n;
sum[0] = 0;
for(i = 1; i<=n; i++)
{
scanf("%d",&a[i]);
sum[i] = sum[i-1]+a[i];
}
int ans = -INF;
for(i = n+1; i<n+k;i++)
sum[i] = sum[i-1]+a[i-n];
n = n+k-1;
deque<int> Q;
Q.clear();
for(i = 1; i<=n; i++)
{
while(!Q.empty() && sum[i-1]<sum[Q.back()])
Q.pop_back();
while(!Q.empty() && Q.front()<i-k)
Q.pop_front();
Q.push_back(i-1);
if(sum[i]-sum[Q.front()]>ans)
{
ans = sum[i]-sum[Q.front()];
head = Q.front()+1;
end = i;
}
}
if(end>j)
end%=j;
printf("%d %d %d\n",ans,head,end);
}
return 0;
}

208
HDOJ/3416_autoAC.cpp Normal file
View File

@ -0,0 +1,208 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<climits>
#include<queue>
using namespace std;
#define clr(x)memset(x,0,sizeof(x))
#define min(a,b)(a)<(b)?(a):(b)
const int INF=INT_MAX;
const int maxn=1005;
const int maxm=1000000;
struct node
{
int from,to,next,c;
}e[maxm];
int tot;
int head[maxn];
void add(int s,int u,int f1,int f2)
{
e[tot].from=s;
e[tot].to=u;
e[tot].c=f1;
e[tot].next=head[s];
head[s]=tot++;
e[tot].from=u;
e[tot].to=s;
e[tot].c=f2;
e[tot].next=head[u];
head[u]=tot++;
}
int q[maxn];
int cnt[maxn];
int d[maxn];
int low[maxn];
int cur[maxn];
int maxflow(int s,int t,int n)
{
int *front=q,*rear=q;
for(int i=1;i<=n;i++)
{
d[i]=n;
cnt[i]=0;
}
cnt[n]=n-1;
cnt[0]++;
d[t]=0;
*rear++=t;
while(front<rear)
{
int v=*front++;
for(int i=head[v];i!=-1;i=e[i].next)
{
if(d[e[i].to]==n&&e[i^1].c>0)
{
d[e[i].to]=d[v]+1;
cnt[n]--;
cnt[d[e[i].to]]++;
*rear++=e[i].to;
}
}
}
int flow=0, u=s, top=0;
low[0]=INF;
for(int i=1;i<=n;i++)
cur[i]=head[i];
while(d[s]<n)
{
int &i=cur[u];
for(;i!=-1;i=e[i].next)
{
if(e[i].c>0&&d[u]==d[e[i].to]+1)
{
low[top+1]=min(low[top],e[i].c);
q[++top]=i;
u=e[i].to;
break;
}
}
if(i!=-1)
{
if(u==t)
{
int minf=low[top];
for(int p=1,i;p<=top;++p)
{
i=q[p];
e[i].c-=minf;
e[i^1].c+=minf;
}
flow+=minf;
u=s;
low[0]=INF;
top=0;
}
}
else
{
int old_du=d[u];
cnt[old_du]--;
d[u]=n-1;
for(int i=head[u];i!=-1;i=e[i].next)
if(e[i].c>0&&d[u]>d[e[i].to])
d[u]=d[e[i].to];
cnt[++d[u]]++;
if(d[u]<n)
cur[u]=head[u];
if(u!=s)
{
u=e[q[top]].from;
--top;
}
if(cnt[old_du]==0)
break;
}
}
return flow;
}
struct EDGE
{
int from,to,next,w;
}edge[maxm],ee[maxm];
int head2[maxn];
int head3[maxn];
int tt;
int t3;
void add2(int s,int t,int wi)
{
edge[tt].from = s;
edge[tt].to = t;
edge[tt].w = wi;
edge[tt].next = head2[s];
head2[s] = tt++;
}
void add3(int s,int t,int wi)
{
ee[t3].from = s;
ee[t3].to = t;
ee[t3].w = wi;
ee[t3].next = head3[s];
head3[s] = t3++;
}
struct dd
{
int xu, di;
bool operator < (dd t)const{
return t.di<di;
}
}x,in;
priority_queue<dd>dis;
int d1[maxn];
int d2[maxn];
void dijkstra(int u,int*d,int*hed,EDGE*ed,int&t)
{
int i;
in.xu = u;
in.di = 0;
d[u] = 0;
dis.push(in);
while(!dis.empty())
{
x = dis.top();
dis.pop();
for(i = hed[x.xu]; i; i = ed[i].next){
in.xu = ed[i].to;
in.di = x.di+ed[i].w;
if(in.di < d[in.xu]){
d[in.xu] = in.di;
dis.push(in);
}
}
}
}
int main()
{
int T, i;
int n, m, st, en;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n, &m);
clr(head2);
clr(head3);
tt = 1;
t3 = 1;
int a,b,c;
while(m--)
{
scanf("%d %d %d",&a, &b, &c);
add2(a,b,c);
add3(b,a,c);
}
scanf("%d %d",&st,&en);
for(i = 1; i <= n; i++)
d2[i] = d1[i] = INF;
dijkstra(st,d1,head2,edge,tt);
dijkstra(en,d2,head3,ee,t3);
memset(head,-1,sizeof(head));
tot = 0;
for(i = 1; i < tt; i++)
{
if(d1[edge[i].from]+d2[edge[i].to]+edge[i].w == d1[en])
add(edge[i].from,edge[i].to,1,0);
}
int res = maxflow(st,en,n);
printf("%d\n",res);
}
return 0;
}

76
HDOJ/3417_autoAC.cpp Normal file
View File

@ -0,0 +1,76 @@
#include<cstdio>
#include<cstring>
#define ll __int64
ll M=19880502ll,A[6],B[6][9999];
int n,v[9999];
ll gcd(ll a,ll b)
{
if(!b)return a;
return gcd(b,a%b);
}
ll egcd(ll a,ll b,ll &x,ll &y)
{
if(!b){x=1;y=0;return a;}
ll d=egcd(b,a%b,x,y),t=x;
x=y;
y=t-a/b*y;
return d;
}
ll Pow(ll a,ll b)
{
ll r=1;
while(b)
{
if(b&1)r=r*a%M;
a=a*a%M;
b>>=1;
}
return r;
}
ll sol(int k)
{
ll x,y,t=B[0][k],m=A[0];
for(int i=1;i<n;i++)
{
ll d=egcd(m,A[i],x,y),tp=B[i][k]-t;
if(tp%d!=0)return -1;
x=tp/d*x%A[i];
if(x<0)x+=A[i];
t=x*m+t;
m=m/gcd(m,A[i])*A[i];
}
return t%m;
}
int main()
{
while(~scanf("%d",&n))
{
memset(v,0,sizeof(v));
memset(B,0,sizeof(B));
for(int i=0;i<n;i++)
{
scanf("%I64d",A+i);
ll tp=A[i];
for(ll k=2;k*k<=tp;++k)
if(tp%k==0)
{
v[k]=1;
while(tp%k==0)
{
++B[i][k];
tp/=k;
}
}
if(tp>1){v[tp]=1;B[i][tp]=1;}
}
ll res=1;
for(int i=2;i<9998;i++)
if(v[i])
{
ll t=sol(i);
if(!~t){res=-1;break;}
res=res*Pow((ll)i,t)%M;
}
printf("%I64d\n",res);
}
}

37
HDOJ/3418_autoAC.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef __int64 LL;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
LL a[105];
int judge(LL mid,LL n,LL m){
LL sum = 0;
for(int i = 0; i <n; ++i)
if(a[i] >= mid) sum += mid;
else sum += a[i];
if(sum >= m * mid) return 1;
return 0;
}
int main()
{ LL n,m,sum,minx;
while(~scanf("%I64d%I64d",&n,&m)){
sum = 0,minx = INF;
for(int i = 0; i < n; ++i){
scanf("%d",&a[i]);
minx = min(minx,a[i]);
sum += a[i];
}
LL L = minx,R = sum/m,mid,ans;
while(L <= R){
mid = (R + L) >> 1;
if(judge(mid,n,m)){
ans = mid;
L = mid + 1;
}else R = mid - 1;
}
printf("%I64d\n",ans);
}
return 0;
}

79
HDOJ/3419_autoAC.cpp Normal file
View File

@ -0,0 +1,79 @@
#include<stdio.h>
#include<string.h>
int a,b,c;
int ans;
int hash[10];
int base[11],limit;
int t_a;
int t_b;
void judge()
{
int i;
int t;
int temp[10];
t=t_a*t_b;
if(limit<=t || t<base[c]) return ;
memset(temp,0,sizeof(temp));
while(t)
{
if(hash[t%10]) return ;
temp[t%10]=1;
t/=10;
}
t=0;
for(i=1;i<10;i++) if(temp[i]) t++;
if(t!=c) return ;
ans++;
}
void DFS2(int k)
{
int i;
if(k>=base[b+1]) return ;
if(base[b]<=k) {t_b=k;judge();return ;}
for(i=1;i<10;i++)
{
if(hash[i]==0)
{
hash[i]=1;
DFS2(k*10+i);
hash[i]=0;
}
}
}
void DFS(int k)
{
int i;
if(k>=base[a+1]) return;
if(base[a]<=k)
{
t_a=k;
DFS2(0);
return ;
}
for(i=1;i<10;i++)
{
if(hash[i]==0)
{
hash[i]=1;
DFS(k*10+i);
hash[i]=0;
}
}
}
int main()
{
int i;
while(scanf("%d%d%d",&a,&b,&c),a||b||c)
{
if(a==0 || b==0 || c==0) {printf("0\n");continue;}
if(a+b-2>c) {printf("0\n");continue;}
base[1]=1;
for(i=2;i<=10;i++) base[i]=base[i-1]*10;
limit=base[c+1];
ans=0;
memset(hash,0,sizeof(hash));
DFS(0);
printf("%d\n",ans);
}
return 0;
}

18
HDOJ/3420_autoAC.cpp Normal file
View File

@ -0,0 +1,18 @@
#include<stdio.h>
#include<stdlib.h>
int cmp(const void *a, const void *b){
return *(int *)a - *(int *)b;
}
int main() {
int x[1001], n, i, j, tickets;
while(scanf("%d", &n) != EOF){
for(i = 0; i < n; i++)
scanf("%d", x + i);
qsort(x, n, sizeof(x[0]), cmp);
tickets = 0;
for(i = 0; i < n; i++)
if(x[i] * (n - i) > tickets)
tickets = x[i] * (n - i);
printf("%d\n", tickets);
}
}

41
HDOJ/3421_autoAC.cpp Normal file
View File

@ -0,0 +1,41 @@
#include<stdio.h>
__int64 he;
int main()
{
int t,n,i,ret,qian,j,dang;
scanf("%d",&t);
for(i=1;i<=t;)
{
he=0;
scanf("%d",&n);
qian=-1;
ret=0;
int ji=0;
for(j=1;j<=n;j++)
{
scanf("%d",&dang);
if(dang>0) he+=dang;
if(ji)
{
if(dang<0)
{
ji=0;
}
}else
{
if(dang==0)
{
}else if(dang>0)
{
ji=1;
ret++;
}
}
}
if(i!=1)
printf("\n");
printf("Case %d:\n",i++);
printf("%d %I64d\n",ret,he);
}
return 0;
}

10
HDOJ/3422_autoAC.cpp Normal file
View File

@ -0,0 +1,10 @@
#include <stdio.h>
int main()
{
long long i,j,n;
while(scanf("%I64d",&n)!=EOF)
{
printf("%I64d %I64d\n",4*n*n+6*n-1,2*n*n+2*n+1);
}
return 0;
}

113
HDOJ/3423_autoAC.cpp Normal file
View File

@ -0,0 +1,113 @@
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <ctime>
#include <vector>
#include <algorithm>
#define ll long long
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
using namespace std;
const int INF = 1e9;
const int maxn = 50005;
struct Edge{
int v, w, next;
}et[maxn * 4];
struct node{
int u, v, l, id;
}ed[maxn * 10];
int eh[maxn], fa[maxn], son[maxn], ans[maxn];
ll dis1[maxn], dis2[maxn];
int n, m, num;
ll Min;
void init(){
memset(eh, -1, sizeof(eh));
for(int i = 1; i <= n; i++) fa[i] = i;
num = 0;
}
void add(int u, int v, int w){
Edge e = {v, w, eh[u]};
et[num] = e;
eh[u] = num++;
}
int find(int x){
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
bool cmp(node a, node b){
if(a.l != b.l) return a.l < b.l;
return a.id < b.id;
}
void dfs1(int u, int pre){
son[u] = 1;
dis1[u] = 0;
for(int i = eh[u]; i != -1; i = et[i].next)
{
int v = et[i].v, w = et[i].w;
if(v == pre) continue;
dfs1(v, u);
son[u] += son[v];
dis1[u] += son[v] * w + dis1[v];
}
}
void dfs2(int u, int pre, int pedge){
if(pre == -1) dis2[u] = 0;
else dis2[u] = dis2[pre] + dis1[pre] - dis1[u] - son[u] * pedge + ((ll)n - son[u]) * pedge;
for(int i = eh[u]; i != -1; i = et[i].next)
{
int v = et[i].v, w = et[i].w;;
if(v == pre) continue;
dfs2(v, u, w);
}
if(Min > dis1[u] + dis2[u]) Min = dis1[u] + dis2[u];
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &m);
init();
for(int i = 0; i < m; i++)
{
scanf("%d%d%d", &ed[i].u, &ed[i].v, &ed[i].l);
ed[i].id = i;
}
sort(ed, ed + m, cmp);
ll ans1 = 0;
for(int i = 0; i < m; i++)
{
int u = ed[i].u, v = ed[i].v, l = ed[i].l;
int ru = find(u), rv = find(v);
if(ru == rv) continue;
fa[ru] = rv;
add(u, v, l);
add(v, u, l);
ans1 += l;
}
int cnt = 0;
for(int i = 1; i <= n; i++)
if(i == find(i)) cnt++;
if(cnt > 1)
{
printf("Poor mayor.\n");
continue;
}
Min = 0x7fffffffffffffffLL;
dfs1(1, -1);
dfs2(1, -1, 0);
printf("%I64d\n", ans1);
cnt = 0;
for(int i = 1; i <= n; i++)
if(dis1[i] + dis2[i] == Min)
ans[cnt++] = i;
for(int i = 0; i < cnt - 1; i++)
printf("%d ", ans[i]);
printf("%d\n", ans[cnt - 1]);
printf("%I64d\n", 2 * Min);
}
return 0;
}

17
HDOJ/3424_autoAC.cpp Normal file
View File

@ -0,0 +1,17 @@
#include <cstdio>
#include <cmath>
using namespace std;
#define F(x) abs(x)>1e-6
int main(){
double x,y,a,b,s,f;
while(scanf("%lf%lf",&x,&y)&&(F(x+1.0)||F(y+1.0))){
s=f=0;
while(scanf("%lf%lf",&a,&b)&&(F(a)||F(b))){
if(b<y){
s+=a-x;f+=y-b;
}
x=a;y=b;
}
printf("%.0lf\n",s/f*y);
}
}

172
HDOJ/3425_autoAC.cpp Normal file
View File

@ -0,0 +1,172 @@
#include<stdio.h>
#include<cmath>
#include<algorithm>
#include<string.h>
#include<iostream>
#define eps 1e-8
#define N 20010
using namespace std;
int Sig(double a)
{
return a<-eps?-1:a>eps;
}
struct Point
{
double x,y;
Point(){}
Point(double x0,double y0):x(x0),y(y0){}
bool operator <(Point pt)
{
if(pt.x==x)
return y<pt.y;
return x<pt.x;
}
bool operator >(Point pt)
{
if(pt.x==x)
return y>pt.y;
return x>pt.x;
}
bool operator ==(Point pt)
{
return !Sig(pt.x-x) && !Sig(pt.y-y);
}
Point operator +(Point pt)
{
return Point(x+pt.x,y+pt.y);
}
Point operator -(Point pt)
{
return Point(x-pt.x,y-pt.y);
}
Point operator *(double t)
{
return Point(x*t,y*t);
}
void Input()
{
scanf("%lf%lf",&x,&y);
}
};
struct Range
{
Point a,b;
Range(){}
Range(Point a0,Point b0):a(a0),b(b0){}
bool operator <(Range r)
{
if(a==r.a)
return b<r.b;
return a<r.a;
}
};
int cmp(Range r1,Range r2)
{
return r1<r2;
}
Point s,e;
Point p[110];
Range rang[110];
double r[110];
int cnt;
Point Intersection(Point u1,Point u2,Point v1,Point v2)
{
Point ret=u1;
double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))/
((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
ret.x+=(u2.x-u1.x)*t;
ret.y+=(u2.y-u1.y)*t;
return ret;
}
double Dis(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double Xmult(Point o,Point a,Point b)
{
return (a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);
}
void Count(Point p,double r)
{
double d=fabs(Xmult(p,s,e))/Dis(s,e);
if(d-r>=0)
return ;
double h=sqrt(r*r-d*d);
Point tmp=Point(e.y-s.y,s.x-e.x);
Point o=Intersection(p,p+tmp,s,e);
Point p1,p2;
p1=o+(e-s)*(h/Dis(s,e));
p2=o-(e-s)*(h/Dis(s,e));
if(p2<p1)
swap(p1,p2);
if(p2<s || p2==s || e<p1 || e==p1)
return ;
if(p1<s)
p1=s;
if(e<p2)
p2=e;
rang[cnt++]=Range(p1,p2);
}
int main()
{
int n;
while(scanf("%d",&n) && n)
{
s.Input();
e.Input();
if(e<s)
swap(s,e);
for(int i=0;i<n;i++)
{
p[i].Input();
scanf("%lf",&r[i]);
}
cnt=0;
for(int i=0;i<n;i++)
Count(p[i],r[i]);
if(cnt==0)
{
printf("0.00\n");
continue;
}
if(s.x==e.x)
{
sort(rang,rang+cnt,cmp);
double sum=0;
double left=rang[0].a.y,right=rang[0].b.y;
for(int i=1;i<cnt;i++)
{
if(rang[i].a.y<=right)
right=max(rang[i].b.y,right);
else
{
sum+=(right-left);
left=rang[i].a.y;
right=rang[i].b.y;
}
}
sum+=(right-left);
printf("%.2lf\n",sum/(e.y-s.y)*100);
}
else
{
sort(rang,rang+cnt,cmp);
double sum=0;
double left=rang[0].a.x,right=rang[0].b.x;
for(int i=1;i<cnt;i++)
{
if(rang[i].a.x<=right)
right=max(rang[i].b.x,right);
else
{
sum+=(right-left);
left=rang[i].a.x;
right=rang[i].b.x;
}
}
sum+=(right-left);
printf("%.2lf\n",sum/(e.x-s.x)*100);
}
}
return 0;
}

88
HDOJ/3426_autoAC.cpp Normal file
View File

@ -0,0 +1,88 @@
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
vector<string> st;
int tag_type(const string& s) {
int cnt=0;
for(int i=0; i<s.size(); ++i) {
if(s[i]=='/') { cnt++; continue; }
if(!((s[i]<='z' && s[i]>='a') || (s[i]>='0' && s[i]<='9'))) return 0;
}
if(s.size()-cnt==0) return 0;
if(s[0]=='/' && cnt == 1) return 2;
if(s[s.size()-1]=='/' && cnt == 1) return 3;
if(cnt == 0) return 1;
return 0;
}
bool hex(const string& s) {
if(s.size() <= 1 || s.size()%2==0) return false;
if(s[0] != 'x') return false;
for(int i=1; i<s.size(); ++i)
if( !((s[i]<='F' && s[i] >='A') || (s[i]<='9' && s[i]>='0') || (s[i]<='f' && s[i]>='a')) ) return false;
return true;
}
bool bad_qoute(const string& s) {
return !(s=="lt" || s=="gt" || s=="amp" || hex(s));
}
bool bad_txt(const string& s) {
string qoute;
for(int i=0; i<s.size(); ++i)
if(s[i]=='&') {
for(int j=i+1; j<s.size() && s[j] != ';'; ++j) qoute += s[j];
if(bad_qoute(qoute)) return true;
qoute="";
}
else if(s[i]=='<' || s[i] =='>') return true;
else if(s[i]>127 || s[i]<32) return true;
return false;
}
void get_l(stringstream& os, string& s, char del) {
char t;
while(!os.eof()) {
os >> t;
if(t==del) return;
s += t;
}
}
int main() {
string ln;
while(getline(cin, ln)) {
stringstream ss(ln);
st.clear();
string s;
int inv=0;
while(!ss.eof()) {
s="";
getline(ss,s,'<');
if(bad_txt(s)) {
inv=1;
break;
}
if(ss.eof()) break;
s="";
getline(ss,s,'>');
if(ss.eof()) {
inv=4;
break;
}
int tty = tag_type(s);
if(tty==0) {
inv=2;
break;
}
else if(tty==1) st.push_back(s);
else if(tty==2) {
if(st.size()==0 || st.back() != s.substr(1)) {
inv=3;
break;
}
else st.pop_back();
}
}
if(inv || st.size() > 0) puts("invalid");
else puts("valid");
}
return 0;
}

69
HDOJ/3427_autoAC.cpp Normal file
View File

@ -0,0 +1,69 @@
#include<stdio.h>
#include<string.h>
#define NN 155
char str[NN];
int mark[NN][NN];
int num[NN];
char cha[NN];
int dfs(int l, int r)
{
int i, t;
if (l == r){
if (num[l] > 1)
return 1;
else
return 0;
}
if (mark[l][r]>= 0)
return mark[l][r];
char ch = cha[l];
for (i = l + 1; i <= r; i++){
if (cha[i] == ch){
if (mark[l + 1][i - 1] = dfs(l + 1, i - 1)){
t = mark[i][r];
mark[i][r] = -1;
num[i] += num[l];
mark[i][r] = dfs(i, r);
num[i] -= num[l];
if (mark[i][r] == 1)
{
mark[i][r] = t;
return 1;
}
mark[i][r] = t;
}
}
}
if (num[l] > 1 && (mark[l + 1][r] = dfs(l + 1, r)))
return 1;
return 0;
}
int main()
{
int len, time, index, i;
while (scanf("%s", str) != EOF){
len = strlen(str);
if (len == 0){
puts("solvable");
continue;
}
time = 1;
index = 0;
for (i = 1; i <= len; i++){
if (str[i] != str[i - 1]){
cha[index] = str[i - 1];
num[index] = time;
time = 1;
index++;
}
else
time++;
}
memset(mark, -1, sizeof(mark));
if (dfs(0, index - 1))
puts("solvable");
else
puts("unsolvable");
}
return 0;
}

195
HDOJ/3429_autoAC.cpp Normal file
View File

@ -0,0 +1,195 @@
#include <cstdio>
#include <cstring>
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
#define i64 __int64
class fraction
{
private:
inline i64 Gcd (i64 x,i64 y)
{
return y==0?x:Gcd(y,x%y);
}
i64 Lcm (i64 x,i64 y)
{
x=x/Gcd(x,y)*y;
if(x<0) x=-x;
return x;
}
public:
i64 a,b;
fraction () {}
fraction (i64 x)
{
a=x; b=1;
}
fraction (i64 x,i64 y)
{
a=x; b=y;
Refresh();
}
void Refresh ()
{
if (b<0) b=-b,a=-a;
i64 k=Gcd(a,b);
if (k<0) k=-k;
a/=k; b/=k;
}
fraction Inverse () const
{
return fraction (b,a);
}
fraction operator + (fraction p)
{
fraction ans;
ans.b=Lcm(b,p.b);
ans.a=ans.b/b*a+ans.b/p.b*p.a;
ans.Refresh();
return ans;
}
fraction operator - (fraction p)
{
fraction ans;
ans.b=Lcm(b,p.b);
ans.a=ans.b/b*a-ans.b/p.b*p.a;
ans.Refresh();
return ans;
}
fraction operator * (fraction p)
{
fraction ans;
ans.a=a*p.a;
ans.b=b*p.b;
ans.Refresh();
return ans;
}
fraction operator / (fraction p)
{
fraction ans;
ans.a=a*p.b;
ans.b=b*p.a;
ans.Refresh();
return ans;
}
bool operator < (const fraction &p) const
{
return a*p.b<b*p.a;
}
bool operator > (const fraction &p) const
{
return a*p.b>b*p.a;
}
bool operator == (const fraction &p) const
{
return a*p.b==b*p.a;
}
fraction operator | (fraction p)
{
fraction t1=fraction (b,a);
fraction t2=p.Inverse ();
t1=t1+t2;
return t1.Inverse();
}
void print ()
{
printf("%I64d/%I64d\n",a,b);
}
};
string s;
int len;
fraction read (int &now)
{
int fz=0,fm=0;
int i;
for (i=now;i<len;i++)
{
if (s[i]=='/') break;
fz*=10;
fz+=s[i]-'0';
}
for (i=i+1;i<len;i++)
{
if (isdigit(s[i]))
{
fm*=10;
fm+=s[i]-'0';
}
else break;
}
fraction tmp(fz,fm);
now=i-1;
return tmp;
}
fraction cal (int &now)
{
fraction ans;
int front=-1;
for (int i=now+1;i<len;i++)
{
if (s[i]=='(')
{
if(front==-1)
ans=cal(i);
else if(front==0)
ans=ans+cal(i);
else if(front==1)
ans=ans|cal(i);
}
else if (isdigit(s[i]))
{
if (front==-1)
ans=read(i);
else if (front==0)
ans=ans+read(i);
else if (front==1)
ans=ans|read(i);
}
else if (s[i]==')')
{
now=i;
return ans;
}
else if (s[i]=='&')
front=0;
else if (s[i]=='|')
front=1;
}
}
int main ()
{
while (getline(cin,s))
{
len=s.length();
fraction ans;
int flag=-1;
for (int i=0;i<len;i++)
{
if (s[i]=='(')
{
if (flag==-1)
ans=cal(i);
else if (flag==0)
ans=ans+cal(i);
else if(flag==1)
ans=ans|cal(i);
}
else if (isdigit(s[i]))
{
if (flag==-1)
ans=read(i);
else if (flag==0)
ans=ans+read(i);
else if (flag==1)
ans=ans|read(i);
}
else if (s[i]=='&')
flag=0;
else if (s[i]=='|')
flag=1;
}
ans.print();
}
return 0;
}

76
HDOJ/3430_autoAC.cpp Normal file
View File

@ -0,0 +1,76 @@
#include <cstdio>
#include <cstring>
const int N = 520;
int a[N + 5], b[N + 5], ta[N + 5], tb[N + 5];
bool vis[N + 5];
long long m[N + 5], c[N + 5];
long long exgcd(long long a, long long b, long long &x, long long &y) {
if (!b) {
x = 1;
y = 0;
return a;
}
long long res = exgcd(b, a % b, y, x);
y -= a/b*x;
return res;
}
long long mod(long long a, long long b) {
long long res = a % b;
if (res < 0) res += b;
return res;
}
void solve(int n) {
memset(vis, false, sizeof(vis));
int cnt = 0;
for (int i = 1; i <= n; ++i)
if (!vis[i]) {
int num = 0, t = i;
while (!vis[t]) {
vis[t] = true;
ta[++num] = t;
tb[num] = b[t];
t = a[t];
}
bool same = false;
for (int i = 1; i <= num; ++i)
if (tb[i] == ta[1]) {
same = true;
int j = i, k = 1;
do {
if (tb[j] != ta[k]) same = false;
j = j % num + 1;
k = k % num + 1;
} while (i != j);
t = i;
break;
}
if (!same) {
printf("-1\n");
return;
}
m[++cnt] = num;
c[cnt] = (num - t + 1) % num;
}
n = cnt;
long long ans = c[1], LCM = m[1];
for (int i = 2; i <= n; ++i) {
long long x, y, g = exgcd(LCM, m[i], x, y);
if ((c[i] - ans) % g) {
printf("-1\n");
return;
}
ans = mod(ans + LCM*mod((c[i] - ans)/g*x, m[i]/g), LCM/g*m[i]);
LCM = LCM/g*m[i];
}
printf("%I64d\n", ans);
return;
}
int main() {
int n;
while (scanf("%d", &n), n) {
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = 1; i <= n; ++i) scanf("%d", &b[i]);
solve(n);
}
return 0;
}

64
HDOJ/3432_autoAC.cpp Normal file
View File

@ -0,0 +1,64 @@
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
const double eps=1e-6;
int dcmp(double x)
{
if(fabs(x)<eps)return 0;
else return x<0?-1:1;
}
int main()
{
double width,height,door,workers;
while(scanf("%lf%lf%lf%lf",&width,&height,&door,&workers))
{
if(width==0&&height==0&&door==0&&workers==0)break;
double area=width*height;
area/=workers;
double d1=width-door;
double prex=width;
double prey=0;
for(int i=0;i<workers-1;++i)
{
double tmp=area;
double x,y;
if(dcmp(prey-height)!=0)
{
if(dcmp(prex-width)==0)
{
x=width;
y=2*tmp/d1+prey;
if(y>height)
{
tmp-=(height-prey)*d1/2;
y=height;
x=width-tmp*2/y;
}
}
else if(dcmp(prex)==0)
{
x=0;
y=prey-2*tmp/door;
}
}
else
{
y=height;
x=prex-tmp*2/height;
if(x<0)
{
tmp=tmp-prex*prey/2;
x=0;
y=height-tmp*2/door;
}
}
prex=x;
prey=y;
printf("%.3lf %.3lf ",x,y);
}
printf("\n");
}
return 0;
}

53
HDOJ/3433_autoAC.cpp Normal file
View File

@ -0,0 +1,53 @@
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int dp[55][205], a[205], b[205];
int _, n, x, y, kase = 0;
bool check(int t)
{
memset(dp, -1, sizeof(dp));
dp[0][0] = 0;
for(int i = 1; i <= n; ++i)
{
if(dp[i][x] >= y) return true;
for(int j = 0; j <= x; ++j)
{
if(dp[i - 1][j] != -1)
{
int temp = min(t/a[i], x-j);
for(int k = 0; k <= temp; ++k)
{
int t1 = (t - a[i] * k) / b[i];
dp[i][j + k] = max(dp[i][j + k], dp[i - 1][j] + t1);
}
}
}
}
if(dp[n][x] >= y) return true;
return false;
}
int main(void)
{
scanf("%d", &_);
while(_--)
{
scanf("%d%d%d", &n, &x, &y);
for(int i = 1; i <= n; ++i)
scanf("%d%d", &a[i], &b[i]);
int l = 0, r = a[1] * x + b[1] * y;
int ans = r;
while(l <= r)
{
int mid = (l + r) >> 1;
if(check(mid))
{
ans = mid;
r = mid - 1;
}
else l = mid + 1;
}
printf("Case %d: %d\n", ++kase, ans);
}
return 0;
}

38
HDOJ/3434_autoAC.cpp Normal file
View File

@ -0,0 +1,38 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define L 1000100
int a[L],b[L],p[L];
long long min(long long x,long long y)
{
if (x<y) return x;
return y;
}
int main()
{
int T;
int cas=1;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
int len=1;
b[len]=a[1];
for(int i=2;i<=n;i++) if(a[i]!=a[i-1]) b[++len]=a[i];
p[1]=0;
for(int i=2;i<=len;i++) p[i]=b[i]-b[i-1];
long long sum=0,ans=0;
for(int i=2;i<=len;i++)
{
if(p[i]*sum<0) ans+=min(abs(sum),abs(p[i]));
sum+=p[i];
}
sum=abs(sum);
ans+=sum;
printf("Case %d: %I64d %I64d\n",cas++,ans,sum+1);
}
return 0;
}

96
HDOJ/3435_autoAC.cpp Normal file
View File

@ -0,0 +1,96 @@
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int inf=1<<28;
int g[1005][1005];
int lx[1005],ly[1005];
bool sx[1005],sy[1005];
int link[1005],n;
vector<int> q[1010];
int min(int a,int b)
{
if(a<b) return a;
return b;
}
bool path(int k)
{
sx[k]=true;
for(int i=1; i<=n; i++)
{
if(!sy[i]&&(lx[k]+ly[i]==g[k][i]))
{
sy[i]=1;
if(link[i]==-1||path(link[i]))
{
link[i]=k;
return true;
}
}
}
return false;
}
int BestMatch()
{
int d,sum;
memset(ly,0,sizeof(ly));
memset(link,-1,sizeof(link));
for(int i=1; i<=n; i++)
{
lx[i]=-inf;
for(int j=1; j<=n; j++)
if(lx[i]<g[i][j]) lx[i]=g[i][j];
}
for(int k=1; k<=n; k++)
{
while(1)
{
memset(sx,0,sizeof(sx));
memset(sy,0,sizeof(sy));
if(path(k)) break;
d=inf;
for(int i=1; i<=n; i++)
if(sx[i])
for(int j=1; j<=n; j++)
if(!sy[j])
d=min(d,lx[i]+ly[j]-g[i][j]);
for(int i=1; i<=n; i++)
{
if(sx[i]) lx[i]-=d;
if(sy[i]) ly[i]+=d;
}
}
}
sum=0;
for(int i=1; i<=n; i++)
{
if(link[i]==-1||g[link[i]][i]==-inf) return -1;
sum+=g[link[i]][i];
}
return -sum;
}
int main()
{
int m,T,cas=1;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) q[i].clear();
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
g[i][j]=inf;
for(int i=1; i<=m; i++)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
g[x][y]=g[y][x]=min(g[x][y],c);
}
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++) g[i][j]=-g[i][j];
int t=BestMatch();
if(t==-1) printf("Case %d: NO\n",cas++);
else printf("Case %d: %d\n",cas++,t);
}
return 0;
}

249
HDOJ/3436_autoAC.cpp Normal file
View File

@ -0,0 +1,249 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXD 100010
int N, Q, T, node, tx[MAXD], begin[MAXD], end[MAXD], nodep[MAXD];
int num[2 * MAXD], size[2 * MAXD], left[2 * MAXD], right[2 * MAXD], pre[2 * MAXD], key[2 * MAXD];
char b[MAXD][10];
int ob[MAXD];
int cmp(const void *_p, const void *_q)
{
int *p = (int *)_p, *q = (int *)_q;
return *p - *q;
}
void update(int cur)
{
size[cur] = size[left[cur]] + size[right[cur]] + num[cur];
}
void newnode(int &cur, int k)
{
cur = ++ node;
size[cur] = num[cur] = end[k] - begin[k] + 1;
key[cur] = k;
nodep[k] = cur;
left[cur] = right[cur] = 0;
}
void build(int &cur, int x, int y, int p)
{
int mid = (x + y) / 2;
newnode(cur, mid);
pre[cur] = p;
if(x == y)
return ;
if(x < mid)
build(left[cur], x, mid - 1, cur);
if(mid < y)
build(right[cur], mid + 1, y, cur);
update(cur);
}
void init()
{
int i, j, k;
scanf("%d%d", &N, &Q);
k = 0;
tx[k ++] = 0;
for(i = 0; i < Q; i ++)
{
scanf("%s%d", b[i], &ob[i]);
if(b[i][0] == 'T' || b[i][0] == 'Q')
tx[k ++] = ob[i];
}
tx[k ++] = N;
qsort(tx, k, sizeof(tx[0]), cmp);
N = 0;
for(i = 1; i < k; i ++)
if(tx[i] != tx[i - 1])
{
if(tx[i] - tx[i - 1] > 1)
{
begin[N] = tx[i - 1] + 1, end[N] = tx[i] - 1;
++ N;
}
begin[N] = end[N] = tx[i];
++ N;
}
T = node = left[0] = right[0] = size[0] = num[0] = 0;
build(T, 0, N - 1, 0);
}
void leftrotate(int x)
{
int y = right[x], p = pre[x];
right[x] = left[y];
if(right[x])
pre[right[x]] = x;
left[y] = x;
pre[x] = y;
pre[y] = p;
if(p == 0)
T = y;
else
right[p] == x ? right[p] = y : left[p] = y;
update(x);
}
void rightrotate(int x)
{
int y = left[x], p = pre[x];
left[x] = right[y];
if(left[x])
pre[left[x]] = x;
right[y] = x;
pre[x] = y;
pre[y] = p;
if(p == 0)
T = y;
else
right[p] == x ? right[p] = y : left[p] = y;
update(x);
}
void splay(int x, int goal)
{
int y, z;
for(;;)
{
if((y = pre[x]) == goal)
break;
if((z = pre[y]) == goal)
right[y] == x ? leftrotate(y) : rightrotate(y);
else
{
if(right[z] == y)
{
if(right[y] == x)
leftrotate(z), leftrotate(y);
else
rightrotate(y), leftrotate(z);
}
else
{
if(left[y] == x)
rightrotate(z), rightrotate(y);
else
leftrotate(y), rightrotate(z);
}
}
}
update(x);
}
int BS(int x)
{
int max, mid, min;
min = 0, max = N;
for(;;)
{
mid = (max + min) / 2;
if(mid == min)
break;
if(begin[mid] <= x)
min = mid;
else
max = mid;
}
return mid;
}
int Delete(int &cur, int p)
{
int k;
if(cur == T || right[cur] == 0)
{
if(cur == T)
{
k = Delete(left[cur], cur);
left[k] = left[T], right[k] = right[T];
if(left[k])
pre[left[k]] = k;
if(right[k])
pre[right[k]] = k;
T = k;
pre[T] = 0;
update(T);
}
else
{
k = cur;
if(left[k])
pre[left[k]] = p;
cur = left[k];
}
return k;
}
else
{
k = Delete(right[cur], cur);
update(cur);
return k;
}
}
void Insert(int &cur, int k, int p)
{
if(cur == 0)
{
newnode(cur, k);
pre[cur] = p;
return ;
}
Insert(left[cur], k, cur);
update(cur);
}
void Top(int x)
{
int k, cur;
k = BS(x);
cur = nodep[k];
splay(cur, 0);
if(left[T] == 0 || right[T] == 0)
{
T = left[T] + right[T];
pre[T] = 0;
}
else
Delete(T, 0);
Insert(T, k, 0);
splay(node, 0);
}
void Query(int x)
{
int k, cur;
k = BS(x);
cur = nodep[k];
splay(cur, 0);
printf("%d\n", size[left[cur]] + 1);
}
int Search(int cur, int x)
{
int ls = left[cur], rs = right[cur], k = key[cur];
if(x <= size[ls])
return Search(left[cur], x);
else if(x <= size[ls] + num[cur])
return begin[k] + (x - size[ls]) - 1;
else
return Search(right[cur], x - size[ls] - num[cur]);
}
void Rank(int x)
{
printf("%d\n", Search(T, x));
}
void solve()
{
int i, j, k;
for(i = 0; i < Q; i ++)
{
if(b[i][0] == 'T')
Top(ob[i]);
else if(b[i][0] == 'Q')
Query(ob[i]);
else
Rank(ob[i]);
}
}
int main()
{
int t, tt;
scanf("%d", &t);
for(tt = 0; tt < t; tt ++)
{
init();
printf("Case %d:\n", tt + 1);
solve();
}
return 0;
}

117
HDOJ/3437_autoAC.cpp Normal file
View File

@ -0,0 +1,117 @@
#include <stdio.h>
#include <algorithm>
#include <math.h>
#define zero(x) if(dcmp(x)==0)x=0
#define cross(p0,p1,p2) ((p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x))
#define dot(p0,p1,p2) ((p1.x-p0.x)*(p2.x-p0.x)+(p1.y-p0.y)*(p2.y-p0.y))
#define luoti(h) (sqrt(2*(h)/9.18))
#define ponseg(p0,p1,p2) (dcmp(cross(p0,p1,p2))==0&&dcmp(dot(p0,p1,p2))<=0)
using namespace std;
const double eps=1e-4;
const double inf=1e10;
struct point{
double x,y;
};
struct wx{
double z,k;
int n,type;
point p[11];
}rec[11];
double cmp(wx a,wx b){
return a.z>b.z;
}
int dcmp(double x){
return (x>eps)-(x<-eps);
}
bool reach(double vx,double vy,double x,double y,double z,wx pol){
double sum=luoti(z-pol.z);
point p;
p.x=x+sum*vx;
p.y=y+sum*vy;
for(int i=0;i<pol.n;i++)
if(ponseg(p,pol.p[i],pol.p[i+1]))
return 1;
int judge=dcmp(cross(p,pol.p[0],pol.p[1]));
for(int i=1;i<pol.n;i++)
if(dcmp(cross(p,pol.p[i],pol.p[i+1]))*judge<0)
return 0;
return 1;
}
int segsec(point p1,point p2,point p3,point p4,point &cp){
double u=cross(p1,p2,p3),v=cross(p2,p1,p4);
if(dcmp(u+v)){
cp.x=(p3.x*v+p4.x*u)/(v+u);
cp.y=(p3.y*v+p4.y*u)/(v+u);
if(ponseg(cp,p1,p2)&&ponseg(cp,p3,p4))return 1;
}return 0;
}
void roat(double x,double y,double &vx,double &vy,double k){
double s=sin(k),c=cos(k);
zero(s);
zero(c);
vx=(x*c-y*s);
vy=(x*s+y*c);
}
double judge(point a,point b,double vx,double vy){
if(dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0)return 0;
if(dcmp(a.x-b.x)==0)return fabs((a.y-b.y)/vy);
return fabs((a.x-b.x)/vx);
}
double into(double &vx,double &vy,double &x,double &y,double &z,wx pol){
double sum=luoti(z-pol.z);
x+=(sum*vx);
y+=(sum*vy);
z=pol.z;
point p0,p1,cp;
p0.x=x;
p0.y=y;
if(pol.type==1)vx=pol.k;
else if(pol.type==2)vy=pol.k;
else roat(vx,vy,vx,vy,pol.k);
if(dcmp(vx)==0&&dcmp(vy)==0){
for(int i=0;i<pol.n;i++)
if(ponseg(p0,pol.p[i],pol.p[i+1]))
return 0.0;
return -inf;
}
p1.x=x+10000*vx;
p1.y=y+10000*vy;
for(int i=0;!segsec(p0,p1,pol.p[i],pol.p[i+1],cp)&&i<pol.n;i++);
x=cp.x;
y=cp.y;
sum+=judge(p0,cp,vx,vy);
return sum;
}
int main(){
int tim,n;
scanf("%d",&tim);
for(int cas=0;cas<tim;){
double vx,vy,x=0,y=0,z;
printf("Case %d: ",++cas);
scanf("%lf%lf%lf%d",&vx,&vy,&z,&n);
for(int i=0;i<n;i++){
scanf("%lf%d",&rec[i].z,&rec[i].n);
for(int j=0;j<rec[i].n;j++)
scanf("%lf%lf",&rec[i].p[j].x,&rec[i].p[j].y);
rec[i].p[rec[i].n]=rec[i].p[0];
scanf("%d%lf",&rec[i].type,&rec[i].k);
if(rec[i].z<=0||rec[i].z>z){
i--;
n--;
}
}
if(z<0){
puts("Forever!");
continue;
}
sort(rec,rec+n,cmp);
double times=0;
for(int i=0;times>=0&&i<n;i++)
if(reach(vx,vy,x,y,z,rec[i])){
times+=into(vx,vy,x,y,z,rec[i]);
}
if(times<0)puts("Forever!");
else printf("%.2lf\n",times+luoti(z));
}
return 0;
}

130
HDOJ/3439_autoAC.cpp Normal file
View File

@ -0,0 +1,130 @@
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn=100005;
ll n,k,m;
ll f1[2*maxn],f2[maxn],c[maxn],mul[maxn],num[maxn],tot;
ll cp()
{
f1[0]=1%m;f1[1]=0;
ll i;
for(i=2;i<=2*m;++i)
f1[i]=(((f1[i-1]+f1[i-2])%m)*(i-1))%m;
return f1[(n-k)%(2*m)];
}
void prem()
{
ll i,mm;
mm=m;
tot=0;
for(i=2;i*i<=mm;++i)
{
if(mm%i==0)
{
c[tot]=i;mul[tot]=1;num[tot]=0;
while(mm%i==0)
{
mul[tot]*=i;
num[tot]++;
mm/=i;
}
tot++;
}
}
if(mm!=1)
{
c[tot]=mm;mul[tot]=mm;num[tot]=1;
tot++;
}
}
ll quickpow(ll a,ll b,ll c)
{
ll ret=1;
while(b)
{
if(b&1)
{
(ret*=a)%=c;
}
(a*=a)%=c;
b>>=1;
}
return ret%c;
}
void ex_gcd(ll a,ll b,ll &x,ll &y)
{
if(b==0)
{
x=1;y=0;
return ;
}
ex_gcd(b,a%b,x,y);
ll t=x;
x=y;
y=t-(a/b)*y;
}
ll count(ll a,ll p)
{
if(a<p)
return 0;
else
return a/p+count(a/p,p);
}
ll count2(ll a,ll p,ll mp)
{
if(a<p)
return f2[a];
return ((f2[a%mp]*quickpow(f2[mp-1],a/mp,mp))%mp*count2(a/p,p,mp))%mp;
}
ll work1(ll id)
{
ll p=c[id];ll mp=mul[id];ll nm=num[id];
ll t1;
t1=count(n,p)-count(k,p)-count(n-k,p);
if(t1>=nm)
return 0;
ll ret=quickpow(p,t1,mp);
ll i,x,y;
f2[0]=1;
for(i=1;i<=mp;++i)
{
f2[i]=f2[i-1];
if(i%p!=0)
f2[i]*=i;
f2[i]%=mp;
}
ex_gcd(count2(k,p,mp)*count2(n-k,p,mp),mp,x,y);
(ret*=x)%=mp;
(ret*=count2(n,p,mp))%=mp;
return ret;
}
ll work()
{
ll i,ret=0,tmp,x,y;
prem();
for(i=0;i<tot;++i)
{
tmp=work1(i);
ex_gcd(m/mul[i],mul[i],x,y);
ret+=(((m/mul[i])%m*x)%m*tmp)%m;
}
ret=(ret%m+m)%m;
return (ret*cp())%m;
}
int main()
{
int t,cas=0;
scanf("%d",&t);
while(t--)
{
cas++;
scanf("%I64d%I64d%I64d",&n,&k,&m);
printf("Case %d: %I64d\n",cas,work());
}
return 0;
}

92
HDOJ/3440_autoAC.cpp Normal file
View File

@ -0,0 +1,92 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
int T,n,d,dis[1010],nxt[1010],h,in[1010],cnt[1010];
const int inf=1000000000;
using namespace std;
struct node
{
int id,h;
node(int a=0,int b=0):id(a),h(b){}
friend bool operator < (node a,node b)
{
return a.h<b.h;
}
};
struct edge
{
int y,l;
edge(int a,int b):y(a),l(b){}
};
vector<edge>vt[1010];
node a[1010];
int spfa(int x,int y)
{
fill(dis,dis+1+n,inf);
memset(in,0,sizeof(in));
dis[x]=0;
in[x]=1;
memset(cnt,0,sizeof(cnt));
cnt[x]=1;
queue<int>q;
q.push(x);
while(!q.empty())
{
int s=q.front();
q.pop();
in[s]=0;
for(int i=0;i<vt[s].size();i++)
{
int y=vt[s][i].y,l=vt[s][i].l;
if(dis[y]>dis[s]+l)
{
dis[y]=dis[s]+l;
if(!in[y])
{
q.push(y);
cnt[y]++;
if(cnt[y]>n)return -1;
in[y]=1;
}
}
}
}
return dis[y];
}
int main()
{
scanf("%d",&T);
for(int cas=1;cas<=T;cas++)
{
scanf("%d%d",&n,&d);
for(int i=1;i<=n;i++)
vt[i].clear();
for(int i=1;i<=n;i++)
{
scanf("%d",&h);
a[i]=node(i,h);
}
sort(a+1,a+1+n);
for(int i=1;i<n;i++)
{
vt[i+1].push_back(edge(i,-1));
}
for(int i=1;i<n;i++)
{
vt[min(a[i+1].id,a[i].id)].push_back(edge(max(a[i+1].id,a[i].id),d));
}
int a1=a[1].id,a2=a[n].id,k;
if(a1<a2)
{
k=spfa(a1,a2);
}
else
{
k=spfa(a2,a1);
}
printf("Case %d: %d\n",cas,k);
}
return 0;
}

154
HDOJ/3441_autoAC.cpp Normal file
View File

@ -0,0 +1,154 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MOD 1000000007
#define MAXD 40010
using namespace std;
int isprime[MAXD], prime[MAXD], P, p[MAXD], pn;
long long A, C, K, ANS, T;
void exgcd(long long a, long long b, long long &x, long long &y)
{
if(b == 0)
x = 1, y = 0;
else
exgcd(b, a % b, y, x), y -= x * (a / b);
}
long long powmod(long long a, long long n)
{
long long ans = 1;
while(n)
{
if(n & 1)
ans = ans * a % MOD;
n >>= 1;
a = a * a % MOD;
}
return ans;
}
void prepare()
{
int i, j, k = 40000;
memset(isprime, -1, sizeof(isprime));
P = 0;
for(i = 2; i <= k; i ++)
if(isprime[i])
{
prime[P ++] = i;
for(j = i * i; j <= k; j += i)
isprime[j] = 0;
}
}
long long block(long long n, long long c)
{
long long ans, x, y;
ans = powmod(c, n * n);
ans = (ans + 2 * powmod(c, n * n / 4 + (n & 1))) % MOD;
ans = (ans + powmod(c, n * n / 2 + (n & 1))) % MOD;
exgcd(4, MOD, x, y);
x = (x % MOD + MOD) % MOD;
ans = ans * x % MOD;
return ans;
}
long long euler(long long n)
{
int i;
long long ans = n;
for(i = 0; i < pn; i ++)
if(n % p[i] == 0)
ans = ans / p[i] * (p[i] - 1);
return ans;
}
long long prepareBK()
{
int i, j, nx, ny, x, y, cnt;
long long N = 1;
nx = x = A - 1, ny = y = A + 1;
pn = 0;
for(i = 0; i < P && prime[i] * prime[i] <= ny; i ++)
{
cnt = 0;
if(x % prime[i] == 0 || y % prime[i] == 0)
p[pn ++] = prime[i];
while(x % prime[i] == 0)
++ cnt, x /= prime[i];
while(y % prime[i] == 0)
++ cnt, y /= prime[i];
for(j = 0, cnt /= 2; j < cnt; j ++)
N *= prime[i];
}
if(x > y)
i = x, x = y, y = i;
if(x > 1)
p[pn ++] = x;
if(y > 1)
p[pn ++] = y;
if(x == y)
N *= x;
return N;
}
void dfs(int cur, long long R, long long x, long long &c)
{
int i, cnt = 0;
long long t = 1;
if(cur == pn)
{
long long ans, n, x, y;
n = euler(K / R) % MOD;
T = (T + n * powmod(c, R)) % MOD;
return ;
}
while(x % p[cur] == 0)
x /= p[cur], ++ cnt;
for(i = 0; i <= cnt; i ++)
{
dfs(cur + 1, R * t, x, c);
t *= p[cur];
}
}
void findB(int cur, long long B, long long x)
{
int i, cnt = 0;
long long t = 1;
if(cur == pn)
{
long long n, x, y, c;
c = block(B, C);
K = (A * A - 1) / (B * B);
T = 0;
dfs(0, 1, K, c);
exgcd(K, MOD, x, y);
x = (x % MOD + MOD) % MOD;
T = (T * x) % MOD;
ANS = (ANS + T * C) % MOD;
return ;
}
while(x % p[cur] == 0)
++ cnt, x /= p[cur];
for(i = 0; i <= cnt; i ++)
{
findB(cur + 1, B * t, x);
t *= p[cur];
}
}
void solve()
{
ANS = 0;
findB(0, 1, prepareBK());
printf("%I64d\n", ANS);
}
int main()
{
int t, tt;
prepare();
scanf("%d", &t);
for(tt = 0; tt < t; tt ++)
{
printf("Case %d: ", tt + 1);
scanf("%I64d%I64d", &A, &C);
if(A == 1)
printf("%I64d\n", C);
else
solve();
}
return 0;
}

140
HDOJ/3442_autoAC.cpp Normal file
View File

@ -0,0 +1,140 @@
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct node
{
int damage,x,y;
int mark[5];
bool operator <(const node &a)const
{
return a.damage<damage;
}
};
typedef struct tt
{
int x,y,t;
}NODE;
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
node cur,ne;
int n,m,tag;NODE cur1,next1;
char a[55][55];int mark[55][55],mark1[55][55];
void bfs(int x,int y)
{
priority_queue<node> qu;
cur.x=x;cur.y=y;cur.damage =0;
queue<NODE> q;
int i,j;
for(i=0;i<5;i++)
{
cur.mark[i]=0;
}
qu.push(cur);
while(!qu.empty ())
{
cur=qu.top ();
qu.pop();
if(a[cur.x][cur.y]=='!')
{
tag=1;
return ;
}
for(i=0;i<4;i++)
{
ne.x=cur.x+dir[i][0];
ne.y=cur.y+dir[i][1];
if(ne.x>=1&&ne.x<=n&&ne.y>=1&&ne.y<=m&&a[ne.x][ne.y]!='#'&&a[ne.x][ne.y]!='A'&&a[ne.x][ne.y]!='B'&&a[ne.x][ne.y]!='D'&&a[ne.x][ne.y]!='E')
{
ne.damage =cur.damage ;
for(j=0;j<5;j++)
{
ne.mark[j]=cur.mark[j];
}
if(a[ne.x][ne.y]=='C' && cur.mark[2]==0)
{
ne.damage +=3;
ne.mark[2]=1;
}
memset(mark1,0,sizeof(mark1));
while(!q.empty ())
q.pop ();
cur1.x=ne.x;
cur1.y=ne.y;
cur1.t=0;
q.push(cur1);
while(!q.empty ())
{
cur1=q.front ();
q.pop();
if(cur1.t>3)
break;
if(ne.mark[0]==0 && cur1.t<=2 && a[cur1.x][cur1.y]=='A')
{
ne.damage+=1;
ne.mark[0]=1;
}
if(ne.mark[1]==0 && cur1.t<=3 && a[cur1.x][cur1.y]=='B')
{
ne.damage +=2;
ne.mark[1]=1;
}
if(ne.mark[3]==0 && cur1.t<=2&&a[cur1.x][cur1.y]=='D')
{
ne.damage +=4;
ne.mark[3]=1;
}
if(ne.mark[4]==0 && cur1.t<=1&&a[cur1.x][cur1.y]=='E')
{
ne.damage +=5;
ne.mark [4]=1;
}
for(j=0;j<=4;j++)
{
next1.x=cur1.x+dir[j][0];
next1.y=cur1.y+dir[j][1];
if(next1.x>=1&&next1.x<=n&&next1.y>=1&&next1.y<=m&&!mark1[next1.x][next1.y])
{
mark1[next1.x][next1.y]=1;
next1.t=cur1.t+1;
q.push(next1);
}
}
}
if(mark[ne.x][ne.y]>ne.damage)
{
mark[ne.x][ne.y]=ne.damage;
qu.push(ne);
}
}
}
}
}
int main()
{
int t,i,j,sx,sy,count=0;
scanf("%d",&t);
while(t--)
{
count++;tag=0;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
{
getchar();
for(j=1;j<=m;j++)
{
scanf("%c",&a[i][j]);
mark[i][j]=0xfffffff;
if(a[i][j]=='$')
{
sx=i;sy=j;
}
}
}
bfs(sx,sy);
if(tag)
printf("Case %d: %d\n",count,cur.damage );
else
printf("Case %d: %d\n",count,-1);
}
return 0;
}

14
HDOJ/3443_autoAC.cpp Normal file
View File

@ -0,0 +1,14 @@
#include <cstdio>
#define LL __int64
int main()
{
LL x,k;
while(scanf("%I64d",&x)&&x){
k=1;
while(k<x)k=k*10+1;
k/=10;
while(x%k!=0)k/=10;
printf("%I64d\n",x/k);
}
return 0;
}

170
HDOJ/3446_autoAC.cpp Normal file
View File

@ -0,0 +1,170 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N=250;
int n;
int head;
int tail;
int Start;
int Finish;
int link[N];
int Father[N];
int Base[N];
int Q[N];
bool mark[N];
bool mat[N][N];
bool InBlossom[N];
bool in_Queue[N];
void BlossomContract(int x,int y){
memset(mark,0,sizeof(mark));
memset(InBlossom,0,sizeof(InBlossom));
#define pre Father[link[i]]
int lca,i;
for (i=x;i;i=pre) {i=Base[i]; mark[i]=true; }
for (i=y;i;i=pre) {i=Base[i]; if (mark[i]) {lca=i; break;} }
for (i=x;Base[i]!=lca;i=pre){
if (Base[pre]!=lca) Father[pre]=link[i];
InBlossom[Base[i]]=true;
InBlossom[Base[link[i]]]=true;
}
for (i=y;Base[i]!=lca;i=pre){
if (Base[pre]!=lca) Father[pre]=link[i];
InBlossom[Base[i]]=true;
InBlossom[Base[link[i]]]=true;
}
#undef pre
if (Base[x]!=lca) Father[x]=y;
if (Base[y]!=lca) Father[y]=x;
for (i=1;i<=n;i++)
if (InBlossom[Base[i]]){
Base[i]=lca;
if (!in_Queue[i]){
Q[++tail]=i;
in_Queue[i]=true;
}
}
}
void Change(){
int x,y,z;
z=Finish;
while (z){
y=Father[z];
x=link[y];
link[y]=z;
link[z]=y;
z=x;
}
}
void FindAugmentPath(){
memset(Father,0,sizeof(Father));
memset(in_Queue,0,sizeof(in_Queue));
for (int i=1;i<=n;i++) Base[i]=i;
head=0; tail=1;
Q[1]=Start;
in_Queue[Start]=1;
while (head!=tail){
int x=Q[++head];
for (int y=1;y<=n;y++)
if (mat[x][y] && Base[x]!=Base[y] && link[x]!=y)
if ( Start==y || link[y] && Father[link[y]] )
BlossomContract(x,y);
else if (!Father[y]){
Father[y]=x;
if (link[y]){
Q[++tail]=link[y];
in_Queue[link[y]]=true;
}
else{
Finish=y;
Change();
return;
}
}
}
}
void Edmonds(){
memset(link,0,sizeof(link));
for (Start=1;Start<=n;Start++)
if (link[Start]==0)
FindAugmentPath();
}
int MaxMatch()
{
Edmonds();
int cnt=0;
for (int i=1;i<=n;i++)
if (link[i]) cnt++;
return cnt;
}
void output(){
memset(mark,0,sizeof(mark));
int cnt=0;
for (int i=1;i<=n;i++)
if (link[i]) cnt++;
printf("%d\n",cnt);
for (int i=1;i<=n;i++)
if (!mark[i] && link[i]){
mark[i]=true;
mark[link[i]]=true;
printf("%d %d\n",i,link[i]);
}
}
char str[50][50];
const int dx[]={-1,-1,-1,1,1,1,0,0,2,-2,2,-2,2,-2,2,-2,1,-1,-1,1};
const int dy[]={-1,1,0,0,1,-1,-1,1,2,2,-2,-2,1,-1,-1,1,2,-2,2,-2};
int main()
{
int ci,pl=1;scanf("%d",&ci);
while(ci--)
{
int r,c;scanf("%d%d",&r,&c);
n=r*c;
for(int i=0;i<r;i++) scanf("%s",str[i]);
int maxMatch,maxMatchK;
memset(mat,0,sizeof(mat));
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
int p1=i*c+j+1;
if(str[i][j]=='#'||str[i][j]=='K') continue;
for(int k=0;k<20;k++)
{
int x=i+dx[k],y=j+dy[k];
if(x>=0&&x<r&&y>=0&&y<c){
if(str[x][y]=='#'||str[x][y]=='K') continue;
int p2=x*c+y+1;
mat[p1][p2]=1;
}
}
}
}
maxMatch=MaxMatch();
memset(mat,0,sizeof(mat));
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
int p1=i*c+j+1;
if(str[i][j]=='#') continue;
for(int k=0;k<24;k++)
{
int x=i+dx[k],y=j+dy[k];
if(x>=0&&x<r&&y>=0&&y<c){
if(str[x][y]=='#') continue;
int p2=x*c+y+1;
mat[p1][p2]=1;
}
}
}
}
maxMatchK=MaxMatch();
if(maxMatch==maxMatchK)
printf("Case #%d: daizhenyang lose\n",pl++);
else printf("Case #%d: daizhenyang win\n",pl++);
}
return 0;
}

40
HDOJ/3448_autoAC.cpp Normal file
View File

@ -0,0 +1,40 @@
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m,k;
int wi[60];
int cnt;
void dfs(int id,int num,int weight)
{
cnt=max(cnt,weight);
if(id==k) return ;
dfs(id+1,num,weight);
if(weight+wi[id]<=m&&num+1<=n) dfs(id+1,num+1,weight+wi[id]);
}
int main()
{
while(scanf("%d%d",&n,&m)==2)
{
scanf("%d",&k);
int tsum;
for(int i=0;i<k;i++)
{
scanf("%d",&wi[i]);
}
sort(wi,wi+k);
tsum=0;
for(int i=k-1;i>=k-n;i--) tsum+=wi[i];
if(wi[0]>m) tsum=0;
if(tsum<=m)
{
printf("%d\n",tsum);
continue;
}
cnt=0;
dfs(0,0,0);
printf("%d\n",cnt);
}
return 0;
}

30
HDOJ/3449_autoAC.cpp Normal file
View File

@ -0,0 +1,30 @@
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int Ni = 55;
const int Mi = 100005;
int dp[Ni][Mi];
int main()
{
int n,m,tv,i,j,k,pi,c,w;
memset(dp[0],0,sizeof(dp[0]));
while(~scanf("%d%d",&n,&tv))
{
for(i=1;i<=n;i++)
{
scanf("%d%d",&pi,&m);
for(j=0;j<pi;j++) dp[i][j]=-1;
for(j=tv;j>=pi;j--) dp[i][j]=dp[i-1][j-pi];
for(k=0;k<m;k++)
{
scanf("%d%d",&c,&w);
for(j=tv;j>=c;j--) if(dp[i][j-c]!=-1)
dp[i][j]=max(dp[i][j],dp[i][j-c]+w);
}
for(j=tv;j>=0;j--) dp[i][j]=max(dp[i][j],dp[i-1][j]);
}
printf("%d\n",dp[n][tv]);
}
return 0;
}

69
HDOJ/3450_autoAC.cpp Normal file
View File

@ -0,0 +1,69 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int maxn=100002;
const int oo=1000000000;
const int mod=9901;
int c[maxn],n,d,b[maxn],a[maxn];
void add(int x,int val)
{
while(x<=maxn)
{
c[x]+=val;
c[x]%=mod;
x+=(x&-x);
}
}
int sum(int x)
{
int s=0;
while(x>0)
{
s+=c[x];
s%=mod;
x-=(x&-x);
}
return s;
}
int main()
{
int i,id,up,down,val,ans;
while(~scanf("%d%d",&n,&d))
{
map<int,int>flag;
map<int,int>::iterator it;
flag[0]=0;
memset(c,0,sizeof(c));
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b,b+n);
int cnt=2;
flag[b[0]]=1;
for(i=1; i<n; i++)
if(b[i]!=b[i-1])
flag[b[i]]=cnt++;
flag[oo]=cnt++;
for(i=0; i<n; i++)
{
it=flag.lower_bound(a[i]);
id=it->second;
it=flag.upper_bound(a[i]+d);
up=it->second-1;
it=flag.lower_bound(a[i]-d);
down=it->second;
val=sum(up)-sum(down-1)+1;
val=(val%mod+mod)%mod;
add(id,val);
}
ans=sum(cnt);
ans=((ans-n)%mod+mod)%mod;
printf("%d\n",ans);
}
return 0;
}

121
HDOJ/3451_autoAC.cpp Normal file
View File

@ -0,0 +1,121 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int board[105][105];
int sep[105][105];
int n,m,l;
struct bnode
{
int x,y,d,time;
};
int dir[4][2]={0,-1,0,1,-1,0,1,0};
void bfs(int x,int y)
{
queue<bnode> sav;
while(!sav.empty())sav.pop();
board[x][y]++;
if(board[x][y]>l){
board[x][y]=0;
sep[x][y]=0;
for(int i=0;i<4;++i)
{
bnode tmp;
tmp.x=x+dir[i][0];
tmp.y=y+dir[i][1];
tmp.d=i;
tmp.time=1;
if(tmp.x<=n&&tmp.x>=1&&tmp.y<=m&&tmp.y>=1)
sav.push(tmp);
}
}
while(!sav.empty())
{
bnode now=sav.front();
sav.pop();
if(board[now.x][now.y]<l&&board[now.x][now.y]!=0)
{
board[now.x][now.y]++;
}
else if(board[now.x][now.y]==l)
{
board[now.x][now.y]=0;
sep[now.x][now.y]=now.time;
for(int i=0;i<4;++i)
{
bnode tmp;
tmp.x=now.x+dir[i][0];
tmp.y=now.y+dir[i][1];
tmp.d=i;
tmp.time=now.time+1;
if(tmp.x<=n&&tmp.x>=1&&tmp.y<=m&&tmp.y>=1)
sav.push(tmp);
}
}
else if(board[now.x][now.y]==0)
{
if(sep[now.x][now.y]!=now.time)
{
bnode tmp;
tmp.x=now.x+dir[now.d][0];
tmp.y=now.y+dir[now.d][1];
tmp.d=now.d;
tmp.time=now.time+1;
if(tmp.x<=n&&tmp.x>=1&&tmp.y<=m&&tmp.y>=1)
sav.push(tmp);
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(sep,0,sizeof(sep));
scanf("%d%d%d",&n,&m,&l);
bool flag=false;
for(int i=1;i<=n;++i)
{
for(int j=1;j<=m;++j)
{
scanf("%d",&board[i][j]);
if(board[i][j])flag=true;
}
}
int q;
scanf("%d",&q);
while(q--)
{
int x,y;
scanf("%d%d",&x,&y);
if(!flag)continue;
bfs(x,y);
flag=false;
for(int i=1;i<=n;++i)
{
if(flag)break;
for(int j=1;j<=m;++j)
{
if(board[i][j]){flag=true;break;}
}
}
}
if(!flag)printf("YES\n");
else
{
printf("NO\n");
for(int i=1;i<=n;++i)
{
for(int j=1;j<=m;++j)
{
printf("%d ",board[i][j]);
}
printf("\n");
}
}
}
return 0;
}

55
HDOJ/3452_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include<stdio.h>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 2005
#define INF 0x7fffffff
int dp[N];
int tmp=0;
int e, ne[N], v[N], w[N],adj[N];
void add(int x, int y, int z)
{
v[e] = y, w[e] = z;
ne[e] = adj[x], adj[x] = e ++;
}
void dfs(int cur, int fa)
{
int i, flag = 0;
dp[cur] = 0;
for(i = adj[cur]; i != -1; i = ne[i])
if(v[i] != fa)
{
flag = 1;
dfs(v[i], cur);
dp[cur] += min(w[i], dp[v[i]]);
}
if(flag == 0) dp[cur] = INF;
}
int main()
{
int n,r,a,b,c;
while(scanf("%d%d",&n,&r)!=EOF)
{
if(n==0 && r==0)
break;
memset(adj,-1,sizeof(adj));
for(int i=1; i<n; i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
for(int i=0; i<=n; i++)
dp[i]=INF;
if(n==1)
printf("0\n");
else
{
e=0;
dfs(r,-1);
printf("%d\n",dp[r]);
}
}
return 0;
}

45
HDOJ/3455_autoAC.cpp Normal file
View File

@ -0,0 +1,45 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
const int INF=0x3f3f3f;
const int maxn=1e5+100;
int dp[maxn][15];
int ha[10*maxn];
int num[maxn],n;
int main()
{
while(~scanf("%d",&n)&&n)
{
memset(dp,INF,sizeof(dp));
memset(ha,-1,sizeof(ha));
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
ha[num[i]]=i;
}
dp[2][num[2]-num[1]]=0;
for(int i=2;i<=n;i++)
{
for(int j=1;j<=10;j++)
{
if(j>num[i]) break;
for(int k=j+1;k<=10;k++)
{
int tt=num[i]-j;
if(ha[tt]>=0)
dp[i][j]=min(dp[ha[tt]][k-j]+1,dp[i][j]);
}
}
}
int ans=INF;
for(int i=1;i<=10;i++)
ans=min(ans,dp[n][i]);
if(ans<INF) printf("%d\n",ans);
else printf("-1\n");
}
return 0;
}

33
HDOJ/3456_autoAC.cpp Normal file
View File

@ -0,0 +1,33 @@
#include<iostream>
#include<string>
using namespace std;
string word;
string buf;
int main(){
bool sign=true;
while(cin>>word){
if(sign){
buf+="Forty-two";
sign=false;
}
if(word[word.size()-1]=='.'){
buf.clear();
sign=true;
continue;
}
if(word[word.size()-1]=='?'){
buf+=" ";
buf+=word;
buf[buf.size()-1]='.';
cout<<buf<<endl;
buf.clear();
sign=true;
continue;
}
if(word != "What"){
buf+=" ";
buf+=word;
}
}
return 0;
}

56
HDOJ/3457_autoAC.cpp Normal file
View File

@ -0,0 +1,56 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define Max(a,b) a>b?a:b
using namespace std;
int n;
int dp[1111],map[1111][1111];
struct node
{
int x1,x2,y1,y2;
}s[1111];
int dfs(int x)
{
int i,j,k,l;
if(dp[x])return dp[x];
for(i=0;i<n;i++)
if(map[x][i])
{
dp[x]=Max(dp[x],dfs(i)+1);
}
dp[x]=Max(dp[x],1);
return dp[x];
}
int main (void)
{
int i,j,k,l,x1,x2,y1,y2,sum;
while(scanf("%d",&n)!=EOF&&n)
{
for(i=0;i<n;i++)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
s[i].x1=x1;
s[i].x2=x2;
s[i].y1=y1;
s[i].y2=y2;
}
memset(map,0,sizeof(map));
memset(dp,0,sizeof(dp));
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(s[i].x2<s[j].x1&&s[i].y2<s[j].y1)
{
map[i][j]=1;
}
}
sum=0;
for(i=0;i<n;i++)
{
sum=Max(sum,dfs(i));
}
printf("%d\n",sum);
}
return 0;
}

49
HDOJ/3458_autoAC.cpp Normal file
View File

@ -0,0 +1,49 @@
#include<iostream>
#include<algorithm>
using namespace std;
#define N 200020
struct Node {
int x,y,lt,id ;
}point[N];
int n,len,maxlp[N],by[N];
int cmp(Node a,Node b) {
if(a.x != b.x)
return a.x < b.x ;
if(a.y == b.y)
return a.lt > b.lt ;
return a.y < b.y ;
}
int find(int len,int _y) {
int mid, left = 1, right = len ;
while(left <= right) {
mid = (left+right) >> 1 ;
if(by[mid] >= _y)
right = mid-1 ;
else left = mid+1 ;
}
return left ;
}
int main() {
while(scanf("%d",&n) && n) {
for(int i=0;i<n;i++) {
scanf("%d%d",&point[2*i].x,&point[2*i].y);
point[2*i].lt = 1 ;
scanf("%d%d",&point[2*i+1].x,&point[2*i+1].y);
point[2*i+1].lt = 0 ;
point[2*i].id = point[2*i+1].id = i ;
}
n *= 2 ;
sort(point,point+n,cmp) ;
len = 0 ;
for(int i=0;i<n;i++) {
if(point[i].lt == 1) maxlp[point[i].id] = find(len,point[i].y) ;
else {
int tmp = maxlp[point[i].id] ;
if(tmp > len) by[++len] = point[i].y ;
else by[tmp] = min(by[tmp],point[i].y) ;
}
}
printf("%d\n",len);
}
return 0;
}

202
HDOJ/3459_autoAC.cpp Normal file
View File

@ -0,0 +1,202 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct node
{
int x,y;
} cube[10][10],side[10][10];
char color[10],rubik[10][10];
int ans[1000];
int flag,step;
void init()
{
cube[0][0].x=3,cube[0][0].y=2;
cube[0][1].x=3,cube[0][1].y=1;
cube[0][2].x=4,cube[0][2].y=2;
cube[1][0].x=3,cube[1][0].y=3;
cube[1][1].x=3,cube[1][1].y=4;
cube[1][2].x=4,cube[1][2].y=3;
cube[2][0].x=2,cube[2][0].y=2;
cube[2][1].x=2,cube[2][1].y=1;
cube[2][2].x=1,cube[2][2].y=2;
cube[3][0].x=2,cube[3][0].y=3;
cube[3][1].x=1,cube[3][1].y=3;
cube[3][2].x=2,cube[3][2].y=4;
cube[4][0].x=3,cube[4][0].y=0;
cube[4][1].x=5,cube[4][1].y=2;
cube[4][2].x=3,cube[4][2].y=7;
cube[5][0].x=5,cube[5][0].y=3;
cube[5][1].x=3,cube[5][1].y=5;
cube[5][2].x=3,cube[5][2].y=6;
cube[6][0].x=0,cube[6][0].y=2;
cube[6][1].x=2,cube[6][1].y=7;
cube[6][2].x=2,cube[6][2].y=0;
cube[7][0].x=0,cube[7][0].y=3;
cube[7][1].x=2,cube[7][1].y=5;
cube[7][2].x=2,cube[7][2].y=6;
side[0][0].x=0,side[0][0].y=2;
side[0][1].x=0,side[0][1].y=3;
side[0][2].x=1,side[0][2].y=2;
side[0][3].x=1,side[0][3].y=3;
side[1][0].x=2,side[1][0].y=0;
side[1][1].x=2,side[1][1].y=1;
side[1][2].x=3,side[1][2].y=0;
side[1][3].x=3,side[1][3].y=1;
side[2][0].x=2,side[2][0].y=2;
side[2][1].x=2,side[2][1].y=3;
side[2][2].x=3,side[2][2].y=2;
side[2][3].x=3,side[2][3].y=3;
side[3][0].x=2,side[3][0].y=4;
side[3][1].x=2,side[3][1].y=5;
side[3][2].x=3,side[3][2].y=4;
side[3][3].x=3,side[3][3].y=5;
side[4][0].x=2,side[4][0].y=6;
side[4][1].x=2,side[4][1].y=7;
side[4][2].x=3,side[4][2].y=6;
side[4][3].x=3,side[4][3].y=7;
side[5][0].x=4,side[5][0].y=2;
side[5][1].x=4,side[5][1].y=3;
side[5][2].x=5,side[5][2].y=2;
side[5][3].x=5,side[5][3].y=3;
}
char get_color(int A,int B,int C)
{
for(int i=0; i<8; i++)
{
if(rubik[cube[i][0].x][cube[i][0].y]==color[A]&&rubik[cube[i][1].x][cube[i][1].y]==color[B]&&rubik[cube[i][2].x][cube[i][2].y]!=color[C])
return rubik[cube[i][2].x][cube[i][2].y];
if(rubik[cube[i][1].x][cube[i][1].y]==color[A]&&rubik[cube[i][0].x][cube[i][0].y]==color[B]&&rubik[cube[i][2].x][cube[i][2].y]!=color[C])
return rubik[cube[i][2].x][cube[i][2].y];
if(rubik[cube[i][0].x][cube[i][0].y]==color[A]&&rubik[cube[i][2].x][cube[i][2].y]==color[B]&&rubik[cube[i][1].x][cube[i][1].y]!=color[C])
return rubik[cube[i][1].x][cube[i][1].y];
if(rubik[cube[i][2].x][cube[i][2].y]==color[A]&&rubik[cube[i][0].x][cube[i][0].y]==color[B]&&rubik[cube[i][1].x][cube[i][1].y]!=color[C])
return rubik[cube[i][1].x][cube[i][1].y];
if(rubik[cube[i][1].x][cube[i][1].y]==color[A]&&rubik[cube[i][2].x][cube[i][2].y]==color[B]&&rubik[cube[i][0].x][cube[i][0].y]!=color[C])
return rubik[cube[i][0].x][cube[i][0].y];
if(rubik[cube[i][2].x][cube[i][2].y]==color[A]&&rubik[cube[i][1].x][cube[i][1].y]==color[B]&&rubik[cube[i][0].x][cube[i][0].y]!=color[C])
return rubik[cube[i][0].x][cube[i][0].y];
}
}
void turn_x(char maze[10][10])
{
char tmp;
tmp=maze[2][4];
maze[2][4]=maze[2][5];
maze[2][5]=maze[3][5];
maze[3][5]=maze[3][4];
maze[3][4]=tmp;
tmp=maze[1][3];
maze[1][3]=maze[2][6];
maze[2][6]=maze[5][3];
maze[5][3]=maze[3][3];
maze[3][3]=tmp;
tmp=maze[0][3];
maze[0][3]=maze[3][6];
maze[3][6]=maze[4][3];
maze[4][3]=maze[2][3];
maze[2][3]=tmp;
}
void turn_y(char maze[10][10])
{
char tmp;
tmp=maze[2][0];
maze[2][0]=maze[2][6];
maze[2][6]=maze[2][4];
maze[2][4]=maze[2][2];
maze[2][2]=tmp;
tmp=maze[2][1];
maze[2][1]=maze[2][7];
maze[2][7]=maze[2][5];
maze[2][5]=maze[2][3];
maze[2][3]=tmp;
tmp=maze[0][2];
maze[0][2]=maze[0][3];
maze[0][3]=maze[1][3];
maze[1][3]=maze[1][2];
maze[1][2]=tmp;
}
void turn_z(char maze[10][10])
{
char tmp;
tmp=maze[2][1];
maze[2][1]=maze[1][3];
maze[1][3]=maze[3][4];
maze[3][4]=maze[4][2];
maze[4][2]=tmp;
tmp=maze[3][1];
maze[3][1]=maze[1][2];
maze[1][2]=maze[2][4];
maze[2][4]=maze[4][3];
maze[4][3]=tmp;
tmp=maze[2][2];
maze[2][2]=maze[2][3];
maze[2][3]=maze[3][3];
maze[3][3]=maze[3][2];
maze[3][2]=tmp;
}
int get_h(char mid[10][10])
{
int i,j,sum = 0;
for(i = 0; i<6; i++)
{
for(j = 0; j<4; j++)
{
if(mid[side[i][j].x][side[i][j].y]!=color[i])
sum++;
}
}
return (sum+7)/8;
}
int IDA(char mid[10][10],int cnt)
{
if(cnt+get_h(mid)>step)
return 0;
if(cnt == step)
return 1;
for(int i = 0; i<3; i++)
{
char tem[10][10];
for(int x = 0; x<6; x++)
for(int y = 0; y<8; y++)
tem[x][y]=mid[x][y];
if(i == 0)
turn_x(tem);
else if(i == 1)
turn_y(tem);
else
turn_z(tem);
ans[cnt] = i;
if(IDA(tem,cnt+1))
return 1;
}
return 0;
}
int main()
{
int i;
init();
while(~scanf("%s",rubik[0]))
{
for(i = 1; i<6; i++)
scanf("%s",rubik[i]);
if(!strcmp(rubik[0],"........"))
break;
color[1]=rubik[3][0];
color[5]=rubik[5][2];
color[4]=rubik[3][7];
color[0]=get_color(1,4,5);
color[2]=get_color(1,5,4);
color[3]=get_color(4,5,1);
step = 0;
while(1)
{
if(IDA(rubik,0)) break;
step++;
}
for(i = 0; i<step; i++)
printf("%c",ans[i]+'X');
printf("\n");
}
return 0;
}

67
HDOJ/3460_autoAC.cpp Normal file
View File

@ -0,0 +1,67 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct dic
{
struct dic *child[26];
};
struct dic *root;
int max;
int ans;
void insert(char *source)
{
struct dic *now,*newnode;
int i,j;
int len;
len=strlen(source);
max=max>len?max:len;
now=root;
for(i=0;i<len;i++)
{
if(now->child[source[i]-'a']!=NULL) now=now->child[source[i]-'a'];
else
{
newnode=(struct dic*)malloc(sizeof(struct dic));
for(j=0;j<26;j++) newnode->child[j]=NULL;
now->child[source[i]-'a']=newnode;
now=newnode;
}
}
}
void count(struct dic *now)
{
int j;
for(j=0;j<26;j++)
{
if(now->child[j]==NULL) continue;
else
{
ans++;
count(now->child[j]);
}
}
}
int main()
{
int n;
int i,j;
char str[55];
while(scanf("%d",&n)!=-1)
{
root=(struct dic *)malloc(sizeof(struct dic));
for(j=0;j<26;j++) root->child[j]=0;
max=0;
for(i=0;i<n;i++)
{
scanf("%s",str);
insert(str);
}
ans=0;
count(root);
ans*=2;
ans-=max;
ans+=n;
printf("%d\n",ans);
}
return 0;
}

58
HDOJ/3461_autoAC.cpp Normal file
View File

@ -0,0 +1,58 @@
#include<stdio.h>
#include<math.h>
int set[10000005];
int count;
#define mod 1000000007
int find(int x)
{
int r=x;
while(r!=set[r])
r=set[r];
int i=x;
while(i!=r)
{
int j=set[i];
set[i]=r;
i=j;
}
return r;
}
void merge(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
set[fx]=fy;
count++;
}
}
long long exp(int n){
long long sum=1, tmp=26;
while(n){
if(n&1){
sum = sum*tmp;
sum %= mod;
}
tmp = (tmp*tmp)%mod;
n>>=1;
}
return sum;
}
int main()
{
int n,m,l,r;
while(scanf("%d%d",&n,&m)!=EOF)
{
count=0;
for(int i=0;i<=n;i++)
set[i]=i;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&l,&r);
merge(l-1,r);
}
printf("%lld\n",exp(n-count)%mod);
}
return 0;
}

138
HDOJ/3462_autoAC.cpp Normal file
View File

@ -0,0 +1,138 @@
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
#define inf 1000000000
struct edge
{
int to,cost,next;
int T, mod;
}e[32000];
struct node
{
int x,y,z;
node(int _x=0, int _y=0, int _z=0):x(_x),y(_y),z(_z){}
bool operator<(const node& a)const
{
return y>a.y;
}
};
priority_queue<node> que;
bool visit[4000];
int dis[4000][2];
int pre[4000];
int next[8000];
int n,m,num;
int a[40][40];
void addedge(int from, int to, int cost, int T, int mod)
{
e[num].to=to;e[num].cost=cost;
e[num].T=T;e[num].mod=mod;
e[num].next=pre[from];
pre[from]=num;
num++;
}
void make_map()
{
num=1;
memset(pre, 0, sizeof(pre));
for (int i=0; i<n; i++)
for (int j=0; j<m; j++)
{
addedge(4*i*m+4*j, 4*i*m+4*j+1, 1, a[i][j], 1);addedge(4*i*m+4*j+1, 4*i*m+4*j, 1, a[i][j], 1);
addedge(4*i*m+4*j, 4*i*m+4*j+2, 1, a[i][j], 0);addedge(4*i*m+4*j+2, 4*i*m+4*j, 1, a[i][j], 0);
addedge(4*i*m+4*j+3, 4*i*m+4*j+1, 1, a[i][j],0);addedge(4*i*m+4*j+1, 4*i*m+4*j+3, 1, a[i][j], 0);
addedge(4*i*m+4*j+3, 4*i*m+4*j+2, 1, a[i][j],1);addedge(4*i*m+4*j+2, 4*i*m+4*j+3, 1, a[i][j], 1);
if (j!=m-1)
{
addedge(4*i*m+4*j+1,4*i*m+4*(j+1),2,0,0);addedge(4*i*m+4*(j+1),4*i*m+4*j+1,2,0,0);
addedge(4*i*m+4*j+3,4*i*m+4*(j+1)+2,2,0,0);addedge(4*i*m+4*(j+1)+2,4*i*m+4*j+3,2,0,0);
}
if (i!=n-1)
{
addedge(4*i*m+4*j+2,4*(i+1)*m+4*j,1,0,0);addedge(4*(i+1)*m+4*j,4*i*m+4*j+2,1,0,0);
addedge(4*i*m+4*j+3,4*(i+1)*m+4*j+1,1,0,0);addedge(4*(i+1)*m+4*j+1,4*i*m+4*j+3,1,0,0);
}
}
}
void get_in(int to,int cost,int id)
{
if (dis[to][id]<=cost||dis[to][0]<=cost) return;
dis[to][id]=cost;
que.push(node(to,cost,id));
}
int dfs(int x,int tt)
{
while(!que.empty()) que.pop();
for (int i=0;i<4*n*m;i++)
{
visit[i]=0;
dis[i][0]=dis[i][1]=inf;
}
que.push(node(x,tt,0));
while(!que.empty())
{
node out=que.top();
que.pop();
int x=out.x,y=out.y,z=out.z;
if (visit[x]) continue;
if (z==0)
visit[x]=1;
if (x==4*n*m-1) break;
for (int i=pre[x]; i!=0; i=e[i].next)
if (!visit[e[i].to])
{
int ty;
if (e[i].T==0)
{
ty=y+e[i].cost;
get_in(e[i].to,ty,z);
}
else
{
int tmp=(y+e[i].T-1)/e[i].T;
if (y%e[i].T==0) tmp++;
if (tmp%2==e[i].mod)
{
ty=y+1;
get_in(e[i].to,ty,z);
}
else
{
if (z==0)
{
ty=y+1;
get_in(e[i].to,ty,1);
}
ty=y+e[i].T-y%e[i].T+1;
get_in(e[i].to,ty,z);
}
}
}
}
return min(dis[4*n*m-1][0],dis[4*n*m-1][1]);
}
int main()
{
while (scanf("%d%d",&n,&m)!=EOF)
{
n--;m--;
for (int i=0; i<n; i++)
for (int j=0; j<m; j++)
scanf("%d",&a[i][j]);
make_map();
char tt[50];
int ta,tb;
scanf("%s",tt);
for (int i=0;tt[i];i++)
if (tt[i]==':') tt[i]=' ';
sscanf(tt,"%d %d",&ta,&tb);
ta=ta*60+tb;
int ans=dfs(0,ta);
ta=ans/60;
tb=ans%60;
printf("%02d:%02d\n",ta,tb);
}
return 0;
}

138
HDOJ/3463_autoAC.cpp Normal file
View File

@ -0,0 +1,138 @@
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
#define inf 1000000000
struct edge
{
int to,cost,next;
int T, mod;
}e[32000];
struct node
{
int x,y,z;
node(int _x=0, int _y=0, int _z=0):x(_x),y(_y),z(_z){}
bool operator<(const node& a)const
{
return y>a.y;
}
};
priority_queue<node> que;
bool visit[4000];
int dis[4000][2];
int pre[4000];
int next[8000];
int n,m,num;
int a[40][40];
void addedge(int from, int to, int cost, int T, int mod)
{
e[num].to=to;e[num].cost=cost;
e[num].T=T;e[num].mod=mod;
e[num].next=pre[from];
pre[from]=num;
num++;
}
void make_map()
{
num=1;
memset(pre, 0, sizeof(pre));
for (int i=0; i<n; i++)
for (int j=0; j<m; j++)
{
addedge(4*i*m+4*j, 4*i*m+4*j+1, 1, a[i][j], 1);addedge(4*i*m+4*j+1, 4*i*m+4*j, 1, a[i][j], 1);
addedge(4*i*m+4*j, 4*i*m+4*j+2, 1, a[i][j], 0);addedge(4*i*m+4*j+2, 4*i*m+4*j, 1, a[i][j], 0);
addedge(4*i*m+4*j+3, 4*i*m+4*j+1, 1, a[i][j],0);addedge(4*i*m+4*j+1, 4*i*m+4*j+3, 1, a[i][j], 0);
addedge(4*i*m+4*j+3, 4*i*m+4*j+2, 1, a[i][j],1);addedge(4*i*m+4*j+2, 4*i*m+4*j+3, 1, a[i][j], 1);
if (j!=m-1)
{
addedge(4*i*m+4*j+1,4*i*m+4*(j+1),2,0,0);addedge(4*i*m+4*(j+1),4*i*m+4*j+1,2,0,0);
addedge(4*i*m+4*j+3,4*i*m+4*(j+1)+2,2,0,0);addedge(4*i*m+4*(j+1)+2,4*i*m+4*j+3,2,0,0);
}
if (i!=n-1)
{
addedge(4*i*m+4*j+2,4*(i+1)*m+4*j,1,0,0);addedge(4*(i+1)*m+4*j,4*i*m+4*j+2,1,0,0);
addedge(4*i*m+4*j+3,4*(i+1)*m+4*j+1,1,0,0);addedge(4*(i+1)*m+4*j+1,4*i*m+4*j+3,1,0,0);
}
}
}
void get_in(int to,int cost,int id)
{
if (dis[to][id]<=cost||dis[to][0]<=cost) return;
dis[to][id]=cost;
que.push(node(to,cost,id));
}
int dfs(int x,int tt)
{
while(!que.empty()) que.pop();
for (int i=0;i<4*n*m;i++)
{
visit[i]=0;
dis[i][0]=dis[i][1]=inf;
}
que.push(node(x,tt,0));
while(!que.empty())
{
node out=que.top();
que.pop();
int x=out.x,y=out.y,z=out.z;
if (visit[x]) continue;
if (z==0)
visit[x]=1;
if (x==4*n*m-1) break;
for (int i=pre[x]; i!=0; i=e[i].next)
if (!visit[e[i].to])
{
int ty;
if (e[i].T==0)
{
ty=y+e[i].cost;
get_in(e[i].to,ty,z);
}
else
{
int tmp=(y+e[i].T-1)/e[i].T;
if (y%e[i].T==0) tmp++;
if (tmp%2==e[i].mod)
{
ty=y+1;
get_in(e[i].to,ty,z);
}
else
{
if (z==0)
{
ty=y+1;
get_in(e[i].to,ty,1);
}
ty=y+e[i].T-y%e[i].T+1;
get_in(e[i].to,ty,z);
}
}
}
}
return min(dis[4*n*m-1][0],dis[4*n*m-1][1]);
}
int main()
{
while (scanf("%d%d",&n,&m)!=EOF)
{
n--;m--;
for (int i=0; i<n; i++)
for (int j=0; j<m; j++)
scanf("%d",&a[i][j]);
make_map();
char tt[50];
int ta,tb;
scanf("%s",tt);
for (int i=0;tt[i];i++)
if (tt[i]==':') tt[i]=' ';
sscanf(tt,"%d %d",&ta,&tb);
ta=ta*60+tb;
int ans=dfs(0,ta);
ta=ans/60;
tb=ans%60;
printf("%02d:%02d\n",ta,tb);
}
return 0;
}

61
HDOJ/3464_autoAC.cpp Normal file
View File

@ -0,0 +1,61 @@
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
const int mod=100007;
int dp[30];
int succ[mod],cycle;
void get_T(int m)
{
int cnt=1;
memset(succ,-1,sizeof(succ));
succ[0]=0;
int now=0;
while(1)
{
now=(now*10+2)%m;
if (succ[now]!=-1)
{
cycle=cnt-succ[now];
break;
}
succ[now]=cnt++;
}
}
int cal(int x,int m)
{
if (x==1) return 1;
get_T(m);
int tmp=cal(x-1,cycle);
int now=0;
while(tmp--)
{
now=now*10+2;
if (now>=m)
{
int t=(now-mod)/m;
now=now-t*m;
}
}
return now;
}
int main()
{
for (int i=1;i<10;i++)
{
dp[i]=(9*cal(i,mod)+1)%mod;
}
int n;
while(scanf("%d",&n)!=EOF)
{
int ans;
if (n<=9)
ans=n==1?1:dp[n-1];
else
if (n%2==1)
ans=dp[8];
else ans=dp[7];
printf("%d\n",ans);
}
return 0;
}

79
HDOJ/3465_autoAC.cpp Normal file
View File

@ -0,0 +1,79 @@
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
#define lowbit(x) (x&(-x))
const int maxn=5e4+10;
struct node{
double a,b;
int num;
}e[maxn];
double l,r;
int c[maxn];
int cmp1(node x,node y)
{
if(x.a==y.a)return x.b<y.b;
return x.a<y.a;
}
int cmp2(node x,node y)
{
if(x.b==y.b)return x.a>y.a;
return x.b>y.b;
}
void add(int x,int val)
{
while(x<maxn)
{
c[x]+=val;
x+=lowbit(x);
}
}
int sum(int x)
{
int s=0;
while(x>0)
{
s+=c[x];
x-=lowbit(x);
}
return s;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int t,tt,i,j,ans=0;
double x1,y1,x2,y2,k,b;
t=tt=0;
scanf("%lf%lf",&l,&r);
for(i=0;i<n;i++)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
if(x1==x2)
{
if(x1<r&&x1>l)tt++;
continue;
}
k=(y2-y1)/(x2-x1);
b=y1-k*x1;
e[t].a=l*k+b;
e[t++].b=r*k+b;
}
sort(e,e+t,cmp1);
for(i=0;i<t;i++)
e[i].num=i+1;
sort(e,e+t,cmp2);
memset(c,0,sizeof(c));
for(i=0;i<t;i++)
{
add(e[i].num,1);
ans+=sum(e[i].num-1);
}
printf("%d\n",ans+tt*t);
}
return 0;
}

46
HDOJ/3466_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <map>
#define LL long long
using namespace std;
typedef struct
{
int p,q,v;
}item;
const int N = 510;
const int M = 5010;
int n,m;
item t[N];
int dp[M];
void zero_dp(int i)
{
for(int j = m; j >= t[i].p && j >= t[i].q; j--)
dp[j] = max(dp[j], dp[j-t[i].p]+t[i].v);
}
int cmp(const void *a, const void *b)
{
item *aa = (item *)a, *bb = (item *)b;
return (aa->q-aa->p) - (bb->q-bb->p);
}
int main()
{
int i,j,k;
while(~scanf("%d%d", &n, &m))
{
for(i = 0; i < n; i++)
{
scanf("%d%d%d", &t[i].p, &t[i].q, &t[i].v);
if(t[i].q > m) i--,n--;
}
qsort(t, n, sizeof(t[0]), cmp);
memset(dp, 0, sizeof(dp));
for(i = 0; i < n; i++)
zero_dp(i);
printf("%d\n", dp[m]);
}
return 0;
}

218
HDOJ/3467_autoAC.cpp Normal file
View File

@ -0,0 +1,218 @@
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
const int maxn = 10;
const double eps = 1e-8;
const double PI = acos(-1.0);
int dcmp(double x)
{ return (x > eps) - (x < -eps); }
struct Point
{
double x, y;
Point(double x=0, double y=0):x(x), y(y) {}
void read() { scanf("%lf%lf", &x, &y); }
};
typedef Point Vector;
Point operator + (const Vector& a, const Vector& b)
{ return Point(a.x+b.x, a.y+b.y); }
Point operator - (const Vector& a, const Vector& b)
{ return Point(a.x-b.x, a.y-b.y); }
Vector operator * (const Vector& a, double p)
{ return Point(a.x*p, a.y*p); }
Vector operator / (const Vector& a, double p)
{ return Point(a.x/p, a.y/p); }
bool operator == (const Point& a, const Point& b)
{ return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; }
double Dot(const Vector& a, const Vector& b)
{ return a.x*b.x + a.y*b.y; }
double Cross(const Vector& a, const Vector& b)
{ return a.x*b.y - a.y*b.x; }
double Length(const Vector& a)
{ return sqrt(Dot(a, a)); }
Vector unit(const Vector& a)
{ return a / Length(a); }
Vector Normal(const Vector& a)
{
double l = Length(a);
return Vector(-a.y/l, a.x/l);
}
double Angle(const Vector& a)
{ return atan2(a.y, a.x); }
Point Rotate(const Point& p, double angle, const Point& o = Point(0, 0))
{
Vector t = p - o;
t = Vector(t.x*cos(angle)-t.y*sin(angle), t.x*sin(angle)+t.y*cos(angle));
return t + o;
}
struct Region
{
double st, ed;
Region(double s=0, double e=0):st(s), ed(e) {}
};
struct Circle
{
Point c;
double r;
Circle() {}
Circle(Point c, double r):c(c), r(r) {}
void read() { c.read(); scanf("%lf", &r); }
double area() const { return PI * r * r; }
bool contain(const Circle& rhs) const
{ return dcmp(Length(c-rhs.c) + rhs.r - r) <= 0; }
bool contain(const Point& p) const
{ return dcmp(Length(c-p) - r) <= 0; }
bool intersect(const Circle& rhs) const
{ return dcmp(Length(c-rhs.c) - r - rhs.r) < 0; }
bool tangency(const Circle& rhs) const
{ return dcmp(Length(c-rhs.c) - r - rhs.r) == 0; }
Point get_point(double ang) const
{ return Point(c.x + r * cos(ang), c.y + r * sin(ang)); }
};
void IntersectionPoint(const Circle& c1, const Circle& c2, Point& p1, Point& p2)
{
double d = Length(c1.c - c2.c);
double l = (c1.r*c1.r + d*d - c2.r*c2.r) / (2 * d);
double h = sqrt(c1.r*c1.r - l*l);
Point mid = c1.c + unit(c2.c-c1.c) * l;
Vector t = Normal(c2.c - c1.c) * h;
p1 = mid + t;
p2 = mid - t;
}
double IntersectionArea(const Circle& c1, const Circle& c2)
{
double area = 0.0;
const Circle& M = c1.r > c2.r ? c1 : c2;
const Circle& N = c1.r > c2.r ? c2 : c1;
double d = Length(c1.c-c2.c);
if(d < M.r + N.r && d > M.r - N.r)
{
double Alpha = 2.0 * acos((M.r*M.r + d*d - N.r*N.r) / (2 * M.r * d));
double Beta = 2.0 * acos((N.r*N.r + d*d - M.r*M.r) / (2 * N.r * d));
area = ( M.r*M.r*(Alpha - sin(Alpha)) + N.r*N.r*(Beta - sin(Beta)) ) / 2.0;
}
else if(d <= M.r - N.r) area = N.area();
return area;
}
struct Region_vector
{
int n;
Region v[5];
void clear() { n = 0; }
void add(const Region& r) { v[n++] = r; }
} *last, *cur;
Circle cir[maxn];
bool del[maxn];
double r;
int n = 5;
bool IsOnlyOnePoint()
{
bool flag = false;
Point t;
for(int i = 0; i < n; ++i)
{
for(int j = i + 1; j < n; ++j)
{
if(cir[i].tangency(cir[j]))
{
t = (cir[i].c + cir[j].c) / 2;
flag = true;
break;
}
}
}
if(!flag) return false;
for(int i = 0; i < n; ++i)
if(!cir[i].contain(t)) return false;
printf("Only the point (%.2f, %.2f) is for victory.\n", t.x, t.y);
return true;
}
bool solve()
{
if(IsOnlyOnePoint()) return true;
memset(del, false, sizeof(del));
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
{
if(del[j] || i == j) continue;
if(cir[i].contain(cir[j]))
{
del[i] = true;
break;
}
}
double ans = 0.0;
for(int i = 0; i < n; ++i)
{
if(del[i]) continue;
last->clear();
Point p1, p2;
for(int j = 0; j < n; ++j)
{
if(del[j] || i == j) continue;
if(!cir[i].intersect(cir[j])) return false;
cur->clear();
IntersectionPoint(cir[i], cir[j], p1, p2);
double rs = Angle(p2 - cir[i].c);
double rt = Angle(p1 - cir[i].c);
if(dcmp(rs) < 0) rs += 2 * PI;
if(dcmp(rt) < 0) rt += 2 * PI;
if(last->n == 0)
{
if(dcmp(rt - rs) < 0)
{
cur->add(Region(rs, 2*PI));
cur->add(Region(0, rt));
}
else cur->add(Region(rs, rt));
}
else
{
for(int k = 0; k < last->n; ++k)
{
if(dcmp(rt - rs) < 0)
{
if(dcmp(last->v[k].st-rt) >= 0 && dcmp(last->v[k].ed-rs) <= 0) continue;
if(dcmp(last->v[k].st-rt) < 0) cur->add(Region(last->v[k].st, std::min(last->v[k].ed, rt)));
if(dcmp(last->v[k].ed-rs) > 0) cur->add(Region(std::max(last->v[k].st, rs), last->v[k].ed));
}
else
{
if(dcmp(rt-last->v[k].st <= 0 || dcmp(rs-last->v[k].ed) >= 0)) continue;
cur->add(Region(std::max(rs, last->v[k].st), std::min(rt, last->v[k].ed)));
}
}
}
std::swap(cur, last);
if(last->n == 0) break;
}
for(int j = 0; j < last->n; ++j)
{
p1 = cir[i].get_point(last->v[j].st);
p2 = cir[i].get_point(last->v[j].ed);
ans += Cross(p1, p2) / 2;
double ang = last->v[j].ed - last->v[j].st;
ans += cir[i].r * cir[i].r * (ang - sin(ang)) / 2;
}
}
if(dcmp(ans) == 0) return false;
printf("The total possible area is %.2f.\n", ans);
return true;
}
int main(void)
{
last = new Region_vector;
cur = new Region_vector;
while(scanf("%lf", &r) == 1)
{
Point t;
for(int i = 0; i < n; ++i)
{
t.read();
cir[i] = Circle(t, r);
}
if(!solve())
puts("Poor iSea, maybe 2012 is coming!");
}
return 0;
}

197
HDOJ/3468_autoAC.cpp Normal file
View File

@ -0,0 +1,197 @@
#include <cstring>
#include <cstdio>
#include <queue>
#include <cstdlib>
#include <cctype>
#define MAXN 105*105*2
#define MAXM 105*105*100
#define inf 0x3f3f3f3f
using namespace std;
struct node
{
int u,v,f;
};
node e[MAXM];
int first[MAXN],ne[MAXM];
int gap[MAXN],d[MAXN],curedge[MAXN],pre[MAXN];
int cc, n,m;
inline void add_edge(int u,int v,int f)
{
e[cc].u=u;
e[cc].v=v;
e[cc].f=f;
ne[cc]=first[u];
first[u]=cc;
cc++;
e[cc].u=v;
e[cc].v=u;
e[cc].f=0;
ne[cc]=first[v];
first[v]=cc;
cc++;
}
int ISAP(int s,int t,int n)
{
int cur_flow,flow_ans=0,u,tmp,neck,i,v;
memset(d,0,sizeof(d));
memset(gap,0,sizeof(gap));
memset(pre,-1,sizeof(pre));
for(i=0;i<=n;i++)
curedge[i]=first[i];
gap[0]=n+1;
u=s;
while(d[s]<=n)
{
if(u==t)
{
cur_flow=inf;
for(i=s;i!=t;i=e[curedge[i]].v)
{
if(cur_flow>e[curedge[i]].f)
{
neck=i;
cur_flow=e[curedge[i]].f;
}
}
for(i=s;i!=t;i=e[curedge[i]].v)
{
tmp=curedge[i];
e[tmp].f-=cur_flow;
e[tmp^1].f+=cur_flow;
}
flow_ans+=cur_flow;
u=neck;
}
for(i=curedge[u];i!=-1;i=ne[i])
{
v=e[i].v;
if(e[i].f&&d[u]==d[v]+1)
break;
}
if(i!=-1)
{
curedge[u]=i;
pre[v]=u;
u=v;
}
else
{
if(0==--gap[d[u]])
break;
curedge[u]=first[u];
for(tmp=n+5,i=first[u];i!=-1;i=ne[i])
if(e[i].f)
tmp=min(tmp,d[e[i].v]);
d[u]=tmp+1;
++gap[d[u]];
if(u!=s)
u=pre[u];
}
}
return flow_ans;
}
char map[105][105];
int rally[105*105];
int gold[105*105];
int pos[105*105];
int dist[55][105*105];
int vis[105][105];
int xx[4]={0,1,0,-1};
int yy[4]={1,0,-1,0};
int trans(char word)
{
if(word>='A'&&word<='Z')
return word-'A';
else
return word-'a'+26;
}
void bfs(int s)
{
queue<int> q;
memset(vis,0,sizeof(vis));
memset(dist[s],inf,sizeof(dist[s]));
int t=pos[s];
vis[t/m][t%m]=1;
q.push(t);
dist[s][t]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
int x=u/m;
int y=u%m;
int i;
for(i=0;i<4;i++)
{
int nx=x+xx[i];
int ny=y+yy[i];
if(nx<0||nx>=n||ny<0||ny>=m)
continue;
if(vis[nx][ny]||map[nx][ny]=='#')
continue;
vis[nx][ny]=1;
dist[s][nx*m+ny]=dist[s][x*m+y]+1;
q.push(nx*m+ny);
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(first,-1,sizeof(first));
memset(ne,-1,sizeof(ne));
cc=0;
memset(pos,0,sizeof(pos));
int i,j;
int cnt1=0;
int cnt2=0;
for(i=0;i<n;i++)
{
scanf("%s",map[i]);
for(j=0;j<m;j++)
{
if(isalpha(map[i][j]))
{
pos[trans(map[i][j])]=i*m+j;
cnt1++;
}
else if(map[i][j]=='*')
{
gold[cnt2]=i*m+j;
cnt2++;
}
}
}
for(i=0;i<cnt1;i++)
bfs(i);
int flag=1;
for(i=1;i<=cnt1-1;i++)
{
for(j=0;j<cnt2;j++)
{
if(dist[i][gold[j]]+dist[i-1][gold[j]]==dist[i-1][pos[i]])
{
add_edge(i,cnt1+j,1);
}
if(dist[i-1][pos[i]]==inf)
{
flag=0;
}
}
}
if(!flag)
{
printf("-1\n");
continue;
}
int s=0,t=cnt1+cnt2;
for(i=1;i<=cnt1-1;i++)
add_edge(s,i,1);
for(i=0;i<cnt2;i++)
add_edge(cnt1+i,t,1);
int res=ISAP(s,t,t);
printf("%d\n",res);
}
return 0;
}

19
HDOJ/3469_autoAC.cpp Normal file
View File

@ -0,0 +1,19 @@
#include<stdio.h>
#include<stdlib.h>
int main()
{
int t, n, re, i;
scanf("%d", &t);
for(i=1; i<=t; i++)
{
scanf("%d", &n);
if(n==1)
re=1;
else if(n==2)
re=2;
else
re=2*n-4;
printf("Case %d: %d\n", i, re);
}
return 0;
}

76
HDOJ/3470_autoAC.cpp Normal file
View File

@ -0,0 +1,76 @@
#include <stdio.h>
#include <cstring>
#include <algorithm>
#define maxn 1000010
using namespace std;
typedef long long LL;
int width,height,n;
int L,R,U,D,v;
bool diffx,diffy;
int idx[maxn],idy[maxn];
char ch[10];
void init(int id[],int n)
{
id[0]=id[n]=0;
for (int i=1;i<n;i++)
id[i]=1;
}
int fold(int id[], int &L, int &R, int v, bool &diff)
{
bool dir=false;
int ret;
v+=L;
ret=id[v];
if (abs(L-v)>abs(R-v)) diff=!diff,dir=true;
for (int x=v,y=v;x!=L&&y!=R;x--,y++)
{
if (!dir)
{
id[y]+=id[x];
id[x]=0;
}
else
{
id[x]+=id[y];
id[y]=0;
}
}
if (!dir) L=v;
else R=v;
return ret;
}
int main()
{
int test;
scanf("%d",&test);
for (int ii=1;ii<=test;ii++)
{
scanf("%d%d",&width,&height);
scanf("%d",&n);
diffx=diffy=0;
L=D=0;U=height;R=width;
init(idx,width);
init(idy,height);
LL ans=0;
for (int i=1;i<=n;i++)
{
scanf("%s%d",ch,&v);
if (ch[0]=='L'||ch[0]=='R')
{
if (diffx) v=R-L-v;
if (!diffx && ch[0]=='R') diffx=!diffx;
if (diffx && ch[0]=='L') diffx=!diffx;
ans+=(LL)height*fold(idx, L, R, v, diffx);
}
else
{
if (diffy) v=U-D-v;
if (!diffy && ch[0]=='U') diffy=!diffy;
if (diffy && ch[0]=='D') diffy=!diffy;
ans+=(LL)width*fold(idy, D, U, v, diffy);
}
}
printf("Case %d: %I64d\n",ii,ans);
}
return 0;
}

76
HDOJ/3471_autoAC.cpp Normal file
View File

@ -0,0 +1,76 @@
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <algorithm>
#include <vector>
#define MAX 1<<30
using namespace std;
const double eps=1e-8;
typedef long long ll;
int main()
{
typedef struct
{
double x;
double y;
double z;
} plus;
plus r[100],xa[100],p[100];
int t,i,j,k,ca=0;
double temp[6],pp[100];
plus o,v,m,f;
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf%lf",&o.x,&o.y,&o.z);
scanf("%lf%lf%lf",&v.x,&v.y,&v.z);
printf("Case %d: ",++ca);
for(i=0;i<8;i++)
scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].z);
m.x=p[4].x-p[0].x;
m.y=p[4].y-p[0].y;
m.z=p[4].z-p[0].z;
if(m.x*v.x+m.y*v.y+m.z*v.z<=eps)
{
printf("Intelligent Larrionda!!!\n");
continue;
}
temp[0] =( (p[0].x-o.x)*m.x+(p[0].y-o.y)*m.y+(p[0].z-o.z)*m.z )/(m.x*v.x+m.y*v.y+m.z*v.z);
temp[1] =( (p[4].x-o.x)*m.x+(p[4].y-o.y)*m.y+(p[4].z-o.z)*m.z )/(m.x*v.x+m.y*v.y+m.z*v.z);
if(temp[0]<=eps&&temp[1]<=eps)
{
printf("Intelligent Larrionda!!!\n");
continue;
}
f.x = o.x + temp[0]*v.x;
f.y = o.y + temp[0]*v.y;
f.z = o.z + temp[0]*v.z;
for(i=0;i<4;i++)
{
r[i].x = p[i].x - f.x;
r[i].y = p[i].y - f.y;
r[i].z = p[i].z - f.z;
}
for(i=0;i<3;i++)
{
xa[i].x= r[i+1].z*r[i].y-r[i+1].y*r[i].z;
xa[i].y= r[i+1].x*r[i].z-r[i+1].z*r[i].x;
xa[i].z= r[i+1].y*r[i].x-r[i+1].x*r[i].y;
}
xa[3].x=-( r[3].z*r[0].y-r[3].y*r[0].z);
xa[3].y=-( r[3].x*r[0].z-r[3].z*r[0].x);
xa[3].z=-( r[3].y*r[0].x-r[3].x*r[0].y);
for(i=0;i<3;i++)
{
if(xa[i].x*xa[i+1].x+xa[i].y*xa[i+1].y+xa[i].z*xa[i+1].z>0);
else
{printf("Intelligent Larrionda!!!\n");break;}
}
if(i==3)
printf("Stupid Larrionda!!!\n");
}
return 0;
}

189
HDOJ/3472_autoAC.cpp Normal file
View File

@ -0,0 +1,189 @@
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MAXN = 30;
const int MAXM = 10000;
const int INF = 0x3f3f3f3f;
struct Edge
{
int to,next,cap,flow;
}edge[MAXM];
int tol;
int head[MAXN];
int gap[MAXN], dep[MAXN], pre[MAXN], cur[MAXN];
void init()
{
tol = 0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w,int rw = 0)
{
edge[tol].to = v;
edge[tol].cap = w;
edge[tol].next = head[u];
edge[tol].flow = 0;
head[u] = tol++;
edge[tol].to = u;
edge[tol].cap = rw;
edge[tol].next = head[v];
edge[tol].flow = 0;
head[v] = tol++;
}
int sap(int start,int end,int N)
{
memset(gap,0,sizeof(gap));
memset(dep,0,sizeof(dep));
memcpy(cur,head,sizeof(head));
int u = start;
pre[u] = -1;
gap[0] = N;
int ans = 0;
while(dep[start] < N)
{
if(u == end)
{
int Min = INF;
for(int i = pre[u];i != -1;i = pre[edge[i^1].to])
if(Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
for(int i = pre[u]; i != -1;i = pre[edge[i^1].to])
{
edge[i].flow += Min;
edge[i^1].flow -= Min;
}
u = start;
ans += Min;
continue;
}
bool flag = false;
int v;
for(int i = cur[u]; i != -1;i = edge[i].next)
{
v = edge[i].to;
if(edge[i].cap - edge[i].flow && dep[v] + 1 == dep[u])
{
flag = true;
cur[u] = pre[v] = i;
break;
}
}
if(flag)
{
u = v;
continue;
}
int Min = N;
for(int i = head[u]; i != -1;i = edge[i].next)
if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
{
Min = dep[edge[i].to];
cur[u] = i;
}
gap[dep[u]] --;
if(!gap[dep[u]])return ans;
dep[u] = Min+1;
gap[dep[u]]++;
if(u != start) u = edge[pre[u]^1].to;
}
return ans;
}
int in[30],out[30];
int F[30];
int find(int x)
{
if(F[x] == -1)return x;
else return F[x] = find(F[x]);
}
void bing(int u,int v)
{
int t1 = find(u), t2 = find(v);
if(t1 != t2)F[t1] = t2;
}
char str[100];
int main()
{
int T,n;
scanf("%d",&T);
int iCase = 0;
while(T--)
{
iCase++;
scanf("%d",&n);
memset(F,-1,sizeof(F));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
init();
int k;
int s = -1;
while(n--)
{
scanf("%s%d",str,&k);
int len = strlen(str);
int u = str[0] - 'a';
int v = str[len-1] - 'a';
out[u]++;
in[v]++;
s = u;
if(k == 1)
addedge(u,v,1);
bing(u,v);
}
bool flag = true;
int cnt = 0;
int s1 = -1, s2 = -1;
for(int i = 0;i < 26;i++)
if(in[i] || out[i])
{
if(find(i) != find(s))
{
flag = false;
break;
}
if((in[i] + out[i])&1)
{
cnt++;
if(s1 == -1)s1 = i;
else s2 = i;
}
}
if(cnt != 0 && cnt != 2)flag = false;
if(!flag)
{
printf("Case %d: Poor boy!\n",iCase);
continue;
}
if(cnt == 2)
{
out[s1]++;
in[s2]++;
addedge(s1,s2,1);
}
for(int i = 0;i < 26;i++)
{
if(out[i] - in[i] > 0)
addedge(26,i,(out[i] - in[i])/2);
else if(in[i] - out[i] > 0)
addedge(i,27,(in[i] - out[i])/2);
}
sap(26,27,28);
for(int i = head[26];i != -1;i = edge[i].next)
if(edge[i].cap > 0 && edge[i].cap > edge[i].flow)
{
flag = false;
break;
}
if(flag)printf("Case %d: Well done!\n",iCase);
else printf("Case %d: Poor boy!\n",iCase);
}
return 0;
}

97
HDOJ/3473_autoAC.cpp Normal file
View File

@ -0,0 +1,97 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXK 20
#define MAXD 100010
int N, M, sa[MAXD], a[MAXD], rank[MAXK][MAXD], h[MAXK][MAXD];
long long int A[MAXK][MAXD], ans;
int cmp(const void *_p, const void *_q)
{
int *p = (int *)_p, *q = (int *)_q;
if(a[*p] == a[*q])
return *p - *q;
return a[*p] - a[*q];
}
void init()
{
int i, j, k;
scanf("%d", &N);
for(i = 1; i <= N; i ++)
{
scanf("%d", &a[i]);
sa[i] = i;
}
}
void build(int lx, int rx, int d)
{
if(lx == rx)
{
A[d][lx] = a[sa[rank[d][lx]]];
return ;
}
int i, j, k, p = 0, mid = (lx + rx) / 2;
for(i = lx; i <= rx; i ++)
{
if(rank[d][i] <= mid)
rank[d + 1][lx + p ++] = rank[d][i];
else
rank[d + 1][mid + i - lx + 1 - p] = rank[d][i];
h[d][i] = p;
A[d][i] = a[sa[rank[d][i]]] + (i == lx ? 0 : A[d][i - 1]);
}
build(lx, mid, d + 1);
build(mid + 1, rx, d + 1);
}
int search(int lx, int rx, int x, int y, int k, int d)
{
if(lx == rx)
return sa[rank[d][lx]];
int j, n, m, mid = (lx + rx) / 2, tx, ty;
n = h[d][y], m = x == lx ? 0 : h[d][x - 1];
if(n - m >= k)
{
j = search(lx, mid, lx + m, lx + n - 1, k, d + 1);
tx = mid + 1 + x - lx - m, ty = mid + 1 + y - lx - n;
if(tx <= ty)
ans += A[d + 1][ty] - (tx == mid + 1 ? 0 : A[d + 1][tx - 1]) - (long long int)(ty - tx + 1) * a[j];
}
else
{
j = search(mid + 1, rx, mid + 1 + x - lx - m, mid + 1 + y - lx - n, k - n + m, d + 1);
tx = lx + m, ty = lx + n - 1;
if(tx <= ty)
ans += (long long int)(ty - tx + 1) * a[j] - A[d + 1][ty] + (tx == lx ? 0 : A[d + 1][tx - 1]);
}
return j;
}
void solve()
{
int i, j, k, x, y;
qsort(sa + 1, N, sizeof(sa[0]), cmp);
for(i = 1; i <= N; i ++)
rank[0][sa[i]] = i;
build(1, N, 0);
scanf("%d", &M);
for(i = 0; i < M; i ++)
{
scanf("%d%d", &x, &y);
++ x, ++ y;
k = (y - x) / 2 + 1;
ans = 0;
search(1, N, x, y, k, 0);
printf("%I64d\n", ans);
}
}
int main()
{
int t, tt;
scanf("%d", &t);
for(tt = 0; tt < t; tt ++)
{
init();
printf("Case #%d:\n", tt + 1);
solve();
printf("\n");
}
return 0;
}

134
HDOJ/3475_autoAC.cpp Normal file
View File

@ -0,0 +1,134 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAXN 110
#define MAXM 15
#define MAXD 10010
#define HASH 10007
#define ST 1030
#define INF 0x3f3f3f3f
int N, M, D, d[MAXN][MAXM][8], g[MAXN][MAXM], first[ST], e, next[ST], v[ST];
char b[MAXN];
struct HashMap
{
int head[HASH], size, next[MAXD], st[MAXD], ans[MAXD];
void init()
{
memset(head, -1, sizeof(head)), size = 0;
}
void push(int _st, int _ans)
{
int i, h = _st % HASH;
for(i = head[h]; i != -1; i = next[i])
if(st[i] == _st) break;
if(i == -1)
{
if(_st == -1) for(;;);
st[size] = _st, ans[size] = _ans;
next[size] = head[h], head[h] = size ++;
}
else ans[i] = std::min(ans[i], _ans);
}
}hm[2];
void add(int x, int y)
{
v[e] = y;
next[e] = first[x], first[x] = e ++;
}
void prepare(int i)
{
int j, k, st;
memset(first, -1, sizeof(first)), e = 0;
for(j = 0; j <= D; j ++)
{
st = 0;
for(k = 0; k < M; k ++)
if(j & 1 << k)
{
if(d[i][k][5]) st ^= 1 << k - 1;
if(d[i][k][4]) st ^= 1 << k;
if(d[i][k][3]) st ^= 1 << k + 1;
}
add(st, j);
}
}
void init()
{
int i, j, k;
scanf("%d%d", &N, &M);
memset(d, 0, sizeof(d));
memset(g, 0, sizeof(g));
gets(b);
for(i = 0; i < N; i ++)
{
gets(b);
for(j = 0; j < M; j ++)
{
g[i][j] = b[2 * j] == 'o';
if(j < M - 1 && b[2 * j + 1] == '-') d[i][j][2] = d[i][j + 1][6] = 1;
}
if(i < N - 1)
{
gets(b);
for(j = 0; j < M; j ++)
{
if(b[2 * j] == '|') d[i][j][0] = d[i + 1][j][4] = 1;
if(j < M - 1)
{
if(b[2 * j + 1] == '\\' || b[2 * j + 1] == 'X') d[i][j][1] = d[i + 1][j + 1][5] = 1;
if(b[2 * j + 1] == '/' || b[2 * j + 1] == 'X') d[i][j + 1][7] = d[i + 1][j][3] = 1;
}
}
}
}
D = (1 << M) - 1;
}
void solve()
{
int i, j, k, t, cur = 0, st, tc, td, nl, ust, cst, cnt, ans = INF;
hm[0].init();
for(i = st = 0; i < M; i ++) if(g[0][i]) st |= 1 << i;
hm[0].push(st, 0);
for(i = 0; i < N; i ++)
{
hm[cur ^ 1].init();
prepare(i);
for(j = nl = 0; j < M; j ++) if(g[i + 1][j]) nl |= 1 << j;
for(j = 0; j < hm[cur].size; j ++)
{
ust = hm[cur].st[j] >> M, cst = hm[cur].st[j] & D, td = nl;
for(k = first[ust]; k != -1; k = next[k])
{
st = v[k], cnt = 0;
tc = cst ^ st, td = nl;
for(t = 0; t < M; t ++)
if(st & 1 << t)
{
++ cnt;
if(d[i][t][0]) td ^= 1 << t;
if(d[i][t][1]) td ^= 1 << t + 1;
if(d[i][t][2]) tc ^= 1 << t + 1;
if(d[i][t][6]) tc ^= 1 << t - 1;
if(d[i][t][7]) td ^= 1 << t - 1;
}
hm[cur ^ 1].push(tc << M | td, hm[cur].ans[j] + cnt);
}
}
cur ^= 1;
}
for(i = 0; i < hm[cur].size; i ++)
if((hm[cur].st[i] >> M) == 0) ans = std::min(ans, hm[cur].ans[i]);
printf("%d\n", ans == INF ? -1 : ans);
}
int main()
{
int t, tt;
scanf("%d", &t);
for(tt = 1; tt <= t; tt ++)
{
init();
printf("Case %d: ", tt);
solve();
}
return 0;
}

5
HDOJ/3476_autoAC.cpp Normal file
View File

@ -0,0 +1,5 @@
#include<cstdio>
int main(){
printf("13579680\n");
return 0;
}

80
HDOJ/3478_autoAC.cpp Normal file
View File

@ -0,0 +1,80 @@
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
struct Node
{
int v;
int next;
}Edge[500010*2];
int pre[100010];
void add(int u,int v,int index)
{
Edge[index].v=v;
Edge[index].next=pre[u];
pre[u]=index;
}
int flag[100010];
int c[100010];
int bfs(int v)
{
queue <int> q;
flag[v]=1;
q.push(v);
while(!q.empty())
{
int b=q.front();
q.pop();
for(int i=pre[b];i!=-1;i=Edge[i].next)
{
int e=Edge[i].v;
if(flag[e]==flag[b])
return 0;
if(!flag[e])
{
flag[e]=flag[b]==1?2:1;
q.push(e);
}
}
}
return 1;
}
int main()
{
int n,m,q,cnt=1;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&q);
int a,b,index=1;
memset(flag,0,sizeof(flag));
memset(pre,-1,sizeof(pre));
memset(c,0,sizeof(c));
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
add(a,b,index++);
add(b,a,index++);
c[a]++,c[b]++;
}
int state=0;
for(int i=1;i<=n;i++)
{
if(!flag[i]&&c[i])
{
if(!bfs(i))
{
state=1;
break;
}
}
}
printf("Case %d: ",cnt++);
if(state)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

67
HDOJ/3480_autoAC.cpp Normal file
View File

@ -0,0 +1,67 @@
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;
#define maxn 10005
int save[maxn],q[maxn],sum[maxn],dp[5005][maxn],ans;
int n,m,h,t;
int M(int k){return k*k;}
void solve()
{
for(int i=1;i<=n;i++)
{
dp[1][i]=M(save[i]-save[1]);
}
for(int i=2;i<=m;i++)
{
h=t=1;
q[1]=0;
q[++t]=i-1;
for(int j=i;j<=n;j++)
{
while(h<t)
{
int p1 = q[h], p2 = q[h+1];
int x1 = save[p1+1],x2=save[p2+1];
int y1 = dp[i-1][p1]+x1*x1 , y2=dp[i-1][p2]+x2*x2;
if(y2-y1<2*save[j]*(x2 - x1))
h++;
else
break;
}
int k = q[h];
dp[i][j] = dp[i - 1][k]+(save[j]-save[k+1]) * (save[j]-save[k+1]);
while(h< t && j != n)
{
int p1 = q[t-1], p2 = q[t], p3 = j;
int x1 = save[p1+1] , x2=save[p2+1] , x3=save[p3+1];
int y1 = dp[i-1][p1]+x1*x1, y2=dp[i-1][p2] + x2 * x2, y3 = dp[i - 1][p3] + x3 * x3;
if((y3 - y2) * (x2 - x1) <= (y2 - y1) * (x3 - x2))
t--;
else
break;
}
q[++t] = j;
}
}
ans=dp[m][n];
}
int main()
{
int cas;
scanf("%d",&cas);
for(int k=1;k<=cas;k++)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&save[i]);
}
sort(save+1,save+1+n);
solve();
printf("Case %d: %d\n",k, ans);
}
return 0;
}

39
HDOJ/3481_autoAC.cpp Normal file
View File

@ -0,0 +1,39 @@
#include<cstdio>
#include<algorithm>
const int MOD = 987654321;
long long dp[110][10];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m),n>0)
{
if(m==1){puts("0");continue;}
if(m==2){printf("%d\n",n==1?2:0);continue;}
memset(dp,0,sizeof(dp));
dp[1][1]=m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<m;j++)
{
dp[i][j]%=MOD;
dp[i+1][j+1]+=dp[i][j]*(m-j);
for(int k=2;k<=j;k++)
dp[i+1][k]+=dp[i][j];
if(j>1||i==1)
{
for(int k=1;1+k<=m-1;k++)
{
dp[i+k][1]+=dp[i][j];
}
}
}
}
long long ans=0;
for(int j=1;j<m;j++)
{
ans+=dp[n][j];
}
printf("%I64d\n",ans%MOD);
}
return 0;
}

40
HDOJ/3482_autoAC.cpp Normal file
View File

@ -0,0 +1,40 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 1000009
#define ll long long
using namespace std;
const ll mod=987654321;
ll f[maxn];
int main()
{
ll n,m;
f[1]=1;
for(ll i=2;i<maxn-1;i++)
{
f[i]=f[i-1]*i;
if(f[i]>mod)f[i]%=mod;
}
while(scanf("%lld%lld",&n,&m)&&(n+m)>0)
{
ll ans=1;
if(n<m||m==1||m==2)
{
while(n>0)
{
if(n&1)ans*=m;
if(ans>mod)ans%=mod;
n>>=1;
m*=m;
if(m>mod)m%=mod;
}
}
else
{
ans=f[m]+m;
if(ans>mod)ans%=mod;
}
printf("%lld\n",ans);
}
return 0;
}

57
HDOJ/3483_autoAC.cpp Normal file
View File

@ -0,0 +1,57 @@
#include <iostream>
#include <cstring>
using namespace std;
#define M 55
#define LL long long
#define FF(i, n) for(int i = 0; i < n; i++)
int ans[M], mod, C[M][M];
int ret[M][M], init[M][M];
void ini(int n, int x)
{
memset(init, 0, sizeof(init));
FF(i, n) FF(j, i+1)
init[i][j] = (LL)x*C[i][j] % mod;
FF(i, n) ans[i] = x;
ans[n] = 0;
init[n][n-1] = init[n][n] = 1;
}
void matmul(int a[][M], int b[][M], int n)
{
int tp[M][M] = {0};
FF(i, n) FF(k, n) if(a[i][k]) FF(j, n) if(b[k][j])
tp[i][j] = (tp[i][j] + (LL)a[i][k]*b[k][j]) % mod;
FF(i, n) FF(j, n) a[i][j] = tp[i][j];
}
void matmul(int a[], int b[][M], int n)
{
int tp[M] = {0};
FF(j, n) if(a[j]) FF(i, n) if(b[i][j])
tp[i] = (tp[i] + (LL)a[j]*b[i][j]) % mod;
FF(i, n) a[i] = tp[i];
}
void qmod(int n, int b)
{
FF(i, n) FF(j, n) ret[i][j] = (i==j);
for( ; b; b >>= 1)
{
if (b & 1) matmul(ret, init, n);
matmul(init, init, n);
}
}
int main()
{
int i, j, n, x;
while (cin >> n >> x >> mod, n >= 1)
{
for(i = 0; i <= x; i++)
C[i][0] = C[i][i] = 1;
for(i = 2; i <= x; i++)
for(j = 1; j < i; j++)
C[i][j] = ((LL)C[i-1][j-1]+C[i-1][j]) % mod;
ini(x+1, x);
qmod(x+2, n);
matmul(ans, ret, x+2);
cout << ans[x+1] << endl;
}
return 0;
}

88
HDOJ/3484_autoAC.cpp Normal file
View File

@ -0,0 +1,88 @@
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int ma[105][105],tar[105][105],tmp[105][105];
int n,m;
void init()
{
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
scanf("%d",&ma[i][j]);
}
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
scanf("%d",&tar[i][j]);
}
}
void cpy(int c)
{
for (int i=1;i<=m;i++)
{
for (int j=1;j<=n;j++)
{
tmp[j][i]=ma[j][i];
}
}
}
void change_col(int a,int b)
{
for (int i=1;i<=n;i++)
{
int temp=tmp[i][a];
tmp[i][a]=tmp[i][b];
tmp[i][b]=temp;
}
}
void change_row(int c)
{
for (int i=1;i<=m;i++)
tmp[c][i]^=1;
}
bool ok(int a,int b)
{
for (int i=1;i<=n;i++)
{
if (tar[i][a]!=tmp[i][b])
return false;
}
return true;
}
int main()
{
while (scanf("%d%d",&n,&m))
{
if (n==-1) break;
init();
bool ans=0;
for (int i=1;i<=m;i++)
{
cpy(i);
change_col(1,i);
for (int j=1;j<=n;j++)
{
if (tmp[j][1]!=tar[j][1])
change_row(j);
}
for (int j=2;j<=m;j++)
{
for (int k=j;k<=m;k++)
{
ans=ok(j,k);
if (ans)
{
change_col(j,k);
break;
}
}
}
if (ans) break;
}
if (ans) puts("Yes");
else
puts("No");
}
return 0;
}

24
HDOJ/3485_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include<iostream>
using namespace std;
int f[10005];
int n;
int deal(int n)
{
if(f[n]!=0)
return f[n];
f[n-1]=deal(n-1)%9997;
f[n-2]=deal(n-2)%9997;
f[n-4]=deal(n-4)%9997;
return ((f[n-1]+f[n-2]+f[n-4])%9997);
}
int main()
{
while(scanf("%d",&n)!=EOF&&n!=-1)
{
f[0]=1;
f[1]=2;
f[2]=4;
f[3]=7;
printf("%d\n",deal(n));
}
}

59
HDOJ/3486_autoAC.cpp Normal file
View File

@ -0,0 +1,59 @@
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,k,l,r,m,tn,temp,t,M,ss;
int f[200001][18];
int lg[200001];
void pre_rmq()
{
for(int j=1;(1<<j)<=n;j++)
{
for(int i=1;i+(1<<j)-1<=n;i++)
f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
}
}
int find(int x,int y)
{
t=lg[y-x+1];
return max(f[x][t],f[y-(1<<t)+1][t]);
}
bool check(int k)
{
tn=n/k*k;
temp=0;
for(int i=1;i<=tn;i=i+tn/k)
temp+=find(i,i+tn/k-1);
if(temp>=M+1) return 1;
return 0;
}
int main()
{
for(int i=2;i<=200000;i++)
lg[i]=lg[i>>1]+1;
while(scanf("%d%d",&n,&M)!=EOF)
{
ss=0;
if(n==-1&&M==-1) break;
for(int i=1;i<=n;i++)
{
scanf("%d",&f[i][0]);
ss+=f[i][0];
}
if(ss<=M)
{
printf("-1\n");
continue;
}
pre_rmq();
l=1;r=n;
while(l<=r)
{
m=(l+r)>>1;
if(check(m)) r=m-1;
else l=m+1;
}
printf("%d\n",l);
}
}

174
HDOJ/3487_autoAC.cpp Normal file
View File

@ -0,0 +1,174 @@
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<algorithm>
#define N 300015
#define inf 1<<29
#define MOD 100000007
#define LL long long
#define Key_value ch[ch[root][1]][0]
#define _match(a,b) ((a)==(b))
using namespace std;
int n,q;
int size[N],pre[N],key[N],num[N],rev[N];
int ch[N][2],tot,root,node[N];
void NewNode(int &r,int k,int father){
r=++tot;
ch[r][0]=ch[r][1]=0;
pre[r]=father;
rev[r]=0;
key[r]=k;
}
void Push_Up(int r){
size[r]=size[ch[r][0]]+size[ch[r][1]]+1;
}
void Push_Down(int r){
if(rev[r]){
swap(ch[r][0],ch[r][1]);
rev[ch[r][0]]^=1;
rev[ch[r][1]]^=1;
rev[r]=0;
}
}
void Bulid(int &r,int L,int R,int father){
if(L>R)
return ;
int mid=(L+R)/2;
NewNode(r,mid,father);
Bulid(ch[r][0],L,mid-1,r);
Bulid(ch[r][1],mid+1,R,r);
Push_Up(r);
}
void Init(){
tot=root=0;
ch[root][0]=ch[root][1]=pre[root]=rev[root]=size[root]=0;
NewNode(root,-1,0);
NewNode(ch[root][1],-1,root);
size[root]=2;
Bulid(Key_value,1,n,ch[root][1]);
Push_Up(ch[root][1]);
Push_Up(root);
}
void Rotate(int x,int kind){
int y=pre[x];
Push_Down(y);
Push_Down(x);
ch[y][!kind]=ch[x][kind];
pre[ch[x][kind]]=y;
if(pre[y])
ch[pre[y]][ch[pre[y]][1]==y]=x;
pre[x]=pre[y];
ch[x][kind]=y;
pre[y]=x;
Push_Up(y);
}
void Splay(int r,int goal){
Push_Down(r);
while(pre[r]!=goal){
if(pre[pre[r]]==goal)
Rotate(r,ch[pre[r]][0]==r);
else{
int y=pre[r];
int kind=(ch[pre[y]][0]==y);
if(ch[y][kind]==r){
Rotate(r,!kind);
Rotate(r,kind);
}
else{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
Push_Up(r);
if(goal==0) root=r;
}
int Get_Kth(int r,int k){
Push_Down(r);
int t=size[ch[r][0]];
if(t==k-1)
return r;
if(t>=k)
return Get_Kth(ch[r][0],k);
else
return Get_Kth(ch[r][1],k-t-1);
}
int Get_Min(int r){
Push_Down(r);
while(ch[r][0]){
r=ch[r][0];
Push_Down(r);
}
return r;
}
int Get_Max(int r){
Push_Down(r);
while(ch[r][1]){
r=ch[r][1];
Push_Down(r);
}
return r;
}
void Reversal(int a,int b){
int x=Get_Kth(root,a);
int y=Get_Kth(root,b+2);
Splay(x,0);
Splay(y,root);
rev[Key_value]^=1;
}
void Cut(int a,int b,int c){
int x=Get_Kth(root,a);
int y=Get_Kth(root,b+2);
Splay(x,0);
Splay(y,root);
int tmp=Key_value;
Key_value=0;
Push_Up(ch[root][1]);
Push_Up(root);
int z=Get_Kth(root,c+1);
Splay(z,0);
int m=Get_Min(ch[root][1]);
Splay(m,root);
Key_value=tmp;
pre[Key_value]=ch[root][1];
Push_Up(ch[root][1]);
Push_Up(root);
}
int cnt;
void InOrder(int r){
if(r==0)
return;
Push_Down(r);
InOrder(ch[r][0]);
if(cnt>=1&&cnt<=n){
if(cnt>1) printf(" ");
printf("%d",key[r]);
}
cnt++;
InOrder(ch[r][1]);
}
int main(){
while(scanf("%d%d",&n,&q)!=EOF){
if(n==-1&&q==-1)
break;
Init();
while(q--){
char str[10];
int a,b,c;
scanf("%s",str);
if(str[0]=='C'){
scanf("%d%d%d",&a,&b,&c);
Cut(a,b,c);
}
else{
scanf("%d%d",&a,&b);
Reversal(a,b);
}
}
cnt=0;
InOrder(root);
printf("\n");
}
return 0;
}

75
HDOJ/3488_autoAC.cpp Normal file
View File

@ -0,0 +1,75 @@
#include<stdio.h>
#include<string.h>
#define inf 1000000
#define N 205
int n,y[N],g[N][N],lx[N],ly[N],slack[N];
char vx[N],vy[N];
int search(int u)
{
vx[u]=1;
for(int v=1;v<=n;v++)
if(lx[u]+ly[v]==g[u][v])
{
if(vy[v]==1) continue;
vy[v]=1;
if(y[v]==-1||search(y[v]))
{
y[v]=u;
return 1;
}
}
else if(lx[u]+ly[v]-g[u][v]<slack[v])
slack[v]=lx[u]+ly[v]-g[u][v];
return 0;
}
int KM()
{
int i,j,min;
memset(y,-1,sizeof(y));
memset(ly,0,sizeof(ly));
memset(lx,0,sizeof(lx));
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
slack[j]=inf;
while(1)
{
memset(vx,0,sizeof(vx));
memset(vy,0,sizeof(vy));
if(search(i)) break;
for(min=inf,j=1;j<=n;j++)
if(!vy[j]&&slack[j]<min)
min=slack[j];
for(j=1;j<=n;j++)
if(vx[j]) lx[j]-=min;
for(j=1;j<=n;j++)
if(vy[j]) ly[j]+=min;
else slack[j]-=min;
}
}
int ans=0;
for(i=1;i<=n;i++)
ans+=g[y[i]][i];
return ans;
}
int main()
{
int t,m,i,j,u,v,w;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
g[i][j]=-inf;
for(i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
if(g[u][v]<-w)
g[u][v]=-w;
}
int ans=-1*KM();
printf("%d\n",ans);
}
return 0;
}

38
HDOJ/3489_autoAC.cpp Normal file
View File

@ -0,0 +1,38 @@
# include <iostream>
# include <cstdio>
# include <cstring>
using namespace std;
int p[1002][26],pre[1002],n,m;
char a[10002],b[1002];
int f[10002][1002];
void relax(int &i,const int&j){if(i>j) i=j;}
int main()
{
int Case; scanf("%d",&Case);
while(Case--){
scanf("%s",a+1); scanf("%s",b+1);
n = strlen(a+1); m = strlen(b+1);
for(int i=1; i<=n; i++)a[i]-='a',b[i]-='a';
int k = 0; pre[1] = 0;
for(int i=2; i<=m; i++) {
while(k && b[k+1]!=b[i]) k = pre[k];
if(b[k+1] == b[i]) k++; pre[i]=k;
}
memset(p,0,sizeof(p));
for(int i=1; i<=m; i++) p[i-1][b[i]]=i;
for(int i=1; i<=m; i++)
for(int j=0; j<26; j++)
if(!p[i][j]) p[i][j] = p[pre[i]][j];
for(int j=0; j<26; j++) p[m][j] = m;
memset(f,0x7f,sizeof(f)); f[0][0]=0;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
if( f[i][j]<0x7f7f7f7f )
relax(f[i+1][j], f[i][j]+1),
relax(f[i+1][p[j][a[i+1]]], f[i][j]);
int ans = n+1;
for(int i=0; i<m; i++) relax(ans, f[n][i]);
printf("%d\n",ans);
}
return 0;
}

46
HDOJ/3490_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
map<string, pair<int, int> > ms;
map<int, int> s1, s2;
int n;
inline int calc(map<int, int>& s) {
map<int, int>::iterator ib = s.begin(), ie = s.end();
return (--ie)->first - ib->first;
}
inline void erase(map<int, int> &s, int x) {
if (--s[x] == 0)
s.erase(x);
}
int main() {
int test;
scanf("%d", &test);
for (int cas = 1; cas <= test; ++cas) {
ms.clear(); s1.clear(); s2.clear();
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
int opt; char buf[100];
scanf("%d%s", &opt, buf);
if (opt == 1) {
int x, y;
scanf("%d%d", &x, &y);
ms[buf] = pair<int, int>(x, y);
s1[x - y]++; s2[x + y]++;
} else {
map<string, pair<int, int> >::iterator it = ms.find(buf);
erase(s1, it->second.first - it->second.second);
erase(s2, it->second.first + it->second.second);
ms.erase(it);
}
if (ms.size() == 0)
puts("-1");
else
printf("%d\n", max(calc(s1), calc(s2)));
}
putchar('\n');
}
return 0;
}

128
HDOJ/3491_autoAC.cpp Normal file
View File

@ -0,0 +1,128 @@
#include<iostream>
#include<memory.h>
#include<string>
#include<cstdio>
#include<algorithm>
#include<math.h>
#include<stack>
#include<queue>
using namespace std;
const int MAX=205;
const int inf=999999;
struct node
{
int v,c,next;
}g[MAX*MAX];
int adj[MAX],dis[MAX],cur[MAX],num[MAX],pre[MAX];
int n,m,e,s,t,vn,S,H;
void add(int u,int v,int c)
{
g[e].v=v; g[e].c=c; g[e].next=adj[u]; adj[u]=e++;
g[e].v=u; g[e].c=0; g[e].next=adj[v]; adj[v]=e++;
}
int sap()
{
int i,u,v,flag,aug=inf+1,flow=0;
for(i=0;i<=vn;i++)
{
cur[i]=adj[i];
num[i]=dis[i]=0;
}
num[0]=vn;
pre[s]=u=s;
while(dis[s]<vn)
{
flag=0;
for(i=adj[u];i!=-1;i=g[i].next)
{
v=g[i].v;
if(g[i].c&&dis[u]==dis[v]+1)
{
flag=1;
aug=min(aug,g[i].c);
pre[v]=u;
cur[u]=i;
u=v;
if(u==t)
{
flow+=aug;
while(u!=s)
{
u=pre[u];
g[cur[u]].c-=aug;
g[cur[u]^1].c+=aug;
}
aug=inf+1;
}
break;
}
}
if(flag)
continue;
if(--num[dis[u]]==0)
break;
for(dis[u]=inf,i=adj[u];i!=-1;i=g[i].next)
{
v=g[i].v;
if(g[i].c&&dis[v]<dis[u])
{
dis[u]=dis[v];
cur[u]=i;
}
}
dis[u]++;
num[dis[u]]++;
u=pre[u];
}
return flow;
}
int main()
{
int i,j,k,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&n,&m,&S,&H);
e=0;
vn=2*n+2;
s=S+n;
t=H;
memset(adj,-1,sizeof(adj));
add(s,S,inf);
add(H+n,T,inf);
for(i=1;i<=n;i++)
{
scanf("%d",&k);
if(i!=S&&i!=H)
{
add(i,i+n,k);
}
else
{
add(i,i+n,inf);
}
}
while(m--)
{
scanf("%d%d",&i,&j);
if(i!=s&&j!=t)
{
add(i+n,j,inf);
add(j+n,i,inf);
}
else
{
if(i==s)
{
add(s,j,inf);
}
else
{
add(i+n,t,inf);
}
}
}
printf("%d\n",sap());
}
return 0;
}

66
HDOJ/3492_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include<stdlib.h>
#include <math.h>
#include<stdio.h>
#include <algorithm>
using namespace std;
#define maxN 300
const double eps = 1e-8;
typedef struct
{
double x,y;
}point;
point l[maxN*2];
double direction(point p1,point p2, point p3)
{
return (p3.x-p1.x)*(p2.y-p1.y)-(p2.x-p1.x)*(p3.y-p1.y);
}
bool online(point p1,point p2,point p3)
{
return (min(p1.x,p2.x)<=p3.x&&p3.x<=max(p1.x,p2.x))&&(min(p1.y,p2.y)<=p3.y&&p3.y<=max(p1.y,p2.y));
}
bool insect(point p1,point p2,point p3,point p4)
{
double d3=direction(p1,p2,p3);
double d4=direction(p1,p2,p4);
if(d3*d4<0)
return true;
else if(d3==0) return true;
else if(d4==0) return true;
return false ;
}
int main()
{
int cnt = 0;
int N,n,i,j,k;
bool flag;
scanf("%d",&N);
while(N--)
{
cnt ++;
scanf("%d",&n);
for(i=0;i<2*n-1;i+=2)
{
scanf("%lf%lf%lf%lf",&l[i].x,&l[i].y,&l[i+1].x,&l[i+1].y);
}
bool issame = true;
flag = false;
for(i=0;i< 2*n -1 && flag==false;i++)
for(j=i+1;j < 2*n && flag==false;j++)
{
if (fabs(l[i].x-l[j].x) <= eps && fabs(l[i].y-l[j].y) <= eps)
continue;
issame = false;
flag=true;
for(k=0;k<2*n-1;k+=2)
{
if(insect(l[i],l[j],l[k],l[k+1]) == false)
{flag=false;
break;}
}
}
if(flag==true || issame)
{printf("Yes\n");}
else printf("No\n");
}
return 0;
}

56
HDOJ/3493_autoAC.cpp Normal file
View File

@ -0,0 +1,56 @@
#include<iostream>
#include<string.h>
using namespace std;
const int mod=9997;
int f[3][3],map[3][3];
int t[3][3]={{5,-7,4},{1,0,0},{0,1,0}};
int g[4][1]={1,2,6,19},n;
void multy_2(int v[][3],int u[][3])
{
int c[3][3];
memset(c,0,sizeof(c));
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
for(int k=0;k<3;k++)
{
c[i][j]=(c[i][j]+(v[i][k]*u[k][j]+mod)%mod+mod)%mod;
}
}
}
memcpy(v,c,sizeof(c));
}
void multy_1(int k)
{
for(;k>0;k=k>>1)
{
if(k & 1) multy_2(map,f);
multy_2(f,f);
}
}
void run()
{
multy_1(n-4);
cout<<(g[3][0]*map[0][0]%mod+g[2][0]*map[0][1]%mod+g[1][0]*map[0][2]%mod)%mod<<endl;
}
int main()
{
while(scanf("%d",&n) && n!=0)
{
if(n<=4)
{
cout<<g[n-1][0]<<endl;
continue;
}
memcpy(f,t,sizeof(t));
memset(g,0,sizeof(g));
memset(map,0,sizeof(map));map[0][0]=map[1][1]=map[2][2]=1;
g[0][0]=1;
g[1][0]=2;
g[2][0]=6;
g[3][0]=19;
run();
}
return 0;
}

43
HDOJ/3496_autoAC.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int MAX = 999999;
int val[111],time[111],dp[111][1111];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m,l;
scanf("%d%d%d",&n,&m,&l);
int i,j,k;
for(i = 0; i<n; i++)
scanf("%d%d",&time[i],&val[i]);
for(i = 0; i<=m; i++)
{
for(j = 0; j<=l; j++)
{
if(i==0)
dp[i][j] = 0;
else
dp[i][j] = -MAX;
}
}
for(i = 0; i<n; i++)
{
for(j = m; j>=1; j--)
{
for(k = l; k>=time[i]; k--)
{
dp[j][k] = max(dp[j][k],dp[j-1][k-time[i]]+val[i]);
}
}
}
if(dp[m][l]<0)
dp[m][l] = 0;
printf("%d\n",dp[m][l]);
}
return 0;
}

14
HDOJ/3497_autoAC.cpp Normal file
View File

@ -0,0 +1,14 @@
#include <iostream>
#include <cstdio>
#include <cmath>
#define IP 3.1415926
using namespace std;
int main()
{
double r, h;
while( scanf("%lf %lf", &h, &r) != EOF)
{
double ans = asin(0.01 * r / (4 * h));
printf("%0.2lf\n", 180 * ans / IP);
}
}

157
HDOJ/3498_autoAC.cpp Normal file
View File

@ -0,0 +1,157 @@
#include<cstdio>
#include<vector>
#include<cmath>
#include<map>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll maxn = 105;
int T, n, m, x, y, t, tot, mp[maxn][maxn];
inline void read(int &ret)
{
char c;
do {
c = getchar();
} while (c < '0' || c > '9');
ret = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
ret = ret * 10 + (c - '0');
}
struct DLX
{
#define maxn 500005
#define F(i,A,s) for (int i=A[s];i!=s;i=A[i])
int L[maxn], R[maxn], U[maxn], D[maxn];
int row[maxn], col[maxn], ans[maxn], cnt[maxn];
int n, m, num, sz;
void add(int now, int l, int r, int u, int d, int x, int y)
{
L[now] = l; R[now] = r; U[now] = u;
D[now] = d; row[now] = x; col[now] = y;
}
void reset(int n, int m)
{
num = 0x7FFFFFFF;
this->n = n; this->m = m;
for (int i = 0; i <= m; i++)
{
add(i, i - 1, i + 1, i, i, 0, i);
cnt[i] = 0;
}
L[0] = m; R[m] = 0; sz = m + 1;
}
void insert(int x, int y)
{
int ft = sz - 1;
if (row[ft] != x)
{
add(sz, sz, sz, U[y], y, x, y);
U[D[sz]] = sz; D[U[sz]] = sz;
}
else
{
add(sz, ft, R[ft], U[y], y, x, y);
R[L[sz]] = sz; L[R[sz]] = sz;
U[D[sz]] = sz; D[U[sz]] = sz;
}
++cnt[y]; ++sz;
}
void remove(int now)
{
R[L[now]] = R[now];
L[R[now]] = L[now];
F(i, D, now) F(j, R, i)
{
D[U[j]] = D[j];
U[D[j]] = U[j];
--cnt[col[j]];
}
}
void resume(int now)
{
F(i, U, now) F(j, L, i)
{
D[U[j]] = j;
U[D[j]] = j;
++cnt[col[j]];
}
R[L[now]] = now;
L[R[now]] = now;
}
bool dfs(int x)
{
if (!R[0]) { num = min(num, x); return true; }
int now = R[0];
F(i, R, 0) if (cnt[now]>cnt[i]) now = i;
remove(now);
F(i, D, now)
{
ans[x] = row[i];
F(j, R, i) remove(col[j]);
if (dfs(x + 1)) return true;
F(j, L, i) resume(col[j]);
}
resume(now);
return false;
}
void Remove(int now)
{
F(i, D, now)
{
L[R[i]] = L[i];
R[L[i]] = R[i];
}
}
void Resume(int now)
{
F(i, U, now) L[R[i]] = R[L[i]] = i;
}
int vis[maxn];
int flag[maxn];
int A()
{
int dis = 0;
F(i, R, 0) vis[i] = 0;
F(i, R, 0) if (!vis[i])
{
dis++; vis[i] = 1;
F(j, D, i) F(k, R, j) vis[col[k]] = 1;
}
return dis;
}
void Dfs(int x)
{
if (!R[0]) num = min(num, x);
else if (x + A()<num)
{
int now = R[0];
F(i, R, 0) if (cnt[now]>cnt[i]) now = i;
F(i, D, now)
{
Remove(i); F(j, R, i) Remove(j);
Dfs(x + 1);
F(j, L, i) Resume(j); Resume(i);
}
}
}
}dlx;
int main()
{
while (scanf("%d%d", &n, &m) == 2)
{
memset(mp, 0, sizeof(mp));
dlx.reset(n, n);
while (m--)
{
scanf("%d%d", &x, &y);
mp[x][y] = mp[y][x] = 1;
}
for (int i = 1; i <= n;i++)
for (int j = 1; j <= n;j++)
if (i == j || mp[i][j]) dlx.insert(i, j);
dlx.Dfs(0);
printf("%d\n", dlx.num);
}
return 0;
}

102
HDOJ/3499_autoAC.cpp Normal file
View File

@ -0,0 +1,102 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
typedef __int64 ll;
const int N=100010;
const int M=500010;
const ll inf=1LL<<60;
struct node
{
int to;
ll dis;
node *next;
}E[M<<1],*G1[N],*G2[N],*head;
int n,m,num;
ll d1[N],d2[N];
bool inq[N];
map<string,int> dict;
inline void add(int a,int b,ll c,node *G[])
{
head->to=b;
head->dis=c;
head->next=G[a];
G[a]=head++;
}
inline int change(char *s)
{
if(dict.count(s)) return dict[s];
else return dict[s]=num++;
}
void SPFA(int s,ll d[],node *G[])
{
deque<int> Q;
Q.push_back(s);
memset(inq,false,sizeof(inq));
fill(d,d+N,inf);
d[s]=0;
int to;
ll dis;
while(!Q.empty())
{
int u=Q.front();
Q.pop_front();
inq[u]=false;
for(node *p=G[u];p;p=p->next)
{
to=p->to;
dis=p->dis;
if(d[to]>d[u]+dis)
{
d[to]=d[u]+dis;
if(!inq[to])
{
inq[to]=true;
if(!Q.empty() && d[to]<=d[Q.front()]) Q.push_front(to);
else Q.push_back(to);
}
}
}
}
}
int main()
{
char s1[20],s2[20];
while(~scanf("%d%d",&n,&m))
{
num=0;
dict.clear();
memset(G1,NULL,sizeof(G1));
memset(G2,NULL,sizeof(G2));
head=E;
int s,t;
ll dis;
for(int i=0;i<m;i++)
{
scanf("%s %s %I64d",s1,s2,&dis);
s=change(s1),t=change(s2);
add(s,t,dis,G1);
add(t,s,dis,G2);
}
scanf("%s %s",s1,s2);
s=dict[s1],t=dict[s2];
SPFA(s,d1,G1);
SPFA(t,d2,G2);
ll ans=inf;
for(int i=0;i<n;i++)
{
for(node *p=G1[i];p;p=p->next)
{
int j=p->to;
if(d1[i]<inf && d2[j]<inf) ans=min(ans,d1[i]+d2[j]+(p->dis)/2);
}
}
if(ans==inf) printf("-1\n");
else printf("%I64d\n",ans);
}
return 0;
}