一、已AC的题
D题(zoj3365)
开始看题后没发现什么思路,跑去想C题了,后来发现其他人瞬间把D过了,跑回去看题,才发现自己题目看错了,题目要求的是最终输出的是连续数字,及每个数与其对应的基数的差值,保留差值众数对应的数即可。
赛后来和天成讨论了一下,发现这个题目有个bug,输出的结果连续数只能上升么,如果是下降的话那么样例输出就是
2
5 4 3 2 1 0
而不是
3
3 4 5 6 7 8
题目讲的只是连续的数。。。
#include<iostream>
#include<algorithm>
using namespace std;
#define M 50005
int n;
int a[M],b[M],c[M];
int main()
{
int i,j;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
a[i]-=i;
c[i]=a[i];
}
sort(a+1,a+n+1);
int temp=1;
int max=0;
int t=a[1];
for(i=1;i<n;i++)
{
if(a[i]==a[i+1])
temp++;
else
{
if(max<temp)
{
max=max>temp?max:temp;
t=a[i];
}
temp=1;
}
}
if(a[n]==a[n-1])
{
max=max>temp?max:temp;
t=a[i];
}
if(n==1)
cout<<0<<endl;
else
cout<<n-max<<endl;
for(i=1;i<=n;i++)
{
b[i]-=c[i]-t;
}
for(i=1;i<n;i++)
cout<<b[i]<<" ";
cout<<b[n]<<endl;
}
}
二、赛后想想能过的题
H题(zoj3369)
一个比较简单的入门三维DP,之前写过一道类似的运武器的,可以对状态进行简化,使得空间复杂度为n^3
因为题目状态转移涉及四个变量,并且输出还要求输出路径
dp[i][j][k] 表示打第i个monster并且前面使用了j次fight,mana为k是的health
则有状态转移方程
dp[i+1][j+1][k] = max(a, b);
a = dp[i][j][k] - max(2*si - s0, 0) if(s0 + j >= si) //每次使用fight 有s++ 之前总共使用了j次
b = dp[i][j][k-mi] if(p + i - j >= pi && k >= mi)//每次使用enchant 有s++ 之前使用了 i-j次
最后就是逆向输出路径
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 52;
int dp[MAXN][MAXN][MAXN];
char path[MAXN][MAXN][MAXN];
int ss[MAXN] , pp[MAXN] , mm[MAXN];
int n, h, s0, p0, m0;
char ans[MAXN];
void gao()
{
for(int i = 0; i < n; i++)
for(int j = 0; j <= i; j++)
for(int k = 0; k <= m0; k++)
{
if(dp[i][j][k] > 0)
{
if(s0 + j >= ss[i])
{
int temp = max(2*ss[i] - (s0+j), 0);
if(dp[i+1][j+1][k] < dp[i][j][k] - temp)
{
dp[i+1][j+1][k] = dp[i][j][k] - temp;
path[i+1][j+1][k] = 'D';
}
}
if(p0 + i - j >= pp[i] && k >= mm[i])
{
if(dp[i+1][j][k-mm[i]] < dp[i][j][k])
{
dp[i+1][j][k - mm[i]] = dp[i][j][k];
path[i+1][j][k - mm[i]] = 'E';
}
}
}
}
bool isok = false;
int pnum = -1 , mnum = -1;
for(int j = 0; j <= n; j++)
for(int k = 0; k <= m0; k++)
{
if(dp[n][j][k] > 0)
{
pnum = j;
mnum = k;
isok = true;
break;
}
}
if(isok)
{
for(int i = n-1; i >= 0; i--)
{
ans[i] = path[i+1][pnum][mnum];
if(ans[i] == 'E') mnum += mm[i];
else pnum--;
}
cout<<ans<<endl;
}
else cout<<"UNLUCKY"<<endl;
}
int main()
{
while(scanf("%d%d%d%d%d", &n, &h, &s0, &p0, &m0) != EOF)
{
for(int i = 0; i < n; i++) scanf("%d%d%d", &ss[i], &pp[i], &mm[i]);
for(int i = 0; i <= n; i++)
for(int j = 0; j <= n; j++)
for(int k = 0; k <= m0; k++)
dp[i][j][k] = 0;
dp[0][0][m0] = h;
gao();
}
return 0;
}
C题(zoj 3364)
开始总觉得是凸包,想了很久却没有想到处理办法,后来听文杰一点,焕然大悟。。
接下来就不吐槽了,思路是都是对的,想了两种方法计算黑白格子的数量,wa了这么久,就是一个初始化,a[1]和a[n+1]赋值的顺序问题,初始化函数写的是1到n,结果交了十几次才发现。。。
两个代码都贴上吧,一个1k多b,一个5k多b
思路,只计算水平方向的直线,往右走+,往左走-
1:
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define LL long long
#define abs(x) ((x)>0?(x):-(x))
#define mset(a, b) memset(a, b, sizeof(a))
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
const int MAXN = 50020;
LL minx, miny;
struct node
{
LL x, y;
}a[MAXN];
int wwbb = -1;
LL sum, bnum;
int n;
void init()
{
minx = miny = 0xfffffffffffffffll;
for(int i =1; i <= n; i++)
{
if(minx > a[i].x) minx = a[i].x;
if(miny > a[i].y) miny = a[i].y;
}
LL t = abs(0 - minx) + abs(0 - miny);
if(0 == t % 2) wwbb = 1;//黑
else wwbb = 0;//白
for(int i = 1; i <= n; i++)
{
a[i].x = a[i].x + 0 - minx;
a[i].y = a[i].y + 0 - miny;
}
}
LL calc(LL a, LL b)
{
LL ta = (a+1)/2, tb = (b+1)/2;
LL ret = ta*tb;
ta = a-ta;
tb = b-tb;
ret += ta*tb;
if(wwbb == 0 && 1 == a % 2 && 1 == b % 2)
{
return ret - 1;
}else return ret;
}
void gao()
{
for (int i = 1; i <= n; i++)
{
if (a[i].y == a[i+1].y ){
sum += a[i+1].x*a[i+1].y;
bnum += calc(a[i+1].x, a[i+1].y);
sum -= a[i].x*a[i].y;
bnum -= calc(a[i].x, a[i].y);
}
//cout<<bnum<<" ";
}
}
int main()
{
while(scanf("%d", &n) != EOF)
{
for(int i = 1; i <= n; i++)
scanf("%lld%lld", &a[i].x, &a[i].y);
init();
a[n+1] = a[1];
gao();
//cout<<endl;
LL bans = 0, wans = 0;
wans = abs(sum) - abs(bnum);
bans = abs(bnum);
printf("%lld %lld\n", bans, wans);
sum = bnum = 0;
}
return 0;
}
2:
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define LL long long
#define abs(x) ((x)>0?(x):-(x))
#define mset(a, b) memset(a, b, sizeof(a))
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
const int MAXN = 50020;
LL minx, miny;
int wwbb = -1;
struct node
{
LL x, y;
LL w, b;
}a[MAXN];
int n;
void init()
{
minx = miny = 0xfffffffffffffffll;
for(int i =1; i <= n; i++)
{
if(minx > a[i].x) minx = a[i].x;
if(miny > a[i].y) miny = a[i].y;
}
LL t = abs(0 - minx) + abs(0 - miny);
if(0 == t % 2) wwbb = 1;//黑
else wwbb = 0;//白
for(int i = 1; i <= n; i++)
{
a[i].x = a[i].x + 0 - minx;
a[i].y = a[i].y + 0 - miny;
}
}
void gao()
{
if(wwbb == 0)//白为底
{
for(int i = 1; i <= n; i++)
{
if(a[i+1].x > a[i].x)
{
LL t = a[i+1].x - a[i].x;
if(0 == t % 2)
{
a[i].w = -1 * ((t*a[i+1].y)/2);
a[i].b = -1 * ((t*a[i+1].y)/2);
}
if(1 == t % 2)
{
if(0 == a[i].x % 2)
{
a[i].w = -1 * ((t-1)*a[i+1].y/2 + (a[i+1].y + 1)/2);
a[i].b = -1 * ((t-1)*a[i+1].y/2 + (a[i+1].y)/2);
}
if(1 == a[i].x % 2)
{
a[i].b = -1 * ((t-1)*a[i+1].y/2 + (a[i+1].y + 1)/2);
a[i].w = -1 * ((t-1)*a[i+1].y/2 + (a[i+1].y)/2);
}
}
}
if(a[i+1].x < a[i].x)
{
LL t = a[i].x - a[i+1].x;
if(0 == t % 2)
{
a[i].w = ((t*a[i+1].y)/2);
a[i].b = ((t*a[i+1].y)/2);
}
if(1 == t % 2)
{
if(0 == a[i+1].x % 2)
{
a[i].w = ((t-1)*a[i+1].y/2 + (a[i+1].y + 1)/2);
a[i].b = ((t-1)*a[i+1].y/2 + (a[i+1].y)/2);
}
if(1 == a[i+1].x % 2)
{
a[i].b = ((t-1)*a[i+1].y/2 + (a[i+1].y + 1)/2);
a[i].w = ((t-1)*a[i+1].y/2 + (a[i+1].y)/2);
}
}
}
}
}
if(wwbb == 1)//黑为底
{
for(int i = 1; i <= n; i++)
{
if(a[i+1].x > a[i].x)
{
LL t = a[i+1].x - a[i].x;
if(0 == t % 2)
{
a[i].b = -1 * ((t*a[i+1].y)/2);
a[i].w = -1 * ((t*a[i+1].y)/2);
}
if(1 == t % 2)
{
if(0 == a[i].x % 2)
{
a[i].b = -1 * ((t-1)*a[i+1].y/2 + (a[i+1].y + 1)/2);
a[i].w = -1 * ((t-1)*a[i+1].y/2 + (a[i+1].y)/2);
}
if(1 == a[i].x % 2)
{
a[i].w = -1 * ((t-1)*a[i+1].y/2 + (a[i+1].y + 1)/2);
a[i].b = -1 * ((t-1)*a[i+1].y/2 + (a[i+1].y)/2);
}
}
}
if(a[i+1].x < a[i].x)
{
LL t = a[i].x - a[i+1].x;
if(0 == t % 2)
{
a[i].b = ((t*a[i+1].y)/2);
a[i].w = ((t*a[i+1].y)/2);
}
if(1 == t % 2)
{
if(0 == a[i+1].x % 2)
{
a[i].b = ((t-1)*a[i+1].y/2 + (a[i+1].y + 1)/2);
a[i].w = ((t-1)*a[i+1].y/2 + (a[i+1].y)/2);
}
if(1 == a[i+1].x % 2)
{
a[i].w = ((t-1)*a[i+1].y/2 + (a[i+1].y + 1)/2);
a[i].b = ((t-1)*a[i+1].y/2 + (a[i+1].y)/2);
}
}
}
}
}
}
int main()
{
while(scanf("%d", &n) != EOF)
{
for(int i = 1; i <= n; i++)
scanf("%lld%lld", &a[i].x, &a[i].y);
init();
a[n+1] = a[1];
gao();
LL bans = 0, wans = 0;
for(int i = 1; i <= n; i++)
{
bans += a[i].b;
a[i].b = 0;
wans += a[i].w;
a[i].w = 0;
}
printf("%lld %lld\n", bans, wans);
//cout<<bans<<" "<<wans<<endl;
}
return 0;
}
三、能力范围外的题
A题
学了一下最小费用最大流,手敲了一遍模板,木有感觉。。看来图论得弄专题了。。
不过这题和一般的最小费用最大流不一样,把价格换算成赚的钱,及负数。。
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int INF=999999999;
int pre[105],head[105],d[105];
struct hh
{
int f,e,cap,cost,next;
}edge[8050];
int n,m,cent;
void add1(int f,int e,int cap,int cost)
{
edge[cent].f=f;edge[cent].e=e;
edge[cent].cap=cap;edge[cent].cost=cost;
edge[cent].next=head[f];
head[f]=cent++;
}
void add(int f,int e,int cap,int cost)
{
add1(f,e,cap,cost);
add1(e,f,0,-cost);
}
bool spfa(int s,int t)
{
queue<int>q;
int u,v,cap,cost;
bool in[105]={0};
for(int i=1;i<=n+1;i++)
d[i]=INF;
d[s]=0;in[s]=1;
q.push(s);
while(!q.empty())
{
u=q.front();q.pop();in[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].e;cap=edge[i].cap;cost=edge[i].cost;
if(cap>0&&d[v]>d[u]+cost)
{
d[v]=d[u]+cost;
pre[v]=i;
if(!in[v])
{
q.push(v);in[v]=1;
}
}
}
}
return d[t]<0;
}
int EK(int s,int t)
{
int tp=INF;
pre[s]=-1;
for(int i=pre[t];i!=-1;i=pre[edge[i].f])
{
tp=min(tp,edge[i].cap);
}
for(int i=pre[t];i!=-1;i=pre[edge[i].f])
{
edge[i].cap-=tp;
edge[i^1].cap+=tp;
}
return tp*d[t];
}
int main()
{
int price,f,e,cap,cost,ans;
while(scanf("%d%d",&n,&m)!=EOF)
{
cent=0;ans=0;
for(int i=1;i<=n+1;i++)head[i]=-1;
for(int i=2;i<=n;i++)
{
scanf("%d",&price);
add(i,n+1,INF,-price);
}
for(int i=1;i<=m;i++)
{
scanf("%d%d%d%d",&f,&e,&cap,&cost);
add(f,e,cap,cost);
add(e,f,cap,cost);
}
while(spfa(1,n+1))
ans+=EK(1,n+1);
printf("%d\n",-ans);
}
return 0;
}
E题
判断各个六边形联通块小于S时的大小,升序输出,用并查集维护每个联通块的大小
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 50010
#define V 500
const int xx[6]={0,0,-1,-1,1,1} , yy[6]={1,-1,1,0,1,0} , _yy[6]={1,-1,0,-1,0,-1};
int a[N],b[N],p[N],c[N] , num[2*V+10][2*V+10];
bool land[2*V+10][2*V+10];
int n,s,numn;
int find(int x)
{
return x==p[x]?x:p[x]=find(p[x]);
}
int main()
{
while(scanf("%d%d",&n,&s)!=EOF)
{
numn=0;
memset(land,false,sizeof(land));
scanf("%d%d",&a[0],&b[0]);
land[a[0]+V][b[0]+V]=true;
num[a[0]+V][b[0]+V]=0;
p[0]=0; c[0]=1; numn=1;
for(int i=1; i<n; i++)
{
int x,y,ta,tb;
scanf("%d%d",&ta,&tb);
if(land[ta+V][tb+V]) continue;
int sum,numr,r[10];
sum=0; numr=0;
for(int k=0; k<6; k++)
{
x=ta+xx[k];
y=(ta&1)?tb+yy[k]:tb+_yy[k];
//if(x<-V || x>V || y<-V || y>V) continue;
if(!land[x+V][y+V]) continue;
int m=num[x+V][y+V];
int tmp=find(m);
int j;
for(j=0; j<numr; j++) if(r[j]==tmp) break;
if(j==numr) r[numr++]=tmp;
}
for(int k=0; k<numr; k++) sum += c[r[k]];
if(sum<s)
{
a[numn] = ta; b[numn] = tb;
land[ta+V][tb+V] = true;
num[ta+V][tb+V] = numn;
p[numn] = numn; c[numn] = sum+1;
for(int k=0; k<numr; k++) p[r[k]]=numn;
numn++;
}
}
int Count=0,ans[N];
for(int k=0; k<numn; k++) if(p[k]==k) ans[Count++]=c[k];
sort(ans,ans+Count);
printf("%d\n",Count);
for(int k=0; k<Count; k++)
{
printf("%d",ans[k]);
if(k==Count-1) printf("\n");
else printf(" ");
}
}
return 0;
}
F题
整个分为两步,先求偏移量,N^2复杂度的枚举,接着就是一个动态规划求最大子矩阵
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define CY 135
struct P
{
int r, c, r2, c2, s, w, h;
} ans;
char ar[CY][CY], br[CY][CY];
int C[CY], fro[CY], to[CY];
int N, M, N2, M2;
int main(void)
{
while (2 == scanf("%d%d", &N, &M))
{
memset(ar, '@', sizeof(ar));
for (int i = 0; i < N; ++i) scanf("%s", ar[50 + i] + 50);
scanf("%d%d", &N2, &M2);
for (int i = 0; i < N2; ++i) scanf("%s", br[i]);
ans.s = 0;
for (int r = -N2 + 1; r < N; ++r)
{
for (int c = -M2 + 1; c < M; ++c)
{
memset(C, 0, sizeof(C));
for (int i = 0; i < N2; ++i)
{
for (int j = 0; j < M2; ++j)
{
if (ar[50 + i + r][50 + j + c] == br[i][j])
{
C[j]++;
}
else
{
C[j] = 0;
}
}
for (int j = 0; j < M2; ++j)
{
int k = j - 1;
while (k >= 0 && C[k] >= C[j]) k = fro[k];
fro[j] = k;
}
for (int j = M2 - 1; j >= 0; --j)
{
int k = j + 1;
while (k < M2 && C[k] >= C[j]) k = to[k];
to[j] = k;
}
for (int j = 0; j < M2; ++j)
{
int w = to[j] - fro[j] - 1;
int h = C[j];
if (h * w > ans.s)
{
ans.s = h * w;
ans.r2 = i - C[j] + 1;
ans.c2 = fro[j] + 1;
ans.r = r + ans.r2;
ans.c = c + ans.c2;
ans.h = h;
ans.w = w;
}
}
}
}
}
if (ans.s == 0)
{
printf("0 0\n");
}
else
{
printf("%d %d\n", ans.h, ans.w);
printf("%d %d\n%d %d\n", ans.r + 1, ans.c + 1, ans.r2 + 1, ans.c2 + 1);
}
}
return 0;
}
zoj1985
顺便把F题类似的zoj1985写写,很经典的迭代和枚举
#include <cstdio>
using namespace std;
long long a[100003];
int l[100003],r[100003];
int main(){
int n,i,j;
long long MAX;
while(scanf("%d",&n)&&n){
MAX=a[0]=a[n 1]=-1000000000;
for(i=1;i<=n;i ){
scanf("%lld",a i);
l[i]=1;
j=i;
while(a[j-l[j]]>=a[i]){
j-=l[j];
l[i] =l[j];
}
}
for(i=n;i>=1;i--){
r[i]=1;
j=i;
while(a[j r[j]]>=a[i]){
j =r[j];
r[i] =r[j];
}
if((l[i] r[i]-1)*a[i]>MAX)MAX=(l[i] r[i]-1)*a[i];
}
printf("%lld\n",MAX);
}
return 0;
}
I题
二分覆盖半径,判断是否为二分图,判断是否为二分图是用BFS,每个点加入队列中,如果flag[i]+flag[i+1] = 1,则满足是二分图。
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
#define LL long long
#define abs(x) ((x)>0?(x):-(x))
#define mset(a, b) memset(a, b, sizeof(a))
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
const int MAXN = 1220;
const double eps = 1E-9;
double d[MAXN][MAXN];
int ans[MAXN];
struct Point
{
double x, y;
}p[MAXN];
double inline dis(const Point &l, const Point &r)
{
return sqrt((l.x-r.x)*(l.x-r.x) + (l.y-r.y)*(l.y-r.y));
}
int flag[MAXN];
int n;
bool isok(double rr)
{
mset(flag, -1);
for(int i = 0; i < n; i++)
{
if(flag[i] > -1) continue;
queue<int> q;
q.push(i);
flag[i] = 0;
while(!q.empty())
{
int now = q.front();
q.pop();
for(int j = 0; j < n; j++)
{
if(j == now || d[now][j] > rr) continue;
if(flag[now] == flag[j]) return false;
if(flag[j] == -1)
{
flag[j] = 1-flag[now];
q.push(j);
}
}
}
}
for(int i = 0; i < n; i++) ans[i] = flag[i]+1;
return true;
}
double bs(double l, double r)
{
while(l + eps < r)
{
double mid = (l + r)/2;
if(isok(mid)) l = mid;
else r = mid;
}
return l;
}
int main()
{
while(~scanf("%d", &n) && n)
{
for(int i = 0; i < n; i++)
{
scanf("%lf%lf", &p[i].x, &p[i].y);
for(int j = 0; j < i; j++)
d[i][j] = d[j][i] = dis(p[i], p[j]);
}
double tmp = bs(0.0, 20000.0);
printf("%.7lf\n", tmp*0.5);
for(int i = 0; i < n; i++)
printf("%d%c", ans[i], i == (n-1)?'\n':' ');
}
return 0;
}