好吧这次没有官方题解了。。
G - Icon Design
题解
按要求打印 “NFLS
按照题意模拟即可。
小技巧 (?): 要输出一大坨东西时,可以先用一个 buffer 存要打印的内容 (方便修改),最后统一输出。
#include <cstdio>
#include <cmath>
#define MN 1000000
int n,a[MN+5];
void solve(){
scanf("%d",&n);
int B = ceil(sqrt(n));
int b = n%B;
for(int i=B;i<=n;i+=B){
for(int j=1;j<=B;j++){
a[i-B+j] = i-j+1;
}
}
for(int i=1;i<=b;i++){
a[n-b+i] = n-i+1;
}
for(int i=1;i<=n;i++){
printf("%d%c",a[i]," \n"[i==n]);
}
}
int main(){
int T;
scanf("%d",&T);
while(T--) solve();
}
J - Number Game
题解
给出三个整数 A, B, C,每次可以把 B 变为 A − B 或者把 C 变为 B − C,问 C 能为变为 x
连续两次 B 变为 A − B 或连续两次 C 变为 B − C 相当于没操作 因此必然两种操作交叉进行,假设先 B 变为 A − B, (A, B, C) → (A, A − B, C) → (A, A − B, A − B − C) → (A, B, A − B − C) → (A, B, −A + 2B + C) → (A, A − B, −A + 2B + C) → (A, A − B, −2A + 3B + C) → ... 类似地可以写出先 C 变为 B − C 时的变化情况 观察到 x 只能为 k(A − 2B) + C 或 k(A − 2B) + A − B − C(k 为整数) 要注意特判 A = 2B 的情况
标程
#include <cstdio>
#include <cctype>
#define MN 100000
using ll = __int128;
using ld = long double;
namespace GTI
{
char gc(void)
{
const int S = 1 << 16;
static char buf[S], *s = buf, *t = buf;
if (s == t) t = buf + fread(s = buf, 1, S, stdin);
if (s == t) return EOF;
return *s++;
}
ll gti(void)
{
ll a = 0, b = 1, c = gc();
for (; !isdigit(c); c = gc()) b ^= (c == '-');
for (; isdigit(c); c = gc()) a = a * 10 + c - '0';
return b ? a : -a;
}
int gts(char *s)
{
int len = 0, c = gc();
for (; isspace(c); c = gc());
for (; c != EOF && !isspace(c); c = gc()) s[len++] = c;
s[len] = 0;
return len;
}
int gtl(char *s)
{
int len = 0, c = gc();
for (; isspace(c); c = gc());
for (; c != EOF && c != '\n'; c = gc()) s[len++] = c;
s[len] = 0;
return len;
}
}
using GTI::gti;
using GTI::gts;
using GTI::gtl;
int a[MN+5];
void solve(){
int n;
ll s_xy=0,s_x=0,s_y=0,s_x2=0;
n = gti();
for(int i=1;i<=n;i++){
a[i] = gti();
s_xy += (ll)i*a[i];
s_x += i;
s_y += a[i];
s_x2 += (ll)i*i;
}
ld k = (ld)(n*s_xy-s_x*s_y)/(n*s_x2-(s_x)*(s_x));
ld b = ((ld)s_y/n)-k*((ld)s_x/n);
ld ans = 0;
for(int i=1;i<=n;i++){
ld y = k*i+b;
ans += (y-a[i])*(y-a[i]);
}
printf("%.15Lf\n",ans);
}
int main(){
int T;
T = gti();
while(T--) solve();
}
B - Eezie and Pie
题解
相当于对 i 到 i 的 di 级祖先(或 1 如果该祖先不存在)的链 +1
树上差分:假设想对 v 到 u 加 x,其中 u 是 v 的祖先,则对 v 权值加 x,对 pa(u) 权值减 x,最后统计子树的权值和,即为对应结点的权值
标程
#include<bits/stdc++.h>
using namespace std;
const long double eps = 1e-8;
struct Point{
long double x,y;
void read() {
scanf("%Lf%Lf",&x,&y);
}
Point () {}
Point (long double a1,long double a2) : x(a1),y(a2) {}
Point (int a1,int a2) : x(a1),y(a2) {}
friend Point operator + (Point p1,Point p2){return Point(p1.x+p2.x,p1.y+p2.y);}
friend Point operator - (Point p1,Point p2){return Point(p1.x-p2.x,p1.y-p2.y);}
friend Point operator * (Point p1,long double t){return Point(p1.x*t,p1.y*t);}
friend Point operator / (Point p1,long double k){return Point(p1.x/k,p1.y/k);}
friend bool operator < (Point p1,Point p2){return (abs(p1.x-p2.x)<=eps)?p1.y<p2.y:p1.x<p2.x;}
friend bool operator == (Point p1,Point p2){return (abs(p1.x-p2.x)<=eps && abs(p1.y-p2.y)<=eps);}
Point trans(long double newlen) {
long double len=mo();
long double xx=x/len*newlen,yy=y/len*newlen;
return Point(xx,yy);
}
inline Point rotate(long double sinfi,long double cosfi){
return Point(x*cosfi-y*sinfi,y*cosfi+x*sinfi);
}
inline Point rotate(long double fi){
long double sinfi=sin(fi),cosfi=cos(fi);
return Point(x*cosfi-y*sinfi,y*cosfi+x*sinfi);
}
inline Point rotate90(){
return Point(-y,x);
}
long double mo(){return sqrt(x*x+y*y);}
long double getAngle() {return atan2(y,x);}
};
long double mulcross(Point v1,Point v2){return (v1.x*v2.y-v1.y*v2.x);}
struct Line{
Point p,u;
long double ang;
Line () {}
Line (Point a1,Point a2) : p(a1),u(a2) {}
bool judgeHalfPlane(Point p0) {return mulcross(u,p0-p)>=-eps;}
friend bool operator < (Line l1,Line l2) {return l1.p<l2.p;}
void setAngle() {ang = u.getAngle();}
}dline,uline,lline,rline;
Point inters(Line l1,Line l2){return l1.p+l1.u*(mulcross(l2.p-l1.p,l2.u)/mulcross(l1.u,l2.u));}
struct threePoint{
long double x,y,z;
threePoint (long double a1=0,long double a2=0,long double a3=0) : x(a1),y(a2),z(a3) {}
void read() {scanf("%Lf%Lf%Lf",&x,&y,&z);}
Point project() {return Point(x,y);}
Point getShadow(threePoint light) {
if (z==0) return this->project();
Point u = this->project() - light.project();
if (abs(u.x)<eps && abs(u.y)<eps) return this->project();
if (z<light.z) {
u=u.trans(u.mo()+z*u.mo()/(light.z-z));
return u+light.project();
}else{
throw "point not found";
}
throw "point not found";
}
};
bool cmp(Line l1,Line l2){return ((abs(l1.ang-l2.ang)<eps)?(mulcross(l1.p-l2.p,l2.u)<0):l1.ang<l2.ang);}
int nxt(int i,int l,int r){return i==r?l:i+1;}
vector<Point> halfPlaneIntersection(vector<Line> lineList) {
for (int i=1;i<(int)lineList.size();i++) lineList[i].setAngle();
sort(lineList.begin()+1,lineList.end(),cmp);
const int thisSize = 200005+5;
static Line que[thisSize];
static Point forw[thisSize],bacw[thisSize];
int l=1,r=1;
que[1]=lineList[1];
vector<Point> convexGenerateByHalfPlane;
for (int i=2;i<(int)lineList.size();i++) {
if (lineList[i].ang-lineList[i-1].ang<=eps) continue;
while (r-l+1>=2 && mulcross(forw[r]-lineList[i].p,lineList[i].u)>=0) r--;
while (r-l+1>=2 && mulcross(bacw[l]-lineList[i].p,lineList[i].u)>=0) l++;
que[++r]=lineList[i];
if (abs(mulcross(que[r].u,que[r-1].u))<=eps) return convexGenerateByHalfPlane;
forw[r]=bacw[r-1]=inters(que[r],que[r-1]);
}
while (r-l+1>=2 && mulcross(forw[r]-que[l].p,que[l].u)>=0) r--;
if (abs(mulcross(que[l].u,que[r].u))<=eps) return convexGenerateByHalfPlane;
forw[l]=inters(que[l],que[r]);
for (int i=l;i<=r;i++) {
convexGenerateByHalfPlane.push_back(forw[i]);
}
return convexGenerateByHalfPlane;
}
long double getarea(vector<Point> convex) {
long double ans=0;
Point O(0,0);
int len = convex.size();
for (int i=0;i<len;i++) {
ans+=mulcross(convex[i]-O,convex[(i+1)%len]-O)/2.0;
}
return ans;
}
struct Wall{
int n,height;
long double width;
vector<Point> pointList;
void read() {
scanf("%d%d%Lf",&n,&height,&width);
for (int i=1;i<=n;i++) {
Point tmpPoint;
tmpPoint.read();
pointList.push_back(tmpPoint);
}
}
vector<Line> lineList;
void init() {
Line line;
lineList.push_back(line);
}
void shrink() {
int len = pointList.size();
for (int i=0;i<len;i++) {
Point u = pointList[(i+1)%len] - pointList[i];
lineList.push_back(Line(pointList[i]+u.rotate90().trans(width),u));
}
vector<Point> convex = halfPlaneIntersection(lineList);
if (convex.size()<3) throw (long double)0;
pointList = convex;
}
void isin(threePoint light) {
int len = pointList.size();
for (int i=0;i<len;i++) {
Point u = pointList[(i+1)%len] - pointList[i];
if (mulcross(u,light.project()-pointList[i])<0) return;
}
throw getarea(pointList);
}
void getShadow(threePoint light) {
if (light.z<=height) throw (long double)0;
int len = pointList.size();
vector<Point> anotherConvex;
for (int i=0;i<len;i++) {
threePoint point(pointList[i].x,pointList[i].y,height);
Point projection = point.getShadow(light);
anotherConvex.push_back(projection);
}
len = anotherConvex.size();
for (int i=0;i<len;i++) {
Point u = anotherConvex[(i+1)%len] - anotherConvex[i];
lineList.push_back(Line(anotherConvex[i],u));
}
vector<Point> convex = halfPlaneIntersection(lineList);
if (convex.size()<3) throw (long double)0;
throw getarea(convex);
}
};
void solve()
{
try{
Wall wall;
wall.read();
threePoint light;
light.read();
wall.init();
wall.shrink();
wall.isin(light);
wall.getShadow(light);
}catch(long double ans) {
printf("%.10Lf\n",ans);
}
}
int main()
{
int t;
scanf("%d",&t);
while (t--) solve();
}
M - Z-Game on grid
题解
假如 (n, m) 不是 ‘A’ 或 ‘B’,把它标记为 ‘C’ 如果判断 Alice 是否一定能走到 ‘A’?假如 Alice 想走到 ‘A’,那么最坏的 情况下,Bob 会想方设法走到 ‘B’ 或 ‘C’,此时把 ‘B’ 和 ‘C’ 看成一类 点。 对于 x + y ≡ 0 (mod 2) 的点 (x, y),是 Alice 走,如果 (x + 1, y) 和 (x, y + 1) 中存在至少一个一定能走到 ‘A’,则 Alice 往这个格子走就能 走到 ‘A’,否则 Alice 不能走到 ‘A’
判断 Alice 是否一定能走到 ‘B’ 或 ‘C’ 是类似的
标程
#include<bits/stdc++.h>
#define ll long long
#define cer(x) cerr<<(#x)<<" = "<<(x)<<'\n'
#define endl '\n'
#define N 505
using namespace std;
ll n,m;
string s[N];
int f[4][N][N];
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t; cin>>t;
while(t--){
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>s[i];
}
for(int i=1;i<=3;i++){
for(int j=0;j<=n;j++){
for(int k=0;k<=m;k++){
f[i][j][k]=0;
}
}
}
if(s[n-1][m-1]=='A'){
f[1][n-1][m-1]=1;
}
else if(s[n-1][m-1]=='.'){
f[2][n-1][m-1]=1;
}
else{
f[3][n-1][m-1]=1;
}
for(int j=n-1;j>=0;j--){
for(int k=m-1;k>=0;k--){
if(j==n-1 && k==m-1)continue;
if(s[j][k]=='A'){
f[1][j][k]=1;
}
else if(s[j][k]=='B'){
f[1][j][k]=0;
}
else{
if((j+k)%2==0){
if(j+1<=n-1)f[1][j][k]=max(f[1][j][k],f[1][j+1][k]);
if(k+1<=m-1)f[1][j][k]=max(f[1][j][k],f[1][j][k+1]);
}
else{
f[1][j][k]=1;
if(j+1<=n-1)f[1][j][k]=min(f[1][j][k],f[1][j+1][k]);
if(k+1<=m-1)f[1][j][k]=min(f[1][j][k],f[1][j][k+1]);
}
}
}
}
for(int j=n-1;j>=0;j--){
for(int k=m-1;k>=0;k--){
if(j==n-1 && k==m-1)continue;
if(s[j][k]=='A'){
f[3][j][k]=0;
}
else if(s[j][k]=='B'){
f[3][j][k]=1;
}
else{
if((j+k)%2==0){
if(j+1<=n-1)f[3][j][k]=max(f[3][j][k],f[3][j+1][k]);
if(k+1<=m-1)f[3][j][k]=max(f[3][j][k],f[3][j][k+1]);
}
else{
f[3][j][k]=1;
if(j+1<=n-1)f[3][j][k]=min(f[3][j][k],f[3][j+1][k]);
if(k+1<=m-1)f[3][j][k]=min(f[3][j][k],f[3][j][k+1]);
}
}
}
}
for(int j=n-1;j>=0;j--){
for(int k=m-1;k>=0;k--){
if(j==n-1 && k==m-1)continue;
if(s[j][k]=='A'){
f[2][j][k]=0;
}
else if(s[j][k]=='B'){
f[2][j][k]=0;
}
else{
if((j+k)%2==0){
if(j+1<=n-1)f[2][j][k]=max(f[2][j][k],f[2][j+1][k]);
if(k+1<=m-1)f[2][j][k]=max(f[2][j][k],f[2][j][k+1]);
}
else{
f[2][j][k]=1;
if(j+1<=n-1)f[2][j][k]=min(f[2][j][k],f[2][j+1][k]);
if(k+1<=m-1)f[2][j][k]=min(f[2][j][k],f[2][j][k+1]);
}
}
}
}
if(f[1][0][0])cout<<"yes ";
else cout<<"no ";
if(f[2][0][0])cout<<"yes ";
else cout<<"no ";
if(f[3][0][0])cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
return 0;
}