UVALive 6454 Lights Against Dudely
题目中能往4个方向转的灯只有一盏!!英语有毒
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<sstream>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define MAXN (1000+10)
char s[MAXN][MAXN];
int x[MAXN],y[MAXN],k,n,m;
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
bool inside(int x,int y) {return 1<=x&&x<=n&&1<=y&&y<=m;}
int ans;
bool avi(int x,int y) {
if (!inside(x,y)) return 1;
if (s[x][y]!='#') return 1;
return 0;
}
int b[MAXN][MAXN];
void dfs(int i,int p,int t,int l) {
if (p>=ans) return ;
if (i>k) {
if (t>=k) ans=min(ans,p);
return ;
}
int nowx=x[i],nowy=y[i];
Rep(di,4) {
if (l==1&&di!=3) continue;
int di2=(di+1)%4;
int nowx1=x[i]+dir[di][0],nowy1=y[i]+dir[di][1];
int nowx2=x[i]+dir[di2][0],nowy2=y[i]+dir[di2][1];
if (avi(nowx1,nowy1)&&avi(nowx2,nowy2)) {
int fl1=b[nowx][nowy]; b[nowx][nowy]=0;
int fl2=b[nowx1][nowy1]; b[nowx1][nowy1]=0;
int fl3=b[nowx2][nowy2]; b[nowx2][nowy2]=0;
int c=fl1+fl2+fl3;
if (c) dfs(i+1,p+1,t+c,l|(di!=3));
b[nowx][nowy]=fl1;
b[nowx1][nowy1]=fl2;
b[nowx2][nowy2]=fl3;
}
}
dfs(i+1,p,t,l);
}
int main()
{
// freopen("A.in","r",stdin);
// freopen(".out","w",stdout);
while(cin>>n>>m) {
MEM(b)
if (!n&&!m) break;
For(i,n) scanf("%s",s[i]+1);
k=0;
For(i,n) For(j,m) if (s[i][j]=='.') {
x[++k]=i,y[k]=j; b[i][j]=1;
}
if (k==0) puts("0");
else {
ans=INF;
dfs(1,0,0,0);
if (ans==INF) ans=-1;
printf("%d\n",ans);
}
}
return 0;
}
UVALive 6455 Stealing Harry Potter’s Precious
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<sstream>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define MAXN (1000+10)
char s[MAXN][MAXN];
int x[MAXN],y[MAXN],k,n,m;
int f[MAXN][MAXN];
int d[MAXN][MAXN],dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
bool inside(int x,int y) {return 1<=x&&x<=n&&1<=y&&y<=m;}
void bfs(int sx,int sy,int p) {
queue<pi > q;
MEMI(d)
q.push(mp(sx,sy)); d[sx][sy]=0;
while(!q.empty()) {
int x=q.front().fi,y=q.front().se;
q.pop();
Rep(di,4) {
int xa=x+dir[di][0],ya=y+dir[di][1];
if (inside(xa,ya)&&d[xa][ya]==INF&&s[xa][ya]!='#') {
d[xa][ya]=d[x][y]+1;
q.push(mp(xa,ya));
}
}
}
For(i,n) f[p][i] = d[x[i]][y[i]];
}
int main()
{
// freopen("B.in","r",stdin);
// freopen(".out","w",stdout);
while(cin>>n>>m) {
if (!n&&!m) break;
For(i,n) scanf("%s",s[i]+1);
k=read();
For(i,k) x[i]=read(),y[i]=read();
For(i,n) For(j,m) if (s[i][j]=='@') {
x[++k]=i,y[k]=j; break;
}
For(i,k) bfs(x[i],y[i],i);
ll ans=INF;
int pe[100]={k};
For(i,k-1) pe[i]=i;
do {
ll t=0;
For(i,k-1) t+=(ll)f[pe[i-1]][pe[i]];
ans=min(ans,t);
} while(next_permutation(pe+1,pe+k));
if (ans>=INF) puts("-1");
else cout<<ans<<endl;
}
return 0;
}
UVALive 6456 Zhuge Liang’s Password
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<sstream>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
int n;
int a[50][50],b[5][50][50];
void r(int a[50][50],int b[50][50]) {
For(i,n) For(j,n) b[n-j+1][i]=a[i][j];
}
ll ans;
void c(int b[50][50]) {
ll t=0;
For(i,n) For(j,n) t+=(a[i][j]==b[i][j]);
ans=max(ans,t);
}
int main()
{
// freopen("C.in","r",stdin);
// freopen(".out","w",stdout);
while(cin>>n&&n) {
ans=0;
For(i,n) For(j,n) cin>>a[i][j];
For(i,n) For(j,n) cin>>b[0][i][j];
c(b[0]);
r(b[0],b[1]); c(b[1]);
r(b[1],b[2]); c(b[2]);
r(b[2],b[3]); c(b[3]);
cout<<ans<<endl;
}
return 0;
}
UVALive 6460 Ants
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<sstream>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define MAXN (300000+10)
vector<int> edge[MAXN];
vector<ll> weight[MAXN];
ll a[MAXN];
int n,m;
void dfs(int x,int fa) {
int sz=SI(edge[x]);
Rep(i,sz) {
int v=edge[x][i];
if (v!=fa) {
a[v]^=a[x]^weight[x][i];
dfs(v,x);
}
}
}
#define MAXNode (100000*61+10)
#define Sigma_size (2)
void chang(char s[],ll p) {
Rep(j,61) {
s[j]='0' + (p&1);
p/=2;
}
reverse(s,s+61);
s[61]=0;
}
class Trie
{
public:
int ch[MAXNode][Sigma_size],siz;
ll v[MAXNode],sz[MAXNode];
void mem(int _siz=0){siz=_siz; MEM(ch) MEM(v) MEM(sz) }
int idx(char c){return c-'0';}
void insert(char *s)
{
int u=0,n=strlen(s);
Rep(i,n)
{
int c=idx(s[i]);
if (!ch[u][c])
{
++siz;
MEM(ch[siz]);
ch[u][c]=siz;
v[siz]=v[u]*2+c;
}
u=ch[u][c];
}
sz[u]++;
}
void prework() {
RepD(u,siz) {
Rep(j,2) if (ch[u][j]) sz[u]+=sz[ch[u][j]];
}
}
ll find(int k,ll p) {
char s[100];
chang(s,p);
int u=0,n=strlen(s);
ll ret=0;
Rep(i,n) {
int c=idx(s[i]);
if (ch[u][c^1]&&sz[ch[u][c^1]]>=k)
{
u=ch[u][c^1]; ret=ret*2+1;
} else
{
if (ch[u][c^1]) k-=sz[ch[u][c^1]];
u=ch[u][c];
ret*=2;
}
}
return ret;
}
}T;
#define MAXK (400000+10)
ll ans[MAXK];
typedef pair<ll,pi > p3;
int main()
{
// freopen("G.in","r",stdin);
// freopen(".out","w",stdout);
while(cin>>n&&n) {
For(i,n-1) {
int u=read(),v=read();
ll w; cin>>w;
edge[u].pb(v); edge[v].pb(u);
weight[u].pb(w); weight[v].pb(w);
}
MEM(a)
dfs(1,0);
T.mem();
For(i,n) {
char s[100];
chang(s,a[i]);
T.insert(s);
}
T.prework();
ll maxk=min((ll)n*(n-1),200000LL);
priority_queue< p3 > q;
For(i,n) q.push(mp( T.find(1,a[i]) , mp(i,1) ) );
For(i,maxk) {
p3 now=q.top();
q.pop();
ans[i]=now.fi;
if (now.se.se<n-1) {
++now.se.se;
q.push(mp( T.find(now.se.se,a[now.se.fi ] ) ,now.se) );
}
}
m=read();
while(m--) {
ll k=read();
if (k>(ll)n*(n-1)) {
puts("-1");
} else {
cout<<ans[(int)k]<<endl;
}
}
For(i,n) edge[i].clear(),weight[i].clear();
}
return 0;
}