A. 修路
第一眼看上去是最小生成树, 但是打上去是TLE
几次超时后才发现是并查集求联通块个数= =
联通块个数-1即为最小修的路数
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <climits>
#define MAXN 100005
#define eps 1e-5
#define MOD 1000000009
#define INF 1000000009
#define test
#define For(i,m,n) for(int i=(m);i<(n);i++)
#define vecfor(iter,a) for(vector<int>::iterator iter=a.begin();iter!=a.end();iter++)
#define rep(i,m,n) for(int i=(m);i<=(n);i++)
#define LL long long
/*author birdstorm*/
using namespace std;
const double pi=acos(-1.0);
template<class T>
inline bool read(T &n){
T x=0, tmp=1; char c=getchar();
while((c < '0' || c > '9') && c != '-' && c != EOF) c=getchar();
if(c == EOF) return false;
if(c == '-') c=getchar(), tmp=-1;
while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c=getchar();
n=x*tmp;
return true;
}
template <class T>
inline void write(T n) {
if(n < 0) {
putchar('-');
n=-n;
}
int len=0,data[20];
while(n) {
data[len++]=n%10;
n /= 10;
}
if(!len) data[len++]=0;
while(len--) putchar(data[len]+48);
}
struct edge{
int v,next,w;
bool operator < (const edge &a) const{
return w > a.w;
}
}e[MAXN];
LL a[MAXN];
int vis[MAXN];
struct Disjoint{
vector<int> father,rank;
vector<LL> s;
Disjoint(int n):father(n),rank(n), s(n){
For(i,0,n) father[i]=i;
}
int find(int v){
return father[v]=father[v]==v?v:find(father[v]);
}
void merge(int x,int y){
int a=find(x), b=find(y);
if(a==b) return;
if(rank[a]<rank[b]){
father[a]=b;
}
else{
father[b]=father[a];
if(rank[b]==rank[a]) ++rank[a];
}
}
void clear(int n){
For(i,0,n) father[i]=i, rank[i]=0;
}
};
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int a[20][20];
int u, v, w;
Disjoint D(MAXN);
int n, m;
while(read(n)&&read(m)){
memset(vis,0,sizeof vis);
int ans=0;
D.clear(n+1);
For(i,0,m){
read(u),read(v),read(w);
if(w==0) D.merge(u,v);
}
int cnt=-1;
For(i,1,n+1){
int s=D.find(i);
if(!vis[s]){
vis[s]=1;
cnt++;
}
}
printf("%d\n",cnt);
}
return 0;
}
B. 高兴
C. 排序
大坑题
巨大输入输出, 需要使用桶排和优美的姿势
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <climits>
#define MAXN 100005
#define eps 1e-5
#define MOD 1000000009
#define test
#define For(i,m,n) for(int i=(m);i<(n);i++)
#define vecfor(iter,a) for(vector<int>::iterator iter=a.begin();iter!=a.end();iter++)
#define rep(i,m,n) for(int i=(m);i<=(n);i++)
#define LL long long
/*author birdstorm*/
using namespace std;
const double pi=acos(-1.0);
template<class T>
inline bool read(T &n){
T x = 0, tmp = 1; char c = getchar();
while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
if(c == EOF) return false;
if(c == '-') c = getchar(), tmp = -1;
while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
n = x*tmp;
return true;
}
template <class T>
inline void write(T n) {
if(n < 0) {
putchar('-');
n = -n;
}
int len = 0,data[20];
while(n) {
data[len++] = n%10;
n /= 10;
}
if(!len) data[len++] = 0;
while(len--) putchar(data[len]+48);
}
int vis[MAXN], pos[MAXN];
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int t, n, m, x;
memset(vis,0,sizeof vis);
while(read(n)){
int top=0;
int maxx=-1;
For(i,0,n){
read(x);
if(!vis[x]) pos[top++]=x;
vis[x]++;
maxx=max(maxx,x);
}
sort(pos,pos+top);
For(i,0,top){
int idx=pos[i];
while(vis[idx]){
write(idx), vis[idx]--;
if(idx==maxx&&vis[idx]==0) putchar('\n');
else putchar(' ');
}
}
}
return 0;
}
D. 爱好和平
简单树形dp, 得到每个节点的下方点的个数(包括本身)
所以答案即为 min{ max{total[son_i] }, N-total[k] };
这次最水的就是D了= =
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <climits>
#define MAXN 100005
#define eps 1e-5
#define MOD 1000000009
#define INF 0x3f3f3f3f
#define test
#define For(i,m,n) for(int i=(m);i<(n);i++)
#define vecfor(iter,a) for(vector<int>::iterator iter=a.begin();iter!=a.end();iter++)
#define rep(i,m,n) for(int i=(m);i<=(n);i++)
#define LL long long
/*author birdstorm*/
using namespace std;
const double pi=acos(-1.0);
template<class T>
inline bool read(T &n){
T x=0, tmp=1; char c=getchar();
while((c < '0' || c > '9') && c != '-' && c != EOF) c=getchar();
if(c == EOF) return false;
if(c == '-') c=getchar(), tmp=-1;
while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c=getchar();
n=x*tmp;
return true;
}
template <class T>
inline void write(T n) {
if(n < 0) {
putchar('-');
n=-n;
}
int len=0,data[20];
while(n) {
data[len++]=n%10;
n /= 10;
}
if(!len) data[len++]=0;
while(len--) putchar(data[len]+48);
}
struct edge{
int to,next;
}e[MAXN<<1];
LL a[MAXN];
int vis[MAXN];
int tot;
int head[MAXN];
int total[MAXN];
int dp[MAXN];
void add(int u,int v)
{
e[tot].to=v; e[tot].next=head[u]; head[u]=tot++;
e[tot].to=u; e[tot].next=head[v]; head[v]=tot++;
}
void dfs(int u)
{
dp[u]=0;
total[u]=1;
for(int i=head[u];i!=-1;i=e[i].next){
int v=e[i].to;
if(total[v]==-1) dfs(v);
else continue;
dp[u]=max(dp[u],total[v]);
total[u]+=total[v];
}
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int a[20][20];
int u, v, w;
int n, m;
while(read(n)&&read(m)){
memset(head,-1,sizeof(head));\
memset(total,-1,sizeof(total));
memset(vis,0,sizeof vis);
tot=0;
For(i,0,m){
scanf("%d%d",&u,&v);
add(u,v);
}
memset(dp,0,sizeof(dp));
dfs(1);
int minn=INF, ans;
for(int i=1;i<=n;i++){
int t=max(dp[i],n-total[i]);
if(minn>t){
minn=t;
ans=i;
}
}
printf("%d\n",ans);
}
return 0;
}
E. 萌学妹的手机
想法题, 可以通过变换坐标系使得计算更简便.
写了好久= =
本来思路为在直角坐标系下利用矩形计算, 将点坐标局限后暴力并点
在计算距离时发现很麻烦, 虽然写出来了但是有几个小数据没有通过, 所以就放弃治疗啦~\(≧▽≦)/~
需要注意判断三种情况下答案的不同取值(ry
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define inf 1.0e20
#define eps 1.0e-8
#define For(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,m,n) for(int i=(m);i<=(n);i++)
using namespace std;
typedef struct{
int x, y;
}Point;
typedef struct{
double x, y;
}point;
point s, d;
const double p=sqrt(3);
const double p2=sqrt(3)/2.0;
inline Point getloca(point a, double r)
{
Point tmp;
tmp.y=floor((a.y/1.5)/r+0.5);
tmp.x=floor((a.x/p-a.y/3.0)/r+0.5);
return tmp;
}
inline double dis(point a, point b)
{
double x=a.x-b.x;
double y=a.y-b.y;
return x*x+y*y;
}
int main()
{
int t, n;
double l;
scanf("%d",&t);
while(t--){
scanf("%lf%lf%lf%lf%lf",&l,&s.x,&s.y,&d.x,&d.y);
Point x1=getloca(s,l), x2=getloca(d,l);
double minn=inf;
point t1;
Point tmp, tmpd;
rep(i,x1.x-1,x1.x+1)
rep(j,x1.y-1,x1.y+1){
t1.y=(double)j*1.5*l;
t1.x=(double)i*p*l+(double)j*p2*l;
double dist=dis(t1,s);
if(dist<minn){
minn=dist;
tmp.x=i;
tmp.y=j;
}
}
minn=inf;
rep(i,x2.x-1,x2.x+1){
rep(j,x2.y-1,x2.y+1){
t1.y=(double)j*1.5*l;
t1.x=(double)i*p*l+(double)j*p2*l;
double dist=dis(t1,d);
if(dist<minn){
minn=dist;
tmpd.x=i;
tmpd.y=j;
}
}
}
if(tmpd.y<tmp.y) swap(tmpd,tmp);
int ans=0, dx=tmpd.x-tmp.x, dy=tmpd.y-tmp.y;
if(dx>=0) ans=dx+dy;
else if(dy>=-dx) ans=dy;
else ans=-dx;
printf("%d\n",ans);
}
return 0;
}