【题目记录】——南京2019区域赛


题目集地址 南京2019区域赛

A A Hard Problem

题目地址 A A Hard Problem
题目大意:给出一个正整数n,找到一个最小的整数k使得集合 1 , 2 , … n {1,2,\dots n} 1,2,n的任意一个大小为k的子集都至少存在两个元素使得其中一个是另一个的因子

思路:一开始认为是素数个数加1,但后来找到了反例:1,2,3,4,5,6,7,8,9如果按照素数个数加一,那么k的大小为5,但是可以找到{5,6,7,8,9}这个集合不满足条件
最简单的方法是直接找规律,可以发现k的大小为 2 , 3 , 3 , 4 , 4 … 2,3,3,4,4\dots 2,3,3,4,4,结果就是 (n+1)/2+1
AC代码:

#include <bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int T,n;
int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cin >>T;
    while(T--) {
        cin >>n;
        cout <<(n+1)/2+1<<endl;
    }
    return 0;
}

C Digital Path 记忆化搜索

题目地址C Digital Path
题目大意:给定一个n ∗ m n∗mn∗m的数字矩阵,从任意一个位置开始走,每次只能上下左右走一格,而且下一个位置必须比当前位置的数字大1,能继续往后走就不能停止,求总共有多少条长度大于等于4的路径。
思路:f(i,j,k)表示的是以(i,j)为终点,长度为k的路径条数。因为只要长度大于等于4就符合要求,所以长度大于等于4的就可以归为一类,也就是 1 ≤ k ≤ 4 1 \leq k \leq 4 1k4
另外设两个数组in[i][j]和out[i][j],记录每个点的入度和出度,入度为0的点为路径的起点,出度为0的点为路径的终点。
对图做拓扑序dp,状态转移方程大致可以写成这样, f ( t x , t y , k ) = ( f ( t x , t y , k ) + f ( x , y , k − 1 ) ) % m o d f(tx,ty,k) = (f(tx,ty,k) + f(x,y,k-1))\%mod f(tx,ty,k)=(f(tx,ty,k)+f(x,y,k1))%mod

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

const int N = 1010, mod = 1e9 + 7;

typedef pair<int,int> pii;

int n, m;
int g[N][N], in[N][N], out[N][N], f[N][N][5];
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};

void topsort()
{
    queue<pii> que;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            if(!in[i][j])
            {
                que.push({i,j});
                f[i][j][1] = 1;
            }
    
    while(que.size())
    {
        auto t = que.front();
        que.pop();
        int x = t.first, y = t.second;
        for(int i=0;i<4;i++)
        {
            int tx = x + dx[i], ty = y + dy[i];
            if(tx<1||tx>n||ty<1||ty>m) continue;
            if(g[tx][ty]==g[x][y]+1)
            {
                f[tx][ty][2] = (f[tx][ty][2] + f[x][y][1]) % mod;
				f[tx][ty][3] = (f[tx][ty][3] + f[x][y][2]) % mod;
				f[tx][ty][4] = (f[tx][ty][4] + f[x][y][3] + f[x][y][4]) % mod;
				if(--in[tx][ty]==0)	que.push({tx,ty});
            }
        }
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            scanf("%d",&g[i][j]);
    
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            for(int k=0;k<4;k++)
            {
                int tx = i + dx[k], ty = j + dy[k];
                if(tx<1||tx>n||ty<1||ty>m) continue;
                if(g[tx][ty]==g[i][j]+1) out[i][j] ++;
                if(g[tx][ty]==g[i][j]-1) in[i][j] ++;
            }
    
    int res = 0;
    topsort();
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            if(!out[i][j])
                res = (res + f[i][j][4]) % mod;
    
    printf("%d\n",res);
    return 0;
}

H Prince and Princess

题目地址H Prince and Princess
题目大意:a需要找b,有三类人,支持a找到b(包括a),不支持,无所谓,每个人(除了a)都在不同的房间里,对每一个人可以询问三种问题:b在几号房,你是谁,谁在x号房,支持者会给出真,不支持者会给出假,第三类会随意给出,现在给出三类人的数量,现在判断a是否能找到b,如果能找到,至少询问几个问题

思路:当支持者大于另两类之和的时候,a一定能找到b,当有两个人对于相同的问题给出相同的答案的时候就无法分辨两个人谁真谁假,最坏的情况下就是每一个支持者都对应一个非支持者回答相同的答案,那么只要支持者比非支持者多一个人就可以分辨出正确答案。如果给出的数量为1,0,0,代表只有一个b,就无需询问了
AC代码:

#include <bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int a,b,c;
int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cin >>a>>b>>c;
    if(a==1&&b==0&&c==0)cout <<"YES\n0";
    else if(a>b+c)cout <<"YES\n"<<2*(b+c)+1;
    else cout <<"NO";
    return 0;
}

K Triangle

题目地址K Triangle
题目大意:给出一个三角形,给出一个点,如果这个点不在边界上输出-1,如果点在边界上,求出另一个点,使得两点间连线能够将三角形平分成两个面积相等的部分

思路:首先判断给出的点是否在边界上,如果不在就直接输出-1,如果在,就尝试另外两条边上的两点,求出总三角形面积,如图,求出三角形在不同情况下满足总面积/2的高,然后根据高算出对应坐标即可
AC代码:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const double eps = 1e-10;

int sign(double x)//判断浮点数的符号
{
    if(fabs(x) <= eps) return 0;
    if(x > 0) return 1;
    else return -1;
}

