牛客2021年多校训练营<2>
Draw Grids
题目描述:
输入描述:
The input consists of one line containing two integers {n}n and {m}m.
输出描述:
Print “YES”(without quote) if ZYT can win and “NO”(without quote) if ZYT can’t.
样例:
备注:
思路:
签到题,最优的路径一定与走完的路径结果相同,因此求最长不成环路径长度即可
代码:
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <iomanip>
using namespace std;
#define ll long long
int main()
{
int m, n;
cin >> m >> n;
if ((m * n - 1) % 2 == 0) {
cout << "NO" << endl;
}
else {
cout << "YES" << endl;
}
return 0;
}
Er Ba Game
题目描述:
输入描述:
输出描述:
说明和备注:
思路:
模拟题中情况即可,未提及则为tie
代码:
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <iomanip>
using namespace std;
#define ll long long
int main()
{
int t;
cin >> t;
while (t--) {
int a1, b1, a2, b2;
cin >> a1 >> b1 >> a2 >> b2;
if (a1 > b1) {
swap(a1, b1);
}
if (a2 > b2) {
swap(a2, b2);
}
if (a1 == 2 && b1 == 8) {
if (a2 != 2 || b2 != 8) {
cout << "first" << endl;
continue;
}
else if (a2 == 2 && b2 == 8) {
cout << "tie" << endl;
continue;
}
}
if (a2 == 2 && b2 == 8) {
if (a1 != 2 || b1 != 8) {
cout << "second" << endl;
continue;
}
}
if (a1 == b1 && a2 != b2) {
cout << "first" << endl;
continue;
}
else if (a1 != b1 && a2 == b2) {
cout << "second" << endl;
continue;
}
else if (a1 == b1 && a2 == b2) {
if (a1 > a2) {
cout << "first" << endl;
continue;
}
else if(a1<a2){
cout << "second" << endl;
continue;
}
else {
cout << "tie" << endl;
continue;
}
}
if (a1 != b1 && a2 != b2) {
int x = (a1 + b1) % 10;
int y= (a2 + b2) % 10;
if (x > y) {
cout << "first" << endl;
continue;
}
else if (x < y) {
cout << "second" << endl;
continue;
}
else {
if (b1 > b2) {
cout << "first" << endl;
continue;
}
else if (b1 < b2) {
cout << "second" << endl;
continue;
}
else {
cout << "tie" << endl;
continue;
}
}
}
}
}
Girlfriend
题目描述:
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
Special Judge, 64bit IO Format: %lld
输入描述:
输出描述:
样例:
备注:
思路:
就数学知识得知,满足条件的轨迹是一个阿波罗尼斯圆,而题目就是求两个阿波罗尼斯圆的体积交
代码:
搜到了相关公式,明白了原理,但是不想写了,贴个大佬标程(加了注释方便理解):
#include<bits/stdc++.h>
using namespace std;
const double PI=acos(-1.0);
const double eps=1e-5;
struct node{double x,y,z;}A,B,C,D,O1,O2;
double K1,K2,r1,r2,R;
void Getball(node &p1,node &p2,double K,node &o,double &R)
{
double dist=sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)+(p1.z-p2.z)*(p1.z-p2.z));
double pos1=K/(K+1),pos2=K/(K-1),mid=(pos1+pos2)/2;
R=dist*fabs(pos1-pos2)/2;
o.x=p1.x+(p2.x-p1.x)*mid;
o.y=p1.y+(p2.y-p1.y)*mid;
o.z=p1.z+(p2.z-p1.z)*mid;
}
//求圆心
double V(double r,double h)
{
return PI*(r-h)*r*r-PI*(r*r*r-h*h*h)/3;
}
void Solve()
{
cin>>A.x>>A.y>>A.z;
cin>>B.x>>B.y>>B.z;
cin>>C.x>>C.y>>C.z;
cin>>D.x>>D.y>>D.z;
cin>>K1>>K2;
Getball(A,B,K1,O1,r1);
Getball(C,D,K2,O2,r2);
R=sqrt((O1.x-O2.x)*(O1.x-O2.x)+(O1.y-O2.y)*(O1.y-O2.y)+(O1.z-O2.z)*(O1.z-O2.z));//圆心间距
if(r1+r2<R){puts("0.000");return;}//不交
if(r1+R<r2){printf("%.3lf\n",4*PI*r1*r1*r1/3);return;}//内接
if(r2+R<r1){printf("%.3lf\n",4*PI*r2*r2*r2/3);return;}//内接
double dr=sqrt((r1+r2+R)*(r1+r2-R)*(r1-r2+R)*(r2+R-r1))/R/2;
double d1=sqrt(r1*r1-dr*dr),d1_;
double d2=sqrt(r2*r2-dr*dr),d2_;
if(fabs(d1+d2-R)<eps)d1_=d1,d2_=d2;
else if(fabs(-d1+d2-R)<eps)d1_=-d1,d2_=d2;
else if(fabs(d1-d2-R)<eps)d1_=d1,d2_=-d2;
else d1_=-d1,d2_=-d2;
double V1=V(r1,d1_),V2=V(r2,d2_);
printf("%.3lf\n",V1+V2);//根据公式求体积交
}
int main()
{
int T;cin>>T;
while(T --> 0)Solve();
return 0;
}
Stack
题目描述:
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
Special Judge, 64bit IO Format: %lld
输入描述:
输出描述:
样例:
备注:
思路:
先根据题意,求得每个时刻单调栈的size,然后根据题意去模拟一个单调栈即可(代码有详解)
代码:
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
using namespace std;
typedef long long ll;
stack <ll>s;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<int>res(n + 1);
vector<int>siz(n + 1);
for (int i = 0; i < k; i++) {
int x = 0, y = 0;
cin >> x >> y;
siz[x] = y;
}
for (int i = 1; i <= n; i++) {
if (siz[i]&&siz[i]-siz[i-1]>1) { //一次只能删除若干个或者加入一个数,所以大小变化的差应小于等于1
cout << -1 << endl;
return 0;
}
if (!siz[i]) {
siz[i] = siz[i - 1] + 1;
}
} //更新每个时刻stack的size
int now = 1; //赋值的数
for (int i = n; i >= 1; i--) {
while (s.size() < siz[i]) {
s.push(now); //now++保证数不重复
now++;
}
res[i] = s.top();
s.pop();
} //根据题意模拟单调栈,从后往前满足
for (int i = 1; i <= n; i++) {
cout << res[i] << " ";
}
cout << endl;
}