T1Matrix
题解:
直接做矩阵乘法肯定是要GG的
考虑对于每个询问,我们需要查询的是相乘之后的一个子矩阵的元素和
那么对于一个 <nobr> ∑x2i=x1∑y2j=y1c[i][j] </nobr>,可以由 <nobr> ∑nk=1(∑y2i=y1a[i][k]×∑x2j=x1b[k][j]) </nobr>算得
那么维护a的每一列中的前缀和以及b每一行的前缀和,就可以 <nobr> O(n) </nobr>解决一个询问,总复杂度 <nobr> O(nm) </nobr>
//by sdfzchy
#include<map>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#define mp make_pair
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=2010;
LL n,m,a[N][N],b[N][N];
int x[N];
int main()
{
freopen("matrix.in","r",stdin);
freopen("matrix.out","w",stdout);
scanf("%lld%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&a[i][j]),a[i][j]+=a[i-1][j];
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&b[i][j]),b[i][j]+=b[i][j-1];
for(int i=1,x,y,xx,yy;i<=m;i++)
{
scanf("%d%d%d%d",&x,&y,&xx,&yy);
if(x>xx) swap(x,xx);
if(y>yy) swap(y,yy);
LL ans=0;
for(int j=1;j<=n;j++) ans+=(a[xx][j]-a[x-1][j])*(b[j][yy]-b[j][y-1]);
printf("%lld\n",ans);
}
return 0;
}
T2Tower
思路题
易证所有点移到同一位置,这个点和横坐标和纵坐标都一定在给出点中出现过
所以枚举所有可能出现的横坐标和纵坐标,再暴力计算每个点到这个点的距离,从小到大排序后更新答案即可,复杂度 <nobr> O(n3logn) </nobr>
//by sdfzchy
#include<map>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#define mp make_pair
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
int n;
int x[60],y[60],ans[60],tmp[60];
int main()
{
freopen("tower.in","r",stdin);
freopen("tower.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d%d",&x[i],&y[i]);
memset(ans,0x3f,sizeof(ans));
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
for(int k=1;k<=n;k++) tmp[k]=abs(x[i]-x[k])+abs(y[j]-y[k]);
sort(tmp+1,tmp+n+1);
int sum=0;
for(int i=1;i<=n;i++) sum+=tmp[i],ans[i]=min(ans[i],sum);
}
for(int i=1;i<=n;i++) printf("%d\n",ans[i]);
return 0;
}
T3Tree
树上最大匹配即方案数,外加高精度(懒得写了QAQ)
用 <nobr> f[i][0/1] </nobr>表示以 <nobr> i </nobr>为根的子树中选/不选 <nobr> i </nobr>的最大匹配, <nobr> g[i][0/1] </nobr>表示 <nobr> f </nobr>达到最大匹配时的方案数, <nobr> f′[i] </nobr>表示i的最大匹配, <nobr> g′[i] </nobr>表示对应于 <nobr> f′ </nobr>的方案数
<nobr> f[i][0]=∑max(f[j][0],f[j][1]) </nobr>
<nobr> f[i][1]=max(f[i][0]−f′[j]+f[j][0]+1) </nobr>
<nobr> g[i][0]=∏g[j][0] </nobr>
<nobr> g[i][1]=g[i][0]/g′[j]×g[j][0] </nobr>
其中g是随f的更新而更新的
理论上不连高精度复杂度 <nobr> O(n) </nobr>
//by sdfzchy
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N=1010;
int n,m;
int f[N][2],ff[N],gg[N],head[N],ecnt,fa[N];
LL g[N][2];
struct edge
{
int v,nxt;
}e[N<<1];
void add_edge(int u,int v)
{
e[++ecnt].nxt=head[u];
e[ecnt].v=v;
head[u]=ecnt;
}
void dfs(int u)
{
g[u][0]=1;
for(int i=head[u];i;i=e[i].nxt)
{
int v=e[i].v;
if(fa[u]==v) continue;
fa[v]=u;
dfs(v);
f[u][0]+=ff[v];
g[u][0]*=gg[v];
}
for(int i=head[u];i;i=e[i].nxt)
{
int v=e[i].v;
if(fa[u]==v) continue;
LL fff=f[u][0]-ff[v]+f[v][0]+1;
LL ggg=g[u][0]/gg[v]*g[v][0];
if(fff>f[u][1]) f[u][1]=fff,g[u][1]=ggg;
else if(fff==f[u][1]) g[u][1]=g[u][1]+ggg;
}
if(f[u][1]>ff[u]) ff[u]=f[u][1],gg[u]=g[u][1];
if(f[u][0]>ff[u]) ff[u]=f[u][0],gg[u]=g[u][0];
if(f[u][0]==f[u][1]) ff[u]=f[u][0],gg[u]=g[u][0]+g[u][1];
}
int main()
{
freopen("tree.in","r",stdin);
freopen("tree.out","w",stdout);
scanf("%d",&n);
for(int i=1,u,num;i<=n;i++)
{
scanf("%d%d",&u,&num);
for(int j=1,v;j<=num;j++)
{
scanf("%d",&v);
add_edge(u,v);
add_edge(v,u);
}
}
dfs(1);
if(f[1][0]<f[1][1]) printf("%d\n%lld\n",f[1][1],g[1][1]);
else if(f[1][0]==f[1][1]) printf("%d\n%lld\n",f[1][1],g[1][1]+g[1][0]);
else printf("%d\nlld\n",f[1][0],g[1][0]);
return 0;
}
PS:这套题AK的好多啊,貌似考水了