struct Point
{
    double x,y;
    Point() {}
    //定义运算
    Point(double _x,double _y)
    {
        x = _x;
        y = _y;
    }
    Point operator + (const Point &b)const
    {
        return Point(x+b.x,y+b.y);
    }
    Point operator - (const Point &b)const
    {
        return Point(x-b.x,y-b.y);
    }
    Point operator * (const double &k)const //乘常数
    {
        return Point(x*k,y*k);
    }
    Point operator / (const double &k)const
    {
        return Point(x/k,y/k);
    }
    double len()
    {
        return x * x + y * y;
    }
};
typedef Point Vector;//定义将点重定义为向量
Point p[4],pp;//直接预定义变量,p三个点,pp一个点

double cross(Vector a,Vector b)//叉积
{
    return a.x * b.y - a.y * b.x;
}
double dot(Vector a,Vector b)//点积
{
    return a.x * b.x + a.y * b.y;
}
double dis(Point a,Point b)//两点之间的距离
{
    return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
Point answer(int flag)
{
    Point ans;
    if(flag == 1)
    {
        double d1 = dis(pp,p[2]);
        double d2 = dis(pp,p[3]);
        if(fabs(d1 - d2) <= eps)
            ans = p[1];
        else
        {
            double rate;
            Vector v1,v2,v3;
            if(d1 > d2)
            {
                v1 = pp - p[2],v2 = p[1] - p[2],v3 = p[3] - p[2];
                double dd = fabs(cross(v1,v2)) / v2.len();
                double hh = fabs(cross(v3,v2)) / v2.len();
                rate = hh / dd;
                ans = p[2] + (p[1]-p[2]) * 0.5 * rate;
            }
            else
            {
                v1 = pp - p[3],v2 = p[1] - p[3],v3 = p[2] - p[3];
                double dd = fabs(cross(v1,v2)) / v2.len();
                double hh = fabs(cross(v3,v2)) / v2.len();
                rate = hh / dd;
                ans = p[3] + (p[1]-p[3]) * 0.5 * rate;
            }
        }
    }
    else if(flag == 2)
    {
        double d1 = dis(pp,p[1]);
        double d2 = dis(pp,p[3]);
        if(fabs(d1 - d2) <= eps)
            ans = p[2];
        else
        {
            double rate;
            Vector v1,v2,v3;
            if(d1 > d2)
            {
                v1 = pp - p[1],v2 = p[2] - p[1],v3 = p[3] - p[1];
                double dd = fabs(cross(v1,v2)) / v2.len();
                double hh = fabs(cross(v3,v2)) / v2.len();
                rate = hh / dd;
                ans = p[1] + (p[2]-p[1]) * 0.5 * rate;
            }
            else
            {
                v1 = pp - p[3],v2 = p[2] - p[3],v3 = p[1] - p[3];
                double dd = fabs(cross(v1,v2)) / v2.len();
                double hh = fabs(cross(v3,v2)) / v2.len();
                rate = hh / dd;
                ans = p[3] + (p[2]-p[3]) * 0.5 * rate;
            }
        }
    }
    else if(flag == 3)
    {
        double d1 = dis(pp,p[1]);
        double d2 = dis(pp,p[2]);
        if(fabs(d1 - d2) <= eps)
            ans = p[3];
        else
        {
            double rate;
            Vector v1,v2,v3;
            if(d1 > d2)
            {
                v1 = pp - p[1],v2 = p[3] - p[1],v3 = p[2] - p[1];
                double dd = fabs(cross(v1,v2)) / v2.len();
                double hh = fabs(cross(v3,v2)) / v2.len();
                rate = hh / dd;
                ans = p[1] + (p[3]-p[1]) * 0.5 * rate;
            }
            else
            {
                v1 = pp - p[2],v2 = p[3] - p[2],v3 = p[1] - p[2];
                double dd = fabs(cross(v1,v2)) / v2.len();
                double hh = fabs(cross(v3,v2)) / v2.len();
                rate = hh / dd;
                ans = p[2] + (p[3]-p[2]) * 0.5 * rate;
            }
        }
    }
    return ans;
}
void solve()
{
    Point ans;
    for(int i = 1; i <= 3; i ++)
        scanf("%lf%lf",&p[i].x,&p[i].y);
    scanf("%lf%lf",&pp.x,&pp.y);
    if(pp.x == p[1].x && pp.y == p[1].y)//判断第四个点是否是端点
    {
        Point mid = (p[2] + p[3]) / 2.0;
        printf("%.10f %.10f\n",mid.x,mid.y);
        return ;
    }
    if(pp.x == p[2].x && pp.y == p[2].y)
    {
        Point mid = (p[1] + p[3]) / 2.0;
        printf("%.10f %.10f\n",mid.x,mid.y);
        return ;
    }
    if(pp.x == p[3].x && pp.y == p[3].y)
    {
        Point mid = (p[1] + p[2]) / 2.0;
        printf("%.10f %.10f\n",mid.x,mid.y);
        return ;
    }
    int flag = 0;//判断第四个点是否合法
    Vector v1 = pp - p[1],v2 = pp - p[2],v3 = pp - p[3];
    if(sign(dot(v1,v2)) == -1 && sign(cross(v1,v2)) == 0) flag = 3;
    else if(sign(dot(v1,v3)) == -1 && sign(cross(v1,v3)) == 0) flag = 2;
    else if(sign(dot(v2,v3)) == -1 && sign(cross(v2,v3)) == 0) flag = 1;
    if(!flag)
    {
        printf("-1\n");
        return ;
    }
    ans=answer(flag);//flag 1表示在p2和p3两点之间的边上,2表示在p1和p3之间的边上,3表示在p1和p2之间的边上
    printf("%.10f %.10f\n", ans.x, ans.y);
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值