AtCoder Context 141D-Powerful Discount Tickets【优先队列】 难度:**

题意:

Takahashi is going to buy N items one by one.

The price of the i-th item he buys is Ai yen (the currency of Japan).

He has M discount tickets, and he can use any number of them when buying an item.

If Y tickets are used when buying an item priced X yen, he can get the item for X 2Y (rounded down to the nearest integer) yen.

What is the minimum amount of money required to buy all the items?

Constraints
All values in input are integers.
1≤N,M≤105
1≤Ai≤109
Input
Input is given from Standard Input in the following format:

N M
A1 A2 … AN
Output
Print the minimum amount of money required to buy all the items.

Sample Input 1
Copy
3 3
2 13 8
Sample Output 1
Copy
9
We can buy all the items for 9 yen, as follows:

Buy the 1-st item for 2 yen without tickets.
Buy the 2-nd item for 3 yen with 2 tickets.
Buy the 3-rd item for 4 yen with 1 ticket.
Sample Input 2
Copy
4 4
1 9 3 5
Sample Output 2
Copy
6
Sample Input 3
Copy
1 100000
1000000000
Sample Output 3
Copy
0
We can buy the item priced 1000000000 yen for 0 yen with 100000 tickets.

Sample Input 4
Copy
10 1
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Sample Output 4
Copy
9500000000

题解:

贪心即可,每次都对价格最高的票使用一次优惠券,因此可以把价格放入优先队列,每次取队首进行处理,直到优惠券全部用完为止。

代码:

