Powered By HC TECH : AutoACer Engine

3200-3299
This commit is contained in:
KiritoTRw 2016-09-01 20:53:17 +08:00 committed by GitHub
parent da5da8ec9f
commit 5ed1cf19d0
77 changed files with 6789 additions and 0 deletions

33
HDOJ/3200_autoAC.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAX_N = 8, MAX_S = 1 << MAX_N;
int n, m, t, dp[MAX_S][MAX_S];
char g[MAX_N][MAX_N + 1];
int main() {
while (scanf("%d", &n) != EOF) {
if (!n) break;
for (int i = 0; i < n; i ++) {
scanf("%s", g[i]);
for (int j = 0; j < n; j ++) g[i][j] -= '0';
}
int upper = 1 << n, ans = 0;
for (int i = 0; i < n; i ++) {
memset(dp, 0, sizeof(dp));
dp[1 << i][1 << i] = 1;
for (int mask = 0; mask < upper; mask ++)
for (int s = mask; s; s = (s - 1) & mask)
for (int u = 0; u < n; u ++) if (mask & (1 << u))
for (int v = 0; v < n; v ++) if ((mask & (1 << v)) == 0 && g[u][v]) {
int m1 = mask | (1 << v);
int m2 = (s | (1 << v)) & (~(1 << u));
if (((1 << (v + 1)) - 1) >= m2) dp[m1][m2] += dp[mask][s];
}
for (int s = 0; s < upper; s ++) ans += dp[upper - 1][s];
}
printf("%d\n", ans);
}
return 0;
}

14
HDOJ/3201_autoAC.cpp Normal file
View File

@ -0,0 +1,14 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.1415926
int main()
{
int L;
while((scanf("%d",&L)!=EOF)&&(L!=0))
{
printf("%.2lf\n",((L*L)/PI)/2.0);
}
return (0);
}

34
HDOJ/3203_autoAC.cpp Normal file
View File

@ -0,0 +1,34 @@
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <cstring>
#include <map>
#include <set>
#include <iostream>
#include <cmath>
using namespace std;
const int MAXN = 100005;
int main()
{
int n;
double a, p, b;
double f0, f1, p0, p1;
while (scanf("%d%lf%lf%lf", &n, &p, &a, &b))
{
if (n==0 && a==0 && p==0 && b==0) break;
p /= 100.0;
f0 = min(a, b);
f1 = 0.0;
for (int i = 1; i< n; ++i)
{
p0 = min(f0+b, a+p*f0+(1.0-p)*f1);
p1 = p*f0+(1.0-p)*f1;
f0 = p0;
f1 = p1;
}
printf("%.4lf\n", f1);
}
return 0;
}

105
HDOJ/3205_autoAC.cpp Normal file
View File

@ -0,0 +1,105 @@
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<utility>
using namespace std;
map<int,int> tmp;
set<map<int,int> > ans[1010];
int a[1010],b[1010],c[1010];
bool cmp(vector<pair<int,int> > A,vector<pair<int,int> > B)
{
vector<pair<int,int> >::reverse_iterator pA=A.rbegin(),pB=B.rbegin();
while (1)
{
if (pA->first<pB->first) return true;
if (pA->first>pB->first) return false;
if (abs(pA->second)<abs(pB->second)) return true;
if (abs(pA->second)>abs(pB->second)) return false;
if (pA->second<pB->second) return true;
if (pA->second>pB->second) return false;
++pA;++pB;
}
}
void init()
{
tmp[1]=1;
tmp[0]=-1;
ans[1].insert(tmp);
for (int i=2;i<=1001;++i)
{
for (int j=1;j<i;++j)
if (i%j==0)
for (set<map<int,int> >::iterator p=ans[j].begin();p!=ans[j].end();++p)
ans[i].insert(*p);
for (int j=0;j<=i;++j) a[j]=0;
a[i]=1;
a[0]=-1;
for (set<map<int,int> >::iterator p=ans[i].begin();p!=ans[i].end();++p)
{
for (int j=0;j<=i;++j) b[j]=c[j]=0;
for (std::_Rb_tree_const_iterator<std::pair<const int, int> > it=p->begin();it!=p->end();++it)
b[it->first]=it->second;
int get=0;
for (int j=0;j<=i;++j) if (b[j]) get=j;
for (int j=i;j>=0;--j)
if (a[j])
{
c[j-get]=a[j];
for (int k=get;k>=0;--k)
a[j-get+k]-=c[j-get]*b[k];
}
for (int j=i;j>=0;--j) a[j]=c[j];
}
tmp.clear();
for (int j=i;j>=0;--j)
if (a[j]) tmp[j]=a[j];
ans[i].insert(tmp);
}
}
void work()
{
while (1)
{
int n;
scanf("%d",&n);
if (!n) return;
vector<vector<pair<int,int> > > v;
v.clear();
vector<pair<int,int> > w;
for (set<map<int,int> >::iterator p=ans[n].begin();p!=ans[n].end();++p)
{
w.clear();
for (std::_Rb_tree_const_iterator<std::pair<const int, int> > it=p->begin();it!=p->end();++it)
w.push_back(make_pair(it->first,it->second));
v.push_back(w);
}
sort(v.begin(),v.end(),cmp);
for (vector<vector<pair<int,int> > >::iterator p=v.begin();p!=v.end();++p)
{
w=*p;
putchar('(');
int flag=0;
for (vector<pair<int,int> >::reverse_iterator it=w.rbegin();it!=w.rend();++it)
{
if (flag) putchar((it->second>0)?'+':'-');
it->second=abs(it->second);
if (it->first==0) printf("%d",it->second);
else if (it->first==1) (it->second==1)?putchar('x'):printf("%dx",it->second);
else (it->second==1)?printf("x^%d",it->first):printf("%dx^%d",it->second,it->first);
flag=1;
}
putchar(')');
}
puts("");
}
}
int main()
{
init();
work();
return 0;
}

8
HDOJ/3206_autoAC.cpp Normal file
View File

@ -0,0 +1,8 @@
........
........
........
........
........
..43366.
..121556
44212335

182
HDOJ/3207_autoAC.cpp Normal file
View File

@ -0,0 +1,182 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cassert>
#include <cmath>
using namespace std;
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
const int maxn = 100001;
const int segCnt = 400;
const int inf = 0x7f7f7f7f;
int INIT, segLen = segCnt;
struct Segment {
int e[segCnt];
int Min;
int add;
int addTo;
int lowest;
void init() {
Min = INIT;
for (int i = 0; i < segLen; i++) {
e[i] = INIT;
}
add = addTo = lowest = 0;
}
void update() {
Min = inf;
for (int i = 0; i < segLen; i++) {
if (e[i] <= abs(lowest)) e[i] = 0;
if (e[i]) {
e[i] += add;
e[i] = max(e[i], addTo);
}
Min = min(Min, e[i]);
}
add = addTo = lowest = 0;
}
void Add(int l, int r, int d) {
for (int i = l; i <= r; i++) {
if (e[i]) {
e[i] = max(e[i] + d, 0);
}
}
Min = inf;
for (int i = 0; i < segLen; i++) {
Min = min(Min, e[i]);
}
}
void AddTo(int l, int r, int d) {
for (int i = l; i <= r; i++) {
if (e[i]) {
e[i] = max(e[i], d);
}
}
Min = inf;
for (int i = 0; i < segLen; i++) {
Min = min(Min, e[i]);
}
}
void show() {
puts("Elements: ");
for (int i = 0; i < segLen; i++) {
printf(" %d", e[i]);
}
puts("");
printf("min %d add %d addto %d lowest %d\n", Min, add, addTo, lowest);
puts("~~~");
}
} seg[segCnt];
bool noBreak(int _l, int _r) {
int segL = _l / segLen, segR = _r / segLen;
int restL = _l % segLen, restR = _r % segLen;
if (segL != segR) {
seg[segL].update();
seg[segR].update();
for (int i = restL; i < segLen; i++) {
if (!seg[segL].e[i]) return false;
}
for (int i = 0; i <= restR; i++) {
if (!seg[segR].e[i]) return false;
}
for (int i = segL + 1; i < segR; i++) {
if (!seg[i].Min) return false;
}
} else {
seg[segL].update();
for (int i = restL; i <= restR; i++) {
if (!seg[segL].e[i]) return false;
}
}
return true;
}
void Add(int _l, int _r, int _d) {
int segL = _l / segLen, segR = _r / segLen;
int restL = _l % segLen, restR = _r % segLen;
if (segL != segR) {
seg[segL].update();
seg[segR].update();
seg[segL].Add(restL, segLen - 1, _d);
seg[segR].Add(0, restR, _d);
for (int i = segL + 1; i < segR; i++) {
seg[i].add += _d;
if (seg[i].Min) seg[i].Min += _d;
if (seg[i].addTo) seg[i].addTo += _d;
}
} else {
seg[segL].update();
seg[segL].Add(restL, restR, _d);
}
}
bool Sub(int _l, int _r, int _d) {
if (noBreak(_l, _r)) {
int segL = _l / segLen, segR = _r / segLen;
int restL = _l % segLen, restR = _r % segLen;
if (segL != segR) {
seg[segL].update();
seg[segR].update();
seg[segL].Add(restL, segLen - 1, -_d);
seg[segR].Add(0, restR, -_d);
for (int i = segL + 1; i < segR; i++) {
seg[i].add -= _d;
seg[i].addTo = max(seg[i].addTo - _d, 0);
seg[i].Min = max(seg[i].Min - _d, 0);
if (!seg[i].addTo) seg[i].lowest = min(seg[i].lowest, seg[i].add);
}
} else {
seg[segL].update();
seg[segL].Add(restL, restR, -_d);
}
return true;
}
return false;
}
void AddTo(int _l, int _r, int _d) {
int segL = _l / segLen, segR = _r / segLen;
int restL = _l % segLen, restR = _r % segLen;
if (segL != segR) {
seg[segL].update();
seg[segR].update();
seg[segL].AddTo(restL, segLen - 1, _d);
seg[segR].AddTo(0, restR, _d);
for (int i = segL + 1; i < segR; i++) {
if (seg[i].Min < _d && seg[i].addTo < _d) {
seg[i].addTo = _d;
if (seg[i].Min) seg[i].Min = _d;
}
}
} else {
seg[segL].update();
seg[segL].AddTo(restL, restR, _d);
}
}
int main() {
int N, M;
while (~scanf("%d%d%d", &N, &M, &INIT) && (N + M + INIT)) {
segLen = (int)sqrt((double) N) + 1;
int op, a, b, c;
int cnt = 0;
for (int i = 0; i < segLen; i++) {
seg[i].init();
}
for (int i = 1; i <= M; i++) {
scanf("%d%d%d%d", &op, &a, &b, &c);
switch (op) {
case 1 :
if (Sub(a, b, c)) {
cnt++;
}
break;
case 2 :
Add(a, b, c);
break;
case 3 :
AddTo(a, b, c);
break;
}
}
printf("%d\n", cnt);
}
return 0;
}

75
HDOJ/3208_autoAC.cpp Normal file
View File

@ -0,0 +1,75 @@
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#define ll __int64
#define inf (ll) 1e18+300
#define eps 1e-9
using namespace std;
ll why=(ll)1<<31;
ll num[100];
ll quickpow(ll m,int n)
{
ll b=1;int k=n;
while (n>0)
{
if (n&1)
{
double judge=1.0*(inf)/b;
if (judge<m) return -1;
b*=m;
}
n=n>>1;
if (m>why&&n>0) return -1;
m=m*m;
}
return b;
}
ll find(ll x,int k)
{
ll l=1,r=(ll)pow(x,1.0/k);
ll tt,pp,qq;
pp=quickpow(r,k);
if (pp==x) return r;
if (pp>x||pp==-1) --r;
else
{
tt=quickpow(r+1,k);
if (tt!=-1&&tt<=x) ++r;
}
return r;
}
ll f(ll x)
{
int i,j,k;
ll ans=0;
if (x<=3) return x;
memset(num,0,sizeof(num));
num[1]=x;
for (i=2;i<63;++i)
{
num[i]=find(x,i)-1;
if (!num[i]) break;
}
k=i;
for (i=k-1;i>0;--i)
{
for (j=1;j<i;++j)
if (i%j==0) num[j]-=num[i];
}
ans=num[1];
for (i=2;i<k;++i)
{
ans+=(i*num[i]);
}
return ans;
}
int main ()
{
ll a,b;
while (scanf("%I64d%I64d",&a,&b)&&(a+b))
{
printf("%I64d\n",f(b)-f(a-1));
}
return 0;
}

48
HDOJ/3209_autoAC.cpp Normal file
View File

@ -0,0 +1,48 @@
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
const double PI = acos(-1.0);
int l1, l2, l3, m1, m2, m3;
int hh, mm, ss;
int st, ed;
struct point_t {
double x, y;
point_t() {}
point_t(double _x, double _y) { x = _x, y = _y; }
};
double dist(const point_t &a, const point_t &b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
point_t calc(int t) {
double a = 2.0 * PI * t / 43200.0;
double b = 2.0 * PI * t / 3600.0;
double c = 2.0 * PI * t / 60.0;
double ra = m1 * 1.0 / (m1 + m2 + m3);
double rb = m2 * 1.0 / (m1 + m2 + m3);
double rc = m3 * 1.0 / (m1 + m2 + m3);
point_ A(l1 * ra * cos(a), l1 * ra * sin(a));
point_ B(l2 * rb * cos(b), l2 * rb * sin(b));
point_ C(l3 * rc * cos(c), l3 * rc * sin(c));
return point_t(A.x + B.x + C.x, A.y + B.y + C.y);
}
int main() {
while (scanf("%d", &l1) != EOF && l1 != -1) {
scanf("%d%d%d%d%d", &l2, &l3, &m1, &m2, &m3);
scanf("%d:%d:%d", &hh, &mm, &ss);
st = hh * 3600 + mm * 60 + ss;
scanf("%d:%d:%d", &hh, &mm, &ss);
ed = hh * 3600 + mm * 60 + ss;
double ans = 0.0;
point_t pre = calc(st);
for (int t = st + 1; t <= ed; t ++) {
point_t now = calc(t);
ans += dist(pre, now);
pre = now;
}
printf("%.2lf\n", ans * 0.5);
}
return 0;
}

112
HDOJ/3212_autoAC.cpp Normal file
View File

@ -0,0 +1,112 @@
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <map>
using namespace std;
#define maxn 105
#define type int
const int inf = ~0u >> 1;
struct edge
{
int u,v;
type cost;
edge(){}
edge(int _u,int _v,type _c):u(_u),v(_v),cost(_c){}
}e[maxn * maxn];
int pre[maxn],id[maxn],vis[maxn];
type in[maxn];
type dirmst(int root,int nv,int ne)
{
type ret = 0;
while(1)
{
fill(in,in + nv,inf);
for(int i = 0;i < ne;i++)
{
int u = e[i].u;
int v = e[i].v;
if(e[i].cost < in[v] && u != v)
{
pre[v] = u;
in[v] = e[i].cost;
}
}
for(int i = 0;i < nv;i++)
{
if(i == root)
continue;
if(in[i] == inf)
return -1;
}
int cntnode = 0;
fill(id,id + nv,-1);
fill(vis,vis + nv,-1);
in[root] = 0;
for(int i = 0;i < nv;i++)
{
ret += in[i];
int v = i;
while(vis[v] != i && id[v] == -1 && v != root)
{
vis[v] = i;
v = pre[v];
}
if(v != root && id[v] == -1)
{
for(int u = pre[v]; u != v;u = pre[u])
id[u] = cntnode;
id[v] = cntnode++;
}
}
if(cntnode == 0)
break;
for(int i = 0;i < nv;i++)
if(id[i] == -1)
id[i] = cntnode++;
for(int i = 0;i < ne;i++)
{
int v = e[i].v;
e[i].u = id[e[i].u];
e[i].v = id[e[i].v];
if(e[i].u != e[i].v)
e[i].cost -= in[v];
}
nv = cntnode;
root = id[root];
}
return ret;
}
int main()
{
int n;
while(scanf("%d",&n) == 1 && n)
{
map <string,int> M;
M["THE_WINDY"] = 2;
int cnt = 3;
string a,b;
int w1,w2;
int m = 0;
int tot = 0;
for(int i = 0;i < n;i++)
{
cin >> a >> w1 >> b >> w2;
if(M[a] == 0)
M[a] = cnt++;
if(M[b] == 0)
M[b] = cnt++;
e[m++] = edge(1,M[a],w1);
tot += w1;
if(M[a] != M[b])
{
e[m++] = edge(M[b],M[a],w2);
tot += w2;
}
}
e[m++] = edge(0,1,tot + 1);
e[m++] = edge(0,2,tot + 1);
int ans = dirmst(0,cnt,m);
printf("%d\n",ans - (tot + 1) * 2);
}
}

55
HDOJ/3213_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=1000;
int a1[N],p1[N],a2[N],p2[N],res[N],a[N],b[N];
int m;
void dp(int k)
{
int i,j;
memset(res,-1,sizeof(res));
res[0]=0;
while (k>0)
{
if (k&1)
{
for (i=0; i<=m; i++) b[i]=res[i];
memset(res,-1,sizeof(res));
for (i=0; i<=m; i++)
for (j=0; j<=i; j++)
if (b[j]>-1 && a[i-j]>-1)
res[i]=max(res[i],b[j]+a[i-j]);
}
for (i=0; i<=m; i++) b[i]=a[i];
memset(a,-1,sizeof(a));
for (i=0; i<=m; i++)
for (j=0; j<=i/2; j++)
if (b[j]>-1 && b[i-j]>-1)
a[i]=max(a[i],b[j]+b[i-j]);
k>>=1;
}
}
int main()
{
int n,n1,n2,i,j,ans;
while (scanf("%d%d",&n,&m)==2)
{
if (n==0 && m==0) break;
scanf("%d",&n1);
for (i=1; i<=n1; i++) scanf("%d%d",&a1[i],&p1[i]);
scanf("%d",&n2);
for (i=1; i<=n2; i++) scanf("%d%d",&a2[i],&p2[i]);
memset(a,-1,sizeof(a));
for (i=1; i<=n1; i++)
for (j=1; j<=n2; j++)
if (a1[i]+a2[j]<=m)
a[a1[i]+a2[j]]=max(a[a1[i]+a2[j]],p1[i]+p2[j]);
dp(n);
ans=0;
for (i=0; i<=m; i++) ans=max(ans,res[i]);
printf("%d\n",ans);
}
}

140
HDOJ/3215_autoAC.cpp Normal file
View File

@ -0,0 +1,140 @@
#include<stdio.h>
#include<math.h>
#include<string.h>
#define eps 1e-12
int ans[10005][10];
void init()
{
double t,x,a;
int i,k;
ans[0][1]=1;
for(i=1;i<=10000;i++)
{
x=(double)i;
t=x*log10(2.0)-floor(x*log10(2.0));
a=pow(10.0,t);
k=(int)(a+eps);
if(k==1)
{
ans[i][1]=ans[i-1][1]+1;
ans[i][2]=ans[i-1][2];
ans[i][3]=ans[i-1][3];
ans[i][4]=ans[i-1][4];
ans[i][5]=ans[i-1][5];
ans[i][6]=ans[i-1][6];
ans[i][7]=ans[i-1][7];
ans[i][8]=ans[i-1][8];
ans[i][9]=ans[i-1][9];
}
if(k==2)
{
ans[i][1]=ans[i-1][1];
ans[i][2]=ans[i-1][2]+1;
ans[i][3]=ans[i-1][3];
ans[i][4]=ans[i-1][4];
ans[i][5]=ans[i-1][5];
ans[i][6]=ans[i-1][6];
ans[i][7]=ans[i-1][7];
ans[i][8]=ans[i-1][8];
ans[i][9]=ans[i-1][9];
}
if(k==3)
{
ans[i][1]=ans[i-1][1];
ans[i][2]=ans[i-1][2];
ans[i][3]=ans[i-1][3]+1;
ans[i][4]=ans[i-1][4];
ans[i][5]=ans[i-1][5];
ans[i][6]=ans[i-1][6];
ans[i][7]=ans[i-1][7];
ans[i][8]=ans[i-1][8];
ans[i][9]=ans[i-1][9];
}
if(k==4)
{
ans[i][1]=ans[i-1][1];
ans[i][2]=ans[i-1][2];
ans[i][3]=ans[i-1][3];
ans[i][4]=ans[i-1][4]+1;
ans[i][5]=ans[i-1][5];
ans[i][6]=ans[i-1][6];
ans[i][7]=ans[i-1][7];
ans[i][8]=ans[i-1][8];
ans[i][9]=ans[i-1][9];
}
if(k==5)
{
ans[i][1]=ans[i-1][1];
ans[i][2]=ans[i-1][2];
ans[i][3]=ans[i-1][3];
ans[i][4]=ans[i-1][4];
ans[i][5]=ans[i-1][5]+1;
ans[i][6]=ans[i-1][6];
ans[i][7]=ans[i-1][7];
ans[i][8]=ans[i-1][8];
ans[i][9]=ans[i-1][9];
}
if(k==6)
{
ans[i][1]=ans[i-1][1];
ans[i][2]=ans[i-1][2];
ans[i][3]=ans[i-1][3];
ans[i][4]=ans[i-1][4];
ans[i][5]=ans[i-1][5];
ans[i][6]=ans[i-1][6]+1;
ans[i][7]=ans[i-1][7];
ans[i][8]=ans[i-1][8];
ans[i][9]=ans[i-1][9];
}
if(k==7)
{
ans[i][1]=ans[i-1][1];
ans[i][2]=ans[i-1][2];
ans[i][3]=ans[i-1][3];
ans[i][4]=ans[i-1][4];
ans[i][5]=ans[i-1][5];
ans[i][6]=ans[i-1][6];
ans[i][7]=ans[i-1][7]+1;
ans[i][8]=ans[i-1][8];
ans[i][9]=ans[i-1][9];
}
if(k==8)
{
ans[i][1]=ans[i-1][1];
ans[i][2]=ans[i-1][2];
ans[i][3]=ans[i-1][3];
ans[i][4]=ans[i-1][4];
ans[i][5]=ans[i-1][5];
ans[i][6]=ans[i-1][6];
ans[i][7]=ans[i-1][7];
ans[i][8]=ans[i-1][8]+1;
ans[i][9]=ans[i-1][9];
}
if(k==9)
{
ans[i][1]=ans[i-1][1];
ans[i][2]=ans[i-1][2];
ans[i][3]=ans[i-1][3];
ans[i][4]=ans[i-1][4];
ans[i][5]=ans[i-1][5];
ans[i][6]=ans[i-1][6];
ans[i][7]=ans[i-1][7];
ans[i][8]=ans[i-1][8];
ans[i][9]=ans[i-1][9]+1;
}
}
}
int main()
{
init();
int n,i;
while(scanf("%d",&n)!=EOF)
{
if(n==-1)
break;
for(i=1;i<9;i++)
printf("%d ",ans[n][i]);
printf("%d\n",ans[n][9]);
}
return 0;
}

30
HDOJ/3217_autoAC.cpp Normal file
View File

@ -0,0 +1,30 @@
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
using namespace std;
#define maxn 18
int dp[maxn][1<<18];
int N, M, s, v;
int main(){
while(scanf("%d", &N) && N){
scanf("%d", &M);
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= M; i++){
scanf("%d%d", &s, &v);
dp[0][s] = v;
}
for(int i = 1; i <= N; i++){
for(int sta = 0; sta < (1<<N); sta++ ){
if(sta & 1<<(i-1)) dp[i][sta] = dp[i-1][sta] + dp[i-1][sta-(1<<(i-1))];
else dp[i][sta] = dp[i-1][sta];
}
}
int max = -9999;
for(int sta = 0; sta < (1<<N); sta++){
if(dp[N][sta] > max) max = dp[N][sta];
}
printf("%d\n", max);
}
return 0;
}

