La Salle-Pui Ching Programming Challenge 培正喇沙編程挑戰賽 2017

A Ambiguous Dates

贪心,从小到大取日期

#include 
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 ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i
#define ForD(i,n) for(int i=n;i;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,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector 
#define pi pair
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<' ';\
                        cout<#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
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 MAXN (123456)
ll d[MAXN];
ll Abs(ll x){return max(x,-x);}
int main()
{
//  freopen("A.in","r",stdin);
//  freopen(".out","w",stdout);
    int n=read();
    For(i,n) d[i]=read();
    ll ans=0;
    sort(d+1,d+1+n);ll c=0;
    For(i,n) {
        ll p=d[i];
        if (p>i) {
            ans+=min((ll)n-i,d[i]-i);
        } 
    }


    cout<<2*ans<return 0;
}

B Bacteria Experiment

求树直径

#include 
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 ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i
#define ForD(i,n) for(int i=n;i;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,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector 
#define pi pair
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<' ';\
                        cout<#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
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 MAXN (912345)
vi e[MAXN];
int dep[MAXN]={};
void dfs(int x,int fa) {
    dep[x]=dep[fa]+1;
    for(auto v:e[x]) {
        if (v!=fa) dfs(v,x);
    }
}
int main()
{
//  freopen("B.in","r",stdin);
//  freopen(".out","w",stdout);
    int n=read();
    For(i,n-1) {
        int x=read(),y=read();
        e[x].pb(y);e[y].pb(x);
    }
    dfs(1,0);
    int mx=max_element(dep+1,dep+1+n)-dep;
    MEM(dep);
    dfs(mx,0);
    int l=*max_element(dep+1,dep+1+n);
    int ans=0,dis=1; --l;
    while(dis1;
    cout<return 0;
}

C Cheering

签到

#include 
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 ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i
#define ForD(i,n) for(int i=n;i;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,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector 
#define pi pair
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<' ';\
                        cout<#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
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;
} 
char s[12345];
int main()
{
//  freopen("E.in","r",stdin);
//  freopen(".out","w",stdout);
    cin>>s;
    int n=strlen(s),p=0;
    Rep(i,n-2) {
        if (s[i]=='L'&&s[i+1]=='S'&&s[i+2]=='C') ++p;
    }
    Rep(i,n-3) {
        if (s[i]=='P'&&s[i+1]=='C'&&s[i+2]=='M'&&s[i+3]=='S') --p;
    }
    if (!p) puts("Tie");else puts(p>0?"LSC":"PCMS");

    return 0;
}

H Hit!

给2个圆保证存在不为0的公共部分,求公共部分的任意一个点
分2种情况,内含,内切(此时解取1个圆的圆心),相交圆心连线上的一个点。

