B Out-out-control cars

给2个三角形,坐标均为整数,问它们分别以(vx1,vy1),(vx2,vy2)移动时,是否会相撞(撞一个点就行)

题解:考虑2个三角形相撞拆成线段相撞,进一步转化为其中一条线段的一个端点撞到另一条线上。把运动变为相对运动,于是变成了射线与线段相交的问题。
注意特判初始重合但相对静止的情况。
注意这题卡精度需要用__float128

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define ALL(x) (x).begin(),(x).end()
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define double __float128
ll sqr(ll a){return a*a;}
ld sqr(ld a){return a*a;}
double sqr(double a){return a*a;}
const __float128 eps=1e-20;
int dcmp(double x) {
if (max(x,-x)<eps) return 0; else return x<0 ? -1 : 1;
}

ld PI = 3.141592653589793238462643383;
class P{
public:
double x,y;
P(double x=0,double y=0):x(x),y(y){}
friend ld dis2(P A,P B){return sqr(A.x-B.x)+sqr(A.y-B.y); }
friend ld Dot(P A,P B) {return A.x*B.x+A.y*B.y; }
friend ld Length(P A) {return sqrt(Dot(A,A)); }
friend ld Angle(P A,P B) {
if (dcmp(Dot(A,A))==0||dcmp(Dot(B,B))==0||dcmp(Dot(A-B,A-B))==0) return 0;
return acos(max((ld)-1.0, min((ld)1.0, Dot(A,B) / Length(A) / Length(B) )) );
}

friend P operator- (P A,P B) { return P(A.x-B.x,A.y-B.y); }
friend P operator+ (P A,P B) { return P(A.x+B.x,A.y+B.y); }
friend P operator* (P A,double p) { return P(A.x*p,A.y*p); }
friend P operator/ (P A,double p) { return P(A.x/p,A.y/p); }
friend bool operator< (const P& a,const P& b) {return dcmp(a.x-b.x)<0 ||(dcmp(a.x-b.x)==0&& dcmp(a.y-b.y)<0 );}

};
P read_point() {
P a;
ll c,d;
c=read(),d=read();
a.x=c,a.y=d;
return a;
}
bool operator==(const P& a,const P& b) {
return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y) == 0;
}
typedef P V;

double Cross(V A,V B) {return A.x*B.y - A.y*B.x;}
double Area2(P A,P B,P C) {return Cross(B-A,C-A);}
//Cross(v,w)==0(平行)时,不能调这个函数
P GetLineIntersection(P p,V v,P Q,V w){
V u = p-Q;
double t = Cross(w,u)/Cross(v,w);
return p+v*t;
}
//规范相交-线段相交且交点不在端点
bool SegmentProperIntersection(P a1,P a2,P b1,P b2) {
double c1 = Cross(a2-a1,b1-a1) , c2 = Cross(a2-a1,b2-a1),
c3 = Cross(b2-b1,a1-b1) , c4 = Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}

//点在线段上(不包含端点)
bool OnSegment(P p,P a1,P a2) {
return dcmp(Cross(a1-p,a2-p)) == 0 && dcmp(Dot(a1-p,a2-p))<0;
}


bool is_onshe(P a,V v,V q) {
// cout<<Dot(q-a,v)<<endl;
// cout<<dcmp(Dot(q-a,v))<<endl;
return dcmp(Cross(q-a,v))==0 && dcmp(Dot(q-a,v))>=0;
}
P t1[4],t2[4],v1,v2;
bool check(P A,V b,P C,P D) {
if (Length(b)<eps) {
return A==C||A==D||OnSegment(A,C,D);
}
if (dcmp(Cross(D-C,b)==0)) {
return is_onshe(A,b,C)||is_onshe(A,b,D);
} else {
if (is_onshe(A,b,C)||is_onshe(A,b,D)) return 1;
if (SegmentProperIntersection(A,A+b*2000000000LL,C,D)) return 1;
return 0;
P q=GetLineIntersection(A,b,C,D-C);
return is_onshe(A,b,q)&&OnSegment(q,C,D);
}
}
typedef vector<P> Polygon ;
int isPointInPolygon(P p,Polygon poly) {
int wn=0;
int n=poly.size();
Rep(i,n) {
if (OnSegment(p,poly[i],poly[(i+1)%n])) return -1; //edge
int k=dcmp(Cross(poly[(i+1)%n]-poly[i],p-poly[i]));
int d1 = dcmp(poly[i].y-p.y);
int d2 = dcmp(poly[(i+1)%n].y-p.y);
if ( k > 0 && d1 <= 0 && d2 > 0 ) wn++;
if ( k < 0 && d2 <= 0 && d1 > 0 ) wn--;
}
if (wn!=0) return 1; //inside
return 0; //outside
}