54
HDOJ/3219_autoAC.cpp Normal file
View File

@ -0,0 +1,54 @@
#include <iostream>
#include <cstdio>
using namespace std;
int zhengc[101],yanchi[101];
int times[101],timee[101];
int ts,te;
int main(){
int n;
while(scanf("%d",&n)&&n){
int a,b,c,d,i;
for(i=1;i<=n;i++){
scanf("%d%d",&zhengc[i],&yanchi[i]);
yanchi[i]+=zhengc[i];
scanf("%d:%d %d:%d",&a,&b,&c,&d);
times[i]=a*60+b;
timee[i]=c*60+d;
}
scanf("%d:%d %d:%d",&a,&b,&c,&d);
ts=a*60+b;
te=c*60+d;
bool mark=true;
int tt;
for(i=1;i<=n;i++){
tt=ts+zhengc[i];
if(tt>=1440){
mark=false;
break;
}
if(tt<=times[i] || ts>=timee[i]){
ts+=zhengc[i];
if(ts>=te || ts>=1440){
mark=false;
break;
}
}else{
ts+=yanchi[i];
if(ts>=te || ts>=1440){
mark=false;
break;
}
}
}
if(mark){
if(ts<te){
printf("Lucky YY!\n");
}else{
printf("Poor YY!\n");
}
}else{
printf("Poor YY!\n");
}
}
return 0;
}

82
HDOJ/3220_autoAC.cpp Normal file
View File

@ -0,0 +1,82 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std;
const int MAX=1<<17;
const int x[32]={0,1,3,11,10,8,10,2,8,9,9,0,0,1,3,11,10,8,9,2,12,13,13,4,14,6,12,4,5,7,15,14};
const int y[32]={1,3,11,10,8,0,2,3,9,1,11,2,4,5,7,15,14,12,13,6,13,5,15,6,6,7,4,5,7,15,14,12};
const int aim=((1<<8)-1);
int dist[MAX];
struct Node
{
int st;
int depth;
};
int has(const int& st,const int& op)
{
return st^((1<<(15-x[op]))|(1<<(15-y[op])));
}
void bfs()
{
Node node,next;
queue<Node> q;
node.st=aim;
node.depth=0;
dist[aim]=0;
q.push(node);
while(!q.empty())
{
node=q.front();
q.pop();
if(node.depth>3)
break;
next.depth=node.depth+1;
for(int i=0;i<32;i++)
{
if((node.st&(1<<(15-x[i])))!=(node.st&(1<<(15-y[i]))))
{
next.st=has(node.st,i);
if(dist[next.st]==1000)
{
dist[next.st]=next.depth;
q.push(next);
}
}
}
}
}
int main()
{
int t,cnt=0;
int a;
int st;
for(int i=0;i<MAX;i++)
{
dist[i]=1000;
}
bfs();
scanf("%d",&t);
while(t--)
{
cnt++;
st=0;
for(int i=0;i<16;i++)
{
scanf("%d",&a);
if(a==1)
st|=(1<<(15-i));
}
a=dist[st];
if(a>3)
{
printf("Case #%d: more\n",cnt);
}
else
{
printf("Case #%d: %d\n",cnt,a);
}
}
return 0;
}

117
HDOJ/3221_autoAC.cpp Normal file
View File

@ -0,0 +1,117 @@
# include<stdio.h>
# include<string.h>
# include<stdlib.h>
# define N 1000005
int visit[N],prime[N],K;
__int64 P,Phi;
struct matrix{
__int64 A[2][2];
};
void intt()
{
int i,j;
memset(visit,0,sizeof(visit));
for(i=2;i<=1000;i++)
{
if(visit[i]==0)
{
for(j=i+i;j<=1000000;j+=i)
{
visit[j]=1;
}
}
}
K=0;
for(j=2;j<=1000000;j++)
if(visit[j]==0) prime[++K]=j;
}
matrix power(matrix ans1,matrix ans2)
{
int i,j,k;
matrix ans;
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
ans.A[i][j]=0;
for(k=0;k<=1;k++)
{
ans.A[i][j]+=ans1.A[i][k]*ans2.A[k][j];
if(ans.A[i][j]>Phi)
{
ans.A[i][j]=ans.A[i][j]%Phi+Phi;
}
}
}
}
return ans;
}
matrix mod(matrix un,__int64 k)
{
matrix ans;
ans.A[0][0]=1;
ans.A[0][1]=0;
ans.A[1][0]=0;
ans.A[1][1]=1;
while(k)
{
if(k%2) ans=power(ans,un);
un=power(un,un);
k/=2;
}
return ans;
}
__int64 mod1(__int64 a,__int64 k)
{
__int64 ans;
ans=1;
while(k)
{
if(k%2)
{
ans=ans*a;
ans%=P;
}
a=a*a;
a%=P;
k/=2;
}
return ans%P;
}
int main()
{
int i,ncase,t;
__int64 a,b,aa,bb,n,num,num1,num2;
matrix init,ans;
intt();
scanf("%d",&ncase);
for(t=1;t<=ncase;t++)
{
scanf("%I64d%I64d%I64d%I64d",&a,&b,&P,&n);
printf("Case #%d: ",t);
if(n==1) {printf("%I64d\n",a%P);continue;}
else if(n==2) {printf("%I64d\n",b%P); continue;}
else if(n==3) {printf("%I64d\n",a*b%P);continue;}
if(P==1) {printf("0\n");continue;}
init.A[0][0]=0;
init.A[0][1]=1;
init.A[1][0]=1;
init.A[1][1]=1;
Phi=1;
num=P;
for(i=1;i<=K;i++)
{
if(prime[i]>P) break;
if(P%prime[i]==0) {Phi*=(prime[i]-1);num/=prime[i];}
}
Phi*=num;
ans=mod(init,n-3);
num1=ans.A[1][1];
num2=ans.A[0][1]+ans.A[1][1];
if(num2>Phi) num2=num2%Phi+Phi;
aa=mod1(a,num1);
bb=mod1(b,num2);
printf("%I64d\n",aa*bb%P);
}
return 0;
}

128
HDOJ/3223_autoAC.cpp Normal file
View File

@ -0,0 +1,128 @@
#include<cstdio>
#include<vector>
#include<cmath>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
ll pow(ll a,ll b,ll mod){
ll as=1;
while(b){
if(b&1)as=as*a%mod;
b>>=1; a=a*a%mod;
}
return as;
}
vector<ll>as,f;
ll gcd(ll a,ll b,ll& x,ll& y){
if(b==0){
x=1;y=0;
return a;
}
else{
ll r=gcd(b,a%b,y,x);
y-=a/b*x;
return r;
}
}
void moxianxing(ll a,ll b,ll n){
ll x,y;
ll d=gcd(a,n,x,y);
as.clear();
if(b%d==0){
x%=n;x+=n;x%=n;
as.push_back(x*(b/d)%(n/d));
for(ll i=1;i<d;i++)
as.push_back((as[0]+i*n/d)%n);
}
}
bool gtest(ll g,ll p){
for(int i=0;i<f.size();i++)
if(pow(g,(p-1)/f[i],p)==1)
return 0;
return 1;
}
ll gen(ll p){
ll tmp=p-1;
f.clear();
for(ll i=2;i<=tmp/i;i++)
if(tmp%i==0){
f.push_back(i);
while(tmp%i==0)
tmp/=i;
}
if(tmp!=1)f.push_back(tmp);
ll g=0;
while(++g)if(gtest(g,p))return g;
}
map<ll,int>rec;
ll lisanshu(ll x,ll n,ll m){
ll s=(ll)(sqrt((double)m)+0.5);
ll cur=1;rec.clear();
for(int i=0;i<s;i++){
rec[cur]=i;
cur=cur*x%m;
}
ll mul=cur;cur=1;
for(int i=0;i<s;i++){
ll more=n*pow(cur,m-2,m)%m;
if(rec.count(more))return i*s+rec[more];
cur=cur*mul%m;
}
return -1;
}
int dd[33],hh[30],mi[80];
int mm[20]={0,31,28,31,30,31,30,31,31,30,31,30,31};
void solve(ll x){
int y,mo,d,h,min,s,i;
for(y=2000;1;y++){
s=365*24*60*60;
if(y%4==0&&y%100||y%400==0)s+=24*60*60;
if(y%10==5||y%10==8)s++;
if(x-s<0)break;
x-=s;
}
if(y%4==0&&y%100||y%400==0)
for(i=2;i<13;i++)
mm[i]+=24*60*60;
if(y%10==5||y%10==8){
mm[12]++;dd[31]++;hh[24]++;mi[60]++;
}
for(i=1;i<13;i++)if(x-mm[i]<0)break;x-=mm[i-1];mo=i;
for(i=1;i<33;i++)if(x-dd[i]<0)break;x-=dd[i-1];d=i;
for(i=1;i<25;i++)if(x-hh[i]<0)break;x-=hh[i-1];h=i-1;
for(i=1;i<61;i++)if(x-mi[i]<0)break;x-=mi[i-1];min=i-1;
if(y%4==0&&y%100||y%400==0)
for(i=2;i<13;i++)
mm[i]-=24*60*60;
if(y%10==5||y%10==8){
mm[12]--;dd[31]--;hh[24]--;mi[60]--;
}
printf("%d.%02d.%02d %02d:%02d:%02lld\n",y,mo,d,h,min,x);
}
int main(){
ll a,p,q,g,u,v;
int cs,csi;
for(int i=1;i<15;i++)mm[i]=mm[i]*24*60*60+mm[i-1];
for(int i=1;i<33;i++)dd[i]=i*24*60*60;
for(int i=1;i<30;i++)hh[i]=i*60*60;
for(int i=1;i<80;i++)mi[i]=i*60;
scanf("%d",&cs);
for(csi=1;csi<=cs;csi++){
scanf("%lld%lld%lld",&p,&q,&a);
printf("Case #%d:\n",csi);
if(a==0){puts("2000.01.01 00:00:00");continue;}
as.clear();
g=gen(p);
v=lisanshu(g,a,p);
if(v==-1){puts("Transmission error");continue;}
moxianxing(q,v,p-1);
if((int)as.size()==0){puts("Transmission error");continue;}
for(int i=0;i<as.size();i++)as[i]=pow(g,as[i],p);
sort(as.begin(),as.end());
for(int i=0;i<as.size();i++)
solve(as[i]);
}
return 0;
}

97
HDOJ/3225_autoAC.cpp Normal file
View File

@ -0,0 +1,97 @@
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
#define N 300
int t, n, m, k, ans[N], match[N], num, match2[N];
bool vst[N][N], re[N], tmp[N];
vector<int> jeo[N];
bool find(int id, int limit)
{
if (id <= limit) return false;
for(int i = 0; i < jeo[id].size(); i++)
if (!tmp[jeo[id][i]])
{
tmp[jeo[id][i]] = 1;
if (match[jeo[id][i]] == -1 || find(match[jeo[id][i]], limit))
{
match[jeo[id][i]] = id;
return true;
}
}
return false;
}
bool solve()
{
memset(match, -1, sizeof(int) * (n + 1));
for(int i = 1; i <= n; i++)
{
memset(tmp, 0, sizeof(bool) * (n + 1));
if (!find(i, 0)) return false;
}
return true;
}
bool check(int id, int tar)
{
if(match[tar] == id) return true;
int key = 0;
for(int i = 1; i <= n; i++)
if ((match2[i] = match[i]) == id) key = i;
int t = match[tar]; match[tar] = id; match[key] = -1;
memset(tmp, 0, sizeof(bool) * (n + 1));
if (find(t, id)) return true;
else for(int i = 1; i <= n; i++)
match[i] = match2[i];
return false;
}
bool dfs(int deep)
{
if (deep == n + 1)
{
num++; if (num == k)
return true;
return false;
}
for(int i = 0; i < jeo[deep].size(); i++)
if (!re[jeo[deep][i]] && check(deep, jeo[deep][i]))
{
ans[deep] = jeo[deep][i];
re[jeo[deep][i]] = 1;
if (dfs(deep + 1)) return true;
re[jeo[deep][i]] = 0;
}
return false;
}
int main()
{
scanf("%d", &t);
for(int ca = 1; ca <= t; ca++)
{
scanf("%d%d%d", &n, &m, &k);
for(int i = 1; i <= n; i++)
for(int j =1; j <= n; j++) vst[i][j] = 0;
for(int i = 0; i < m; i++)
for(int j = 1; j <= n; j++)
{
int x;scanf("%d", &x);vst[j][x] = 1;
}
for(int i = 1; i <= n; i++)
{
jeo[i].clear();
for(int j = 1; j <= n; j++)
if (!vst[i][j]) jeo[i].push_back(j);
sort(jeo[i].begin(), jeo[i].end());
}
memset(re, 0, sizeof(bool) * (n + 1)); num = 0;
printf("Case #%d:", ca);
if (solve() && dfs(1))
{
for(int i = 1; i <= n; i++) printf(" %d", ans[i]);
putchar('\n');
}
else printf(" -1\n");
}
return 0;
}

240
HDOJ/3228_autoAC.cpp Normal file
View File

