K
题目大意:给出N和M,有M个输入,每次输入两个数p,x,表示有一个数组b,从a[1]开始到a[p],这p个数的最长上升子序列为x,即b[p] = x。输出符合b数组的a数组,不存在就输出01。
解题思路:显然对于每一个 b[i],必须满足 b[i]≤i,那么对于未给出数值的 b[i],可以考虑先给它们填充上数值,不妨令 b[i] = b[i - 1] + 1,这样构造能够满足b[i]≤i的条件,随后只需要从后往前构造即可。填充完b数组,我们借助栈来从后往前构造a数组即可。
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int b[N], a[N];
int main()
{
int n, m;
cin >> n >> m;
while(m--)
{
int d,dd;
cin >> d >> dd;
b[d] = dd;
}
for(int i = 1; i <= n; i++)
{
if(!b[i])
b[i] = b[i-1]+1;
if(b[i] > b[i-1]+1)
{
puts("-1");
return 0;
}
}
int cnt = 0;
stack<int> s;
for(int i = n; i > 0; i--)
{
while(b[i] > (int)s.size())
{s.push(++cnt);}
a[i] = s.top();
s.pop();
}
for(int i = 1; i <= n; i++)
cout << a[i] << ' ';
cout << "\n";
return 0;
}
F
题目大意:给出四个点A(x0,y0,z0),B(x1,y1,z1),C(x2,y2,z2),D(x3,y3,z3)和k1,k2,要求算出满足下列式子的两图形的相交的体积
以A B P1为例,设P1(x,y,z),依据题目限制条件先写出关系式:
化简后
容易得出这是一个球的方程,然后只需算出两球的相交体积即可。
#include <bits/stdc++.h>
#define INF 99999999
#define LINF LLONG_MAX
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=1;
const double pi = acos(-1);
int T;
double x[5],y[5],z[5];
double k1,k2;
void solve(double x1,double y1,double z1,double r1,double x2,double y2,double z2,double r2){
double ans=0;
//球心距离
double dis=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));
//相离或相切
if(dis>=r1+r2){
ans=0;
}
//内含或内切
else if (dis+r1<=r2){
ans=(4.00/3.00)*pi*r1*r1*r1;
}
else if(dis+r2<=r1){
ans=(4.00/3.00)*pi*r2*r2*r2;
}
//相交
else{
double cal=(r1*r1+dis*dis-r2*r2)/(2.00*dis*r1);
double h=r1*(1-cal);
ans+=(1.00/3.00)*pi*(3.00*r1-h)*h*h;
cal=(r2*r2+dis*dis-r1*r1)/(2.00*dis*r2);
h=r2*(1.00-cal);
ans+=(1.00/3.00)*pi*(3.00*r2-h)*h*h;
}
printf("%.3f\n",ans);
}
int main(){
cin>>T;
while(T--){
for(int i=0;i<4;i++)
cin>>x[i]>>y[i]>>z[i];
cin>>k1>>k2;
//计算球1的球心坐标和半径
double xishu1=k1*k1-1;
double c1x,c1y,c1z,c1r,t;
c1x=(k1*k1*x[1]-x[0])/xishu1;
c1y=(k1*k1*y[1]-y[0])/xishu1;
c1z=(k1*k1*z[1]-z[0])/xishu1;
t=k1*k1*((x[1]*x[1])+(y[1]*y[1])+(z[1]*z[1]))-x[0]*x[0]-y[0]*y[0]-z[0]*z[0];
t/=xishu1;
c1r=sqrt(c1x*c1x+c1y*c1y+c1z*c1z-t);
//计算球2的球心坐标和半径
double xishu2=k2*k2-1;
double c2x,c2y,c2z,c2r;
c2x=(k2*k2*x[3]-x[2])/xishu2;
c2y=(k2*k2*y[3]-y[2])/xishu2;
c2z=(k2*k2*z[3]-z[2])/xishu2;
t=k2*k2*((x[3]*x[3])+(y[3]*y[3])+(z[3]*z[3]))-x[2]*x[2]-y[2]*y[2]-z[2]*z[2];
t/=xishu2;
c2r=sqrt(c2x*c2x+c2y*c2y+c2z*c2z-t);
//计算两个球相交部分体积
solve(c1x,c1y,c1z,c1r,c2x,c2y,c2z,c2r);
}
return 0;
}
I
题目大意:给2个20*20的地图,有两个人同时出发,并且以镜像的方式走路,要求从【20,20】 【20,1】 同时走到【1,20】【1,1】。
解题思路:bfs
#include <bits/stdc++.h>
using namespace std;
string ans;
int flag;
char c1[30][30], c2[30][30];
int vis[25][25][25][25];
int now[25][25][25][25][5];
string p = "DLRU";
int tx1[] = {1,0,0,-1}, ty1[] = {0,-1,1,0}, tx2[] = {1,0,0,-1}, ty2[] = {0,1,-1,0};
struct no
{
int x1,y1,x2,y2;
};
queue<no> q;
int check(int a, int b)
{
if((a+b)<1 || (a+b)>20)
return a;
return a+b;
}
void put()
{
cout << vis[1][20][1][1]-1 << endl;
reverse(ans.begin(), ans.end());
cout << ans << endl;
c1[20][20]='A';
c2[20][1]='A';
for(int i = 1; i <= 20; i++)
{
printf("%s ",c1[i]+1);
printf("%s\n",c2[i]+1);
}
}
int main()
{
for(int i = 1; i <= 20; i++)
{
scanf("%s",c1[i]+1);
scanf("%s",c2[i]+1);
}
vis[20][20][20][1]=1;
q.push({20,20,20,1});
while(!q.empty())
{
auto t = q.front();
q.pop();
for(int i = 0; i < 4; i++)
{
int xx1 = check(t.x1 , tx1[i]);
int yy1 = check(t.y1 , ty1[i]);
int xx2 = check(t.x2 , tx2[i]);
int yy2 = check(t.y2 , ty2[i]);
if(c1[xx1][yy1] == '#')
{
xx1 -= tx1[i];
yy1 -= ty1[i];
}
if(c2[xx2][yy2] == '#')
{
xx2 -= tx2[i];
yy2 -= ty2[i];
}
if(vis[xx1][yy1][xx2][yy2])
continue;
q.