1. 程式人生 > >【CF套題】 Gym101623 NWERC2017

【CF套題】 Gym101623 NWERC2017

【前言】
在BZOJ上翻題看到了幾題,心血來潮就全寫了。
C這個搜尋我不是很會寫,直接看了別人的(然而還是沒寫)
然後這個J讓我知道一定不能在還要用值的時候刪掉一個指標。

【題目】
原題地址

A.Ascending Photo

單獨寫

B.Boss Battle

顯然每次可以縮減一個可能的位置,所以若 n 3 n\leq 3

則答案為 1 1 ,否則答案就是 n 2 n-2

#include<bits/stdc++.h>
#define pb push_back #define mkp make_pair #define fi first #define se second using namespace std; typedef long long ll; typedef pair<int,int> pii; int read() { int ret=0,f=1;char c=getchar(); while(!isdigit(c)) {if(c=='-')f=0;c=getchar();} while(isdigit(c)) ret=ret*10+(c^48),c=getchar(); return
f?ret:-ret; } int main() { #ifndef ONLINE_JUDGE freopen("B.in","r",stdin); freopen("B.out","w",stdout); #endif int n=read(); if(n<=3) puts("1"); else printf("%d\n",n-2); return 0; }

C.Connect the Dots

考慮任意一條線段一定滿足:斜率為2個不超過20的整數的比值且至少經過1個點。
搜尋狀態為(當前畫到了第幾個點,當前所在射線的方向)
轉移為: 在這個點轉一下方向。 直接畫到下一個點。畫到下一個點,中間轉一次方向。

顯然不是我的程式碼

#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;
} 
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)<eps) return 0; else return x<0 ? -1 : 1; 
}
ld PI = 3.141592653589793238462643383;
class P{
public:
    ll x,y;
    P(ll x=0,ll y=0):x(x),y(y){}
    friend ll Dot(P A,P B) {return 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,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 operator-()const {return P(-x,-y);}
}; 
P read_point() {
    P a;
    scanf("%lld%lld",&a.x,&a.y);
    return a;   
} 
bool operator==(const P& a,const P& b) {
    return a.x==b.x&&a.y==b.y;
} 
bool operator!=(const P& a,const P& b) {
    return a.x!=b.x||a.y!=b.y;
} 

typedef P V;
ll Cross(V A,V B) {return A.x*B.y - A.y*B.x;}

int n=4,a[1000][100];
P p[1000];
bool OnRay(P a,V v,P b){
    return  (dcmp(Cross(b-a,v))==0 && dcmp(Dot(b-a,v))>=0) ; 
}
double x_max=10000;
P c;
bool RayIntesection(P a,V v,P b,V w){
    int det=Cross(v,w);
    if(dcmp(det)==0) {
        if (!OnRay(a,v,b) && !OnRay(b,w,a)) return false;
    } else {
        int s = Cross(b-a,w), t = Cross(b-a,v);
        if (s*det < 0 || t*det < 0) return false;
        double ss = double(s)/det;
        double cx = a.x+ss*v.x,cy = a.y+ss*v.y;
        if (abs(cx) > x_max || abs(cy) > x_max) return false;
    }
    return true;
} 
typedef pair<int,P> state;

int dist[100][100][100];

int main()
{
#ifndef ONLINE_JUDGE
	freopen("C.in","r",stdin);
	freopen("C.out","w",stdout);
#endif
  	memset(dist,0x3f3f3f3f,sizeof(dist));
    For(i,4) {
        For(j,4) {
            a[i][j]=read();
            p[a[i][j]-1]=P(j,-i+5);
        }
    }
    vector<P> vs;
    Fork(x,-30,30) Fork(y,-30,30) if(abs(__gcd(x,y))==1) {
        vs.pb(P(x,y));
    } 
    deque<state> q;
    for(auto v:vs) {
        dist[v.x+50][v.y+50][0] = 1;
        q.emplace_back(0,v);
    }
    auto relax = [&] (state s,state t,int len){
        if(dist[t.se.x+50][t.se.y+50][t.fi]>dist[s.se.x+50][s.se.y+50][s.fi]+len) {
            dist[t.se.x+50][t.se.y+50][t.fi]=dist[s.se.x+50][s.se.y+50][s.fi]+len;
            if(!len) q.push_front(t);
            else q.push_back(t);
        }
    };
    int ans=1e9+7;
    int an[16];
    Rep(i,16) an[i]=INF;
    while(!q.empty()){
        int i; P v;
        tie(i,v) = q.front();
        q.pop_front();
        an[i]=min(an[i],dist[v.x+50][v.y+50][i]);
        if (i==15) {
            ans=min(ans,dist[v.x+50][v.y+50][i])    ;
            continue;
        }
        for(auto w:vs) {
            if (v!=w) {
                relax({i,v},{i,w},1); //正好在點i改變方向 
            }
        }
        if(OnRay(p[i],v,p[i+1])) {
            relax({i,v},{i+1,v},0); //不改變方向 
//          cerr<<i<<' '<<dist[v.x+50][v.y+50][i+1]<<endl;
        }
        for(auto w:vs) {
            if (v!=w) {
                if (!RayIntesection(p[i],v,p[i+1],-w)) continue;
                relax({i,v},{i+1,w},1); //在i點延長出去某點改變方向
            }
        }
    }
//  Rep(i,16) cerr<<an[i]<<' ';puts("");
    cout<<ans<<endl;
    return 0;
}

D.Dunglish

按題意模擬,用乘法原理計算即可。

#include<bits/stdc++.h>
#define pb push_back
#define mkp make_pair
#define fi first
#define se second
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
int n,m;
string s[25],st1,st2,st3;
ll ans1,sum;
map<string,pii>mp;
map<string,string>mp2;

int read()
{
	int ret=0,f=1;char c=getchar();
	while(!isdigit(c)) {if(c=='-')f=0;c=getchar();}
	while(isdigit(c)) ret=ret*10+(c^48),c=getchar();
	return f?ret:-ret;
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("D.in","r",stdin);
	freopen("D.out","w",stdout);
#endif
	n=read();
	for(int i=1;i<=n;++i) cin>>s[i];
	m=read();
	for(int i=1;i<=m;++i)
	{
		cin>>st1>>st2>>st3;