@ -0,0 +1,240 @@
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
#include<algorithm>
#define eps 1e-6
#define N 10010
using namespace std;
struct Point
{
double x,y;
Point(){}
Point(double a,double b):x(a),y(b){}
};
struct Line
{
Point a,b;
Line(){}
Line(Point u,Point v):a(u),b(v){}
};
struct Info
{
int n;
Point p[N];
Line l;
Point fa;
};
Info a,b;
struct Node
{
int u,v;
double len;
Node(){}
Node(int u0,int v0,double len0):u(u0),v(v0),len(len0){}
}nod[8*N];
bool cccc(Node a,Node b)
{
return a.len < b.len;
}
int nod_n;
int Sig(double a)
{
return a<-eps?-1:a>eps;
}
Point Intersection(Line u,Line v)
{
Point ret=u.a;
double t=((u.a.x-v.a.x)*(v.a.y-v.b.y)-(u.a.y-v.a.y)*(v.a.x-v.b.x))/
((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x));
ret.x+=(u.b.x-u.a.x)*t;
ret.y+=(u.b.y-u.a.y)*t;
return ret;
}
int cmp(Point a,Point b)
{
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}
double Dis(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int Find(double num,Point p[],int n,int ind)
{
int left=0,right=n;
if(ind==1)
{
while(left+1<right)
{
int mid=(left+right)>>1;
if(Sig(num-p[mid].y)>=0)
left=mid;
else
right=mid;
}
}
else
{
while(left+1<right)
{
int mid=(left+right)>>1;
if(Sig(num-p[mid].x)>=0)
left=mid;
else
right=mid;
}
}
return left;
}
Point operator +(Point a,Point b)
{
Point tmp;
tmp.x=a.x+b.x;
tmp.y=a.y+b.y;
return tmp;
}
void Init()
{
double t;
double ax,ay,bx,by,cx,cy,dx,dy;
scanf("%d%d",&a.n,&b.n);
scanf("%lf%lf%lf%lf",&ax,&ay,&bx,&by);
scanf("%lf%lf%lf%lf",&cx,&cy,&dx,&dy);
for(int i=0;i<a.n;i++)
{
scanf("%lf",&t);
a.p[i].x=ax*t+bx*(1-t);
a.p[i].y=ay*t+by*(1-t);
}
for(int i=0;i<b.n;i++)
{
scanf("%lf",&t);
b.p[i].x=cx*t+dx*(1-t);
b.p[i].y=cy*t+dy*(1-t);
}
sort(a.p, a.p+a.n, cmp);
sort(b.p, b.p+b.n, cmp);
a.l=Line(Point(ax,ay),Point(bx,by));
a.fa=Point(by-ay,ax-bx);
b.l=Line(Point(cx,cy),Point(dx,dy));
b.fa=Point(dy-cy,cx-dx);
nod_n=0;
for(int i=0;i<a.n;i++)
{
Point tp=Intersection(b.l,Line(a.p[i],b.fa+a.p[i]));
if(cx==dx)
{
double y=tp.y;
if(Sig(y-b.p[0].y)<=0)
nod[nod_n++]=Node(i,a.n,Dis(a.p[i],b.p[0]));
else if(Sig(y-b.p[b.n-1].y)>=0)
nod[nod_n++]=Node(i,a.n+b.n-1,Dis(a.p[i],b.p[b.n-1]));
else
{
int pos=Find(y,b.p,b.n-1,1);
double d1=Dis(a.p[i],b.p[pos]);
double d2=Dis(a.p[i],b.p[pos+1]);
nod[nod_n++]=Node(i,a.n+pos,d1);
nod[nod_n++]=Node(i,a.n+pos+1,d2);
}
}
else
{
double x=tp.x;
if(Sig(x-b.p[0].x)<=0)
nod[nod_n++]=Node(i,a.n,Dis(a.p[i],b.p[0]));
else if(Sig(x-b.p[b.n-1].x)>=0)
nod[nod_n++]=Node(i,a.n+b.n-1,Dis(a.p[i],b.p[b.n-1]));
else
{
int pos=Find(x,b.p,b.n-1,2);
double d1=Dis(a.p[i],b.p[pos]);
double d2=Dis(a.p[i],b.p[pos+1]);
nod[nod_n++]=Node(i,a.n+pos,d1);
nod[nod_n++]=Node(i,a.n+pos+1,d2);
}
}
}
for(int i=0;i<b.n;i++)
{
Point tp=Intersection(a.l,Line(b.p[i],a.fa+b.p[i]));
if(ax==bx)
{
double y=tp.y;
if(Sig(y-a.p[0].y)<=0)
nod[nod_n++]=Node(i+a.n,0,Dis(b.p[i],a.p[0]));
else if(Sig(y-a.p[a.n-1].y)>=0)
nod[nod_n++]=Node(i+a.n,a.n-1,Dis(b.p[i],a.p[a.n-1]));
else
{
int pos=Find(y,a.p,a.n-1,1);
double d1=Dis(b.p[i],a.p[pos]);
double d2=Dis(b.p[i],a.p[pos+1]);
nod[nod_n++]=Node(i+a.n,pos,d1);
nod[nod_n++]=Node(i+a.n,pos+1,d2);
}
}
else
{
double x=tp.x;
if(Sig(x-a.p[0].x)<=0)
nod[nod_n++]=Node(i+a.n,0,Dis(b.p[i],a.p[0]));
else if(Sig(x-a.p[a.n-1].x)>=0)
nod[nod_n++]=Node(i+a.n,a.n-1,Dis(b.p[i],a.p[a.n-1]));
else
{
int pos=Find(x,a.p,a.n-1,2);
double d1=Dis(b.p[i],a.p[pos]);
double d2=Dis(b.p[i],a.p[pos+1]);
nod[nod_n++]=Node(i+a.n,pos,d1);
nod[nod_n++]=Node(i+a.n,pos+1,d2);
}
}
}
for(int i=0;i<a.n-1;i++)
nod[nod_n++]=Node(i,i+1,Dis(a.p[i],a.p[i+1]));
for(int i=0;i<b.n-1;i++)
nod[nod_n++]=Node(i+a.n,i+a.n+1,Dis(b.p[i],b.p[i+1]));
}
int parent[20010];
int find(int x)
{
if(parent[x] != x)
parent[x]=find(parent[x]);
return parent[x];
}
int main()
{
int cas;
scanf("%d",&cas);
for(int cc=1;cc<=cas;cc++)
{
Init();
sort(nod,nod+nod_n,cccc);
int n=a.n+b.n;
for(int i=0;i<n;i++)
parent[i]=i;
int tmp=n-1;
double total=0;
int x,y;
int px,py;
for(int i=0;i<nod_n && tmp;i++)
{
x=nod[i].u;
y=nod[i].v;
px=find(x);
py=find(y);
if(px == py)
continue;
total+=nod[i].len;
tmp--;
if(rand()&1)
parent[px]=py;
else
parent[py]=px;
}
printf("Case #%d: %.3f\n",cc,total);
}
return 0;
}

110
HDOJ/3229_autoAC.cpp Normal file
View File

@ -0,0 +1,110 @@
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string.h>
const int MAXN = 1000 + 10;
const int INF = 1<<29;
int N, Track[7][MAXN],conflict[128];
int total[MAXN];
int max(int x, int y){
if (x>y) return x;
else return y;
}
void Build_Confliction(){
int K;
memset(conflict,0,sizeof(int)*128);
scanf("%d",&K);
while (K--){
int t,i,conf = 0;
scanf("%d",&t);
for (i=1; i<=64; i*=2){
conf += (t%10)*i;
t = t/10;
}
for (i=0; i<=127; i++){
conflict[conf|i] = 1;
}
}
return ;
}
void Build_Cut(){
int t,k;
memset(total, 0, sizeof(int)*MAXN);
for (t=1; t<=N; t++){
for (k=0; k<=6; k++)
if (Track[k][t]>0)
total[t] |= (1<<(6-k));
}
}
void init(){
scanf("%d", &N);
memset(Track, 0, sizeof(int)*7*MAXN);
for (int i=0; i<=6; i++){
int C;
scanf("%d", &C);
while (C--){
int t1,t2;
char t3;
scanf("%d%c", &t1, &t3);
if (t3=='-'){
scanf("%d", &t2);
Track[i][t1] = 3; Track[i][t2] = 4;
for (int j=t1+1; j<t2; j++)
Track[i][j] = 2;
}
else
Track[i][t1] = 1;
}
}
Build_Confliction();
Build_Cut();
return ;
}
int solve()
{
int f[MAXN][128],j,k,t,ans = 0;;
for (t=0; t<=N; t++)
for (j=0; j<=127; j++)
f[t][j] = -INF;
f[0][0] = 0;
for (t=1; t<=N; t++){
for (j=0; j<=127; j++)
if ((j&total[t])==j){
int i, add_t = 0, pre = 0, now = 0, j_temp = j;
for (i=0; i<=6; i++){
if (Track[i][t] == 1 && ((1<<(6-i))&j) != 0)
add_t++;
if (Track[i][t] == 3 && ((1<<(6-i))&j) != 0){
++add_t;
now += (1<<(6-i));
}
if (Track[i][t] == 2 && ((1<<(6-i))&j) != 0){
now += (1<<(6-i));
pre += (1<<(6-i));
}
if (Track[i][t] == 4 && ((1<<(6-i))&j) != 0){
++add_t;
pre += (1<<(6-i));
j_temp -= (1<<(6-i));
}
}
if (!conflict[j_temp]){
for (k=0; k<=127; k++)
if (!conflict[k] && ((pre&k)==pre) && f[t-1][k] != -INF)
f[t][now] = max(add_t + f[t-1][k], f[t][now]);
}
}
}
for (j=0; j<=127; j++)
if (f[N][j]>ans) ans = f[N][j];
return ans;
}
int main(){
int T,Cases;
scanf("%d", &T);
for (Cases = 1; Cases <= T; Cases++){
init();
printf("Case #%d: %d\n", Cases, solve());
}
return 0;
}

85
HDOJ/3231_autoAC.cpp Normal file
View File

@ -0,0 +1,85 @@
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
#define N 2005
struct T
{
int v,next;
}E[3][N*100];
struct TT
{
int head,rd,dep;
}V[3][N];
int top[3],ans,n,m;
void Add_Edge(int k,int u,int v)
{
E[k][top[k]].v = v;
E[k][top[k]].next = V[k][u].head;
V[k][u].head = top[k]++;
++V[k][v].rd;
}
bool Top_Sort(int k)
{
queue<int> Q;
for(int i=1;i<=n;i++)
if(V[k][i].rd == 0)
Q.push(i);
int cnt = 0;
while(!Q.empty())
{
++cnt;
int p = Q.front();
for(int i=V[k][p].head;i!=NULL;i=E[k][i].next)
{
int q = E[k][i].v;
--V[k][q].rd;
if(V[k][q].rd == 0)
{
Q.push(q);
V[k][q].dep = V[k][p].dep + 1;
}
}
Q.pop();
}
return cnt == n;
}
int main()
{
int u,v,nn,ncase=0;
char cmd;
while(~scanf("%d%d%*c",&nn,&m),nn)
{
memset(V,0,sizeof(V));
top[0] = top[1] = top[2] = 1;
n = 2*nn;
for(int k=0;k<3;k++)
for(int i=1;i<=nn;i++)
Add_Edge(k,i,i+nn);
while(m--)
{
scanf("%c%d%d%*c",&cmd,&u,&v);
if(cmd == 'I')
{
for(int k=0;k<3;k++)
{
Add_Edge(k,u,v+nn);
Add_Edge(k,v,u+nn);
}
}
else
Add_Edge(cmd-'X',u+nn,v);
}
printf("Case %d: ",++ncase);
if(!Top_Sort(0) || !Top_Sort(1) || !Top_Sort(2))
puts("IMPOSSIBLE\n");
else
{
puts("POSSIBLE");
for(int i=1;i<=nn;i++)
printf("%d %d %d %d %d %d\n",V[0][i].dep,V[1][i].dep,V[2][i].dep,V[0][i+nn].dep,V[1][i+nn].dep,V[2][i+nn].dep);
puts("");
}
}
return 0;
}

17
HDOJ/3232_autoAC.cpp Normal file
View File

@ -0,0 +1,17 @@
#include<stdio.h>
int main()
{
int n, i, cas = 0, d, p, l, v;
while(~scanf("%d%d",&n,&d), n+d)
{
double ans = d*1.0;
for(i = 0; i < n; i++)
{
scanf("%d%d%d",&p, &l, &v);
ans -= l;
ans += 2.0*l / v;
}
printf("Case %d: %.3lf\n\n",++cas, ans);
}
return 0;
}

70
HDOJ/3233_autoAC.cpp Normal file
View File

@ -0,0 +1,70 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const double eps = 1e-7;
struct node{
node(){}
node(double _size,double _rem):size(_size),rem(_rem){}
double size,rem;
};
node Q[1005],data[20005];
bool cmp(node a,node b)
{
if(fabs(a.size-b.size) < eps) return a.rem > b.rem;
return a.size < b.size;
}
double solve(int n,int m,double b)
{
int len = 0;
double ans = 0;
for(int i = 0;i < n;++i)
{
if(len < m)
{
double Size = data[i].size*(1-data[i].rem*0.01);
if(fabs(Size) < eps) continue;
Q[len++] = node(Size,0.0);
for(int j = len-1;j > 0;--j)
{
if(Q[j].size < Q[j-1].size)
swap(Q[j],Q[j-1]);
else
break;
}
}
if(len == m)
{
double t = Q[0].size / (b / m),s = Q[0].size;
ans += t;
for(int j = 1;j < len;++j)
{
Q[j].size -= s;
Q[j-1] = Q[j];
}
len--;
}
}
for(int i = 0;i < len;++i)
{
double t = Q[i].size / (b / (len-i)),s = Q[i].size;
ans += t;
for(int j = i;j < len;++j)
Q[j].size -= s;
}
return ans;
}
int main()
{
int n,m,b,casenum = 1;
while(scanf("%d%d%d",&n,&m,&b) && n+m+b)
{
for(int i = 0;i < n;++i)
scanf("%lf%lf",&data[i].size,&data[i].rem);
sort(data,data+n,cmp);
printf("Case %d: %.2lf\n\n",casenum++,solve(n,m,(double)b));
}
return 0;
}

111
HDOJ/3234_autoAC.cpp Normal file
View File

@ -0,0 +1,111 @@
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int f[20010],r[20010],n;
int map[20010];
int xp[20],ans;
void init()
{
for(int i=0;i<=n;i++)
{
f[i]=i;
r[i]=0;
}
}
int find(int x)
{
if(x==f[x])
return x;
int t=find(f[x]);
r[x]^=r[f[x]];//规x涓惰圭崇郴姹x涓硅圭崇郴
f[x]=t;
return f[x];
}
bool Union(int x,int y,int k)
{
int a=find(x);
int b=find(y);
if(a==b)
{
if((r[x]^r[y])==k)
return true;
return false;
}
if(a==n)
swap(a,b);
f[a]=b;
r[a]=r[x]^r[y]^k;
return true;
}
bool query(int m)
{
memset(map,0,sizeof(map));
for(int i=0;i<m;i++)
{
map[find(xp[i])]++;
ans^=r[xp[i]];
}
int flag=0;
for(int i=0;i<m;i++)
{
int t=find(xp[i]);
if(map[t]%2!=0 && t!=n)
{flag=1;break;}
}
if(flag)
return false;
return true;
}
int main()
{
char s[2],c;
int a,b,v,Q,cas=0;
while(scanf("%d %d",&n,&Q)==2 &&(n||Q))
{
init();
printf("Case %d:\n",++cas);
int fact=0,flag=0;
while(Q--)
{
scanf("%s%d%d",s,&a,&b);
if(s[0]=='I')
{
fact++;
c=getchar();
if(c==' ')
{
scanf("%d",&v);
if(flag)continue;
if(!Union(a,b,v))
{
printf("The first %d facts are conflicting.\n",fact);
flag=1;
}
}
else
{
if(flag)continue;
if(!Union(a,n,b))
{
printf("The first %d facts are conflicting.\n",fact);
flag=1;
}
}
}
else
{
xp[0]=b;
for(int i=1;i<a;i++)
scanf("%d",&xp[i]);
if(flag)continue;
ans=0;
if(query(a))
printf("%d\n",ans);
else printf("I don't know.\n");
}
}
printf("\n");
}
return 0;
}

95
HDOJ/3236_autoAC.cpp Normal file
View File

@ -0,0 +1,95 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
using std::sort;
#define inf -2100000000
#define MAXV1 501
#define MAXV2 51
#define MAXN 301
struct T
{
int p,v,f;
bool operator < (const T a) const
{
return f>a.f;
}
} gift[MAXN];
int res[MAXV1][MAXV2][2];
inline int max (int a,int b,int c=0,int d=0)
{
return (a>b?a:b)>(c>d?c:d)?(a>b?a:b):(c>d?c:d);
}
int main()
{
int v1,v2,n,i,j,k,m,ts;
int cas=1;
while (scanf("%d%d%d",&v1,&v2,&n))
{
if (v1==0&&v2==0&&n==0) break;
memset(res,0,sizeof(res));
ts=0;
for (i=1; i<=n; ++i)
{
scanf("%d%d%d",&gift[i].p,&gift[i].v,&gift[i].f);
if (gift[i].f==1) ts+=gift[i].v;
}
sort(gift+1,gift+n+1);
for (k=1; k<=n; ++k)
{
if (gift[k].f==0) break;
for (i=v1; i>=0; --i)
{
for (j=v2; j>=0; --j)
{
if (i>=gift[k].p&&j>=gift[k].p)
{
res[i][j][1]=max(res[i][j][0]+gift[k].v,res[i][j-gift[k].p][1]+gift[k].v,res[i-gift[k].p][j][1]+gift[k].v);
res[i][j][0]=max(res[i][j-gift[k].p][0]+gift[k].v,res[i-gift[k].p][j][0]+gift[k].v);
}
else if (i>=gift[k].p)
{
res[i][j][1]=max(res[i][j][0]+gift[k].v,res[i-gift[k].p][j][1]+gift[k].v);
res[i][j][0]=res[i-gift[k].p][j][0]+gift[k].v;
}
else if (j>=gift[k].p)
{
res[i][j][1]=max(res[i][j][0]+gift[k].v,res[i][j-gift[k].p][1]+gift[k].v);
res[i][j][0]=res[i][j-gift[k].p][0]+gift[k].v;
}
else
{
res[i][j][1]=res[i][j][0]+gift[k].v;
}
}
}
}
m=0;
if (res[v1][v2][1]<ts&&res[v1][v2][0]<ts) m=-1;
if (m==0)
{
for (; k<=n; ++k)
{
for (i=v1; i>=0; --i)
{
for (j=v2; j>=0; --j)
{
if (res[i][j][0]>=ts) res[i][j][1]=max(res[i][j][1],res[i][j][0]+gift[k].v);
if (j>=gift[k].p&&res[i][j-gift[k].p][0]>=ts) res[i][j][0]=max(res[i][j-gift[k].p][0]+gift[k].v,res[i][j][0]);
if (i>=gift[k].p&&res[i-gift[k].p][j][0]>=ts) res[i][j][0]=max(res[i-gift[k].p][j][0]+gift[k].v,res[i][j][0]);
if (j>=gift[k].p&&res[i][j-gift[k].p][1]>=ts) res[i][j][1]=max(res[i][j-gift[k].p][1]+gift[k].v,res[i][j][1]);
if (i>=gift[k].p&&res[i-gift[k].p][j][1]>=ts) res[i][j][1]=max(res[i-gift[k].p][j][1]+gift[k].v,res[i][j][1]);
}
}
}
for (i=1; i<=v1; ++i)
{
for (j=1; j<=v2; ++j)
{
m=max(m,res[i][j][0],res[i][j][1]);
}
}
}
printf("Case %d: %d\n\n",cas++,m);
}
return 0;
}

93
HDOJ/3240_autoAC.cpp Normal file
View File

@ -0,0 +1,93 @@
#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;
const int N=10001;
typedef long long LL;
int su[N],num[N];
int n,m,sui;
void mul(LL &res,int k)
{
for(int i=0;i<sui;i++)
{
while(k%su[i]==0)
{
k/=su[i];
num[i]++;
}
}
res=(res*k)%m;
}
int ext_gcd(int a,int b,int &x,int &y)
{
int t,ret;
if(!b)
{
x=1;y=0;return a;
}
ret=ext_gcd(b,a%b,y,x);
y-=x*(a/b);
return ret;
}
void chu(LL &res,int k)
{
for(int i=0;i<sui;i++)
{
while(k%su[i]==0&&num[i]>0)
{
k/=su[i];
num[i]--;
}
}
if(k!=1)
{
int x,y,temp;
temp=ext_gcd(k,m,x,y);
x=(x%m+m)%m;
res=(res*x)%m;
}
}
int main()
{
int i,j,k,t;
while(scanf("%d%d",&n,&m)&&(n+m))
{
sui=0,t=m;
for(i=2;i*i<=t;i++)
if(t%i==0)
{
su[sui++]=i;
while(t%i==0)
t/=i;
}
if(t>1)
su[sui++]=t;
memset(num,0,sizeof(num));
LL res=1,sum=1,l;
for(i=2;i<=n;i++)
{
mul(res,4*i-2);
chu(res,i+1);
l=res;
for(j=0;j<sui;j++)
for(k=0;k<num[j];k++)
l=l*su[j]%m;
sum=(sum+l)%m;
}
printf("%lld\n",sum);
}
return 0;
}

92
HDOJ/3242_autoAC.cpp Normal file
View File

@ -0,0 +1,92 @@
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int N = 10005;
char str[N];
struct node
{
char s[81];
int len;
int flag;
}list1[N],list2[N];
int n1,n2;
int op;
void solve()
{
int len = strlen(str);
memset(list1,0,sizeof(list1));
memset(list2,0,sizeof(list2));
n1 = n2 = 0;
int i,j;
for(i = 1;i < len;)
{
while(str[i] != ',' && str[i] != ']')
list1[n1].s[list1[n1].len ++] = str[i ++];
list1[n1].s[list1[n1].len] = '\0';
n1 ++;
if(str[i] == ']')
break;
i ++;
}
i +=2;
if(str[i] == '+')
op = 1;
else
op = 0;
i += 4;
for(;i < len - 1;)
{
while(str[i] != ',' && str[i] != ']')
list2[n2].s[list2[n2].len ++] = str[i ++];
list2[n2].s[list2[n2].len] = '\0';
n2 ++;
i ++;
}
if(op == 1)
{
printf("[%s",list1[0].s);
for(i = 1;i < n1;i ++)
printf(",%s",list1[i].s);
for(i = 0;i < n2;i ++)
printf(",%s",list2[i].s);
printf("]\n");
return;
}
for(i = 0;i < n2;i ++)
{
for(j = 0;j < n1;j ++)
{
if(list1[j].flag)
continue;
if(strcmp(list1[j].s,list2[i].s) == 0)
{
list1[j].flag = 1;
break;
}
}
}
printf("[");
for(j = n1 - 1;j >= 0;j --)
if(list1[j].flag == 0)
break;
for(i = 0;i < j;i ++)
{
if(list1[i].flag == 0)
printf("%s,",list1[i].s);
}
printf("%s]\n",list1[j].s);
}
int main()
{
while(gets(str))
{
if(str[0] == '.')
break;
solve();
}
return 0;
}

95
HDOJ/3244_autoAC.cpp Normal file
View File

@ -0,0 +1,95 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int n,m;
const int maxn = 110;
const int inf = 0x3f3f3f3f;
struct node
{
int s1,s2;
int p1,p2;
int x,y;
}t[maxn];
int cal_r()
{
int ret=inf;
for(int i=1;i<=n;i++)
{
if(t[i].s1*1.0/t[i].p1 >= t[i].s2*1.0/t[i].p2)
{
int tmp = (m/t[i].p1*t[i].s1)+t[i].y;
int p = tmp/t[i].x;
ret=min(ret,p);
}
else
{
int tmp = (m/t[i].p2*t[i].s2)+t[i].y;
int p = tmp/t[i].x;
ret=min(ret,p);
}
}
return ret;
}
int dp[6000000+20];
int cal_need(int i,int need)
{
int w[2],c[2];
c[0]=t[i].p1;
w[0]=t[i].s1;
c[1]=t[i].p2;
w[1]=t[i].s2;
int V=need+t[i].s2;
for(int i=1;i<=V;i++)
dp[i]=inf;
dp[0]=0;
for(int i=0;i<2;i++)
{
for(int v=w[i];v<=V;v++)
dp[v]=min(dp[v],dp[v-w[i]]+c[i]);
}
int ret = inf;
for(int i=need;i<=V;i++)
ret = min(ret,dp[i]);
return ret;
}
bool judge(int k)
{
int s=0;
for(int i=1;i<=n;i++)
{
int need = t[i].x*k-t[i].y;
if(need<=0)
continue;
int tmp = cal_need(i,need);
s+=tmp;
if(s>m)
return false;
}
return true;
}
int main()
{
while(~scanf("%d%d",&n,&m) && n+m)
{
for(int i=1;i<=n;i++)
scanf("%d%d%d%d%d%d",&t[i].x,&t[i].y,&t[i].s1,&t[i].p1,&t[i].s2,&t[i].p2);
int r=cal_r();
int l=0;
int ans = 0;
while(l<=r)
{
int mid=(l+r)>>1;
if(judge(mid))
{
ans=mid;
l=mid+1;
}
else
r=mid-1;
}
printf("%d\n",ans);
}
return 0;
}

93
HDOJ/3245_autoAC.cpp Normal file
View File

@ -0,0 +1,93 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define min(a,b) a>b?b:a
#pragma comment(linker,"/STACK:1000000000,1000000000")
const int maxn = 10010;
const int INF = 1000000000;
int dp[maxn][110];
int sum[maxn],sumd[maxn];
int head[maxn];
struct Edge{
int v,next;
}edge[2*maxn];int N,L,E;
void add_edge(int a,int b)
{
edge[E].v=b;
edge[E].next=head[a];
head[a]=E++;
}
void dfs(int u,int fa)
{
int i,j,k,v;
sum[u]=1;sumd[u]=0;
fill(dp[u],dp[u]+L+1,INF);
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(v==fa) continue;
dfs(v,u);
sumd[u]+=sumd[v]+sum[v];
sum[u]+=sum[v];
}
dp[u][0]=sumd[u];
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(v==fa) continue;
for(j=1;j<=L;j++)
dp[u][j]=min(dp[u][j],dp[v][j-1]+sumd[u]-sumd[v]-sum[v]);
}
}
int ans[maxn];
void solve(int u,int fa)
{
int i,a,v;
if(u==0) ans[u]=sumd[0];
else ans[u]=N-sum[u]+sumd[fa]-sum[u];
int tmp[110];
fill(tmp,tmp+L+1,INF);
int mi=INF;
for( i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(v==fa) continue;
for( a=0;a<=L-2;a++)
mi=min(mi,tmp[L-2-a]+dp[v][a]-sumd[v]-sum[v]);
for( a=0;a<=L-1;a++)
{
tmp[a]=min(tmp[a],dp[v][a]-sumd[v]-sum[v]);
if(a) tmp[a]=min(tmp[a],tmp[a-1]);
}
}
mi=min(mi,tmp[L-1]);
sumd[u]=ans[u];
ans[u]+=mi;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(v==fa) continue;
solve(v,u);
}
}
int main()
{
int i,j,k,a,b;
while(scanf("%d%d",&N,&L),(N||L))
{
if(N==1) {puts("0");continue;}
E=0;
memset(head,-1,sizeof(head));
for(i=0;i<N-1;i++)
{
scanf("%d%d",&a,&b);
add_edge(a,b);
add_edge(b,a);
}
dfs(0,0);
solve(0,0);
printf("%d\n",*min_element(ans,ans+N));
}
return 0;
}

112
HDOJ/3247_autoAC.cpp Normal file
View File

