#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+;
const int maxn = 2e2+;
const double eps = 1e-;
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==?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=;a%=mod; if(b<) return ; for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
ll read(){
ll x=,f=;char ch=getchar();
while (ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while (ch>=''&&ch<=''){x=x*+ch-'';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) / ;
ret.y = (p1.y + p2.y) / ;
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?: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+; ;++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-+n)%n].x - o.x)*cos(-ang) - (ans[(i-+n)%n].y - o.y)*sin(-ang);
ans[i%n].y = o.y + (ans[(i-+n)%n].x - o.x)*sin(-ang) + (ans[(i-+n)%n].y - o.y)*cos(-ang);
}
rep(i,,n) printf("%.8f %.8f\n",f(ans[i].x),f(ans[i].y));
return ;
}
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+;
const int maxn = 2e2+;
const double eps = 1e-;
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==?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=;a%=mod; if(b<) return ; for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
ll read(){
ll x=,f=;char ch=getchar();
while (ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while (ch>=''&&ch<=''){x=x*+ch-'';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) / ;
ret.y = (p1.y + p2.y) / ;
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?: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+; ;++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-+n)%n].x - o.x)*cos(-ang) - (ans[(i-+n)%n].y - o.y)*sin(-ang);
ans[i%n].y = o.y + (ans[(i-+n)%n].x - o.x)*sin(-ang) + (ans[(i-+n)%n].y - o.y)*cos(-ang);
}
rep(i,,n) printf("%.8f %.8f\n",f(ans[i].x),f(ans[i].y));
return ;
}
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+;
const int maxn = 2e2+;
const double eps = 1e-;
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==?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=;a%=mod; if(b<) return ; for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
ll read(){
ll x=,f=;char ch=getchar();
while (ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while (ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int l[maxn];
int main(){
int n;
scanf("%d",&n);
rep(i,,n) scanf("%d",l+i);
sort(l,l+n);
int sum = n - ,ans = ;;
rep(i,,n) {
while(l[i]--&&sum){
ans++;
sum--;
if(!sum) break;
}
if(!sum) break;
sum--;
}
printf("%d\n",ans);
return ;
}
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+;
const int maxn = 2e2+;
const double eps = 1e-;
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==?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=;a%=mod; if(b<) return ; for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
ll read(){
ll x=,f=;char ch=getchar();
while (ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while (ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m;
struct edge{
int v,nt,c;
}e[maxn*maxn];
int pre[maxn],tot;
int dis[][maxn];
bool in[maxn];
void init(){
tot = ;
mt(in,false);mt(pre,-);
rep(i,,) rep(j,,n+) dis[i][j] = inf;
dis[][] = dis[][] = dis[][] = dis[][] = ;
}
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();
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,,) if(j != col) {
if(dis[col][v] > dis[j][u] + ){
dis[col][v] = dis[j][u] + ;
if(!in[v]) q.push(v),in[v] = true;
}
}
}
}
}
int main(){
scanf("%d%d",&n,&m);
init();
rep(i,,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,,) mn = min(dis[i][n],mn);
printf("%d\n",mn==inf?-:mn);
return ;
}
/*
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+;
const int maxn = 2e5+;
const double eps = 1e-;
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==?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=;a%=mod; if(b<) return ; for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
ll read(){
ll x=,f=;char ch=getchar();
while (ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while (ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n;
int f[maxn<<];
void init(){
rep(i,,*n+) f[i] = i;
}
map<pii,int> m;
int id = ;
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 = ;
scanf("%d",&n);
init();
rep(i,,n+){
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 ;
}
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+;
const int maxn = 2e2+;
const double eps = 1e-;
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==?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=; if(b<) return ; for(;b;b>>=){if(b&)res=res*a;a=a*a;}return res;}
ll read(){
ll x=,f=;char ch=getchar();
while (ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while (ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,k;
int ans = ;
int dp[];
int fd_pos(int now){
if(!now) return -;
rep(i,,n) if(kpow(,i) == now) return i;
}
bool ok(int x,int nowpos){
rep(i,,x){
int pos = fd_pos(dp[i]);
if(pos == -) 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+){
if(!d) ans++;
return;
}
rep(i,,n){
int now = kpow(,i);
if(!ok(x,i) || !d) continue;
dp[x] = now;
if(d) dfs(x + ,d - );
}
dp[x] = ;
dfs(x + ,d);
}
int main(){
scanf("%d%d",&n,&k);
if(!k) return puts(""),;
if(k > n) return puts(""),;
dfs(,k);
printf("%d\n",ans);
return ;
}
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+;
const int maxn = 2e2+;
const double eps = 1e-;
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==?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=; if(b<) return ; for(;b;b>>=){if(b&)res=res*a;a=a*a;}return res;}
ll read(){
ll x=,f=;char ch=getchar();
while (ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while (ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,k;
int ans = ;
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 + ) return;
int canpos = (( << n)- ) & (~(r | l | d));
while(canpos) {
int t = lowbit(canpos);
canpos -= t;
dfs(x + , (l | t) << , (r | t) >> , d | t,num + );
}
dfs(x + , l << , r >> ,d,num);
}
int main(){
scanf("%d%d",&n,&k);
if(!k) return puts(""),;
if(k > n) return puts(""),;
dfs(,,,,);
printf("%d\n",ans);
return ;
}