int main()
{
// freopen("B.in","r",stdin);
// freopen(".out","w",stdout);
int T=read();
For(kcase,T) {
Rep(i,3) t1[i]=read_point();v1=read_point();
Rep(i,3) t2[i]=read_point();v2=read_point();
bool fl=0;
if (v1==v2) {
Polygon poly1,poly2;
Rep(i,3) poly1.pb(t1[i]);
Rep(j,3) {
if (isPointInPolygon(t2[j],poly1)) fl=1;
}
Rep(i,3) poly2.pb(t2[i]);
Rep(j,3) {
if (isPointInPolygon(t1[j],poly2)) fl=1;
}
}
Rep(i,3) {
P C=t1[i],D=t1[(i+1)%3];
Rep(j,3) {
P A=t2[j];
V b=v2-v1;
if (check(A,b,C,D)) {
fl=1;
}
}
}
Rep(i,3) {
P C=t2[i],D=t2[(i+1)%3];
Rep(j,3) {
P A=t1[j];
V b=v1-v2;
if (check(A,b,C,D)) fl=1;
}
}
printf("Case #%d: ",kcase);
puts(fl?"YES":"NO");
}

return 0;
}

D Hack Portals

有一堆Portal在一条数轴上,每个Portal有一个冷却时间ci,位置xi,现在你需要hack所有的Portal(一个Portal必须在冷却时间结束后hack),你的起点为0,终点为W,求最小花费时间。

根据多年玩ingress的经验,
先hack一个中间的Portal以后再走到左边hack再走到右边hack,中间Portal最后一次经过的时间会被更新,因此可以假定在任意时刻没被hack的Portal一定是数轴上连续一段区间,区间dp。

#include<bits/stdc++.h> 
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma
#define
#define
#define
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define
int n,L,K;
ll f[MAXN][MAXN][2];
ll Abs(ll x) {
return max(x,-x);
}
ll x[MAXN],c[MAXN];
pi p[MAXN];
int main()
{
// freopen("D.in","r",stdin);
// freopen(".out","w",stdout);
int T=read();
For(kcase,T) {
ll ans=INF;
n=read(),L=read(),K=read();
For(i,n) p[i].fi=read(),p[i].se=read();
sort(p+1,p+1+n);
For(i,n) x[i]=p[i].fi,c[i]=p[i].se;

memset(f,127/2,sizeof(f));
f[1][n][0]=max(x[1],c[1]);
f[1][n][1]=max(x[n],c[n]);

ForD(len,n) if (len>1){
For(i,n-len+1) {
int j=i+len-1;
gmin(f[i+1][j][0],max(c[i+1],f[i][j][0]+Abs(x[i]-x[i+1])));
gmin(f[i+1][j][1],max(c[j], f[i][j][0]+Abs(x[i]-x[j])));

gmin(f[i][j-1][0],max(c[i],f[i][j][1]+Abs(x[j]-x[i])));
gmin(f[i][j-1][1],max(c[j-1],f[i][j][1]+Abs(x[j]-x[j-1])));

}
}
For(i,n) {

(x[i]-K) );
}
Pr(kcase,ans);
}

return 0;
}

E Half-consecutive Numbers

ci=n(n+1)/2,i=1,2,⋯,已知n,求最小的i使得i≥n,且ci是完全平方数。n<=1e16
考虑ci是完全平方数,那么i或i+1中存在一个奇数,这个数也是完全平方数,那么可以在10^8之内暴力枚举满足条件的i,打表。

#include<bits/stdc++.h> 
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma
#define
#define
#define
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
ll a[123456];
ll is_sq(ll i) {
ll t=sqrt(i);t=t*t;
if (t==i) return 1;return 0;
}
ll check(ll i) {
ll t=i*(i+1)/2,p=sqrt(t);
if (p*p==t) return 1;return 0;
}
ll ans[1234]={0,1,8,49,288,1681,9800,57121,332928,1940449,11309768,65918161,384199200,2239277041,13051463048,76069501249,443365544448,2584123765441,15061377048200,87784138523761,511643454094368,2982076586042449,17380816062160328};
int main()
{
// freopen("E.in","r",stdin);
// freopen("e.out","w",stdout);
int T=read();


For(kcase,T) {
ll n;
scanf("%lld",&n);
int i=1;
while(ans[i]<n) ++i;
printf("Case #%d: %lld\n",kcase,ans[i]);
}
return 0;
}