@ -0,0 +1,112 @@
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define eps 1e-10
#define N 100005
#define inf 1<<20
#define zero(a) (fabs(a)<eps)
#define lson (step<<1)
#define rson (step<<1|1)
using namespace std;
struct Trie{
int next[2],fail;
int virus,source;
void Init(){next[0]=next[1]=0;fail=source=virus=0;}
}tree[N];
int n,m;
int tot,cnt;
int path[205][205];
int pos[205];
int dp[1<<10][205];
char str[50005];
void Insert(char *s,int len,int kind){
int p=0;
for(int i=0;i<len;i++){
if(tree[p].next[s[i]-'0']==0){
tree[++tot].Init();
tree[p].next[s[i]-'0']=tot;
}
p=tree[p].next[s[i]-'0'];
}
if(kind>=0) tree[p].source=1<<kind;
else tree[p].virus=1;
}
void Bulid_fail(){
int que[N];
int head=0,tail=0;
que[tail++]=0;
while(head<tail){
int p=que[head++];
for(int i=0;i<2;i++){
if(tree[p].next[i]==0) tree[p].next[i]=tree[tree[p].fail].next[i];
else{
int q=tree[p].next[i];
if(p) tree[q].fail=tree[tree[p].fail].next[i];
tree[q].source|=tree[tree[q].fail].source;
tree[q].virus|=tree[tree[q].fail].virus;
que[tail++]=q;
}
}
}
}
void Bfs(int u){
int que[N];
int head=0,tail=0;
int dist[N];
memset(dist,-1,sizeof(dist));
que[tail++]=pos[u];
dist[pos[u]]=0;
while(head<tail){
int p=que[head++];
for(int i=0;i<2;i++){
int q=tree[p].next[i];
if(dist[q]<0&&!tree[q].virus){
dist[q]=dist[p]+1;
que[tail++]=q;
}
}
}
for(int i=0;i<cnt;i++)
path[u][i]=dist[pos[i]];
}
int DP(){
memset(dp,-1,sizeof(dp));
dp[0][0]=0;
for(int i=0;i<(1<<n);i++){
for(int j=0;j<cnt;j++){
if(dp[i][j]<0) continue;
for(int k=0;k<cnt;k++){
if(path[j][k]<0) continue;
if(dp[i|tree[pos[k]].source][k]==-1) dp[i|tree[pos[k]].source][k]=dp[i][j]+path[j][k];
else dp[i|tree[pos[k]].source][k]=min(dp[i|tree[pos[k]].source][k],dp[i][j]+path[j][k]);
}
}
}
int ans=inf;
for(int i=0;i<cnt;i++)
if(dp[(1<<n)-1][i]!=-1) ans=min(ans,dp[(1<<n)-1][i]);
return ans==inf?-1:ans;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF&&n+m){
tree[0].Init();cnt=1;tot=0;
for(int i=0;i<n;i++){
scanf("%s",str);
Insert(str,strlen(str),i);
}
for(int i=0;i<m;i++){
scanf("%s",str);
Insert(str,strlen(str),-1);
}
Bulid_fail();
for(int i=1;i<=tot;i++)
if(tree[i].source)
pos[cnt++]=i;
for(int i=0;i<cnt;i++)
Bfs(i);
printf("%d\n",DP());
}
return 0;
}

24
HDOJ/3248_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include<stdio.h>
int main()
{
int n,m,n1;
while(scanf("%d",&n)!=EOF&&n!=-1)
{
m=n;
n/=30;
n=15-n;
if(n>=12)
n-=12;
if(m%30==0)
printf("Exactly %d o'clock\n",n);
else
{
if(n==0)
n1=11;
else
n1=n-1;
printf("Between %d o'clock and %d o'clock\n",n1,n);
}
}
return 0;
}

46
HDOJ/3249_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define inf 0x3f3f3f3f
struct node
{
int l;
int p;
}a[10005];
bool cmp(node b,node c)
{
return b.p<c.p;
}
int dp[10005];
int search(int k)
{
for(int i=1;i<=k;i++)
{
if(a[k].p<dp[i])
return i-1;
}
}
int main()
{
int n;
while(scanf("%d",&n)&&n)
{
for(int i=1;i<=n;i++)
scanf("%d%d",&a[i].l,&a[i].p);
sort(a+1,a+1+n,cmp);
memset(dp+1,0x3f,sizeof(int)*(n+1));
dp[0]=-inf;
int top=0;
int ans=0;
for(int i=1;i<=n;i++)
{
top=search(i);
dp[top+1]=min(dp[top+1],a[i].p>=dp[top]+a[i].l?a[i].p:dp[top]+a[i].l);
if(top+1>ans)
ans=top+1;
}
printf("%d\n",ans);
}
return 0;
}

181
HDOJ/3250_autoAC.cpp Normal file
View File

@ -0,0 +1,181 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const int MAXN=10;
int hei[10];
int mp[300];
int a[30][5];
int p[100][30];
char s[100];
int first,anss;
int Cnt[10]={2,4,4,1,2,4,2};
int Dx[7][4]=
{
0,0,0,0,
0,-1,-1,-1,
0,-1,-1,-1,
0,0,-1,-1,
0,0,-1,-1,
0,-1,-1,-1,
0,0,-1,-1
};
int Dy[7][4]=
{
0,1,2,3,
0,0,1,2,
0,-2,-1,0,
0,1,0,1,
0,1,-1,0,
0,-1,0,1,
0,1,1,2
};
int rotx(int x,int y,int rot)
{
if (rot==0) return x;
if (rot==1) return y;
if (rot==2) return -x;
if (rot==3) return -y;
}
int roty(int x,int y,int rot)
{
if (rot==0) return y;
if (rot==1) return -x;
if (rot==2) return -y;
if (rot==3) return x;
}
bool set(int now,int id,int pos,int rot)
{
for (int i=0; i<4; i++)
{
int dx1=Dx[id][i];
int dy1=Dy[id][i];
int dx=rotx(dx1,dy1,rot);
int dy=roty(dx1,dy1,rot);
if (pos+dy<0||pos+dy>=5) return false;
}
int target=0;
for (int ii=MAXN; ii>=0; ii--)
{
for (int i=0; i<4; i++)
{
int dx1=Dx[id][i];
int dy1=Dy[id][i];
int dx=rotx(dx1,dy1,rot);
int dy=roty(dx1,dy1,rot);
if (ii+dx<0)
{
target=ii+1;
break;
}
if (a[ii+dx][pos+dy]==1)
{
target=ii+1;
break;
}
}
if (target!=0) break;
}
for (int i=0; i<4; i++)
{
int dx1=Dx[id][i];
int dy1=Dy[id][i];
int dx=rotx(dx1,dy1,rot);
int dy=roty(dx1,dy1,rot);
//printf("%d %d\n",pos+dx,target+dy);
a[target+dx][pos+dy]=1;
}
p[now][0]=target;
p[now][1]=0;
for (int i=MAXN; i>=0; i--)
{
bool find=true;
for (int j=0; j<5; j++)
if (a[i][j]!=1) find=false;
if (find)
{
p[now][p[now][1]+2]=i;
for (int j=i; j<MAXN; j++)
for (int k=0; k<5; k++)
a[j][k]=a[j+1][k];
for (int k=0; k<5; k++)
a[MAXN][k]=0;
p[now][1]++;
}
}
return true;
}
void remove(int now,int id,int pos,int rot)
{
for (int ii=p[now][1]-1; ii>=0; ii--)
{
int line=p[now][ii+2];
for (int i=MAXN; i>line; i--)
for (int j=0; j<5; j++)
a[i][j]=a[i-1][j];
for (int j=0; j<5; j++)
a[line][j]=1;
}
int target=p[now][0];
for (int i=0; i<4; i++)
{
int dx1=Dx[id][i];
int dy1=Dy[id][i];
int dx=rotx(dx1,dy1,rot);
int dy=roty(dx1,dy1,rot);
a[target+dx][pos+dy]=0;
}
}
void dfs(int now,int end)
{
if (now==end)
{
int ans=0;
for (int i=0; i<5; i++)
{
int max=0;
for (int j=MAXN; j>=0; j--)
if (a[j][i]==1)
{
max=(j+1);
break;
}
ans+=max*max;
}
if (first||ans<anss)
anss=ans;
first=0;
return;
}
for (int i=0; i<5*Cnt[mp[s[now]]]; i++)
{
int x=i/Cnt[mp[s[now]]];
int rot=i%Cnt[mp[s[now]]];
if (set(now,mp[s[now]],x,rot))
{
dfs(now+1,end);
remove(now,mp[s[now]],x,rot);
}
}
}
int main()
{
mp['I']=0;
mp['J']=1;
mp['L']=2;
mp['O']=3;
mp['S']=4;
mp['T']=5;
mp['Z']=6;
int t;
scanf("%d",&t);
for (int ii=1; ii<=t; ii++)
{
scanf("%s",s);
memset(a,0,sizeof(a));
first=1;
int len=strlen(s);
dfs(0,len);
printf("Case %d: %d\n",ii,anss);
}
return 0;
}

101
HDOJ/3251_autoAC.cpp Normal file
View File

@ -0,0 +1,101 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAXD 1010
#define MAXM 202010
#define INF 0x3f3f3f3f
int N, M, F, first[MAXD], e, next[MAXM], v[MAXM], flow[MAXM], id[MAXM], MAX;
int S, T, d[MAXD], q[MAXD], work[MAXD], Q;
void add(int x, int y, int z, int i)
{
v[e] = y, flow[e] = z, id[e] = i;
next[e] = first[x], first[x] = e ++;
}
void init()
{
int i, x, y, z;
scanf("%d%d%d", &N, &M, &F);
S = 1, T = N + 1;
memset(first, -1, sizeof(first[0]) * (T + 1)), e = 0;
for(i = 1; i <= M; i ++)
{
scanf("%d%d%d", &x, &y, &z);
add(x, y, z, i), add(y, x, 0, i);
}
MAX = 0;
for(i = 0; i < F; i ++)
{
scanf("%d%d", &x, &y), MAX += y;
add(x, T, y, 0), add(T, x, 0, 0);
}
}
int bfs()
{
int i, j, rear = 0;
memset(d, -1, sizeof(d[0]) * (T + 1));
d[S] = 0, q[rear ++] = S;
for(i = 0; i < rear; i ++)
for(j = first[q[i]]; j != -1; j = next[j])
if(flow[j] && d[v[j]] == -1)
{
d[v[j]] = d[q[i]] + 1, q[rear ++] = v[j];
if(v[j] == T) return 1;
}
return 0;
}
int dfs(int cur, int a)
{
if(cur == T) return a;
for(int &i = work[cur]; i != -1; i = next[i])
if(flow[i] && d[v[i]] == d[cur] + 1)
if(int t = dfs(v[i], std::min(a, flow[i])))
{
flow[i] -= t, flow[i ^ 1] += t;
return t;
}
return 0;
}
int dinic()
{
int ans = 0, t;
while(bfs())
{
memcpy(work, first, sizeof(first[0]) * (T + 1));
while(t = dfs(S, INF)) ans += t;
}
return ans;
}
void DFS(int cur)
{
int i;
d[cur] = 1;
for(i = first[cur]; i != -1; i = next[i])
if(flow[i] && !d[v[i]])
DFS(v[i]);
}
void solve()
{
int i, ans = dinic(), n = 0;
printf("%d\n", MAX - ans);
memset(d, 0, sizeof(d[0]) * (T + 1));
DFS(S);
for(i = 0; i < M; i ++)
if(d[v[i << 1 | 1]] && !d[v[i << 1]])
q[n ++] = id[i << 1];
std::sort(q, q + n);
printf("%d", n);
for(i = 0; i < n; i ++) printf(" %d", q[i]);
printf("\n");
}
int main()
{
int t, tt;
scanf("%d", &t);
for(tt = 1; tt <= t; tt ++)
{
init();
printf("Case %d: ", tt);
solve();
}
return 0;
}

196
HDOJ/3252_autoAC.cpp Normal file
View File

@ -0,0 +1,196 @@
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <vector>
#include <cstring>
#include <set>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAX=1005,INF=1<<30;
const string END="#CODE-END";
int len;
map<string,int> mymap;
map<string,int>::iterator ite;
vector<string> A,B;
int cnt;
int f[MAX][MAX];
int ma[MAX],mb[MAX];
vector<int> a,b;
int Test;
void read(vector<string> &t,vector<int> &arr){
string s;
while(getline(cin,s)){
if(s==END)
break;
ite=mymap.find(s);
if(ite==mymap.end())
mymap[s]=len++;
t.push_back(s);
arr.push_back(mymap[s]);
}
return ;
}
void createM(){
int n1=(int)a.size()-1,n2=(int)b.size()-1;
memset(f,0,sizeof(f));
for(int i=n1;i>=1;i--){
for(int j=n2;j>=1;j--){
if(a[i]==b[j])
f[i][j]=f[i+1][j+1]+1;
else
f[i][j]=max(f[i+1][j],f[i][j+1]);
}
}
int L=f[1][1];
cnt=f[1][1];
int x = -1,c=0;
for(int i = 1; i<=n1; i++)
{
for(int j = 1; j<= n2; j++)
{
if(a[i] == b[j] && f[i][j] == L && j > x)
{
x = j;
L--;
ma[c]=i-1;
mb[c]=j-1;
x = j;
c++;
break;
}
}
}
return ;
}
struct DD{
bool isM;
int b,e;
};
DD DA[MAX*3],DB[MAX*3];
void cut(){
int front=0,rear=0;
int len=0;
for(int i=0;i<2*cnt;i++,len++){
rear=ma[len];
if(front<rear){
DA[i].isM=0;
DA[i].b=front;
DA[i].e=rear-1;
}
else {
DA[i].isM=0;
DA[i].b=-1;
DA[i].e=-1;
}
++i;
DA[i].isM=1;
DA[i].b=ma[len];
DA[i].e=ma[len];
front=ma[len]+1;
}
rear=(int)A.size();
if(front<rear){
DA[cnt*2].isM=0;
DA[cnt*2].b=front;
DA[cnt*2].e=rear-1;
}
else {
DA[cnt*2].isM=0;
DA[cnt*2].b=-1;
DA[cnt*2].e=-1;
}
front=rear=0;
len=0;
for(int i=0;i<2*cnt;i++,len++){
rear=mb[len];
if(front<rear){
DB[i].isM=0;
DB[i].b=front;
DB[i].e=rear-1;
}
else {
DB[i].isM=0;
DB[i].b=-1;
DB[i].e=-1;
}
++i;
DB[i].isM=1;
DB[i].b=mb[len];
DB[i].e=mb[len];
front=mb[len]+1;
}
rear=(int)B.size();
if(front<rear){
DB[cnt*2].isM=0;
DB[cnt*2].b=front;
DB[cnt*2].e=rear-1;
}
else {
DB[cnt*2].isM=0;
DB[cnt*2].b=-1;
DB[cnt*2].e=-1;
}
return ;
}
void output(){
for(int i=0;i<2*cnt+1;i++){
if(DA[i].isM)
cout<<A[DA[i].b]<<endl;
else if(DA[i].b==-1&&DA[i].e==-1){
if(DB[i].b==-1&&DB[i].e==-1)
continue;
else{
for(int j=DB[i].b;j<=DB[i].e;j++){
cout<<B[j]<<endl;
}
}
}
else if(DB[i].b==-1&&DB[i].e==-1){
if(DA[i].b==-1&&DA[i].e==-1)
continue;
else{
for(int j=DA[i].b;j<=DA[i].e;j++){
cout<<A[j]<<endl;
}
}
}
else {
cout<<"//**** CONFLICT STARTS ****//"<<endl
<<"//code in A:"<<endl;
for(int j=DA[i].b;j<=DA[i].e;j++){
cout<<A[j]<<endl;
}
cout<<"//code in B:"<<endl;
for(int j=DB[i].b;j<=DB[i].e;j++){
cout<<B[j]<<endl;
}
cout<<"//***** CONFLICT ENDS *****//"<<endl;
}
}
return ;
}
int main()
{
int test=1;
cin>>Test;
cin.ignore();
while(Test--){
cout<<"Case "<<test++<<":"<<endl;
len=0;
mymap.clear();
A.clear();B.clear();
a.clear();b.clear();
a.push_back(-1);b.push_back(-1);
read(A,a);
read(B,b);
createM();
cut();
output();
cout<<endl;
}
return 0;
}

110
HDOJ/3253_autoAC.cpp Normal file
View File

@ -0,0 +1,110 @@
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
int n,x[210],y[210],d[210],rx[210],ry[210];
struct heap
{
int t1,t2;
bool cmp(const int &u,const int &v)
{ return t1*x[u]+t2*y[u]>t1*x[v]+t2*y[v]; }
int n,h[210],e[210];
void del(int i)
{
e[h[i]]=0;
if (i==n)
{ n--; return; }
h[i]=h[n--]; e[h[i]]=i;
down(i); up(i);
}
void build(void)
{
for(int i=(n>>1);i>=1;i--)
down(i);
}
void up(int i)
{
int t,j;
while(i>1)
{
j=(i>>1);
if (cmp(h[i],h[j]))
{
t=h[i]; h[i]=h[j]; h[j]=t;
e[h[i]]=i; e[h[j]]=j;
}
else break;
i=j;
}
}
void down(int i)
{
int t,j;
while((j=(i<<1))<=n)
{
if (j<n&&cmp(h[j+1],h[j])) j++;
if (cmp(h[j],h[i]))
{
t=h[i]; h[i]=h[j]; h[j]=t;
e[h[i]]=i; e[h[j]]=j;
}
else break;
i=j;
}
}
}h1,h2,h3,h4;
int main(void)
{
int u,c,i,l1,r1,l2,r2;
scanf("%d",&c);
for(u=1;u<=c;u++)
{
scanf("%d",&n);
h1.n=h2.n=h3.n=h4.n=n;
for(i=1;i<=n;i++)
{
scanf("%d%d%d",x+i,y+i,d+i);
h1.h[i]=h1.e[i]=i;
h2.h[i]=h2.e[i]=i;
h3.h[i]=h3.e[i]=i;
h4.h[i]=h4.e[i]=i;
}
h1.t1=1; h1.t2=1;
h2.t1=-1; h2.t2=1;
h3.t1=-1; h3.t2=-1;
h4.t1=1; h4.t2=-1;
h1.build();
h2.build();
h3.build();
h4.build();
for(i=n;i>=1;i--)
{
l1=x[h1.h[1]]+y[h1.h[1]]-d[i];
r1=x[h3.h[1]]+y[h3.h[1]]+d[i];
l2=-x[h2.h[1]]+y[h2.h[1]]-d[i];
r2=-x[h4.h[1]]+y[h4.h[1]]+d[i];
if (i<n)
{
r1=min(r1,rx[i+1]+ry[i+1]+d[i+1]-d[i]);
l1=max(l1,rx[i+1]+ry[i+1]-d[i+1]+d[i]);
r2=min(r2,-rx[i+1]+ry[i+1]+d[i+1]-d[i]);
l2=max(l2,-rx[i+1]+ry[i+1]-d[i+1]+d[i]);
}
if ((l1+l2)%2!=0)
if (l1<r1)
l1++;
else l2++;
rx[i]=(l1-l2)/2;
ry[i]=(l1+l2)/2;
h1.del(h1.e[i]);
h2.del(h2.e[i]);
h3.del(h3.e[i]);
h4.del(h4.e[i]);
}
printf("Case %d:\n",u);
for(i=1;i<=n;i++)
printf("%d %d\n",rx[i],ry[i]);
}
return 0;
}

143
HDOJ/3255_autoAC.cpp Normal file
View File

@ -0,0 +1,143 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX 150105
#define lch(i) ((i)<<1)
#define rch(i) ((i)<<1|1)
struct rec{
__int64 x1,y1,x2,y2,v;
};
struct segment{
__int64 l,r,h,val;
};
struct tree{
int l,r,cnt,sum;
int mid()
{ return (l+r)>>1; }
};
typedef struct rec rec;
typedef struct segment segment;
typedef struct tree tree;
rec rr[MAX];
segment ss[MAX];
tree tt[MAX];
__int64 pos[MAX];
__int64 v[MAX];
int N,M;
int cmp_rec(rec a ,rec b)
{ return a.v < b.v; }
int cmp_segment(segment a , segment b)
{ return a.h < b.h; }
void input()
{
scanf("%d%d",&N,&M);
for(int i=1; i<=M; i++) scanf("%I64d",&v[i]);
for(int i=0; i<N; i++)
{
int k;
scanf("%I64d%I64d%I64d%I64d%d",&rr[i].x1, &rr[i].y1, &rr[i].x2, &rr[i].y2, &k);
rr[i].v=v[k];
}
}
void build(int l ,int r ,int rt)
{
tt[rt].l=l; tt[rt].r=r;
tt[rt].cnt=0; tt[rt].sum=0;
if(l==r) return ;
int mid=tt[rt].mid();
build(l, mid, lch(rt));
build(mid+1, r, rch(rt));
}
int binary(int key ,int low, int high)
{
int mid;
while(low<=high)
{
mid=(low+high)>>1;
if(pos[mid]==key) return mid;
else if(key < pos[mid]) high=mid-1;
else low=mid+1;
}
return -1;
}
void cal(int rt)
{
if(tt[rt].cnt)
tt[rt].sum=pos[tt[rt].r+1]-pos[tt[rt].l];
else if(tt[rt].l==tt[rt].r)
tt[rt].sum=0;
else
tt[rt].sum=tt[lch(rt)].sum + tt[rch(rt)].sum;
}
void updata(int l , int r , int val ,int rt)
{
if(tt[rt].l==l && tt[rt].r==r)
{
tt[rt].cnt += val ;
cal(rt);
return ;
}
int mid=tt[rt].mid();
if(r<=mid)
updata(l,r,val,lch(rt));
else if(l>mid)
updata(l,r,val,rch(rt));
else
{
updata(l,mid,val,lch(rt));
updata(mid+1,r,val,rch(rt));
}
cal(rt);
}
__int64 solve(int nn)
{
int m=0;
for(int i=nn; i<N; i++)
{
ss[m].l=rr[i].x1; ss[m].r=rr[i].x2; ss[m].h=rr[i].y1; ss[m].val=1;
ss[m+1].l=rr[i].x1; ss[m+1].r=rr[i].x2; ss[m+1].h=rr[i].y2; ss[m+1].val=-1;
pos[m]=rr[i].x1; pos[m+1]=rr[i].x2;
m+=2;
}
sort(pos,pos+m);
sort(ss,ss+m,cmp_segment);
int mm=1;
for(int i=1; i<m; i++)
if(pos[i]!=pos[i-1])
pos[mm++]=pos[i];
build(0,mm-1,1);
__int64 ans=0,res=0;
for(int i=0; i<m-1; i++)
{
int l=binary(ss[i].l,0,mm-1);
int r=binary(ss[i].r,0,mm-1)-1;
if(l<=r) updata(l,r,ss[i].val,1);
res += tt[1].sum * (ss[i+1].h-ss[i].h);
}
return res;
}
int main()
{
int T;
scanf("%d",&T);
for(int t=1; t<=T; t++)
{
input();
sort(rr,rr+N,cmp_rec);
int s[MAX],kk;
s[1]=0; kk=1;
for(int i=1; i<N; i++)
if(rr[i].v!=rr[i-1].v)
s[++kk]=i;
__int64 ans=0,res;
for(int k=1; k<=kk; k++)
{
res=solve(s[k]);
if(k==1) ans += res * rr[s[k]].v;
else ans += res * ( rr[s[k]].v - rr[s[k-1]].v );
}
printf("Case %d: %I64d\n",t,ans);
}
return 0;
}

