排队买票 2种方法

排队买票

Time Limit:1000MS  Memory Limit:65536K
Total Submit:240 Accepted:81

Description

一个演唱会即将举行,现有 N 个歌迷排队买票,一个人一张,而售票处规定,一个人每次最多买两张,假设买一张票的时间为 Ti ( 1<=i<=N ) , 队伍中相邻的歌迷(第 j 个人和第 j+1 个人)也可以由其中的一个人买两张票的时间为 Ri, 假如 Rj < Ti+Tj+1, 这样做可以缩短后面的歌迷等待的时间,现给出 N , Ti,Rj, 求每个人买到票的最短时间和方法。

Input

Output

Sample Input

4
1 2 3 4
4 4 5

Sample Output

8

Source

elba

公式:

    顺推:

   。。。。。。

   a[i]+a[i+1]>b[i]

    then f[i]:=f[i]+b[i-1];

    else f[i]:=f[i]+a[i]; 

  • var
      i,j,k,n,m:longint;
      a,f,b:array[-100..10000]of longint;
    begin
      readln(n);
      for i:=1 to n do
        begin
          read(a[i]);
        end;
      for i:=1 to n-1 do
      begin
        read(b[i]);
         end;
      k:=0;
      f[1]:=a[1];
      for i:=2 to n do
        if f[i-1]+a[i]>f[i-2]+b[i-1] then f[i]:=f[i-2]+b[i-1]
                                     else f[i]:=f[i-1]+a[i];
      k:=0;
      writeln(f[n]);
    end.
    
    
.....

var

  i,j,k,n,m:longint;
  a,f,b:array[-10..20000]of longint;
begin
  readln(n);
  for i:=1 to n do
    begin
      read(a[i]);
    end;
  for i:=1 to n-1 do
  begin
    read(b[i]);
    if i=1 then b[i-1]:=b[i];
  end;
  k:=0;
  for i:=n downto 1 do
    begin
    if k<>1 then
      if ((a[i-2]+a[i-1]>=b[i-2]) and (b[i-1]+a[i-2]<b[i-2]+a[i]) and (a[i]+a[i-1]>=b[i-1]))
      then begin
             f[i]:=f[i]+b[i-1];
             k:=1;


          end else
        begin
          k:=0;
          f[i]:=f[i]+a[i];
        end
      else k:=0;
    end;
  k:=0;
  for i:=1 to n do
    k:=k+f[i];
  writeln(k);
end.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
排队问题可以用 C 语言来实现。一般来说,可以使用队列数据结构来解决这个问题。具体实现步骤如下: 1. 定义一个队列数据结构,包括队列头指针、队列尾指针和队列元素个数等成员变量。 2. 定义一个函数来模拟售员的行为,比如 sell_ticket() 函数。每当有人来时,就将其加入队列中。 3. 定义一个函数来模拟者的行为,比如 buy_ticket() 函数。每当有人来时,就从队列中取出一个人,并将其从队列中删除。 4. 在主函数中,可以模拟一定时间内的售过程。具体来说,可以使用循环来模拟时间的流逝,每隔一定时间调用 sell_ticket() 或 buy_ticket() 函数。 下面是一个简单的排队问题的实现示例: ```c #include <stdio.h> #include <stdlib.h> #define MAX_QUEUE_SIZE 100 typedef struct { int* data; int front; int rear; int size; } Queue; void init_queue(Queue* q) { q->data = (int*)malloc(sizeof(int) * MAX_QUEUE_SIZE); q->front = 0; q->rear = -1; q->size = 0; } int is_full(Queue* q) { return q->size == MAX_QUEUE_SIZE; } int is_empty(Queue* q) { return q->size == 0; } void enqueue(Queue* q, int x) { if (is_full(q)) { printf("Queue is full.\n"); return; } q->rear = (q->rear + 1) % MAX_QUEUE_SIZE; q->data[q->rear] = x; q->size++; } int dequeue(Queue* q) { if (is_empty(q)) { printf("Queue is empty.\n"); return -1; } int x = q->data[q->front]; q->front = (q->front + 1) % MAX_QUEUE_SIZE; q->size--; return x; } void sell_ticket(Queue* q) { int x; printf("Enter the number of tickets to sell: "); scanf("%d", &x); for (int i = 0; i < x; i++) { printf("Enter the ticket buyer's ID: "); scanf("%d", &x); enqueue(q, x); } } void buy_ticket(Queue* q) { if (is_empty(q)) { printf("No ticket to buy.\n"); return; } int x = dequeue(q); printf("Ticket buyer's ID: %d\n", x); } int main() { Queue q; init_queue(&q); int t; printf("Enter the simulation time (in seconds): "); scanf("%d", &t); for (int i = 0; i < t; i++) { if (i % 5 == 0) { sell_ticket(&q); } if (i % 3 == 0) { buy_ticket(&q); } } return 0; } ``` 在这个示例程序中,我们定义了一个 Queue 结构体来表示队列,包括队列元素数组、队列头指针、队列尾指针和队列元素个数等成员变量。我们使用 init_queue() 函数来初始化队列,使用 is_full() 和 is_empty() 函数来判断队列是否已满或已空,使用 enqueue() 和 dequeue() 函数来向队列中添加元素或删除元素。 在主函数中,我们通过循环来模拟一定时间内的售过程。在每个五秒钟时刻,我们调用 sell_ticket() 函数来模拟售员的行为,将者的 ID 加入队列中。在每个三秒钟时刻,我们调用 buy_ticket() 函数来模拟者的行为,从队列中取出一个者的 ID。 这只是一个简单的示例程序,实际应用中可能还需要考虑更多的细节和复杂情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值