#include<iostream>
#include<queue>
using namespace std;
long long a[100005];
int main()
{
	long long n,m,k,price,sum=0;
	priority_queue<long long>que;
	scanf("%lld%lld",&n,&m);
	for(int i=0;i<n;i++)scanf("%lld",&a[i]);
	for(int i=0;i<n;i++)que.push(a[i]);
	while(m>0)
	{
		price=que.top();
		price/=2;
		m--;
		que.pop();
		que.push(price);
	}
	while(!que.empty())
	{
	   sum+=que.top();
	   que.pop();
	}
	printf("%lld\n",sum);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的火车订票系统的设计方案。首先,我们需要安装Python的GUI库tkinter和SQLite3数据库。 在Python中使用SQLite3数据库,我们需要先创建一个数据库文件,并在其中创建表格来存储数据。我们可以创建一个名为“train_tickets.db”的数据库文件,并在其中创建三个表格:车次信息表、售票记录表和旅客流量统计表。 车次信息表包含车次编号、出发站、到达站、出发时间、到达时间、票价等字段。 售票记录表包含订单编号、车次编号、出发站、到达站、出发时间、到达时间、座位号、票价等字段。 旅客流量统计表包含站点名称、日期、进站人数、出站人数、在站人数等字段。 接下来,我们可以设计一个简单的GUI界面,包含一个菜单,菜单中的选项为查询车次信息、售票记录和票据信息、各站旅客流量统计和退出系统。当用户选择某个选项后,程序将从数据库中查询相关数据并在界面上显示。 下面是一个简单的代码示例,仅供参考: ```python import tkinter as tk import sqlite3 # 创建GUI界面 root = tk.Tk() root.title("火车订票系统") # 创建数据库连接 conn = sqlite3.connect('train_tickets.db') c = conn.cursor() # 创建车次信息表 c.execute('''CREATE TABLE IF NOT EXISTS train_info (train_no TEXT PRIMARY KEY, depart_station TEXT, arrive_station TEXT, depart_time TEXT, arrive_time TEXT, price INTEGER)''') # 创建售票记录表 c.execute('''CREATE TABLE IF NOT EXISTS ticket_info (order_no TEXT PRIMARY KEY, train_no TEXT, depart_station TEXT, arrive_station TEXT, depart_time TEXT, arrive_time TEXT, seat_no TEXT, price INTEGER)''') # 创建旅客流量统计表 c.execute('''CREATE TABLE IF NOT EXISTS passenger_flow (station_name TEXT, date TEXT, in_num INTEGER, out_num INTEGER, on_num INTEGER)''') # 查询车次信息 def query_train_info(): # 获取用户输入 depart_station = depart_station_entry.get() arrive_station = arrive_station_entry.get() depart_time = depart_time_entry.get() # 查询车次信息 c.execute('SELECT * FROM train_info WHERE depart_station=? AND arrive_station=? AND depart_time=?', (depart_station, arrive_station, depart_time)) train_info = c.fetchall() # 在界面上显示查询结果 result_text.delete('1.0', tk.END) for info in train_info: result_text.insert(tk.END, f'车次编号:{info[0]}\n') result_text.insert(tk.END, f'出发站:{info[1]}\n') result_text.insert(tk.END, f'到达站:{info[2]}\n') result_text.insert(tk.END, f'出发时间:{info[3]}\n') result_text.insert(tk.END, f'到达时间:{info[4]}\n') result_text.insert(tk.END, f'票价:{info[5]}\n\n') # 查询售票记录和票据信息 def query_ticket_info(): # 获取用户输入 order_no = order_no_entry.get() # 查询售票记录和票据信息 c.execute('SELECT * FROM ticket_info WHERE order_no=?', (order_no,)) ticket_info = c.fetchall() # 在界面上显示查询结果 result_text.delete('1.0', tk.END) for info in ticket_info: result_text.insert(tk.END, f'订单编号:{info[0]}\n') result_text.insert(tk.END, f'车次编号:{info[1]}\n') result_text.insert(tk.END, f'出发站:{info[2]}\n') result_text.insert(tk.END, f'到达站:{info[3]}\n') result_text.insert(tk.END, f'出发时间:{info[4]}\n') result_text.insert(tk.END, f'到达时间:{info[5]}\n') result_text.insert(tk.END, f'座位号:{info[6]}\n') result_text.insert(tk.END, f'票价:{info[7]}\n\n') # 查询各站旅客流量统计 def query_passenger_flow(): # 获取用户输入 station_name = station_name_entry.get() date = date_entry.get() # 查询各站旅客流量统计 c.execute('SELECT * FROM passenger_flow WHERE station_name=? AND date=?', (station_name, date)) passenger_flow = c.fetchall() # 在界面上显示查询结果 result_text.delete('1.0', tk.END) for info in passenger_flow: result_text.insert(tk.END, f'站点名称:{info[0]}\n') result_text.insert(tk.END, f'日期:{info[1]}\n') result_text.insert(tk.END, f'进站人数:{info[2]}\n') result_text.insert(tk.END, f'出站人数:{info[3]}\n') result_text.insert(tk.END, f'在站人数:{info[4]}\n\n') # 创建菜单 menu_label = tk.Label(root, text="请选择您的进一步操作:") menu_label.grid(row=0, column=0, columnspan=2) query_train_info_button = tk.Button(root, text="查询车次信息", command=query_train_info) query_train_info_button.grid(row=1, column=0) depart_station_label = tk.Label(root, text="出发站:") depart_station_label.grid(row=2, column=0) depart_station_entry = tk.Entry(root) depart_station_entry.grid(row=2, column=1) arrive_station_label = tk.Label(root, text="到达站:") arrive_station_label.grid(row=3, column=0) arrive_station_entry = tk.Entry(root) arrive_station_entry.grid(row=3, column=1) depart_time_label = tk.Label(root, text="出发时间:") depart_time_label.grid(row=4, column=0) depart_time_entry = tk.Entry(root) depart_time_entry.grid(row=4, column=1) query_ticket_info_button = tk.Button(root, text="查询售票记录和票据信息", command=query_ticket_info) query_ticket_info_button.grid(row=5, column=0) order_no_label = tk.Label(root, text="订单编号:") order_no_label.grid(row=6, column=0) order_no_entry = tk.Entry(root) order_no_entry.grid(row=6, column=1) query_passenger_flow_button = tk.Button(root, text="各站旅客流量统计", command=query_passenger_flow) query_passenger_flow_button.grid(row=7, column=0) station_name_label = tk.Label(root, text="站点名称:") station_name_label.grid(row=8, column=0) station_name_entry = tk.Entry(root) station_name_entry.grid(row=8, column=1) date_label = tk.Label(root, text="日期:") date_label.grid(row=9, column=0) date_entry = tk.Entry(root) date_entry.grid(row=9, column=1) result_text = tk.Text(root) result_text.grid(row=10, column=0, columnspan=2) exit_button = tk.Button(root, text="退出系统", command=root.quit) exit_button.grid(row=11, column=0, columnspan=2) # 运行GUI界面 root.mainloop() # 关闭数据库连接 conn.close() ``` 这个程序使用了SQLite3数据库存储车次信息、售票记录和旅客流量统计信息,并提供了一个简单的GUI界面,让用户可以通过界面来查询相关信息。当用户点击某个查询按钮时,程序将从数据库中查询相关数据,并在界面上显示查询结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值