今日SGU 5.26
#include<bits/stdc++.h> #define de(x) cout<<#x<<"="<<x<<endl; #define dd(x) cout<<#x<<"="<<x<<" "; #define rep(i,a,b) for(int i=a;i<(b);++i) #define repd(i,a,b) for(int i=a;i>=(b);--i) #define repp(i,a,b,t) for(int i=a;i<(b);i+=t) #define ll long long #define mt(a,b) memset(a,b,sizeof(a)) #define fi first #define se second #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define pii pair<int,int> #define pdd pair<double,double> #define pdi pair<double,int> #define mp(u,v) make_pair(u,v) #define sz(a) (int)a.size() #define ull unsigned long long #define ll long long #define pb push_back #define PI acos(-1.0) #define qc std::ios::sync_with_stdio(false) #define db double #define all(a) a.begin(),a.end() const int mod = 1e9+7; const int maxn = 2e2+5; const double eps = 1e-6; using namespace std; bool eq(const db &a, const db &b) { return fabs(a - b) < eps; } bool ls(const db &a, const db &b) { return a + eps < b; } bool le(const db &a, const db &b) { return eq(a, b) || ls(a, b); } ll gcd(ll a,ll b) { return a==0?b:gcd(b%a,a); }; ll lcm(ll a,ll b) { return a/gcd(a,b)*b; } ll kpow(ll a,ll b) {ll res=1;a%=mod; if(b<0) return 1; for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll read(){ ll x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } struct Point{ db x,y; }ans[maxn],p1,p2,o; Point get_mid(Point p1,Point p2){ Point ret; ret.x = (p1.x + p2.x) / 2; ret.y = (p1.y + p2.y) / 2; return ret; } Point get_o(Point p1,Point p2,int n1,int n2,int n){//正多边形中点,外接圆圆心 Point mid = get_mid(p1,p2),ret; db ang = PI / 2.0 - 1.0 * (n2 - n1) * PI / n; // 向量的思想 ret.x = mid.x - (p1.y - mid.y) * tan(ang); ret.y = mid.y + (p1.x - mid.x) * tan(ang); return ret; } db f(db x){//会出现-0的情况 return fabs(x)<eps?0:x; } int main(){ int n,n1,n2; scanf("%d%d%d",&n,&n1,&n2); scanf("%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y); if(n1 > n2) swap(n1,n2), swap(p1,p2); n1--; n2--; o = get_o(p1,p2,n1,n2,n); db ang = 2.0 * PI / n; ans[n1] = p1; // dd(o.x)de(o.y) for(int i = n1+1; ;++i){ if(i%n==n1) break; // x0,y0绕rx0,ry0逆时针旋转a度 // x0= (x - rx0)*cos(a) - (y - ry0)*sin(a) + rx0 ; // y0= (x - rx0)*sin(a) + (y - ry0)*cos(a) + ry0 ; ans[i%n].x = o.x + (ans[(i-1+n)%n].x - o.x)*cos(-ang) - (ans[(i-1+n)%n].y - o.y)*sin(-ang); ans[i%n].y = o.y + (ans[(i-1+n)%n].x - o.x)*sin(-ang) + (ans[(i-1+n)%n].y - o.y)*cos(-ang); } rep(i,0,n) printf("%.8f %.8f\n",f(ans[i].x),f(ans[i].y)); return 0; }
SGU 120
题意:给你正n边形上的两个点,让你求出正n边形上的所有点,1 - n是顺时针
收获:求正n边形中心或者说是外接圆的圆心,向量旋转的公式[x*cosA-y*sinA x*sinA+y*cosA](向量逆时针旋转A)
#include<bits/stdc++.h> #define de(x) cout<<#x<<"="<<x<<endl; #define dd(x) cout<<#x<<"="<<x<<" "; #define rep(i,a,b) for(int i=a;i<(b);++i) #define repd(i,a,b) for(int i=a;i>=(b);--i) #define repp(i,a,b,t) for(int i=a;i<(b);i+=t) #define ll long long #define mt(a,b) memset(a,b,sizeof(a)) #define fi first #define se second #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define pii pair<int,int> #define pdd pair<double,double> #define pdi pair<double,int> #define mp(u,v) make_pair(u,v) #define sz(a) (int)a.size() #define ull unsigned long long #define ll long long #define pb push_back #define PI acos(-1.0) #define qc std::ios::sync_with_stdio(false) #define db double #define all(a) a.begin(),a.end() const int mod = 1e9+7; const int maxn = 2e2+5; const double eps = 1e-6; using namespace std; bool eq(const db &a, const db &b) { return fabs(a - b) < eps; } bool ls(const db &a, const db &b) { return a + eps < b; } bool le(const db &a, const db &b) { return eq(a, b) || ls(a, b); } ll gcd(ll a,ll b) { return a==0?b:gcd(b%a,a); }; ll lcm(ll a,ll b) { return a/gcd(a,b)*b; } ll kpow(ll a,ll b) {ll res=1;a%=mod; if(b<0) return 1; for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll read(){ ll x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } struct Point{ db x,y; }ans[maxn],p1,p2,o; Point get_mid(Point p1,Point p2){ Point ret; ret.x = (p1.x + p2.x) / 2; ret.y = (p1.y + p2.y) / 2; return ret; } Point get_o(Point p1,Point p2,int n1,int n2,int n){//正多边形中点,外接圆圆心 Point mid = get_mid(p1,p2),ret; db ang = PI / 2.0 - 1.0 * (n2 - n1) * PI / n; // 向量的思想 ret.x = mid.x - (p1.y - mid.y) * tan(ang); ret.y = mid.y + (p1.x - mid.x) * tan(ang); return ret; } db f(db x){//会出现-0的情况 return fabs(x)<eps?0:x; } int main(){ int n,n1,n2; scanf("%d%d%d",&n,&n1,&n2); scanf("%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y); if(n1 > n2) swap(n1,n2), swap(p1,p2); n1--; n2--; o = get_o(p1,p2,n1,n2,n); db ang = 2.0 * PI / n; ans[n1] = p1; // dd(o.x)de(o.y) for(int i = n1+1; ;++i){ if(i%n==n1) break; // x0,y0绕rx0,ry0逆时针旋转a度 // x0= (x - rx0)*cos(a) - (y - ry0)*sin(a) + rx0 ; // y0= (x - rx0)*sin(a) + (y - ry0)*cos(a) + ry0 ; ans[i%n].x = o.x + (ans[(i-1+n)%n].x - o.x)*cos(-ang) - (ans[(i-1+n)%n].y - o.y)*sin(-ang); ans[i%n].y = o.y + (ans[(i-1+n)%n].x - o.x)*sin(-ang) + (ans[(i-1+n)%n].y - o.y)*cos(-ang); } rep(i,0,n) printf("%.8f %.8f\n",f(ans[i].x),f(ans[i].y)); return 0; }
SGU 186
题意:给你n个链子,每个链子有li个串,你可以在一个时间段里,把一个串从一个链子拆下来,然后用它把起来的链子连起来,
问你最少用多少时间来把所有的链子拼成一个链子
收获:贪心
#include<bits/stdc++.h> #define de(x) cout<<#x<<"="<<x<<endl; #define dd(x) cout<<#x<<"="<<x<<" "; #define rep(i,a,b) for(int i=a;i<(b);++i) #define repd(i,a,b) for(int i=a;i>=(b);--i) #define repp(i,a,b,t) for(int i=a;i<(b);i+=t) #define ll long long #define mt(a,b) memset(a,b,sizeof(a)) #define fi first #define se second #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define pii pair<int,int> #define pdd pair<double,double> #define pdi pair<double,int> #define mp(u,v) make_pair(u,v) #define sz(a) (int)a.size() #define ull unsigned long long #define ll long long #define pb push_back #define PI acos(-1.0) #define qc std::ios::sync_with_stdio(false) #define db double #define all(a) a.begin(),a.end() const int mod = 1e9+7; const int maxn = 2e2+5; const double eps = 1e-6; using namespace std; bool eq(const db &a, const db &b) { return fabs(a - b) < eps; } bool ls(const db &a, const db &b) { return a + eps < b; } bool le(const db &a, const db &b) { return eq(a, b) || ls(a, b); } ll gcd(ll a,ll b) { return a==0?b:gcd(b%a,a); }; ll lcm(ll a,ll b) { return a/gcd(a,b)*b; } ll kpow(ll a,ll b) {ll res=1;a%=mod; if(b<0) return 1; for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll read(){ ll x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int l[maxn]; int main(){ int n; scanf("%d",&n); rep(i,0,n) scanf("%d",l+i); sort(l,l+n); int sum = n - 1,ans = 0;; rep(i,0,n) { while(l[i]--&&sum){ ans++; sum--; if(!sum) break; } if(!sum) break; sum--; } printf("%d\n",ans); return 0; }
SGU 226
题意:给你n个点,m个边,每个边有一个颜色,他要你找到点1到点n的最短距离,路径上的相邻两条边的颜色不能一样
收获:spfa + 颜色限制
#include<bits/stdc++.h> #define de(x) cout<<#x<<"="<<x<<endl; #define dd(x) cout<<#x<<"="<<x<<" "; #define rep(i,a,b) for(int i=a;i<(b);++i) #define repd(i,a,b) for(int i=a;i>=(b);--i) #define repp(i,a,b,t) for(int i=a;i<(b);i+=t) #define ll long long #define mt(a,b) memset(a,b,sizeof(a)) #define fi first #define se second #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define pii pair<int,int> #define pdd pair<double,double> #define pdi pair<double,int> #define mp(u,v) make_pair(u,v) #define sz(a) (int)a.size() #define ull unsigned long long #define ll long long #define pb push_back #define PI acos(-1.0) #define qc std::ios::sync_with_stdio(false) #define db double #define all(a) a.begin(),a.end() const int mod = 1e9+7; const int maxn = 2e2+5; const double eps = 1e-6; using namespace std; bool eq(const db &a, const db &b) { return fabs(a - b) < eps; } bool ls(const db &a, const db &b) { return a + eps < b; } bool le(const db &a, const db &b) { return eq(a, b) || ls(a, b); } ll gcd(ll a,ll b) { return a==0?b:gcd(b%a,a); }; ll lcm(ll a,ll b) { return a/gcd(a,b)*b; } ll kpow(ll a,ll b) {ll res=1;a%=mod; if(b<0) return 1; for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll read(){ ll x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,m; struct edge{ int v,nt,c; }e[maxn*maxn]; int pre[maxn],tot; int dis[5][maxn]; bool in[maxn]; void init(){ tot = 0; mt(in,false);mt(pre,-1); rep(i,1,4) rep(j,2,n+1) dis[i][j] = inf; dis[0][1] = dis[1][1] = dis[2][1] = dis[3][1] = 0; } void add(int u,int v,int c){ e[tot].v = v; e[tot].c = c; e[tot].nt = pre[u]; pre[u] = tot++; } void spfa(){ queue<int> q; q.push(1); while(sz(q)){ int u = q.front(); q.pop(); in[u] = false; de(u) for(int i = pre[u]; ~i; i = e[i].nt){ int v = e[i].v , col = e[i].c; rep(j,1,4) if(j != col) { if(dis[col][v] > dis[j][u] + 1){ dis[col][v] = dis[j][u] + 1; if(!in[v]) q.push(v),in[v] = true; } } } } } int main(){ scanf("%d%d",&n,&m); init(); rep(i,0,m) { int u,v,c; scanf("%d%d%d",&u,&v,&c); add(u,v,c); } spfa(); int mn = inf; // rep(j,1,n+1) rep(i,1,4) printf("%d%c",dis[i][j]," \n"[i==3]); rep(i,1,4) mn = min(dis[i][n],mn); printf("%d\n",mn==inf?-1:mn); return 0; } /* 5 5 1 2 2 1 3 2 2 3 3 2 4 1 3 5 2 3 3 1 2 2 2 2 1 2 3 2 */
SGU 174
题意:给你n个线段,线段只会在端点相交,然后问你到第几步的时候 ,线段围成一个封闭区间
收获:并查集无向图判环
#include<bits/stdc++.h> #define de(x) cout<<#x<<"="<<x<<endl; #define dd(x) cout<<#x<<"="<<x<<" "; #define rep(i,a,b) for(int i=a;i<(b);++i) #define repd(i,a,b) for(int i=a;i>=(b);--i) #define repp(i,a,b,t) for(int i=a;i<(b);i+=t) #define ll long long #define mt(a,b) memset(a,b,sizeof(a)) #define fi first #define se second #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define pii pair<int,int> #define pdd pair<double,double> #define pdi pair<double,int> #define mp(u,v) make_pair(u,v) #define sz(a) (int)a.size() #define ull unsigned long long #define ll long long #define pb push_back #define PI acos(-1.0) #define qc std::ios::sync_with_stdio(false) #define db double #define all(a) a.begin(),a.end() const int mod = 1e9+7; const int maxn = 2e5+5; const double eps = 1e-6; using namespace std; bool eq(const db &a, const db &b) { return fabs(a - b) < eps; } bool ls(const db &a, const db &b) { return a + eps < b; } bool le(const db &a, const db &b) { return eq(a, b) || ls(a, b); } ll gcd(ll a,ll b) { return a==0?b:gcd(b%a,a); }; ll lcm(ll a,ll b) { return a/gcd(a,b)*b; } ll kpow(ll a,ll b) {ll res=1;a%=mod; if(b<0) return 1; for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll read(){ ll x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n; int f[maxn<<1]; void init(){ rep(i,1,2*n+5) f[i] = i; } map<pii,int> m; int id = 1; int get_id(pii x){ if(m.count(x)) return m[x]; m[x] = id; ++id; return m[x]; } int fd(int x) { return x==f[x]?x:f[x]=fd(f[x]); } int main(){ int ans = 0; scanf("%d",&n); init(); rep(i,1,n+1){ int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); int id1 = get_id(mp(x1,y1)); int id2 = get_id(mp(x2,y2)); int f1 = fd(id1), f2 = fd(id2); // dd(id1)dd(f1)dd(id2)de(f2) if(f1 == f2) { if(!ans) ans = i; } else f[f1] = f2; } printf("%d\n",ans); return 0; }
SGU 224
题意:n*n的矩形放k个皇后
收获:位运算实现n皇后
大佬的题解:http://www.matrix67.com/blog/archives/266
我曾经傻傻的超时代码
#include<bits/stdc++.h> #define de(x) cout<<#x<<"="<<x<<endl; #define dd(x) cout<<#x<<"="<<x<<" "; #define rep(i,a,b) for(int i=a;i<(b);++i) #define repd(i,a,b) for(int i=a;i>=(b);--i) #define repp(i,a,b,t) for(int i=a;i<(b);i+=t) #define ll long long #define mt(a,b) memset(a,b,sizeof(a)) #define fi first #define se second #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define pii pair<int,int> #define pdd pair<double,double> #define pdi pair<double,int> #define mp(u,v) make_pair(u,v) #define sz(a) (int)a.size() #define ull unsigned long long #define ll long long #define pb push_back #define PI acos(-1.0) #define qc std::ios::sync_with_stdio(false) #define db double #define all(a) a.begin(),a.end() const int mod = 1e9+7; const int maxn = 2e2+5; const double eps = 1e-6; using namespace std; bool eq(const db &a, const db &b) { return fabs(a - b) < eps; } bool ls(const db &a, const db &b) { return a + eps < b; } bool le(const db &a, const db &b) { return eq(a, b) || ls(a, b); } ll gcd(ll a,ll b) { return a==0?b:gcd(b%a,a); }; ll lcm(ll a,ll b) { return a/gcd(a,b)*b; } ll kpow(ll a,ll b) {ll res=1; if(b<0) return 1; for(;b;b>>=1){if(b&1)res=res*a;a=a*a;}return res;} ll read(){ ll x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,k; int ans = 0; int dp[12]; int fd_pos(int now){ if(!now) return -1; rep(i,0,n) if(kpow(2,i) == now) return i; } bool ok(int x,int nowpos){ rep(i,1,x){ int pos = fd_pos(dp[i]); if(pos == -1) continue; if(pos == nowpos) return false; if(abs(x - i) == abs(nowpos - pos)) return false; } return true; } void dfs(int x,int d){ if(x == n+1){ if(!d) ans++; return; } rep(i,0,n){ int now = kpow(2,i); if(!ok(x,i) || !d) continue; dp[x] = now; if(d) dfs(x + 1,d - 1); } dp[x] = 0; dfs(x + 1,d); } int main(){ scanf("%d%d",&n,&k); if(!k) return puts("1"),0; if(k > n) return puts("0"),0; dfs(1,k); printf("%d\n",ans); return 0; }
AC代码
#include<bits/stdc++.h> #define de(x) cout<<#x<<"="<<x<<endl; #define dd(x) cout<<#x<<"="<<x<<" "; #define rep(i,a,b) for(int i=a;i<(b);++i) #define repd(i,a,b) for(int i=a;i>=(b);--i) #define repp(i,a,b,t) for(int i=a;i<(b);i+=t) #define ll long long #define mt(a,b) memset(a,b,sizeof(a)) #define fi first #define se second #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define pii pair<int,int> #define pdd pair<double,double> #define pdi pair<double,int> #define mp(u,v) make_pair(u,v) #define sz(a) (int)a.size() #define ull unsigned long long #define ll long long #define pb push_back #define PI acos(-1.0) #define qc std::ios::sync_with_stdio(false) #define db double #define all(a) a.begin(),a.end() const int mod = 1e9+7; const int maxn = 2e2+5; const double eps = 1e-6; using namespace std; bool eq(const db &a, const db &b) { return fabs(a - b) < eps; } bool ls(const db &a, const db &b) { return a + eps < b; } bool le(const db &a, const db &b) { return eq(a, b) || ls(a, b); } ll gcd(ll a,ll b) { return a==0?b:gcd(b%a,a); }; ll lcm(ll a,ll b) { return a/gcd(a,b)*b; } ll kpow(ll a,ll b) {ll res=1; if(b<0) return 1; for(;b;b>>=1){if(b&1)res=res*a;a=a*a;}return res;} ll read(){ ll x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,k; int ans = 0; int lowbit(int x) { return x & (-x); } void dfs(int x,int l,int r,int d,int num){ if(num == k){ ans++; return; } if(x == n + 1) return; int canpos = ((1 << n)- 1) & (~(r | l | d)); while(canpos) { int t = lowbit(canpos); canpos -= t; dfs(x + 1, (l | t) << 1, (r | t) >> 1, d | t,num + 1); } dfs(x + 1, l << 1, r >> 1,d,num); } int main(){ scanf("%d%d",&n,&k); if(!k) return puts("1"),0; if(k > n) return puts("0"),0; dfs(1,0,0,0,0); printf("%d\n",ans); return 0; }