https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=360
只过了3题:C,D,I
C题:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int p[1000005],s[1000005],a[1000005];
int main()
{
int T,i,high,ans;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&p[i]);
for(i=1;i<=n;i++)
scanf("%d",&s[i]);
high=1005;
for(i=n;i>=1;i--)
{
high=min(high,s[i]);
high=max(high,p[i]);
a[i]=high;
}
high=1005;
ans=0;
for(i=1;i<=n;i++)
{
high=min(high,s[i]);
high=max(high,p[i]);
a[i]=min(a[i],high);
ans=ans+a[i]-p[i];
//printf("%d ",a[i]-p[i]);
}
printf("%d\n",ans);
}
return 0;
}
D题:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxx = 1005;
int n,m;
struct pp{
int x,y;
};
int a[maxx][maxx];
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
int dd[4] = {0,1,2,3};
int vis[maxx][maxx];
char s[1005];
queue<pp> q;
int ok(int x,int y,int d){
if(!vis[x][y])
if(x >= 0 && x < n)
{
if(y >= 0 && y < m)
{
if(d == 0) {
if(a[x][y] == 5 || (a[x][y] <=2 && a[x][y] != 0))
return 1;
}
if(d == 1) {
if(a[x][y] >= 3)
return 1;
}
if(d == 2) {
if(a[x][y] == 5 || (a[x][y] <=3 && a[x][y] >= 2))
return 1;
}
if(d == 3) {
if(a[x][y] == 5 || (a[x][y] ==1 || a[x][y] == 4))
return 1;
}
}
}
return 0;
}
void bfs(int x,int y,int d)
{
pp c;
c.x = x;
c.y = y;
q.push(c);
while(!q.empty())
{
pp t = q.front();
int x = t.x;
int y = t.y;
vis[x][y] = 1;
if(a[x][y] == 5)
{
for(int i = 0; i < 4; i++)
{
int nx = dx[i] + x;
int ny = dy[i] + y;
int nd = dd[i];
if(ok(nx,ny,nd))
{
t.x = nx;
t.y = ny;
q.push(t);
}
}
}
int nx,ny,nd;
if(a[x][y] == 1)
{
nx = dx[1] + x;
ny = dy[1] + y;
nd = dd[1];
t.x = nx,t.y = ny;
if(ok(nx,ny,nd))
q.push(t);
nx = dx[2] + x;
ny = dy[2] + y;
nd = dd[2];
t.x = nx,t.y = ny;
if(ok(nx,ny,nd))
q.push(t);
}
if(a[x][y] == 2)
{
nx = dx[1] + x;
ny = dy[1] + y;
nd = dd[1];
t.x = nx,t.y = ny;
if(ok(nx,ny,nd))
q.push(t);
nx = dx[3] + x;
ny = dy[3] + y;
nd = dd[3];
t.x = nx,t.y = ny;
if(ok(nx,ny,nd))
q.push(t);
}
if(a[x][y] == 3)
{
nx = dx[0] + x;
ny = dy[0] + y;
nd = dd[0];
t.x = nx,t.y = ny;
if(ok(nx,ny,nd))
q.push(t);
nx = dx[3] + x;
ny = dy[3] + y;
nd = dd[3];
t.x = nx,t.y = ny;
if(ok(nx,ny,nd))
q.push(t);
}
if(a[x][y] == 4)
{
nx = dx[0] + x;
ny = dy[0] + y;
nd = dd[0];
t.x = nx,t.y = ny;
if(ok(nx,ny,nd))
q.push(t);
nx = dx[2] + x;
ny = dy[2] + y;
nd = dd[2];
t.x = nx,t.y = ny;
if(ok(nx,ny,nd))
q.push(t);
}
q.pop();
}
//printf("%d %d\n",x,y);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(vis,0,sizeof(vis));
scanf("%d%d",&n,&m);
for(int i = 0; i < n; i++)
{
scanf("%s",s);
for(int j = 0; j < m; j++)
{
a[i][j] = s[j] - 'A';
}
}
int cnt = 0;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
if(!vis[i][j] && a[i][j] != 0) {
bfs(i,j,-1);
cnt++;
}
}
}
cout<<cnt<<endl;
/*
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j ++)
{
printf("%d ",a[i][j]);
}
cout<<endl;
}
*/
}
return 0;
}
I题:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int f[1000005];
struct st
{
int id;
int data;
}p[1000005];
int ans;
struct st2
{
int q;
int ans;
};
vector<st2> v;
int t,n,m,r;
int a[1005][1005];
bool cmp(st a,st b)
{
return a.data>b.data;
}
void init(int n)
{
for (int i=0;i<=n;i++)
{
f[i]=i;
}
}
int find_father(int x)
{
if (f[x]==x) return x;
else return f[x]=find_father(f[x]);
}
void Union(int x,int y)
{
f[find_father(x)]=find_father(y);
}
int ch(int x,int y)
{
return (x-1)*m+y-1;
}
pair<int,int> ch(int x)
{
return {x/m+1,x%m+1};
}
void add(int z)
{
auto pa=ch(z);
int x=pa.first;
int y=pa.second;
a[x][y]=1;
vector<int> v;
if (a[x-1][y]==1) v.push_back(find_father(ch(x-1,y)));
if (a[x+1][y]==1) v.push_back(find_father(ch(x+1,y)));
if (a[x][y-1]==1) v.push_back(find_father(ch(x,y-1)));
if (a[x][y+1]==1) v.push_back(find_father(ch(x,y+1)));
int s=v.size();
sort(v.begin(),v.end());
for (int i=0;i<(int)v.size()-1;i++)
{
if (v[i]==v[i+1]) s--;
}
v.clear();
v.push_back(ch(x,y));
if (a[x-1][y]==1) v.push_back(ch(x-1,y));
if (a[x+1][y]==1) v.push_back(ch(x+1,y));
if (a[x][y-1]==1) v.push_back(ch(x,y-1));
if (a[x][y+1]==1) v.push_back(ch(x,y+1));
for (int i=0;i<(int)v.size()-1;i++)
{
if (find_father(v[i])!=find_father(v[i+1])) Union(v[i],v[i+1]);
}
ans-=s-1;
//cout<<x<<' '<<y<<' '<<-(s-1)<<endl;
}
int main()
{
scanf("%d",&t);
while (t--)
{
v.clear();
scanf("%d%d",&n,&m);
init(n*m);
memset(a,0,sizeof(a));
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
{
p[ch(i,j)].id=ch(i,j);
scanf("%d",&p[ch(i,j)].data);
}
}
sort(p,p+n*m,cmp);
ans=0;
st2 xx;
xx.q=p[0].data+1;
xx.ans=0;
v.push_back(xx);
for (int i=0;i<n*m;i++)
{
add(p[i].id);
while (i<n*m-1 && p[i].data==p[i+1].data)
{
i++;
add(p[i].id);
}
st2 x;
x.q=p[i].data;
x.ans=ans;
v.push_back(x);
}
reverse(v.begin(),v.end());
scanf("%d",&r);
int k=0;
while (r--)
{
int d;
scanf("%d",&d);
while (k<(int)v.size()-1 && v[k].q<=d) k++;
printf("%d ",v[k].ans);
//if (r) printf(" ");
}
printf("\n");
}
return 0;
}