130
HDOJ/3256_autoAC.cpp Normal file
View File

@ -0,0 +1,130 @@
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
const int M=10010;
const int N=510;
char s[110];
struct Line
{
__int64 x1, y1, x2, y2;
} ll[N], pos;
int n, flag[M*2], ans;
void input(__int64 &a)
{
scanf("%s", &s);
int i, j, len=strlen(s);
a = 0;
i = 0;
if(s[0]=='-')
i++;
for(; i<len && s[i]!='.'; i++)
{
a *= 10;
a += s[i]-'0';
}
for(i++, j=0; i<len; i++, j++)
{
a *= 10;
a += s[i]-'0';
}
while(j<4)
{
a *= 10;
j++;
}
if(s[0]=='-')
a = -a;
}
void cal(int i, Line li)
{
if(li.x1==li.x2)
{
if(li.x1==i*10000)
{
if(li.y1>li.y2)
swap(li.y1, li.y2);
if(li.y1<=0)
li.y1 = li.y1/10000;
else
li.y1 = li.y1/10000+1;
if(li.y2>=0) //...
li.y2 /= 10000;
else
li.y2 = li.y2/10000-1;
for(int j=li.y1; j<=li.y2; j++)
{
if(flag[j+M]!=i)
{
flag[j+M] = i;
ans++;
}
}
}
return;
}
if(!(li.x1<=i*10000 && i*10000<=li.x2) )
return;
if((i*10000-li.x1)*(li.y2-li.y1)%(li.x2-li.x1)==0)
{
int j = (i*10000-li.x1)*(li.y2-li.y1)/(li.x2-li.x1)+li.y1;
if(j%10000==0)
{
j /= 10000;
if(flag[j+M]!=i)
{
flag[j+M] = i;
ans++;
}
}
}
}
int main()
{
int i, j, cas, cas1;
scanf("%d", &cas);
for(cas1=1; cas1<=cas; cas1++)
{
scanf("%d", &n);
pos.x1 = M*M*10;
pos.x2 = -M*M*10;
for(i=0; i<n; i++)
{
input(ll[i].x1);
input(ll[i].y1);
input(ll[i].x2);
input(ll[i].y2);
if(ll[i].x1 > ll[i].x2)
{
swap(ll[i].x1, ll[i].x2);
swap(ll[i].y1, ll[i].y2);
}
if(ll[i].x1<pos.x1)
pos.x1 = ll[i].x1;
if(ll[i].x2<pos.x1)
pos.x1 = ll[i].x2;
if(ll[i].x1>pos.x2)
pos.x2 = ll[i].x1;
if(ll[i].x2>pos.x2)
pos.x2 = ll[i].x2;
}
for(i=0; i<M*2; i++)
flag[i] = -1000000;
ans = 0;
pos.x1 /= 10000;
pos.x1--;
pos.x2 /= 10000;
pos.x2++;
for(i=pos.x1; i<=pos.x2; i++)
{
for(j=0; j<n; j++)
{
cal(i, ll[j]);
}
}
printf("Case %d: %d\n", cas1, ans);
}
return 0;
}

77
HDOJ/3257_autoAC.cpp Normal file
View File

@ -0,0 +1,77 @@
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
string str[17]={"0000","0001","0010","0011","0100","0101","0110","0111",
"1000","1001","1010","1011","1100","1101","1110","1111"};
char map[8][500];
string trans(string ch)
{
string s="";
for(int i=0;i<2;i++)
{
if(ch[i]>='A'&&ch[i]<='F')
s+=str[ch[i]-'A'+10];
else
s+=str[ch[i]-'0'];
}
return s;
}
int main()
{
string ch,str;
int t,n,i,j,k,p;
scanf("%d",&t);
for(int Case=1;Case<=t;Case++)
{
scanf("%d",&n);
int minx=10,maxn=-1;
p=0;
for(i=0;i<n;i++)
{
for(k=0;k<5;k++)
{
str="";
ch="";
cin>>ch;
ch=trans(ch);
for(j=0;j<=7;j++)
{
if(ch[j]=='1')
map[j][p]='#';
else map[j][p]=' ';
}
p++;
}
if(i!=n-1)
{
for(j=0;j<=7;j++)
map[j][p]=' ';
p++;
}
}
printf("Case %d:\n\n",Case);
bool flag=0,tag=0;
for(i=7;i>=0;i--)
{
for(j=0;j<p;j++)
if(map[i][j]=='#') {flag=1;break;}
if(flag) break;
}
for(k=0;k<=7;k++)
{
for(j=0;j<p;j++)
if(map[k][j]=='#') {tag=1;break;}
if(tag) break;
}
for(;i>=k;i--)
{
for(j=0;j<p;j++)
printf("%c",map[i][j]);
printf("\n");
}
printf("\n");
}
return 0;
}

70
HDOJ/3259_autoAC.cpp Normal file
View File

@ -0,0 +1,70 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MaxN = 3005;
char s1[MaxN],s2[MaxN];
typedef long long LL;
LL dp[2][MaxN];
int main()
{
int T,cas=0;scanf("%d",&T);
while(T--)
{
scanf("%s%s",s1,s2+1);
int n = strlen(s2+1), m = strlen(s1);
bool ok = 1;
for(int i = 1; i <= n; i++)if(s2[i]!='*')ok=0;
if(ok)
{
printf("Case %d: %d\n",++cas,m*(m+1)/2);
continue;
}
memset(dp, 0, sizeof(dp));
for(int i = 0; i <= m; i++)dp[0][i]= 1;
ok = 1;
bool first = 1;
for(int i = 1; i <= n; i++)
{
int p = i&1;
int q = 1-p;
if(s2[i]=='*')
{
dp[p][0] = dp[q][0];
if(first)
for(int j = 1; j <= m; j++)
dp[p][j] = dp[p][j-1]+dp[q][j];
else
{
LL mx = dp[q][0];
for(int j = 1; j <= m; j++)
{
mx = max(mx, dp[q][j]);
dp[p][j] = mx;
}
}
first = 0;
}
else if(s2[i]=='?')
{
dp[p][0] = 0;
for(int j = 1; j <= m; j++)
dp[p][j] = dp[q][j-1];
}
else
{
dp[p][0] = 0;
for(int j = 1; j <= m; j++)
{
if(s1[j-1]==s2[i])dp[p][j] = dp[q][j-1];
else dp[p][j] = 0;
}
}
}
LL ans = 0;
for(int i = 1; i <= m; i++)
ans += dp[n&1][i];
printf("Case %d: %I64d\n",++cas,ans);
}
return 0;
}

57
HDOJ/3260_autoAC.cpp Normal file
View File

@ -0,0 +1,57 @@
#include<cstdio>
#include<cstring>
using namespace std;
#define inf (-((1<<30)-1))
#define max(a,b) ((a)>(b)?(a):(b))
char opt[105][1005];
int value[105][1005];
int dp[1005];
int n,m,k;
void dfs(int x,int y,int v,int ki,int money,int pre)
{
if(x==1&&y!=pre)
{
dp[y]=max(dp[y],dp[pre]+money);
return;
}
if(ki>=k) return;
if(y>=m) return;
money+=(opt[x][y]=='$'?value[x][y]:0);
int vi=(x!=1?(opt[x][y]!='$'?value[x][y]:0):0);
for(int i=-1;i<=1;++i)
{
int pos=x+v+vi+i;
if(pos<=1)
dfs(1,y+1,0,0,money,pre);
else
{
if(pos>n) pos=n;
dfs(pos,y+1,v+vi+i,ki+1,money,pre);
}
}
}
int main()
{
char c;
for(; ~scanf("%d%d%d",&n,&m,&k);)
{
if(n+m+k==0) break;
for(int i=1; i<=n; ++i)
{
for(int j=1; j<=m; ++j)
{
for(;c=getchar();)
if(c=='v'||c=='$') break;
opt[i][j]=c;
scanf("%d",&value[i][j]);
}
}
dp[1]=0;
for(int i=2;i<=m;++i) dp[i]=inf;
for(int i=1;i<m;++i)
if(dp[i]!=inf)
dfs(1,i,0,0,0,i);
printf("%d\n",dp[m]+(opt[1][m]=='$'?value[1][m]:0));
}
return 0;
}

89
HDOJ/3262_autoAC.cpp Normal file
View File

@ -0,0 +1,89 @@
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=33;
struct rec
{
int x,y,d;
}ans[MAXN*2];
struct node
{
int h,m,dat,num;
}a[MAXN*2];
int n,m,k;
int map[MAXN][MAXN];
bool b[MAXN][MAXN];
bool cmp(node a,node b)
{
if (a.h<b.h || (a.h==b.h && a.m<b.m)) return 1; else return 0;
}
bool Init()
{
scanf("%d%d%d",&n,&m,&k);
if (!n && !m && !k) return 0;
for (int i=1;i<=n;++i)
for (int j=1;j<=m;++j)
scanf("%d",&map[i][j]);
for (int i=1;i<=k;++i)
scanf("%d:%d %d",&a[i].h,&a[i].m,&a[i].dat),a[i].num=i;
sort(a+1,a+k+1,cmp);
return 1;
}
bool Cleck(int x,int y,int z)
{
for (int i=y;i<=y+z-1;++i)
if (b[x][i]) return 0;
return 1;
}
bool GF(int h)
{
rec Max;
Max.x=Max.y=Max.d=0;
for (int i=1;i<=n;++i)
for (int j=1;j<=m-a[h].dat+1;++j)
if (Cleck(i,j,a[h].dat))
if (map[i][j]>Max.d)
Max.x=i,Max.y=j,Max.d=map[i][j];
if (Max.x && Max.y)
{
ans[a[h].num]=Max;
for (int i=Max.y;i<=Max.y+a[h].dat-1;++i) b[Max.x][i]=1;
return 1;
}
return 0;
}
bool GM(int h)
{
rec Max;
Max.x=Max.y=Max.d=0;
for (int i=1;i<=n;++i)
for (int j=1;j<=m;++j)
if (!b[i][j] && Max.d<map[i][j]) Max.x=i,Max.y=j,Max.d=map[i][j];
if (Max.x && Max.y)
{
ans[a[h].num]=Max,b[Max.x][Max.y]=1;
return 1;
}
return 0;
}
void Solve()
{
memset(ans,0,sizeof(ans));
memset(b,0,sizeof(b));
for (int i=1;i<=k;++i)
{
if (GF(i)) continue;
if (GM(i)) continue;
ans[a[i].num].d=-1;
}
for (int i=1;i<=k;++i)
if (ans[i].d!=-1) printf("%d %d\n",ans[i].x,ans[i].y); else printf("-1\n");
}
int main()
{
while (Init()) Solve();
return 0;
}

173
HDOJ/3263_autoAC.cpp Normal file
View File

