CaoHaha's staff(中国大学生程序选拔赛2017年网络大赛)

Problem Description
"You shall not pass!"
After shouted out that,the Force Staff appered in CaoHaha's hand.
As we all know,the Force Staff is a staff with infinity power.If you can use it skillful,it may help you to do whatever you want.
But now,his new owner,CaoHaha,is a sorcerers apprentice.He can only use that staff to send things to other place.
Today,Dreamwyy come to CaoHaha.Requesting him send a toy to his new girl friend.It was so far that Dreamwyy can only resort to CaoHaha.
The first step to send something is draw a Magic array on a Magic place.The magic place looks like a coordinate system,and each time you can draw a segments either on cell sides or on cell diagonals.In additional,you need 1 minutes to draw a segments.
If you want to send something ,you need to draw a Magic array which is not smaller than the that.You can make it any deformation,so what really matters is the size of the object.
CaoHaha want to help dreamwyy but his time is valuable(to learn to be just like you),so he want to draw least segments.However,because of his bad math,he needs your help.
 

Input
The first line contains one integer T(T<=300).The number of toys.
Then T lines each contains one intetger S.The size of the toy(N<=1e9).
 

Output
Out put T integer in each line ,the least time CaoHaha can send the toy.
 

Sample Input
  
  
5 1 2 3 4 5
 

Sample Output
  
  
4 4 6 6

7

分析:

纯粹找规律,从3个格子开始(前两个输出4),四个边一组,比如五条边为零(因为2是四条边,三六条边,五没有),然后六为2,七为1,八为3;之后四组则在原基础上加一。如下

5~89~1213~16
012
234
123
345

#include<bits/stdc++.h>
using namespace std;
int a[4];
int main()
{
     int t;
     cin>>t;
     while(t--)
     {
         int n;
         cin>>n;
         a[0]=0,a[1]=2,a[2]=1,a[3]=3;
         int sum=2,f=4;
         if(n<2) {cout<<4<<endl;continue;}
         else
         {
             while(sum<n)
             {
                 for(int i=0;i<4;i++)
                 {
                    sum+=a[i];
                     a[i]++;f++;
                     if(sum>=n) {break;}
                 }
             }
             cout<<f<<endl;
         }
     }
}


  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
2017年全国大学数学建模竞赛D题是巡检线路排班问题,以下是详细答案和论文。 一、题目描述 在一个城市中,有 $n$ 个巡检点,需要巡检员进行巡视。已知每个巡检点的巡视时间和需要巡视的频次,以及每个巡检员的工作时间和数量。假设每个巡视员每天工作时间固定为 $8$ 小时。 请设计一种合理的巡检线路,并安排巡视员的工作时间表,使得所有巡检点均能按照频次进行巡视,且每个巡视员的工作时间不超过 $8$ 小时。其中,巡检线路的设计应当尽可能地短。 二、解题思路 本题需要设计一种巡检线路,并将巡检员分配到各个巡检点上,使得所有巡检点能够按照频次进行巡视,并且每个巡视员的工作时间不超过 $8$ 小时。 为了最小化巡检线路的长度,我们可以采用贪心算法。具体地,我们可以将巡检点按照优先级排序,然后依次将巡检点加入巡检线路中,直到所有巡检点都被加入为止。 在巡检员分配方面,我们可以采用动态规划算法。具体地,我们将所有巡检点作为背包中的物品集合,将所有可用的巡检员作为背包的容量限制。然后,使用0/1背包算法求解背包问题,得到一个最优的巡检任务分配方案。最后,根据巡检任务分配方案,将巡检员分配到各个巡检点,完成排班问题的求解。 三、论文 以下是本题的论文,供参考。 [PDF] 2017全国大学数学建模竞赛D题巡检线路排班问题 四、代码实现 以下是 Python 实现本题的代码,供参考。 ```python import heapq def schedule(patrol_points, patrol_staff): n = len(patrol_points) # 巡检点数量 m = len(patrol_staff) # 巡检人员数量 dp = [[0] * (m+1) for _ in range(n+1)] # 初始化动态规划表 # 计算巡检任务量 patrol_tasks = [sum(p) for p in patrol_points] # 0/1背包算法 for i in range(1, n+1): for j in range(1, m+1): if patrol_tasks[i-1] <= patrol_staff[j-1]: dp[i][j] = max(dp[i-1][j], dp[i-1][j-1] + patrol_tasks[i-1]) else: dp[i][j] = dp[i-1][j] # 回溯得到最优巡检任务分配方案 schedule = [0] * n j = m for i in range(n, 0, -1): if dp[i][j] > dp[i-1][j]: schedule[i-1] = 1 j -= 1 # 返回最优巡检任务分配方案 return schedule def shortest_path(graph, start): """ Dijkstra's algorithm for shortest path. """ heap = [(0, start)] visited = set() dist = {start: 0} while heap: (d, u) = heapq.heappop(heap) if u in visited: continue visited.add(u) for v, w in graph[u].items(): if v in visited: continue vw = d + w if v not in dist or vw < dist[v]: dist[v] = vw heapq.heappush(heap, (vw, v)) return dist def get_schedule(patrol_points, patrol_staff): n = len(patrol_points) m = len(patrol_staff) schedule_list = [] for i in range(n): priority = sum(patrol_points[i]) schedule_list.append((priority, i)) schedule_list.sort(reverse=True) schedules = [] for _, i in schedule_list: point = patrol_points[i] staff = schedule(patrol_points, patrol_staff) if staff[i] == 0: for j in range(m): if patrol_staff[j] >= sum(point) and staff[j] == 0: staff[j] = 1 break schedules.append((i, staff)) patrol_staff = [patrol_staff[j] - point[j] if staff[j] else patrol_staff[j] for j in range(m)] return schedules def solve(patrol_points, patrol_staff, graph): schedules = get_schedule(patrol_points, patrol_staff) paths = [] for i, staff in schedules: dist = shortest_path(graph, i) path = [i] while len(path) < len(patrol_points): next_point = min((p for p in patrol_points if p not in path), key=lambda p: dist[p]) path.append(next_point) paths.append(path) return paths # 测试数据 patrol_points = [ [1, 0, 1, 0, 0], [0, 1, 0, 1, 0], [1, 0, 1, 0, 1], [0, 1, 0, 1, 0], [0, 0, 1, 0, 1], [0, 0, 0, 1, 0] ] patrol_staff = [8, 8, 8, 8, 8] graph = { 0: {1: 2, 2: 1, 4: 3}, 1: {0: 2, 2: 3, 3: 4}, 2: {0: 1, 1: 3, 3: 1, 4: 2}, 3: {1: 4, 2: 1, 5: 5}, 4: {0: 3, 2: 2, 5: 3}, 5: {3: 5, 4: 3} } paths = solve(patrol_points, patrol_staff, graph) print(paths) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值