#include
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
#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 
#define pi pair
#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;
} 
ll sqr(ll a){return a*a;}
ld sqr(ld a){return a*a;}
double sqr(double a){return a*a;}
const double eps=1e-10;
int dcmp(double x) {
    if (fabs(x)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;
    scanf("%lf%lf",&a.x,&a.y);
    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 DistanceToLine(P p,P A,P B) {
    V v1 = B-A, v2 = p-A;
    return fabs(Cross(v1,v2))/Length(v1);
}
P GetLineProjection(P p,P A,P B) {
    V v=B-A;
    return A+v*(Dot(v,p-A)/Dot(v,v));
}
struct C{
    P c;
    double r,x,y;
    C(){}
    C(P c,double r):c(c),r(r),x(c.x),y(c.y){}
    P point(double a) {
        return P(c.x+cos(a)*r,c.y+sin(a)*r);
    }
};

struct Line{
    P p;
    V v;
    double ang;
    Line(){}
    Line(P p,V v):p(p),v(v) {ang=atan2(v.y,v.x); }
    bool operator<(const Line & L) const {
        return angdouble a) {
        return p+v*a;
    }
};
int getLineCircleIntersection(Line L,C cir,double &t1,double &t2,vector

& sol) { if (dcmp(DistanceToLine(cir.c,L.p,L.p+L.v)-cir.r)==0) { P A=GetLineProjection(cir.c,L.p,L.p+L.v); sol.pb(A); t1 = (A-L.p).x / L.v.x; return 1; } double a = L.v.x, b = L.p.x - cir.c.x, c = L.v.y, d= L.p.y - cir.c.y; double e = a*a+c*c, f = 2*(a*b + c*d), g = b*b+d*d-cir.r*cir.r; double delta = f*f - 4*e*g; if (dcmp(delta)<0) return 0; else if (dcmp(delta)==0) { t1 = t2 = -f / (2*e); sol.pb(L.point(t1)); return 1; } t1 = (-f - sqrt(delta)) / (2*e); sol.pb(L.point(t1)); t2 = (-f + sqrt(delta)) / (2*e); sol.pb(L.point(t2)); return 2; } double angle(V v) {return atan2(v.y,v.x);} int getCircleCircleIntersection(C C1,C C2,vector

& sol) { double d = Length(C1.c-C2.c); if (dcmp(d)==0) { if (dcmp(C1.r - C2.r)==0) return -1; //2圆重合 return 0; } if (dcmp(C1.r+C2.r-d)<0) return 0; if (dcmp(fabs(C1.r-C2.r)-d)>0) return 0; double a = angle(C2.c-C1.c); double da = acos((C1.r*C1.r+d*d - C2.r*C2.r)/ (2*C1.r*d)); P p1 = C1.point(a-da), p2 = C1.point(a+da); sol.pb(p1); if (p1==p2) return 1; sol.pb(p2); return 2; } int main() { // freopen("H.in","r",stdin); // freopen(".out","w",stdout); double x,y,r,a,b,c; cin>>x>>y>>r>>a>>b>>c; P p1=P(x,y),p2=P(a,b); C c0=C(p1,r),c1=C(p2,c); vector

sol; if (getCircleCircleIntersection(c0,c1,sol)>=2) { printf("%.10lf %.10lf\n",(sol[0].x+sol[1].x)/2 ,(sol[0].y+sol[1].y)/2 ); } else if (rprintf("%.10lf %.10lf\n",x,y ); } else { printf("%.10lf %.10lf\n",a,b ); } return 0; }

Inverted Signs

显然翻转一段只会影响2端点的贡献

#include 
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 ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i
#define ForD(i,n) for(int i=n;i;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,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector 
#define pi pair
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<' ';\
                        cout<#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
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 MAXN (1123456)
ll h[MAXN],s[MAXN];
ll Abs(ll x){return max(x,-x);}
int main()
{
//  freopen("I.in","r",stdin);
//  freopen(".out","w",stdout);
    int n=read();
    For(i,n) h[i]=read();
    ll ans=0;
    For(i,n-1) ans+=Abs(h[i]-h[i+1]);
    s[1]=0;
    Fork(i,2,n) {
        s[i]=Abs(-h[i]-h[i-1]) - Abs(h[i]-h[i-1]);
        s[i]=min(s[i],s[i-1]);
    }
    ll del=0,pcost=0;
    ForD(i,n) {
        ll p;
        if (i==n) p=0;
        else p=Abs(-h[i]-h[i+1]) - Abs(h[i]-h[i+1]);

        pcost=min(p,pcost);
        del= min (del , pcost+s[i]);
    }
    cout<return 0;
}

K Knights

四个角无法在第二阶段被其他Knights影响,只能自己覆盖自己。
覆盖了4个角以后能覆盖原图。

#include 
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 ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i
#define ForD(i,n) for(int i=n;i;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,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector 
#define pi pair
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<' ';\
                        cout<#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
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 MAXN (123456)
int b[2][2]={};
int main()
{
//  freopen("K.in","r",stdin);
//  freopen(".out","w",stdout);
    int n=read(),m=read(),k=read();

    For(i,k) {
        int x=read(),y=read();
        if ((11continue;

        if(1==x&&1==y) b[0][0]=1;
        if(1==x&&m==y) b[0][1]=1;
        if(n==x&&1==y) b[1][0]=1;
        if(n==x&&m==y) b[1][1]=1;
    }
    if (n==1 &&m==1) {
        puts(b[1][1]?"0":"1");
    }
    else if (n==1) {
        cout<<2-b[0][0]-b[0][1]<else if (m==1) {
        cout<<2-b[0][0]-b[1][0]<else {
        cout<<4-b[0][0]-b[1][0]-b[0][1]-b[1][1]<return 0;
}

你可能感兴趣的:(比赛题解)