@ -0,0 +1,173 @@
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;
typedef struct pnode
{
double x,y,d;
pnode( double a, double b ) {x = a;y = b;}
pnode(){};
}point;
point H[ 21 ];
point C[ 21 ];
point P0,Pn;
typedef struct lnode
{
double x,y,dx,dy,d;
int id,hit,sign;
lnode( point a, point b ) {x = a.x;y = a.y;dx = b.x-a.x;dy = b.y-a.y;}
lnode(){};
}line;
double dist( point a, point b )
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double dist( point a, line l )
{
return fabs(l.dx*(a.y-l.y)-l.dy*(a.x-l.x))/sqrt(l.dx*l.dx+l.dy*l.dy);
}
double crossproduct( point a, point b, point c )
{
return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
bool cmp1( point a, point b )
{
if ( a.x == b.x ) return a.y < b.y;
else return a.x < b.x;
}
bool cmp2( point a, point b )
{
double cp = crossproduct( P0, a, b );
if ( cp == 0 ) return a.d < b.d;
else return cp > 0;
}
double Graham( int N )
{
sort( C+0, C+N, cmp1 );
P0 = C[0];
for ( int i = 1 ; i < N ; ++ i )
C[i].d = dist( P0, C[i] );
sort( C+1, C+N, cmp2 );
int top = 2;
for ( int i = 3 ; i < N ; ++ i ) {
while ( top > 0 && crossproduct( C[top-1], C[top], C[i] ) <= 0 ) -- top;
C[++ top] = C[i];
}
C[++ top] = C[0];
int L = 0,R = 1;
double D = 500.000;
do{
while ( crossproduct( C[R], C[L], C[(L+1)%top] ) <= crossproduct( C[(R+1)%top], C[L], C[(L+1)%top] ) )
R = (R+1)%top;
D = min( D, dist( C[R], line( C[L], C[(L+1)%top] ) ) );
L = (L+1)%top;
}while ( L );
return D;
}
bool l_cross_s( line b, line a )
{
double t1 = b.dx*(a.y-b.y)-b.dy*(a.x-b.x);
double t2 = b.dx*(a.y+a.dy-b.y)-b.dy*(a.x+a.dx-b.x);
return t1*t2 < 0;
}
bool s_cross_s( line a, line b )
{
double t1 = 0.0+a.dx*(b.y-a.y)-a.dy*(b.x-a.x);
double t2 = 0.0+a.dx*(b.y+b.dy-a.y)-a.dy*(b.x+b.dx-a.x);
double t3 = 0.0+b.dx*(a.y-b.y)-b.dy*(a.x-b.x);
double t4 = 0.0+b.dx*(a.y+a.dy-b.y)-b.dy*(a.x+a.dx-b.x);
return (t1*t2 < 0)&&(t3*t4 < 0);
}
bool on( point p, line l )
{
if ( l.dx*(p.y-l.y)-l.dy*(p.x-l.x) == 0 )
if ( (p.x-l.x)*(p.x-l.x-l.dx) <= 0 )
if ( (p.y-l.y)*(p.y-l.y-l.dy) <= 0 )
return true;
return false;
}
bool in( point p, point* P, int n )
{
double d[4][2] = {-101,-103,-103,101,101,-103,101,103};
for ( int t = 0 ; t < 4 ; ++ t ) {
line s1 = line( p, point( d[t][0], d[t][1] ) );
int count = 0;
for ( int i = 0 ; i < n ; ++ i ) {
line s2 = line( P[i], P[i+1] );
if ( on( p, s2 ) ) return true;
if ( s_cross_s( s1, s2 ) ) count ++;
if ( on( P[i], s1 ) && l_cross_s( s1, line( P[i+1], P[(i-1+n)%n] ) ) ) count ++;
}
if ( count%2 == 0 ) return false;
}
return true;
}
point crosspoint( line l, line m )
{
point a = point( m.x, m.y );
point b = point( m.x+m.dx, m.y+m.dy );
if ( m.dx*l.dy == m.dy*l.dx ) {
if ( dist( point( l.x, l.y ), a ) < dist( point( l.x, l.y ), b ) )
return a;
else return b;
}else {
double a1 = -l.dy,b1 = l.dx,c1 = l.dx*l.y-l.dy*l.x;
double a2 = -m.dy,b2 = m.dx,c2 = m.dx*m.y-m.dy*m.x;
double x = (c1*b2-c2*b1)/(a1*b2-a2*b1);
double y = (c1*a2-c2*a1)/(b1*a2-b2*a1);
return point( x, y );
}
}
double Calcul( int N )
{
H[N] = H[0];
point X[ 21 ];
double D = 0.0;
for ( int i = 0 ; i < N ; ++ i )
for ( int j = i+1 ; j < N ; ++ j ) {
line l = line( H[i], H[j] );
int S = 0;
for ( int k = 0 ; k < N ; ++ k ) {
line m = line( H[k], H[k+1] );
if ( l_cross_s( l, m ) )
X[S ++] = crosspoint( l, m );
}
X[S ++] = H[i];
X[S ++] = H[j];
P0 = point( -101, -103 );
for ( int k = 0 ; k < S ; ++ k )
X[k].d = dist( P0, X[k] );
sort( X, X+S, cmp2 );
double sum = 0.0;
int fla = 0;
for ( int i = 1 ; i < S ; ++ i ) {
if ( in( point( (X[i-1].x+X[i].x)/2, (X[i-1].y+X[i].y)/2 ), H, N ) ) {
if ( fla ) sum += dist( X[i-1], X[i] );
else sum = dist( X[i-1], X[i] );
D = max( D, sum );
fla = 1;
}
}
}
return D;
}
int main()
{
int T,N,M;
while ( scanf("%d",&T) != EOF )
while ( T -- ) {
scanf("%d",&N);
for ( int i = 0 ; i < N ; ++ i )
scanf("%lf%lf",&H[i].x,&H[i].y);
scanf("%d",&M);
for ( int i = 0 ; i < M ; ++ i )
scanf("%lf%lf",&C[i].x,&C[i].y);
double d = Graham( M );
double D = Calcul( N );
if ( d <= D ) printf("legal\n");
else printf("illegal\n");
}
return 0;
}

73
HDOJ/3264_autoAC.cpp Normal file
View File

@ -0,0 +1,73 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define repf(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll;
const int N = 21;
const double PI = acos(-1.0);
const double EPS = 1e-8;
int t, n;
struct Round {
double x, y;
double r;
} r[N], ori;
double dis(const Round &a, const Round &b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
double insection_of_rounds(Round &a, Round &b) {
double d = dis(a, b);
if (d >= a.r + b.r)
return 0;
if (d <= fabs(a.r - b.r)) {
double r = a.r < b.r ? a.r : b.r;
return PI * r * r;
}
double ang1 = acos((a.r * a.r + d * d - b.r * b.r) / 2. / a.r / d);
double ang2 = acos((b.r * b.r + d * d - a.r * a.r) / 2. / b.r / d);
double ret = ang1 * a.r * a.r + ang2 * b.r * b.r - d * a.r * sin(ang1);
return ret;
}
bool check(Round& ori) {
repf (i, 0, n - 1) {
if (insection_of_rounds(ori, r[i]) * 2 < PI * r[i].r * r[i].r)
return false;
}
return true;
};
double bin(double l, double r, Round& ori) {
double mid;
while (fabs(l - r) >= EPS) {
mid = (l + r) / 2;
ori.r = mid;
if (check(ori)) {
r = mid;
} else {
l = mid + EPS;
}
}
return mid;
}
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
repf (i, 0, n - 1) {
scanf("%lf%lf%lf", &r[i].x, &r[i].y, &r[i].r);
}
double ans = 1e10;
repf (i, 0, n - 1) {
ori.x = r[i].x;
ori.y = r[i].y;
double right = 0;
repf (j, 0, n - 1) {
right = max(right, dis(ori, r[j]) + r[j].r);
}
ans = min(ans, bin(0, right, ori));
}
printf("%.4f\n", ans);
}
return 0;
}

81
HDOJ/3265_autoAC.cpp Normal file
View File

@ -0,0 +1,81 @@
#include<iostream>
#include<vector>
#include<string.h>
#include <algorithm>
using namespace std;
const int maxx=55555;
struct node
{
__int64 sum,color;
}tree[maxx<<3];
struct seg
{
int x1,x2,y,color;
seg(int a,int b,int c,int d):x1(a),x2(b),y(c),color(d){}
bool operator < (const seg &t)const{
return y < t.y;
}
};
void pushUp(int no,int l,int r)
{
if(tree[no].color) tree[no].sum=r-l+1;
else if(l==r) tree[no].sum=0;
else tree[no].sum=tree[no<<1].sum+tree[no<<1|1].sum;
}
void update(int x1,int x2,int color,int l,int r,int no)
{
if(x1<=l&&r<=x2)
{
tree[no].color+=color;
pushUp(no,l,r);
return;
}
int m=(l+r)>>1;
if(x1<=m) update(x1,x2,color,l,m,no<<1);
if(x2>m) update(x1,x2,color,m+1,r,no<<1|1);
pushUp(no,l,r);
}
int main()
{
int n,x1,x2,x3,x4,y1,y2,y3,y4,i;
while(cin>>n,n)
{
vector<seg> v;
for(i=1;i<=n;++i)
{
cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
if(x1<x3)
{
v.push_back(seg(x1,x3,y1,1));
v.push_back(seg(x1,x3,y2,-1));
}
if(x4<x2)
{
v.push_back(seg(x4,x2,y1,1));
v.push_back(seg(x4,x2,y2,-1));
}
if(y1<y3)
{
v.push_back(seg(x3,x4,y1,1));
v.push_back(seg(x3,x4,y3,-1));
}
if(y4<y2)
{
v.push_back(seg(x3,x4,y4,1));
v.push_back(seg(x3,x4,y2,-1));
}
}
sort(v.begin(),v.end());
memset(tree,0,sizeof(tree));
__int64 ret=0;
int end=v.size();
for(i=0;i<end-1;++i)
{
if(v[i].x2>v[i].x1)
update(v[i].x1,v[i].x2-1,v[i].color,0,maxx,1);
ret+=tree[1].sum*(v[i+1].y-v[i].y);
}
printf("%I64d\n",ret);
}
return 0;
}

117
HDOJ/3266_autoAC.cpp Normal file
View File

@ -0,0 +1,117 @@
#include<cstdio>
#include<vector>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int mn=100100,M=379177,base=213;
int n,m;
#define hf(a) ((a)&0x7fffffff)%M
struct node {
node *nxt;
int k,len;
} *ha[M],pool[M];
int pp;
node* find(int k1,int len) {
for(node *p=ha[hf(k1)];p;p=p->nxt)
if(k1==p->k && len==p->len)return p;
return 0;
}
void insert(int k,int len) {
int h=hf(k);
pool[pp].k=k;
pool[pp].len=len;
pool[pp].nxt = ha[h];
ha[h]=&pool[pp++];
}
char ts[mn];
string a,b;
vector<int> a1,b1,a2,b2;
int pw[mn];
void getbit(string &str1,string &str2,vector<int> &bit)
{
int len1,len2;
len1=str1.size();
len2=str2.size();
bit.clear();
int key=0;
memset(ha,0,sizeof(ha));
pp=0;
for(int i=0;i<len1;i++)
{
key+=str1[len1-i-1]*pw[i];
insert(key,i+1);
}
key=0;
for(int i=0;i<len2 && i<len1;i++)
{
key*=base;
key+=str2[i];
node *p=find(key,i+1);
if(p)bit.push_back(1);
else bit.push_back(0);
}
reverse(bit.begin(),bit.end());
}
void mu(vector<int> &a,vector<int> &b)
{
int i,j,c=0,tmp;
i=a.size()-1; j=b.size()-1;
while(1)
{
if(i<0 && j<0)break;
if(i>=0)c+=a[i];
if(j>=0)c-=b[j];
if(c<0) { a[i]=2+c; c=-1; }
else { a[i]=c; c=0; }
i-=1;
j-=1;
}
}
int main()
{
pw[0]=1;
for(int i=1;i<mn;i++)pw[i]=pw[i-1]*base;
while(1)
{
scanf("%d%d",&n,&m);
if(n==0 && m==0)break;
scanf("%s",ts);
a=ts;
scanf("%s",ts);
b=ts;
getbit(b,b,b1);
getbit(b,a,b2);
getbit(a,a,a1);
getbit(a,b,a2);
mu(b1,b2);
mu(a1,a2);
int al=0,bl=0;
for(bl=0;bl<b1.size();bl++)if(b1[bl]!=0)break;
for(al=0;al<a1.size();al++)if(a1[al]!=0)break;
if(b1.size()-bl!=a1.size()-al)
{
if(b1.size()-bl>a1.size()-al)printf("Hamlet\n");
else printf("Laertes\n");
}
else
{
bool f=1;
while(1)
{
if(bl>=b1.size())break;
if(b1[bl]!=a1[al])
{
if(b1[bl]>a1[al])printf("Hamlet\n");
else printf("Laertes\n");
f=0;
break;
}
bl++;
al++;
}
if(f)printf("Equal\n");
}
}
return 0;
}

86
HDOJ/3267_autoAC.cpp Normal file
View File

@ -0,0 +1,86 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long
int n, m;
int l[32], r[32];
bool red[32], blue[32];
int find(int idx[10], int x){
if(idx[x]==-1) return x;
return idx[x]=find(idx, idx[x]);
}
void merge(int gra[10][10],int idx[10], int x, int y){
int rx = find(idx, x);
int ry = find(idx, y);
if(rx==ry) return;
idx[rx] = ry;
for(int k=0;k<n;k++){
if(~idx[k]) continue;
if(k==ry) continue;
gra[ry][k] += gra[rx][k];
gra[k][ry] = gra[ry][k];
}
}
int condense(int gra[10][10],int idx[10]){
bool flag=true;
while(flag){
flag = false;
for(int i=0;i<n;i++){
if(~idx[i]) continue;
for(int j=i+1;j<n;j++){
if(~idx[j]) continue;
if(gra[i][j]>=2){
merge(gra, idx, i, j);
flag = true;
}
}
}
}
int cnt=0;
for(int i=0;i<n;i++){
if(~idx[i]) continue;
cnt++;
}
return cnt;
}
int minmax(int deg, int red, int gra[10][10], int idx[10]){
bool end = true;
for(int i=0;i<m;i++){
if(red&(1<<i)) continue;
int rl = find(idx, l[i]);
int rr = find(idx, r[i]);
if(rl==rr) continue;
end = false;
int tidx[10], tgra[10][10];
memcpy(tidx, idx, sizeof(tidx));
memcpy(tgra, gra, sizeof(tgra));
if(deg&1){
merge(tgra, tidx, rl, rr);
if(condense(tgra, tidx)==1) return 1;
if(minmax(deg+1, red, tgra, tidx)==1) return 1;
} else {
tgra[rl][rr] = tgra[rr][rl] = 0;
if(minmax(deg+1, red|(1<<i), tgra, tidx)==-1) return -1;
}
}
if(end) return -1;
if(0==(deg&1)) return 1;
if(1==(deg&1)) return -1;
}
int gra[10][10], idx[10];
int main(){
while(scanf("%d%d", &n,&m), ~n){
memset(gra, 0, sizeof(gra));
memset(idx, -1, sizeof(idx));
for(int i=0;i<m;i++){
scanf("%d%d", &l[i], &r[i]);
if(l[i]^r[i]){
gra[l[i]][r[i]] ++;
gra[r[i]][l[i]] ++;
}
}
if(condense(gra, idx)==1||minmax(0, 0, gra, idx)==1) puts("YES");
else puts("NO");
}
}

88
HDOJ/3268_autoAC.cpp Normal file
View File

@ -0,0 +1,88 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 30;
const int maxm = 900;
const int INF = 1000000;
vector<vector<pair<int,int> > > edge;
int dist[maxn+10];
bool inQue[maxn+10];
int n;
int num[maxn];
void spfa(int src){
memset(dist,63,sizeof(dist));
memset(inQue,0,sizeof(inQue));
dist[src] = 0;
queue<int> que;
que.push(src);
inQue[src] = 1;
while(!que.empty()) {
int u = que.front();
que.pop();
for(int i = 0; i < edge[u].size(); i++)
if(dist[u]+edge[u][i].second<dist[edge[u][i].first]) {
dist[edge[u][i].first] = dist[u]+edge[u][i].second;
if(!inQue[edge[u][i].first]){
inQue[edge[u][i].first] = 1;
que.push(edge[u][i].first);
}
}
inQue[u] = 0;
}
}
void init(){
edge.clear();
edge.resize(n+1);
}
int main(){
int ncase;
cin >> ncase;
while(ncase--){
scanf("%d",&n);
init();
for(int i = 1; i <= n; i++){
int a,b;
scanf("%d%d",&a,&b);
num[a] = b;
edge[0].push_back(make_pair(a,b-1));
}
for(int i = 1; i <= n; i++){
for(int j = i+1; j <= n; j++){
if(num[i]==num[j]){
edge[i].push_back(make_pair(j,0));
edge[j].push_back(make_pair(i,0));
}
}
}
int m;
scanf("%d",&m);
while(m--){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
edge[a].push_back(make_pair(b,c));
}
spfa(0);
int cnt = 0;
for(int i=1;i<=n;i++){
bool flag=0;
for(int j=1;j<=n;j++){
if(i==j) continue;
for(int k=1;k<=n;k++){
if(k!=i&&k!=j&&dist[i]==dist[j]+dist[k]){
cnt++;
flag=1;
break;
}
}
if(flag) break;
}
}
for(int i = 1; i <= n; i++){
cout<<i<<" "<<dist[i]<<endl;
}
cout<<cnt<<endl;
}
}

95
HDOJ/3269_autoAC.cpp Normal file
View File

@ -0,0 +1,95 @@
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 32
#define cls(a) memset(a, 0, sizeof(a))
#define cls1(a,i,j) memset(a+i, 1, (j-i+1)*sizeof(bool))
#define cls0(a, i) memset(a, 0, i*sizeof(bool))
struct computer{
bool able[1005];
int pcent;
}cpt[N];
bool isSver[N];
int Sver[N];
int Clnt[N];
int rate[N][N];
int main()
{
int d, n, t, k, s, i, j, l, st, ed, m, c;
scanf("%d", &d);
while(d--)
{
cls(isSver);
cls(Clnt);
cls(Sver);
cls(cpt);
cls(rate);
scanf("%d%d", &n, &t);
scanf("%d%d", &s, &k);
for(i=0; i<s; i++)
{
scanf("%d", Sver+i);
isSver[Sver[i]] = 1;
cpt[Sver[i]].pcent = k;
}
for(c=0, i=1; i<=n; i++)
{
if(!isSver[i]) Clnt[c++] = i;
}
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
scanf("%d", rate[i]+j);
}
}
for(i=1; i<=n; i++)
{
scanf("%d", &m);
for(j=0; j<m; j++)
{
scanf("%d%d", &st, &ed);
if(st > t) continue;
if(ed > t) ed = t;
for(l=st; l<ed; l++) cpt[i].able[l] = 1;
}
}
scanf("%d", &m);
for(i=0; i<m; i++)
{
scanf("%d%d", &ed, &st);
cls0(cpt[st].able, ed);
}
for(i=0; i<=t; i++)
{
for(j=0; j<c; j++)
{
if(cpt[Clnt[j]].able[i])
{
for(l=0; l<s; l++)
{
if(cpt[Sver[l]].able[i])
{
cpt[Clnt[j]].pcent += rate[Clnt[j]][Sver[l]];
}
}
}
}
for(j=0; j<c; j++)
if(cpt[Clnt[j]].pcent >= k)
{
isSver[Clnt[j]] = 1;
Sver[s++] = Clnt[j];
for(c--, l=j; l<c; l++)
Clnt[l] = Clnt[l+1];
}
}
for(i=1; i<=n; i++)
{
printf("%d%%\n", cpt[i].pcent>=k ? 100 : (cpt[i].pcent*100/k));
}
}
return 0;
}

79
HDOJ/3270_autoAC.cpp Normal file
View File

@ -0,0 +1,79 @@
#include <stdio.h>
#include <string.h>
char str[100];
long long gcd(long long x,long long y)
{
return y==0?x:gcd(y,x%y);
}
long long Ex_Euclid(long long a,long long b,long long &x,long long &y)
{
long long ans,t;
if (b==0)
{
x=1;
y=0;
ans=0;
return ans;
}
ans=Ex_Euclid(b,a%b,x,y);
t=x;
x=y;
y=t-(a/b)*y;
return ans;
}
int main()
{
int i,j,n;
long long A,B,C,D,x,y,k,t;
while(scanf("%s",str)!=EOF)
{
A=0;
for (i=0;i<strlen(str)-1;i++)
{
A=A*10+str[i]-'0';
}
scanf("%s",str);
scanf("%s",str);
B=0;
for (i=0;i<strlen(str)-1;i++)
{
B=B*10+str[i]-'0';
}
scanf("%s",str);
scanf("%s",str);
C=0;
for (i=0;i<strlen(str);i++)
{
C=C*10+str[i]-'0';
}
if (A==0) A=1;
if (B==0) B=1;
D=gcd(A,B);
if (C%D!=0)
{
printf("No.\n\n");
continue;
}
n=Ex_Euclid(A,B,x,y);
x=x*C/D;
t=B/D;
x=(x%t+t)%t;
k=(C-A*x)/B;
if (k>=0)
{
printf("Yes.\n\n");
continue;
}
y=y*C/D;
t=A/D;
y=(y%t+t)%t;
k=(C-B*y)/A;
if (k>=0)
{
printf("Yes.\n\n");
continue;
}
printf("No.\n\n");
}
return 0;
}

65
HDOJ/3271_autoAC.cpp Normal file
View File

@ -0,0 +1,65 @@
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define N 30
#define inf 1<<29
#define MOD 2007
#define LL long long
using namespace std;
int dp[32][305];
void Init(int b,int m){
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(int i=1;i<32;i++)
for(int j=0;j<=m;j++)
for(int k=0;k<b&&k+j<=m;k++)
dp[i][j+k]+=dp[i-1][j];
}
int clac(int n,int b,int m){
int bit[35],len=0,t=n;
while(t){
bit[++len]=t%b;
t/=b;
}
int sum=0,tot=0;
for(int i=len;i;i--){
for(int j=0;j<bit[i]&&m-tot-j>=0;j++)
sum+=dp[i-1][m-tot-j];
tot+=bit[i];
if(tot>m) break;
}
if(tot==m) sum++;
return sum;
}
int main(){
int cas=0,kind,x,y,b,m,k;
while(scanf("%d%d%d%d%d",&kind,&x,&y,&b,&m)!=EOF){
Init(b,m);
if(x>y) swap(x,y);
printf("Case %d:\n",++cas);
int cnt=clac(y,b,m)-clac(x-1,b,m);
if(kind==1){
printf("%d\n",cnt);
continue;
}
scanf("%d",&k);
if(k>cnt){
puts("Could not find the Number!");
continue;
}
int low=x,high=y,mid;
while(low<high){
mid=(int)((((LL)low+(LL)high))/2);
int now=clac(mid,b,m)-clac(x-1,b,m);
if(now<k)
low=mid+1;
else
high=mid;
}
printf("%d\n",low);
}
return 0;
}

106
HDOJ/3272_autoAC.cpp Normal file
View File

