2020牛客第二场
程序员文章站
2022-05-14 08:09:51
...
B Boundary
题意:
给你n个点,让你找最多有多少个点共圆并且该圆过原点。
解法:pair 维护点个数和坐标,最后找最多的那个;
坑点:注意共线,注意最后可能是一个或者两个点的例外情况,注意公式正确
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
const int INF=0x3f3f3f3f;
const double pi=acos(-1),eps=1e-10;
struct Point{
double x,y;
Point(double x,double y){
this->x=x;
this->y=y;
}
Point(){
x=y=0;
}
};
Point p[5005];
Point waixin(Point a,Point b,Point c){
double a1 = b.x - a.x, b1 = b.y - a.y, c1 = (a1*a1 + b1*b1)/2;
double a2 = c.x - a.x, b2 = c.y - a.y, c2 = (a2*a2 + b2*b2)/2;
double d = a1*b2 - a2*b1;
return Point(a.x + (c1*b2 - c2*b1)/d, a.y + (a1*c2 -a2*c1)/d);
}
pair<double,double>mp[1005*2005];
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);cout.tie(0);
int n,k=0;
scanf("%d",&n);
double ta,tb;
for(int i=0;i<n;i++){
scanf("%lf%lf",&ta,&tb);
p[i].x=ta;
p[i].y=tb;
}
Point z(0,0);
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(fabs(p[i].x*p[j].y-p[i].y*p[j].x)>eps){
Point tmp=waixin(p[i],p[j],z);
mp[k++]=make_pair(tmp.x,tmp.y);
}
}
}
if(k==0){
printf("1\n");
return 0;
}
int cnt=1,ans=1;
sort(mp,mp+k);
for(int i=1;i<k;i++){
if(mp[i].first==mp[i-1].first&&mp[i].second==mp[i-1].second)
cnt++,ans=max(ans,cnt);
else cnt=1;
}
//cout<<ans<<endl;
for(int i=1;i<=n;i++){
if(i*(i-1)/2==ans){
printf("%d\n",i);
return 0;
}
}
return 0;
}
C
#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 2e5 + 5;
int head[N], du[N];
int tot = 0;
vector<int> ans;
struct node
{
int next, v;
}e[N << 1];
void add(int u, int v)
{
e[tot].next = head[u];
e[tot].v = v;
head[u] = tot++;
}
void dfs(int u, int pre)
{
if(du[u] == 1) ans.push_back(u); //度为一的点为叶子节点
for(int i = head[u]; i != -1; i = e[i].next)
{
int v = e[i].v;
if(v != pre) //深度检测,无向图
dfs(v, u);
}
}
int main()
{
int n, u, v;
scanf("%d", &n);
memset(head, -1, sizeof(head));
for(int i = 1; i < n; ++ i)
{
scanf("%d%d", &u, &v);
add(u, v); add(v, u);
++du[u]; ++du[v];
}
int root = 0;
for(int i = 1; i <= n; ++ i) //找到一个根节点
{
if(du[i] != 1) //根节点度数不为1
{
root = i;
break;
}
}
dfs(root, -1);
int len = ans.size();
if(len & 1)
{
++len; //奇数个叶子节点,多出一个答案连根节点
ans.push_back(root);
}
len /= 2;
printf("%d\n", len);
for(int i = 0; i < len; ++i)
{
printf("%d %d\n", ans[i], ans[len + i]);
}
return 0;
}
F
题目类型:单调队列
题目大意:n*m的矩阵,a[i][j]=gcd(i,j),求所有k阶子矩阵中最大元素的和
题目思路:先对行做单调队列,每k个元素的最大值记录下来,再对新数组的列用单调队列,记录每k个元素的最大值,求和即可
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 5005;
int a[maxn][maxn],b[maxn][maxn];
int main()
{
int n,m,k;
ll ans=0;
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
a[i][j]=i*j/__gcd(i,j);
}
}
deque<int>q;
for(int i=1;i<=n;i++){
q.clear();
for(int j=1;j<=m;j++){
while(q.size()&&q.front()<=j-k) q.pop_front();///如果队首元素的下标不属于区间范围,删掉
while(q.size()&&a[i][q.back()]<=a[i][j]) q.pop_back();///如果队尾元素小于准备更新的数,删掉(保持单调性)
q.push_back(j);///压入队列(单调队列模板-max)
b[i][j]=max(b[i][j],a[i][q.front()]);
}
}
for(int j=k;j<=m;j++){
q.clear();
for(int i=1;i<=n;i++){
while(q.size()&&q.front()<=i-k) q.pop_front();
while(q.size()&&b[q.back()][j]<=b[i][j]) q.pop_back();
q.push_back(i);
if(i>=k) ans+=1ll*b[q.front()][j];
}
}
printf("%lld\n",ans);
return 0;
}
上一篇: 2020 牛客多校暑期第二场