2022牛客五一集训派对day1(E、G)

E:

题目描述:

Niuniu wants to tour the cities in Moe country. Moe country has a total of n*m cities. The positions of the cities form a grid with n rows and m columns. We represent the city in row x and column y as (x,y). (1 ≤ x ≤ n,1 ≤ y ≤ m) There are bidirectional railways between adjacent cities. (x1,y1) and (x2,y2) are called adjacent if and only if |x1-x2|+|y1-y2|=1. There are also K bidirectional air lines between K pairs of cities. It takes Niuniu exactly one day to travel by a single line of railway or airplane. Niuniu starts and ends his tour in (1,1). What is the minimal time Niuniu has to travel between cities so that he can visit every city at least once?

Note that the air line may start and end in the same city.

输入描述:

The first line contains oneinteger T(T≤20), which means the number of test cases.

Each test case has the format as described below.

n m K

ax1 ay1 bx1 by1
ax2 ay2 bx2 by2

axK ayK bxK byK

(0 ≤ K ≤ 10. 2 ≤ n,m ≤ 100, 1 ≤ n*m ≤ 100)

There is one bidirectional air line between (axi,ayi) and (bxi,byi). (1 ≤ axi,bxi ≤ n , 1 ≤ ayi,byi ≤ m)

输出描述:


For each test case,print one number in a single line, which is the minimal number of days Niuniu has to travel between cities so that he can visit every city at least once.

示例:

3
2 2 1
1 1 2 2
3 3 1
1 1 3 3
3 3 0

输出:

4
9
10

题意:

在保证经过所有点的情况下找到一条可以回到初始点的路线。其中除了特殊(输入)的路线仅可以上下左右移动

题解:

易知所求路线最短也需要经过所有点,因此肯定大于等于m*n。n,m至少为偶数时一定可以找到nm的路

 

 

同理当m,n均为奇数时且无捷径则至少需要m*n+1的路线。由此可以解出该题。

通过代码:

#include<bits/stdc++.h>
#include<string>
#include<queue>
#include<map>
#include<set>
using namespace std;
typedef int ll;
const int N=1e9;

int main()
{
    int t,n,m,k,sum=0,flag=0;
    int ax1,ay1,bx2,by2;
    cin>>t;
    while(t--){
        cin>>n>>m>>k;
        flag=0;
        for(int i=0;i<k;i++){
            cin>>ax1>>ay1>>bx2>>by2;
            if(ax1!=bx2||ay1!=by2){   //不是一个点判断
                if((ax1+ay1)%2==0&&(bx2+by2)%2==0){
                    flag=1;
                }
            }
        }
        if(n%2==0||m%2==0){
            sum=n*m;
        }
        else{
            if(flag==0){
                sum=n*m+1;
            }
            else{
                sum=n*m;
            }
        }
        cout<<sum<<endl;
    }
}

G:

题目描述:

Niuniu likes mathematics. He also likes drawing pictures. One day, he was trying to draw a regular polygon with n vertices. He connected every pair of the vertices by a straight line as well. He counted the number of regions inside the polygon after he completed his picture. He was wondering how to calculate the number of regions without the picture. Can you calculate the number of regions modulo 1000000007? It is guaranteed that n is odd.

输入描述:

The only line contains one odd number n(3 ≤ n ≤ 1000000000), which is the number of vertices.

输出描述:

Print a single line with one number, which is the answer modulo 1000000007.

示例 1:

输入:3

输出:1

示例2:

输入:5

输出:11

题意:

输出奇数顶点数多边形可以出现的最多区域个数

知识点:

图论(欧拉公式)

题解:

易知该正多边形顶点位于同一圆上,则满足欧拉公式。

已知欧拉公式:V-E+F=2

V(顶点)= n(N边形的N个顶点) + C(n,4)(在圆上的每四个点确定一个交点)

E(边数)=n(N边形外围的N条边)+C(n,2)(每两个顶点连线确定一条边)+2*C(n,4)(多边形内的交点必然有两条线经过)

由此F(平面内所有区域个数)=E-V+2=C(n,2)+C(n,4)+2 

则F1(多边形内部区域个数)=F-n(N边形N条边和圆所围面积个数)-1(圆外)=C(n,2)+C(n,4)-n+1

(图中红色为上述n的部分)

代码:

#include<iostream>
#include<string>
#include<queue>
#include<map>
#include<set>
using namespace std;
typedef long long int ll;
const int N = 1e9;
const int M = 1e9 + 7;
ll n;

ll P_pow(ll a) //逆元
{
    ll sum = 1, ans = a, m = M - 2;
    while (m) 
    {
        if (m & 1) sum = sum * ans % M;
        ans = ans * ans % M;
        m >>= 1;
    }
    return sum;
}
int main()
{
    ll F = 0, E, V;
    cin >> n;
    E = n * (n - 1) % M * P_pow(2);
    V = n * (n - 1) % M * (n - 2) % M * (n - 3) % M * P_pow(24) % M;
    F = E + V - n + 1;
    cout << F % M;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
牛客 a卷2022年第四季度的华为题目中,要求考生设计一种高效的数据结构,能够支持以下几种操作: 1. 添加一个元素 2. 删除一个元素 3. 查找是否存在某个元素 4. 返回元素的总数 该数据结构要求满足空间复杂度较小、时间复杂度较低、能够快速地进行查找和修改等多种操作。 想要编写这样一种数据结构,我们可以参考许多已有的经典算法与数据结构,如二叉树、哈希表、红黑树等,通过综合利用它们的优点来实现这个问题的解决。 例如,我们可以通过哈希表来存储所有元素的值,并在每个哈希链表的元素中再使用红黑树来进行排序与查找。这样,我们既能够轻松地进行元素的添加和删除操作,也能够在查找较大数据范围和数量时保持较高的速度与效率。同时,由于使用了多个数据结构来协同完成这个问题,我们也能够在空间复杂度上适度地进行优化。 当然,在具体设计这个数据结构的过程中,我们还需要考虑一些实践中的细节问题,例如如何避免哈希冲突、如何处理数据丢失与被删除元素所占用的空间等问题,这都需要相应的算法与流程来进行处理。 总体来看,设计这种支持多种操作的高效数据结构,需要我们具备丰富的算法知识和编程实践能力,同时需要我们在具体处理问题时能够将多种算法和数据结构进行有效地结合。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值