@ -0,0 +1,106 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define repf(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll;
const int N = 0;
struct Point {
double x;
double y;
} h, o, p[3];
double dis(const Point& a, const Point& b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
double calc(int a, int b) {
double ret = dis(h, p[a]) + dis(p[a], p[b]) + dis(p[b], h);
double ha, ab, bh;
bool cx = 0, cy = 0;
if (h.x * p[a].x <= 0 || p[a].x * p[b].x <= 0 || p[b].x * h.x <= 0)
cx = 1;
if (h.y * p[a].y <= 0 || p[a].y * p[b].y <= 0 || p[b].y * h.y <= 0)
cy = 1;
if (cx == 0 && cy == 0) {
Point t1, t2;
t1.x = h.x;
t1.y = -h.y;
t2.x = -p[a].x;
t2.y = p[a].y;
ha = ret - dis(h, p[a]) + dis(t1, t2);
t1.x = p[b].x;
t1.y = -p[b].y;
t2.x = -p[a].x;
t2.y = p[a].y;
ab = ret - dis(p[a], p[b]) + dis(t1, t2);
t1.x = p[b].x;
t1.y = -p[b].y;
t2.x = -h.x;
t2.y = h.y;
bh = ret - dis(h, p[b]) + dis(t1, t2);
double ans = min(ha, min(ab, bh));
t1.x = p[a].x;
t1.y = -p[a].y;
t2.x = -p[a].x;
t2.y = p[a].y;
ans = min(ans, ret - dis(p[a], h) + dis(t1, h) - dis(p[a], p[b]) + dis(t2, p[b]));
t1.x = h.x;
t1.y = -h.y;
t2.x = -h.x;
t2.y = h.y;
ans = min(ans, ret - dis(p[a], h) + dis(p[a], t1) - dis(h, p[b]) + dis(t2, p[b]));
t1.x = p[a].x;
t1.y = -p[a].y;
t2.x = -p[a].x;
t2.y = p[a].y;
ans = min(ans, ret - dis(p[a], p[b]) + dis(t1, p[b]) - dis(p[a], h) + dis(t2, h));
t1.x = p[b].x;
t1.y = -p[b].y;
t2.x = -p[b].x;
t2.y = p[b].y;
ans = min(ans, ret - dis(p[a], p[b]) + dis(t1, p[a]) - dis(p[b], h) + dis(t2, h));
t1.x = h.x;
t1.y = -h.y;
t2.x = -h.x;
t2.y = h.y;
ans = min(ans, ret - dis(p[b], h) + dis(p[b], t1) - dis(h, p[a]) + dis(t2, p[a]));
t1.x = p[b].x;
t1.y = -p[b].y;
t2.x = -p[b].x;
t2.y = p[b].y;
ans = min(ans, ret - dis(h, p[b]) + dis(t1, h) - dis(p[b], p[a]) + dis(t2, p[a]));
ret = ans;
} else if (cx == 1 && cy == 0) {
Point tmp;
tmp.x = p[a].x;
tmp.y = -p[a].y;
ha = ret - dis(h, p[a]) + dis(h, tmp);
ab = ret - dis(p[a], p[b]) + dis(tmp, p[b]);
tmp.x = p[b].x;
tmp.y = -p[b].y;
bh = ret - dis(h, p[b]) + dis(h, tmp);
ret = min(ha, min(ab, bh));
} else if (cx == 0 && cy == 1) {
Point tmp;
tmp.x = -p[a].x;
tmp.y = p[a].y;
ha = ret - dis(h, p[a]) + dis(h, tmp);
ab = ret - dis(p[a], p[b]) + dis(tmp, p[b]);
tmp.x = -p[b].x;
tmp.y = p[b].y;
bh = ret - dis(h, p[b]) + dis(h, tmp);
ret = min(ha, min(ab, bh));
}
return ret;
}
int main() {
o.x = 0;
o.y = 0;
int t;
cin >> t;
while (cin >> p[0].x >> p[0].y >> p[1].x >> p[1].y >> h.x >> h.y) {
printf("%.2f\n", calc(0, 1));
}
return 0;
}

58
HDOJ/3274_autoAC.cpp Normal file
View File

@ -0,0 +1,58 @@
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cstdio>
using namespace std;
const int maxn=2001;
int n,m,root,sum,NE=0,vis[maxn],head[maxn];
struct node
{
int u,v,w,next;
} Edge[maxn];
void addEdge(int u,int v,int w)
{
Edge[NE].u=u,Edge[NE].v=v,Edge[NE].w=w,Edge[NE].next=head[u];
head[u]=NE++;
}
void dfs(int u,int fa)
{
int i,j;
for(i=head[u];i!=-1;i=Edge[i].next)
{
int v=Edge[i].v;
if(v==fa)continue;
dfs(v,u);
if(vis[v])
{
sum+=Edge[i].w;
vis[u]=1;
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int i,j,a,b,c;
sum=NE=0;
memset(vis,0,sizeof(vis));
memset(head,-1,sizeof(head));
for(i=1; i<=m; i++)
{
scanf("%d",&root);
vis[root]=1;
}
for(i=1; i<n; i++)
{
scanf("%d%d%d",&a,&b,&c);
addEdge(a,b,c);
addEdge(b,a,c);
}
dfs(root,-1);
cout<<sum<<endl;
}
return 0;
}

128
HDOJ/3275_autoAC.cpp Normal file
View File

@ -0,0 +1,128 @@
#include<iostream>
#include<cstring>
using namespace std;
struct node{
int num;
int flag;
int l;
int r;
int len;
}p[100005*6];
int n,k,nima,cao;
char a[100005];
void build(int l,int r,int n)
{
p[n].l=l;
p[n].r=r;
p[n].len=r-l+1;
p[n].flag=0;
if(l==r)
{
if(a[l]=='1')
p[n].num=0;
else
p[n].num=1;
}
else
{
int mid=(l+r)/2;
build(l,mid,n*2);
build(mid+1,r,n*2+1);
p[n].num=p[n*2].num+p[n*2+1].num;
}
}
void pushdown(int n)
{
p[n].flag=0;
p[n*2].flag^=1;
p[n*2+1].flag^=1;
p[n*2].num=p[n*2].len-p[n*2].num;
p[n*2+1].num=p[n*2+1].len-p[n*2+1].num;
}
void update(int x,int y,int n)
{
if(x==p[n].l&&y==p[n].r)
{
p[n].flag^=1;
p[n].num=p[n].len-p[n].num;
}
else
{
int mid=(p[n].l+p[n].r)/2;
if(x>mid)
update(x,y,n*2+1);
else if(y<=mid)
update(x,y,n*2);
else
{
update(x,mid,n*2);
update(mid+1,y,n*2+1);
}
}
}
void insert(int x,int n)
{
if(nima)
return ;
if(p[n].flag==1)
pushdown(n);
if(p[n].l==p[n].r)
{
if(p[n].num==1)
{
nima=p[n].l;
if(p[n].l+k-1<=cao)
update(p[n].l,p[n].l+k-1,1);
}
}
else
{
int mid=(p[n].l+p[n].r)/2;
if(x>mid)
insert(x,n*2+1);
else
{
insert(x,n*2);
insert(mid+1,n*2+1);
}
}
}
int main()
{
int temp,t;
while(scanf("%d%d",&n,&k)!=EOF&&(n!=0||k!=0))
{
cao=n;
temp=0;
t=0;
getchar();
scanf("%s",a+1);
build(1,n,1);
if(p[1].num==0)
{
printf("0\n");
continue;
}
if(k==0)
{
printf("-1\n");
continue;
}
while(1)
{
nima=0;
insert(temp+1,1);
temp=nima;
if(nima==0)
break;
if(n-temp+1<k)
break;
t++;
}
if(nima==0)
printf("%d\n",t);
else
printf("-1\n");
}
return 0;
}

62
HDOJ/3276_autoAC.cpp Normal file
View File

@ -0,0 +1,62 @@
#include<iostream>
using namespace std;
#define N 50005
#define esp 1e-5
#define inf -10000000001
double a[N],sum[N];
double L[N],R[N];
int q[N];
int n,x,y;
int head,tail;
bool find(double ave){
sum[0]=0;
for(int i=1;i<=n;i++)
sum[i]=sum[i-1]+a[i]-ave;
head=tail=0;
L[x-1]=inf;
for(int i=x;i<=n;i++){
int p=i-x;
while(head<tail&&sum[p]<sum[q[tail-1]])
tail--;
q[tail++]=p;
while(head<tail&&i-y>q[head])
head++;
L[i]=max(L[i-1],sum[i]-sum[q[head]]);
}
sum[n+1]=0;
for(int i=n;i>0;i--)
sum[i]=sum[i+1]+a[i]-ave;
head=tail=0;
R[n-x+2]=inf;
for(int i=n-x+1;i>0;i--){
int p=i+x;
while(head<tail&&sum[p]<sum[q[tail-1]])
tail--;
q[tail++]=p;
while(head<tail&&i+y<q[head])
head++;
R[i]=max(R[i+1],sum[i]-sum[q[head]]);
}
for(int i=x+1;i<=n-x;i++)
if(L[i-1]+R[i+1]>=0)
return true;
return false;
}
int main(){
int k=1;
while(~scanf("%d%d%d",&n,&x,&y)){
for(int i=1;i<=n;i++)
scanf("%lf",&a[i]);
double f=1.0,l=200000.0,ret;
while(l-f>=esp){
double mid=(f+l)/2;
if(find(mid)){
f=mid;
ret=mid;
}
else
l=mid;
}
printf("Case %d: %.3lf\n",k++,ret);
}
}

230
HDOJ/3277_autoAC.cpp Normal file
View File

@ -0,0 +1,230 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<climits>
#define clr(x)memset(x,0,sizeof(x))
#define min(a,b)(a)<(b)?(a):(b)
const int INF=INT_MAX;
const int maxn=800;
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=0;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=0;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;
}
int f[maxn];
int search(int x)
{
return f[x] == x?x:(f[x] = search(f[x]));
}
void join(int x,int y)
{
int fx = search(x);
int fy = search(y);
if(fx != fy){
if(fx < fy)
f[fy] = fx;
else f[fx] = fy;
}
}
struct noq
{
int girl,boy;
}re[maxn*maxn];
int g[maxn][maxn];
int K;
struct Edge
{
int xu,flow;
}edge[1000000];
int top;
int ok(int ti,int s,int t,int n)
{
int i, j, k;
for(i = 0; i < 2*n; i++)
{
e[edge[i].xu].c = ti;
e[edge[i].xu^1].c = 0;
}
for(i = 2*n; i < top; i++)
{
e[edge[i].xu].c = edge[i].flow;
e[edge[i].xu^1].c = 0;
}
int res = maxflow(s,t,t+1);
if(res == n*ti)
return 1;
return 0;
}
int main()
{
int T, n, m;
int i, j, k, F;
scanf("%d",&T);
while(T--)
{
scanf("%d %d %d %d",&n, &m, &K, &F);
for(i = 1; i <= n; i++)
f[i] = i;
for(i = 1; i <= m; i++)
scanf("%d %d",&re[i].girl,&re[i].boy);
int a, b;
while(F--){
scanf("%d %d",&a, &b);
join(a,b);
}
clr(g);
for(i = 1; i <= m; i++)
g[search(re[i].girl)][re[i].boy] = 1;
for(i = 1; i <= n; i++)
for(j = 1;j <= n; j++)
if(g[search(i)][j] == 1)
g[i][j] = 1;
int s = 0;
int t = n*3+1;
tot = 0;
memset(head,-1,sizeof(head));
top = 0;
for(i = 1; i <= n; i++){
edge[top++].xu = tot;
add(s,i,1,0);
}
for(i = n*2+1; i <= n*3; i++)
{
edge[top++].xu = tot;
add(i,t,1,0);
}
for(i = 1; i <= n; i++){
edge[top].xu = tot;
edge[top++].flow = K;
add(i,i+n,K,0);
}
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
{
if(g[i][j]){
edge[top].xu = tot;
edge[top++].flow = 1;
add(i,j+2*n,1,0);
}
else{
edge[top].xu = tot;
edge[top++].flow = 1;
add(i+n,j+2*n,1,0);
}
}
int l = 0, r = n, mid;
int res=0;
while(l <= r){
mid = (l+r)>>1;
if(ok(mid,s,t,n)){
res = mid;
l = mid+1;
}
else r = mid-1;
}
printf("%d\n",res);
}
return 0;
}

118
HDOJ/3278_autoAC.cpp Normal file
View File

@ -0,0 +1,118 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#define N 10
using namespace std;
char s[5][7],str[25];
char v[1<<24];
queue<int> que;
int inline row_op(int r,int n,int op)
{
int tmp[6];
for(int i=0;i<6;i++){
tmp[i]=n&(1<<(r*6+i));
n-=tmp[i];
}
if(op){
tmp[0]<<=5;
for(int i=1;i<6;i++){
tmp[i]>>=1;
}
}else{
for(int i=0;i<5;i++){
tmp[i]<<=1;
}
tmp[5]>>=5;
}
n+=tmp[0]+tmp[1]+tmp[2]+tmp[3]+tmp[4]+tmp[5];
return n;
}
int inline col_op(int c,int n,int op)
{
int tmp[4];
for(int i=0;i<4;i++){
tmp[i]=n&(1<<(i*6+c));
n-=tmp[i];
}
if(op){
tmp[0]<<=18;
tmp[1]>>=6;
tmp[2]>>=6;
tmp[3]>>=6;
}else{
tmp[0]<<=6;
tmp[1]<<=6;
tmp[2]<<=6;
tmp[3]>>=18;
}
n+=tmp[0]+tmp[1]+tmp[2]+tmp[3];
return n;
}
void countMoves()
{
int cnt=0;
while(!que.empty()) que.pop();
memset(v,-1,sizeof(v));
que.push(124800);
v[124800]=0;
while(!que.empty()){
int t=que.front();
que.pop();
for(int i=0;i<4;i++){
int tmp=row_op(i,t,1);
if(v[tmp]==-1){
que.push(tmp);
v[tmp]=v[t]+1;
}
tmp=row_op(i,t,0);
if(v[tmp]==-1){
que.push(tmp);
v[tmp]=v[t]+1;
}
}
for(int j=0;j<6;j++){
int tmp=col_op(j,t,1);
if(v[tmp]==-1){
que.push(tmp);
v[tmp]=v[t]+1;
}
tmp=col_op(j,t,0);
if(v[tmp]==-1){
que.push(tmp);
v[tmp]=v[t]+1;
}
}
}
return;
}
int main()
{
int T,cas=1;
int b,w,g;
cin >> T;
countMoves();
while(T--){
b=w=g=0;
for(int i=0;i<4;i++){
scanf("%s",s[i]);
for(int j=0;j<6;j++){
if(s[i][j]=='B'){
b+=(1<<(i*6+j));
}else if(s[i][j]=='W'){
w+=(1<<(i*6+j));
}else{
g+=(1<<(i*6+j));
}
}
}
int cnt = min(v[b],min(v[w],v[g]));
cout << "Case " << cas << ": " << cnt << endl;
cas++;
}
return 0;
}

21
HDOJ/3279_autoAC.cpp Normal file
View File

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

43
HDOJ/3280_autoAC.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <stdio.h>
int main()
{
int i,j,n,t,b;
int a[10005];
while(scanf("%d",&t)!=EOF)
{
int k=1;
while(t--)
{
scanf("%d %d",&b,&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
int sum=0,temp,flag;
for(i=0;i<n;i++)
{
sum+=a[i];
temp=0;
flag=0;
for(j=i+1;j<n;j++)
{
temp+=a[j];
if(temp==sum)
temp=0;
else if(temp>sum)
{
flag=1;
break;
}
else if(j==n-1)
{
flag=1;
break;
}
}
if(flag==0)
break;
}
printf("%d %d\n",k++,sum);
}
}
return 0;
}

29
HDOJ/3281_autoAC.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int f[55][1005];
void init(){
for(int i = 1; i < 1001; i ++)
f[1][i] = i;
for(int i = 2; i < 51; i ++){
f[i][1] = 1;
for(int j = 2; j < 1001; j ++){
f[i][j] = f[i][j - 1] + 1;;
for(int k = 2; k < j; k ++){
f[i][j] = min(f[i][j], max(f[i - 1][k - 1], f[i][j - k]) + 1);
}
}
}
}
int main(){
int t, T, n, m;
init();
scanf("%d", &T);
while(T --){
scanf("%d%d%d", &t, &n, &m);
cout<<t<<" "<<f[n][m]<<endl;
}
}

67
HDOJ/3282_autoAC.cpp Normal file
View File

@ -0,0 +1,67 @@
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
void run()
{
int n,m;
scanf("%d%d",&n,&m);
int i,num[10001];
int ans[10001]={0};
int u=2;
printf("%d %d\n",n,(m+1)/2);
scanf("%d",&num[1]);
ans[1]=num[1];
for(i=2;i<=m;i++)
{
int tmp,j,k;
scanf("%d",&tmp);
if(tmp<=num[1])
{
for(k=i-1;k>=1;k--)
{
num[k+1]=num[k];
}
num[1]=tmp;
}
else if(tmp>=num[i-1])
{num[i]=tmp;}
else
{
bool f=false;
for(j=i-1;j>=1;j--)
{
if(tmp>=num[j-1] && tmp<=num[j])
{
f=true;
int k;
{
for(k=i-1;k>=j;k--)
{
num[k+1]=num[k];
}
num[j]=tmp;
}
}
if(f==true) break;
}
}
if(i%2==1)
{
ans[u++]=num[(i+1)/2];
}
}
for(i=1;i<u;i++)
{
if(i%10==0 || i==u-1) printf("%d\n",ans[i]);
else printf("%d ",ans[i]);
}
}
int main()
{
int total;
cin>>total;
for(int now=1;now<=total;now++) run();
return 0;
}

20
HDOJ/3283_autoAC.cpp Normal file
View File

@ -0,0 +1,20 @@
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include <cstring>
using namespace std;
char a[100];
int main()
{
int t,i,len,n;
scanf("%d",&t);
for(i=1;i<=t;i++)
{
scanf("%d %s",&n,a);
len=strlen(a);
if (next_permutation(a,a+len)) printf("%d %s\n",i,a);
else
printf("%d BIGGEST\n",i);
}
return 0;
}

24
HDOJ/3284_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <stdio.h>
int dp[102][102][2];
int main()
{
int z,ca,n,k;
scanf("%d",&z);
dp[1][0][0] = dp[1][0][1] = 1;
for(int i=2;i<=100;i++)
{
dp[i][0][0] = dp[i-1][0][0] + dp[i-1][0][1];
dp[i][0][1] = dp[i-1][0][0];
for(int j=1;j<i;j++)
{
dp[i][j][0] = dp[i-1][j][0] + dp[i-1][j][1];
dp[i][j][1] = dp[i-1][j][0] + dp[i-1][j-1][1];
}
}
while(z--)
{
scanf("%d%d%d",&ca,&n,&k);
printf("%d %d\n",ca,dp[n][k][0]+dp[n][k][1]);
}
return 0;
}

86
HDOJ/3285_autoAC.cpp Normal file
View File

@ -0,0 +1,86 @@
#include <stdlib.h>
#include<iostream>
#define eps 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps)
using namespace std;
struct point{int x,y;};
point a[100],b[100];
int xmult(point p1,point p2,point p0){
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
point p1,p2;
int graham_cp(const void* a,const void* b){
int ret=xmult(*((point*)a),*((point*)b),p1);
return zero(ret)?(xmult(*((point*)a),*((point*)b),p2)>0?1:-1):(ret>0?1:-1);
}
void _graham(int n,point* p,int& s,point* ch){
int i,k=0;
for (p1=p2=p[0],i=1;i<n;p2.x+=p[i].x,p2.y+=p[i].y,i++)
if (p1.y-p[i].y>eps||(zero(p1.y-p[i].y)&&p1.x>p[i].x))
p1=p[k=i];
p2.x/=n,p2.y/=n;
p[k]=p[0],p[0]=p1;
qsort(p+1,n-1,sizeof(point),graham_cp);
for (ch[0]=p[0],ch[1]=p[1],ch[2]=p[2],s=i=3;i<n;ch[s++]=p[i++])
for (;s>2&&xmult(ch[s-2],p[i],ch[s-1])<-eps;s--);
}
int graham(int n,point* p,point* convex,int maxsize=1,int dir=1){
point* temp=new point[n];
int s,i;
_graham(n,p,s,temp);
for (convex[0]=temp[0],n=1,i=(dir?1:(s-1));dir?(i<s):i;i+=(dir?1:-1))
if (maxsize||!zero(xmult(temp[i-1],temp[i],temp[(i+1)%s])))
convex[n++]=temp[i];
delete []temp;
return n;
}
int main()
{
int t,ii,n,i,maxx,maxy,min1,max1,m,miny,g;
bool d;
scanf("%d",&t);
for(ii=1;ii<=t;ii++)
{
scanf("%d%d",&m,&n);
for(i=0;i<n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
d=true;
for(i=2;i<n;i++)
if (xmult(a[0],a[i],a[1])!=0) {d=false;break;}
if (d)
{
maxx=1000000000;
maxy=-1000000000;
for(i=0;i<n;i++)
if ((a[i].y>maxy)||(a[i].y==maxy&&a[i].x<maxx))
{
max1=i;
maxx=a[i].x;
maxy=a[i].y;
}
miny=1000000000;
for(i=0;i<n;i++)
if (a[i].y<miny)
{
miny=a[i].y;
min1=i;
}
printf("%d 2\n%d %d\n%d %d\n",ii,maxx,maxy,a[min1].x,a[min1].y);
continue;
}
g=graham(n,a,b,0);
maxx=1000000000;
maxy=-1000000000;
for(i=0;i<g;i++)
if ((b[i].y>maxy)||(b[i].y==maxy&&b[i].x<maxx))
{
max1=i;
maxx=b[i].x;
maxy=b[i].y;
}
printf("%d %d\n",ii,g);
for(i=0;i<g;i++)
printf("%d %d\n",b[(i+max1)%g].x,b[(i+max1)%g].y);
}
return 0;
}

82
HDOJ/3286_autoAC.cpp Normal file
View File

@ -0,0 +1,82 @@
#include<iostream>
#include<vector>
#include<map>
#include<stack>
#include<algorithm>
#include<queue>
#include<list>
#include<set>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<ctype.h>
#include<iomanip>
#include<limits.h>
using namespace std;
#define LL long long
#define pi acos(-1)
#define MAX INT_MAX
#define MIN INT_MIN
#define eps 1e-6
#define N 55
struct point{
int x,y;
}p[N];
int n;
int ans[10010][3];
int tot;
void gao(int y){
int i,j;
double m[5];
int cnt=0;
for(i=1;i<=n;i++){
if(p[i].y==y && p[i-1].y==y)return ;
if(p[i].y==y)m[cnt++]=(double)p[i].x;
if(p[i-1].y==y)m[cnt++]=(double)p[i-1].x;
if((p[i-1].y<y && p[i].y>y) || (p[i-1].y>y && p[i].y<y))
m[cnt++]=1.0*(y-p[i].y)*(p[i].x-p[i-1].x)/(p[i].y-p[i-1].y)+p[i].x;
}
int s,t;
if(!cnt)return ;
double minm=MAX;
double maxm=MIN;
for(i=0;i<cnt;i++)
{
minm=min(minm,m[i]);
maxm=max(maxm,m[i]);
}
s=floor(minm)+1;
t=ceil(maxm)-1;
if(s>t)return ;
ans[tot][0]=y;
ans[tot][1]=s;
ans[tot][2]=t;
tot++;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int ca;
scanf("%d%d",&ca,&n);
int i,j;
int minm=MAX;
int maxm=MIN;
for(i=1;i<=n;i++){
scanf("%d%d",&p[i].x,&p[i].y);
minm=min(minm,p[i].y);
maxm=max(maxm,p[i].y);
}
p[0].x=p[n].x;
p[0].y=p[n].y;
tot=0;
for(i=maxm-1;i>=minm+1;i--){
gao(i);
}
printf("%d %d\n",ca,tot);
for(i=0;i<tot;i++)
printf("%d %d %d\n",ans[i][0],ans[i][1],ans[i][2]);
}
return 0;
}

70
HDOJ/3287_autoAC.cpp Normal file
View File

@ -0,0 +1,70 @@
#include<iostream>
#include<cstring>
#include<cstdio>
#include <string>
#include <deque>
#include<cmath>
#include<algorithm>
using namespace std;
struct node{
int num;
int cnt;
string str;
}p[830000], tmp, t;
int p7[8] = {0, 1, 7, 49, 343, 2401, 16807, 117649};
int tnum[8];
int num = 1 * p7[1] + 2 * p7[2] + 3 * p7[3] + 4 * p7[4] + 5 * p7[5] + 6 * p7[6] ;
int tu[8][8] ={
0, 1, 2, 3, 4, 5, 6, 7,
0, 0, 1, 0, 0, 0, 6, 0,
0, 1, 0, 3, 0, 0, 0, 7,
0, 0, 2, 0, 4, 0, 0, 0,
0, 0, 0, 3, 0, 5, 0, 0,
0, 0, 0, 0, 4, 0, 6, 7,
0, 1, 0, 0, 0, 5, 0, 0,
0, 0, 2, 0, 0, 5, 0, 0,
};
deque<node> q;
int maxx = 0;
void init(){
p[num].num = num;
p[num].cnt = 0;
q.push_back(p[num]);
while(!q.empty()){
tmp = q.front(); q.pop_front();
int k;
for(int i = 1, n = tmp.num;i < 8; i ++){
tnum[i] = n % 7;
if(tnum[i] == 0) k = i;
n /= 7;
}
for(int j = 1; j < 8; j ++)
if(tu[k][j]){
t.cnt = tmp.cnt + 1;
t.num = tmp.num - p7[j] * tnum[j] + p7[k] * tnum[j];
if(p[t.num].num == 0) {
t.str = tmp.str + (char)(tnum[j] + 'A' - 1);
q.push_back(t);
p[t.num] = t;
}
}
}
}
int main(){
init();
string s;
int T, t;
scanf("%d", &T);
while(T --){
scanf("%d", &t);
cin>>s;
int num = 0;
for(int i = 0; i < s.length(); i ++){
num += p7[i + 1] * (s[i] - 'A'+ 1);
}
s = p[num].str;
string str(s.rbegin(), s.rend());
if(p[num].cnt == 0 && num != 114381) cout<<t<<" "<<"NO SOLUTION"<<endl;
else cout<<t<<" "<<p[num].cnt<<" "<<str<<endl;
}
}

59
HDOJ/3288_autoAC.cpp Normal file
View File

@ -0,0 +1,59 @@
#include<iostream>
#include<algorithm>
#include<queue>
#define MAXN 10010
using namespace std;
struct node
{
int pri,num;
bool friend operator<(const node a,const node b)
{
if(a.pri!=b.pri)
return a.pri>b.pri;
return a.num>b.num;
}
};
int hash1[MAXN],h;
priority_queue<node> Q[MAXN];
int main()
{
int n,x,y,r,cc;
char str[10];
while(scanf("%d",&n)==1)
{
h=0;
cc=1;
for(int i=0;i<=n;i++)
while(!Q[i].empty())
Q[i].pop();
memset(hash1,-1,sizeof(hash1));
while(n--)
{
node temp;
scanf("%s",str);
if(str[0]=='R')
{
scanf("%d %d",&x,&y);
if(hash1[x]==-1)
hash1[x]=h++;
temp.num=cc++;
temp.pri=y;
Q[hash1[x]].push(temp);
}
else {
scanf("%d",&r);
if(hash1[r]==-1)
hash1[r]==h++;
if(Q[hash1[r]].empty())
printf("No one fits!\n");
else
{
temp = Q[hash1[r]].top();
printf("%s gets Num %d: %d %d!\n",str,temp.num,r,temp.pri);
Q[hash1[r]].pop();
}
}
}
}
return 0;
}

131
HDOJ/3289_autoAC.cpp Normal file
View File

@ -0,0 +1,131 @@
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int MAXN = 100010;
int spos[MAXN],epos[MAXN],sum[MAXN],w[MAXN];
int n,f[MAXN],index;
bool vis[MAXN];
struct edge
{
int u,v,w;
edge(int a=0,int b=0,int c=0):u(a),v(b),w(c){}
bool friend operator<(const edge a,const edge b)
{
return a.w<b.w;
}
};
priority_queue<edge> Q;
vector<int> g[MAXN];
void init()
{
for(int i=0;i<=n;i++)
{
f[i]=i;
w[i]=sum[i]=0;
g[i].clear();
}
}
int find(int x)
{
if(x==f[x])
return f[x];
f[x]=find(f[x]);
return f[x];
}
void Union(int x,int y)
{
int a=find(x);
int b=find(y);
if(a==b) return ;
f[a]=b;
g[x].push_back(y);
g[y].push_back(x);
}
void Kruskal()
{
while(!Q.empty())
{
Union(Q.top().u,Q.top().v);
Q.pop();
}
}
int lowbit(int x)
{
return x&(-x);
}
void modify(int x,int add)
{
while(x<=n)
{
sum[x]+=add;
x+=lowbit(x);
}
}
int get_sum(int x)
{
int ret=0;
while(x!=0)
{
ret+=sum[x];
x-=lowbit(x);
}
return ret;
}
void dfs(int u)
{
spos[u]=index;
vis[u]=true;
for(int i=0;i<g[u].size();i++)
{
int v=g[u][i];
if(!vis[v])
{
index++;
dfs(v);
}
}
epos[u]=index;
}
int main()
{
int m,k,a,b,c;
char str[5];
while(scanf("%d %d",&n,&m)==2)
{
init();
while(m--)
{
scanf("%d %d %d",&a,&b,&c);
Q.push(edge(a,b,c));
}
Kruskal();
index=1;
memset(vis,false,sizeof(vis));
dfs(1);
scanf("%d",&m);
while(m--)
{
scanf("%s",str);
if(str[0]=='G')
{
scanf("%d %d",&a,&c);
modify(spos[a],c);
w[a]+=c;
}
else if(str[0]=='C')
{
scanf("%d",&a);
modify(spos[a],-w[a]);
w[a]=0;
}
else {
scanf("%d",&a);
int ans=get_sum(epos[a])-get_sum(spos[a]-1);
printf("%d\n",ans);
}
}
}
return 0;
}

46
HDOJ/3290_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include<iostream>
#include<algorithm>
#include<vector>
#define MAXN 20000+10
using namespace std;
vector<int> g[MAXN];
int n,v[MAXN],in[MAXN],out[MAXN];
int dfs(int cnt)
{
if(out[cnt]==0)
return cnt;
vector<int> ss;
for(int i=0;i<out[cnt];i++)
ss.push_back(dfs(g[cnt][i]));
sort(ss.begin(),ss.end());
return ss[(out[cnt]+1)/2-1];
}
int main()
{
int a;
while(scanf("%d",&n)==1)
{
for(int i=1;i<=n;i++)
g[i].clear();
memset(in,0,sizeof(in));
for(int i=1;i<=n;i++)
{
scanf("%d",&out[i]);
for(int j=0;j<out[i];j++)
{
scanf("%d",&a);
g[i].push_back(a);
in[a]++;
}
}
int root;
for(int i=1;i<=n;i++)
if(in[i]==0)
{
root=i;
break;
}
printf("%d\n",dfs(root));
}
return 0;
}

154
HDOJ/3291_autoAC.cpp Normal file
View File

@ -0,0 +1,154 @@
#include <algorithm>
#include <cmath>
using namespace std;
const double eps=1e-9;
const double maxl=1e10;
const double pi=3.1415926535897932384626433832795;
const int maxv=20;
template <class T>
T Max(T a,T b){return a>b?a:b;}
template <class T>
T Min(T a,T b){return a<b?a:b;}
double sqr(double a){return a*a;}
int sgn(double a){if (a>eps) return 1;if (a<-eps) return -1;return 0;}
struct Point
{
double x,y;
Point(){};
Point(double dx,double dy){x=dx;y=dy;}
inline int init(){ return scanf("%lf%lf",&x,&y); }
};
double dis(Point a,Point b);
Point operator -(Point a,Point b){Point c;c.x=a.x-b.x;c.y=a.y-b.y;return c;}
Point operator +(Point a,Point b){Point c;c.x=a.x+b.x;c.y=a.y+b.y;return c;}
Point operator *(Point a,double d){Point c;c.x=a.x*d;c.y=a.y*d;return c;}
Point operator *(double d,Point a){Point c;c.x=a.x*d;c.y=a.y*d;return c;}
Point operator /(Point a,double d){Point c;c.x=a.x/d;c.y=a.y/d;return c;}
double operator *(Point a,Point b){return a.x*b.x+a.y*b.y;}
int operator ==(Point a,Point b){return dis(a,b)<eps;}
int operator !=(Point a,Point b){return dis(a,b)>=eps;}
bool operator < (const Point &l, const Point &r){return sgn(l.y-r.y)<0 ||( sgn(l.y-r.y)==0 && sgn(l.x-r.x)<0 );}
double dis(Point a,Point b){a=a-b;return sqrt(sqr(a.x)+sqr(a.y));}
double crossmuti(Point a,Point b){return a.x*b.y-a.y*b.x;}
struct LineSegment
{
Point pt1,pt2;
LineSegment(){};
LineSegment(Point p1,Point p2){pt1=p1;pt2=p2;}
};
struct Polygon
{
Point vertex[maxv];
int numv;
Polygon(){numv=0;}
Polygon(Point *s,int num){numv=num;memcpy(vertex,s,num*sizeof(Point));}
Point &operator [](int k){return vertex[k];}
};
struct Line
{
double a,b,c;
Line(){};
Line(double p,double q,double r){a=p;b=q;c=r;}
Line(Point p,Point q)
{
a=q.y-p.y;
b=p.x-q.x;
c=p.y*q.x-p.x*q.y;
double one=sqrt(sqr(a)+sqr(b));
if (one>0) {a/=one;b/=one;c/=one;}
}
Line(LineSegment l)
{
Line ls(l.pt1,l.pt2);
a=ls.a;b=ls.b;c=ls.c;
}
double func(Point &p){return a*p.x+b*p.y+c;}
};
int ondiffside(Line l,Point a,Point b)
{
return sgn(l.func(a))*sgn(l.func(b))<0;
}
int PntonSeg(LineSegment ls,Point p)
{
Line l(ls.pt1,ls.pt2);
if (ls.pt1==p||ls.pt2==p) return 1;
if (sgn(l.func(p))==0&&((ls.pt1<p&&p<ls.pt2)||(ls.pt2<p&&p<ls.pt1))) return 1;
return 0;
}
int Intersect(LineSegment L1, LineSegment L2)
{
if (ondiffside(L1,L2.pt1,L2.pt2)&&ondiffside(L2,L1.pt1,L1.pt2))
return 1;
if (PntonSeg(L1,L2.pt1)||PntonSeg(L1,L2.pt2)||PntonSeg(L2,L1.pt1)||PntonSeg(L2,L1.pt2))
return -1;
return 0;
}
int intersection(Line p,Line q,Point &s)
{
if (sgn(p.a*q.b-p.b*q.a)==0) return 0;
s.y=(p.a*q.c-p.c*q.a)/(q.a*p.b-p.a*q.b);
s.x=(p.b*q.c-p.c*q.b)/(p.a*q.b-p.b*q.a);
return 1;
}
int poly_lineSg_cross(Polygon &p,LineSegment l,Point &s)
{
int i;
int n=p.numv;
if (n==0) return 0;
p[n]=p[0];
Point t;
int find=0;
for (i=0;i<n;i++)
if (Intersect(LineSegment(p[i],p[i+1]),l))
{
if (intersection(Line(p[i],p[i+1]),Line(l.pt1,l.pt2),t)==0)
t=p[i];
if (find==0||dis(Point(0,0),t)<dis(Point(0,0),s))
s=t;
find=1;
}
return find;
}
Polygon p[50];
int w[50];
int n,m;
int cat[50];
Point ins[50];
void work()
{
int i,j,k;
int tot=0;
for (i=0;i<n;i++)
{
scanf("%d%d",&p[i].numv,w+i);
for (j=0;j<p[i].numv;j++) p[i][j].init();
}
scanf("%d",&m);
while (m--)
{
scanf("%d",&k);
LineSegment l;
l.pt1=Point(0,0);
l.pt2=maxl*Point(cos((double)-k/180.0*pi),sin((double)-k/180.0*pi));
for (i=0;i<n;i++)
cat[i]=poly_lineSg_cross(p[i],l,ins[i]);
int t=-1;
for (i=0;i<n;i++)
if (cat[i])
if (t==-1||dis(Point(0,0),ins[i])<dis(Point(0,0),ins[t]))
t=i;
if (t>=0)
{
p[t].numv=0;
tot+=w[t];
w[t]=0;
}
}
printf("%d\n",tot);
}
int main()
{
while (scanf("%d",&n)!=EOF)
work();
return 0;
}

71
HDOJ/3292_autoAC.cpp Normal file
View File

@ -0,0 +1,71 @@
#include <cstdio>
#include <cmath>
using namespace std;
#define mod 8191
const int maxn=5;
typedef struct
{
int m[maxn][maxn];
}Matrix;
int N,x,y;
void search()
{
y=1;
while(1){
x=sqrt(N*y*y+1.0);
if(x*x-N*y*y==1) break;
y++;
}
}
Matrix per,d;
void init()
{
d.m[0][0]=x%mod;
d.m[0][1]=y*N%mod;
d.m[1][0]=y%mod;
d.m[1][1]=x%mod;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
per.m[i][j]= (i==j);
}
Matrix Multi(Matrix a,Matrix b)
{
Matrix c;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++){
c.m[i][j]=0;
for(int k=0;k<2;k++)
c.m[i][j]+=(a.m[i][k]*b.m[k][j])%mod;
c.m[i][j]%=mod;
}
return c;
}
Matrix power(Matrix a,int k)
{
Matrix r=per;
while(k)
{
if(k&1) r=Multi(r,a);
a=Multi(a,a);
k>>=1;
}
return r;
}
int main()
{
int k;
while(~scanf("%d%d",&N,&k))
{
int tmp=sqrt(N+0.0);
if(tmp*tmp==N){
printf("No answers can meet such conditions\n");
continue;
}
search();
init();
d=power(d,k-1);
x=(d.m[0][0]*x%mod+d.m[0][1]*y%mod)%mod;
printf("%d\n",x);
}
return 0;
}

56
HDOJ/3293_autoAC.cpp Normal file
View File

@ -0,0 +1,56 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct NODE
{
char name[30];
char lv[30];
char ori[30];
} girl[505];
int cmp(NODE x,NODE y)
{
if(strcmp(x.ori,y.ori))
return strcmp(x.ori,y.ori)<0;
if(strcmp(x.lv,y.lv))
{
if(!strcmp(x.lv,"wonderful"))
return 1;
if(!strcmp(y.lv,"wonderful"))
return 0;
if(!strcmp(y.lv,"so-so"))
return 1;
if(!strcmp(x.lv,"so-so"))
return 0;
}
return strcmp(x.name,y.name)<0;
}
int main()
{
int n,i,cas = 1;
while(~scanf("%d",&n))
{
for(i = 0; i<n; i++)
scanf("%s%s%s",girl[i].name,girl[i].ori,girl[i].lv);
printf("Case %d\n",cas++);
sort(girl,girl+n,cmp);
char s[30];
strcpy(s,girl[0].ori);
int flag = 1;
for(i = 0; i<n; i++)
{
if(flag)
{
printf("%s:\n",s);
flag = 0;
}
printf(" %s %s\n",girl[i].name,girl[i].lv);
if(strcmp(s,girl[i+1].ori))
{
strcpy(s,girl[i+1].ori);
flag = 1;
}
}
}
return 0;
}

38
HDOJ/3294_autoAC.cpp Normal file
View File

@ -0,0 +1,38 @@
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std;
const int MAX=200000+10;
char s[MAX*2],temp[2];
int p[MAX*2];
int main(){
while(scanf("%s%s",temp,s)!=EOF){
int len=strlen(s),id=0,maxid=0;
for(int i=len;i>=0;--i){
s[i+i+2]=s[i];
s[i+i+1]='#';
}
s[0]='*';
for(int i=2;i<2*len+1;++i){
if(p[id]+id>i)p[i]=min(p[2*id-i],p[id]+id-i);
else p[i]=1;
while(s[i+p[i]] == s[i-p[i]])++p[i];
if(id+p[id]<i+p[i])id=i;
if(p[maxid]<p[i])maxid=i;
}
if(p[maxid]-1<2)cout<<"No solution!"<<endl;
else{
cout<<(maxid-p[maxid]+2)/2-1<<' '<<(maxid+p[maxid]-2)/2-1<<endl;
int k=temp[0]-'a';
for(int i=maxid-p[maxid]+2;i<=maxid+p[maxid]-2;i+=2)printf("%c",(s[i]-'a'-k+26)%26+'a');
cout<<endl;
}
}
return 0;
}

184
HDOJ/3295_autoAC.cpp Normal file
View File

@ -0,0 +1,184 @@
#include<stdio.h>
#include<cstdio>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
struct Map
{
int map[6][6];
}star;
struct P
{
int x, y;
};
int n, m, ans;
int di[4][2] = {0,1, -1,0, 0,-1, 1,0};
int check(struct Map &node)
{
int i, j;
for( i = 0 ; i < n ; i++)
for( j = 0 ; j < m ; j++)
if( node.map[i][j] != 0)
return 0;
return 1;
}
int print( struct Map node)
{
int i, j;
printf("\n");
for( i = 0 ; i < n ; i++)
{
for( j = 0 ; j < m ; j++)
printf("%d ", node.map[i][j]);
printf("\n");
}
printf("\n");
return 0;
}
int moveh(struct Map &node)
{
int i, j, flog, flog2, b;
flog2 = 0;
for( j = 0 ; j < m ; j++)
{
flog = 0;
for( i = 0 ; i < n ; i++)
if(node.map[i][j] != 0)
flog = 1;
if( flog == 1 && flog2 == 1)
{
for( i = 0 ; i < n ; i++)
node.map[i][b] = node.map[i][j];
for( i = 0 ; i < n ; i++)
node.map[i][j] = 0;
b++;
}else if( flog == 0 && flog2 == 0)
{
flog2 = 1;
b = j;
}
}
return 0;
}
int moves(int s, struct Map &node)
{
int i, j, flog, b;
flog = 0;
for( i = n - 1 ; i >= 0 ; i--)
{
if( node.map[i][s] == 0 && flog == 0)
{
b = i;
flog = 1;
}
else if( node.map[i][s] != 0 && flog == 1)
{
node.map[b][s] = node.map[i][s];
node.map[i][s] = 0;
b--;
}
}
return 0;
}
int movemap( struct Map &node)
{
int i;
for( i = 0 ; i < m ; i++)
moves(i, node);
moveh(node);
return 0;
}
int change_map(struct Map &node, int mark[][6], int x, int y)
{
int i, j, l, num, flog;
queue<struct P> Q;
struct P per, next;
per.x = x;
per.y = y;
num = node.map[x][y];
mark[x][y] = true;
Q.push(per);
while(!Q.empty())
{
per = Q.front();
Q.pop();
if( node.map[per.x][per.y] == 0)
continue;
node.map[per.x][per.y] = 0;
for(l = 0 ; l < 4 ; l++)
{
i = per.x + di[l][0];
j = per.y + di[l][1];
if( i < 0 || j < 0 || i >= n || j >= m)
continue;
next.x = i;
next.y = j;
if( node.map[i][j] == num)
{
Q.push(next);
mark[i][j] = true;
}
}
}
return 0;
}
int r_map(struct Map &a, struct Map &s)
{
int i, j;
for( i = 0 ; i < n ; i++)
for( j = 0 ; j < m ; j++)
a.map[i][j] = s.map[i][j];
return 0;
}
int DFS(int k, struct Map per)
{
int i, j;
int mark[6][6];
struct Map temp;
memset(mark, 0, sizeof(mark));
if( check(per) == 1)
{
if( k < ans)
ans = k;
return 0;
}
for( i = 0 ; i < n ; i++)
{
for( j = 0 ; j < m ; j++)
{
if( per.map[i][j] == 0)
continue;
if( mark[i][j] == 0)
{
mark[i][j] = 1;
r_map( temp, per);
change_map(per, mark, i, j);
movemap(per);
DFS(k + 1, per);
r_map(per, temp);
}
}
}
return 0;
}
int input()
{
int i, j;
for( i = 0 ; i < n ; i++)
for( j = 0 ; j < m ; j++)
scanf("%d", &star.map[i][j]);
return 0;
}
int main()
{
int i, j;
while(scanf("%d%d", &n, &m) == 2)
{
input();
ans = 65;
DFS(0, star);
printf("%d\n", ans);
}
return 0;
}

42
HDOJ/3296_autoAC.cpp Normal file
View File

@ -0,0 +1,42 @@
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
const double eps = 1e-9;
const double pi = 3.1415926535897932384626;
#define INF 1e18
const int maxn = 2017;
int main()
{
int s[maxn], t[maxn];
int S, T, M;
int ID, P, Q;
int cas = 0;
while(scanf("%d%d%d",&S,&T,&M))
{
if(S==0 && T==0 && M==0)
break;
memset(s,0,sizeof(s));
memset(t,0,sizeof(t));
int sum = 0;
for(int i = 1; i <= S; i++)
{
scanf("%d%d%d",&ID,&P,&Q);
sum+=P+Q;
}
int Sid, Tot;
for(int i = 1; i <= T; i++)
{
scanf("%d%d",&Sid,&Tot);
if(Tot >= M && t[Sid] == 0)
{
sum++;
t[Sid] = 1;
}
}
printf("Case %d: %d\n",++cas,sum);
}
return 0;
}

95
HDOJ/3297_autoAC.cpp Normal file
View File

@ -0,0 +1,95 @@
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<ctime>
#include<deque>
#include<cmath>
#include<vector>
#include<string>
#include<cctype>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#define REP(i,s,t) for(int i=(s);i<=(t);i++)
#define REP2(i,t,s) for(int i=(t);i>=s;i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned long ul;
const int N=20;
const int Maxans=10240+10;
int a[N];
int bit[N];
vector<int>G[Maxans];
int state[1<<17];
bool judge(int x)
{
int cnt=0;
REP(i,1,16)
{
if(x&(1<<(i-1)))
{
bit[++cnt]=a[i];
}
}
return cnt==4;
}
int fuck()
{
REP(i,0,Maxans)
{
G[i].clear();
}
memset(state,0,sizeof(state));
sort(a+1,a+1+16);
REP(i,0,(1<<16)-1)
{
if(judge(i))
{
do
{
int tmp=bit[1]*1+bit[2]*2+bit[3]*3+bit[4]*4;
for(int j=0;j<G[tmp].size();j++)
{
if((i&G[tmp][j])==0)
{
state[i|G[tmp][j]]++;
}
}
G[tmp].push_back(i);
}while(next_permutation(bit+1,bit+1+4));
}
}
int ans=0;
REP(i,0,(1<<16)-1)
{
ans+=state[i]*state[((1<<16)-1)^i];
}
return ans/2;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("test.in","r",stdin);
#endif
int ca=1;
while(~scanf("%d",&a[1]))
{
if(!a[1])
{
break;
}
REP(i,2,16)
{
scanf("%d",&a[i]);
}
int ans=fuck();
printf("Case %d: %d\n",ca++,ans);
}
return 0;
}

72
HDOJ/3298_autoAC.cpp Normal file
View File

@ -0,0 +1,72 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cmath>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <time.h>;
#define cler(arr, val) memset(arr, val, sizeof(arr))
typedef long long LL;
const int MAXN = 200010;
const int MAXM = 200006;
const int INF = 0x3f3f3f3f;
const int mod = 10000007;
LL point[MAXN],num[MAXN];
vector<int>edge[MAXN];
bool vis[MAXN];
LL abs1(LL a)
{
if(a>0) return a;
else return -a;
}
void dfs(int u)
{
vis[u]=true;
for(int i=0;i<edge[u].size();i++)
{
int v=edge[u][i];
if(!vis[v])
{
dfs(v);
point[u]+=point[v];
}
}
}
int main()
{
int m,n,cas=0;
while(scanf("%d%d",&n,&m),m+n)
{
LL sum=0;
cler(point,0);
cler(num,0);
for(int i=1;i<=n;i++)
{
edge[i].clear();
scanf("%I64d",&point[i]);
sum+=point[i];
}
for(int i=1;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
edge[a].push_back(b);
edge[b].push_back(a);
}
LL ans=9999999999999;
cler(vis,false);
dfs(1);
for(int i=1;i<=n;i++)
ans=min(ans,abs1(sum-2*point[i]));
printf("Case %d: %I64d\n",++cas,ans);
}
}

89
HDOJ/3299_autoAC.cpp Normal file
View File

@ -0,0 +1,89 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <math.h>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <time.h>;
#define cler(arr, val) memset(arr, val, sizeof(arr))
typedef long long LL;
const int MAXN = 100010;
const int MAXM = 20006;
const int INF = 0x3f3f3f3f;
const int mod = 10000007;
struct node
{
int x,y;
}point[200];
bool cmp(node a,node b)
{
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}
int y[123],on1[111],on2[111],line[111];
int solve(int n,int m)
{
cler(on1,0);
cler(on2,0);
cler(line,0);
int ans=0;
if(m<=2) return n;
for(int i=0;i<m;i++)
{
for(int j=i+1;j<m;j++)
{
int y1=y[i],y2=y[j];
int num=0;
for(int k=0;k<n;k++)
{
if(k==0||point[k].x!=point[k-1].x)
{
num++;
on1[num]=on2[num]=0;
line[num]=line[num-1]+on2[num-1]-on1[num-1];
}
if(point[k].y>y1&&point[k].y<y2) on1[num]++;
if(point[k].y>=y1&&point[k].y<=y2) on2[num]++;
}
if(num<=2) return n;
int maxv=0;
for(int k=1;k<=num;k++)
{
ans=max(ans,line[k]+on2[k]+maxv);
maxv=max(maxv,on1[k]-line[k]);
}
}
}
return ans;
}
int main()
{
int t,cas=1;
while(scanf("%d",&t),t)
{
for(int i=0;i<t;i++)
{
scanf("%d%d",&point[i].x,&point[i].y);
y[i]=point[i].y;
}
sort(y,y+t);
int tol2=1;
for(int i=1;i<t;i++)
{
if(y[i]!=y[i-1])
y[tol2++]=y[i];
}
sort(point,point+t,cmp);
printf("Case %d: %d\n",cas++,solve(t,tol2));
}
}