CF - 779C. Dishonest Sellers 排序+贪心

本文介绍了一个购物问题的解决方案,主人公Igor计划购买n件商品,并希望利用折扣期节省开支。通过合理的算法设计,文章详细阐述了如何确定购买顺序以实现最低总花费的方法。

1.题目描述:

C. Dishonest Sellers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Igor found out discounts in a shop and decided to buy n items. Discounts at the store will last for a week and Igor knows about each item that its price now is ai, and after a week of discounts its price will be bi.

Not all of sellers are honest, so now some products could be more expensive than after a week of discounts.

Igor decided that buy at least k of items now, but wait with the rest of the week in order to save money as much as possible. Your task is to determine the minimum money that Igor can spend to buy all n items.

Input

In the first line there are two positive integer numbers n and k (1 ≤ n ≤ 2·1050 ≤ k ≤ n) — total number of items to buy and minimal number of items Igor wants to by right now.

The second line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 104) — prices of items during discounts (i.e. right now).

The third line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104) — prices of items after discounts (i.e. after a week).

Output

Print the minimal amount of money Igor will spend to buy all n items. Remember, he should buy at least k items right now.

Examples
input
3 1
5 4 6
3 1 5
output
10
input
5 3
3 4 7 10 3
4 5 5 12 5
output
25
Note

In the first example Igor should buy item 3 paying 6. But items 1 and 2 he should buy after a week. He will pay 3 and 1 for them. So in total he will pay 6 + 3 + 1 = 10.

In the second example Igor should buy right now items 1, 2, 4 and 5, paying for them 3, 4, 10 and 3, respectively. Item 3 he should buy after a week of discounts, he will pay 5 for it. In total he will spend 3 + 4 + 10 + 3 + 5 = 25.


2.题意概述:

一个人打算这两周去商店总共买n件衣服,今日至少要买k件,剩下的可以下周买,正逢打折活动,持续一周,但是老板很黑,有些衣服本周打折的价格反而比下周不打折的价格还高,要你帮他算一下,他买这n件衣服的最少花费。

3.解题思路:

今日至少要买k件,那么肯定先买“真”打折的衣服,如果“真”打折衣服买完还不够k件,则贪心地买差价最小的“假”打折衣服。

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 200100
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e 2.718281828459
#define mod (int)1e9 + 7
using namespace std;
typedef long long ll;
struct node
{
	int a, b, vis;
}p[maxn];
bool cmpa(node x, node y)
{
	if (x.a == y.a)
		return x.b < y.b;
	return x.a < y.a;
}
bool cmp(node x, node y)
{
	return x.a - x.b < y.a - y.b;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	long _begin_time = clock();
#endif
	int n, k;
	while (~scanf("%d%d", &n, &k))
	{
		for (int i = 0; i < n; i++)
			scanf("%d", &p[i].a);
		for (int i = 0; i < n; i++)
		{
			scanf("%d", &p[i].b);
			p[i].vis = 0;
		}
		sort(p, p + n, cmpa);
		int cnt = 0, ans = 0;
		for (int i = 0; i < n; i++)	//先买诚实的产品
		{
			if (p[i].a < p[i].b)
			{
				ans += p[i].a;
				cnt++;
				p[i].vis = 1;
			}
		}
		sort(p, p + n, cmp);
		if (cnt < k)	//只能买说谎的产品了
		{
			for (int i = 0; i < n; i++)
			{
				if (!p[i].vis)
				{
					if (cnt < k)
					{
						ans += p[i].a;
						p[i].vis = 1;
						cnt++;
					}
					else
						break;
				}
			}
		}
		// 买剩下的产品
		for (int i = 0; i < n; i++)
			if (!p[i].vis)
				ans += p[i].b;
		printf("%d\n", ans);
	}

#ifndef ONLINE_JUDGE
	long _end_time = clock();
	printf("time = %ld ms.", _end_time - _begin_time);
#endif
	return 0;
}
如何实现Vue锚点组件的tab栏切换逻辑?以下为标签template代码<template> <div class="charts-container"> <!-- 统计分析悬浮栏 --> <div class="stat-floating-bar"> <!-- 调试信息显示 --> <div style="font-size: 10px; color: #999; margin-bottom: 10px; text-align: center; padding: 5px; background: rgba(0,0,0,0.05); border-radius: 4px;"> <div>当前状态: {{ statType }}</div> <div>滚动监听: ✅</div> <div style="margin-top: 5px;"> <el-button size="mini" type="info" @click="testScrollListener">测试监听</el-button> </div> </div> <div class="stat-tab" :class="{ active: statType === 'personnel' }" @click="scrollToSection('personnel')"> <el-icon><User /></el-icon> <span>人员统计</span> </div> <div class="stat-tab" :class="{ active: statType === 'violation' }" @click="scrollToSection('violation')"> <el-icon><Warning /></el-icon> <span>违规人员统计</span> </div> <div class="stat-tab" :class="{ active: statType === 'dishonest' }" @click="scrollToSection('dishonest')"> <el-icon><CircleClose /></el-icon> <span>失信人员统计</span> </div> </div> <!-- 人员统计区域 --> <div id="personnel-section" class="stat-section"> <div class="header"> <div class="header-title">人员统计</div> <div class="refresh-controls"> <el-button type="primary" size="small" @click="forceReinitCharts('personnel')" :loading="isRefreshing.personnel" > <el-icon><Refresh /></el-icon> 刷新图表 </el-button> </div> </div> <div class="chart-grid"> <!-- 人员统计的4个图表 --> <div class="chart-item"> <div ref="personnelPersonnelTypeChart" class="chart"></div> </div> <div class="chart-item"> <div ref="personnelPositionLevelChart" class="chart"></div> </div> <div class="chart-item"> <div ref="personnelRegionChart" class="chart"></div> </div> <div class="chart-item"> <div ref="personnelGenderChart" class="chart"></div> </div> </div> </div> <!-- 违规人员统计区域 --> <div id="violation-section" class="stat-section"> <div class="header"> <div class="header-title">违规人员统计</div> <div class="refresh-controls"> <el-button type="primary" size="small" @click="forceReinitCharts('violation')" :loading="isRefreshing.violation" > <el-icon><Refresh /></el-icon> 刷新图表 </el-button> </div> </div> <div class="chart-grid"> <!-- 违规人员统计的4个图表 --> <div class="chart-item"> <div ref="violationPersonnelTypeChart" class="chart"></div> </div> <div class="chart-item"> <div ref="violationPositionLevelChart" class="chart"></div> </div> <div class="chart-item"> <div ref="violationRegionChart" class="chart"></div> </div> <div class="chart-item"> <div ref="violationGenderChart" class="chart"></div> </div> </div> </div> <!-- 失信人员统计区域 --> <div id="dishonest-section" class="stat-section"> <div class="header"> <div class="header-title">失信人员统计</div> <div class="refresh-controls"> <el-button type="primary" size="small" @click="forceReinitCharts('dishonest')" :loading="isRefreshing.dishonest" > <el-icon><Refresh /></el-icon> 刷新图表 </el-button> </div> </div> <div class="chart-grid"> <!-- 失信人员统计的4个图表 --> <div class="chart-item"> <div ref="dishonestPersonnelTypeChart" class="chart"></div> </div> <div class="chart-item"> <div ref="dishonestPositionLevelChart" class="chart"></div> </div> <div class="chart-item"> <div ref="dishonestRegionChart" class="chart"></div> </div> <div class="chart-item"> <div ref="dishonestGenderChart" class="chart"></div> </div> </div> </div> </div> </template>
最新发布
08-27
在 Vue 中实现锚点组件的 tab 切换功能并同步滚动位置,可以通过以下几个方面来完成: ### 1. 组件结构设计 在父组件中,需要包含两个主要部分: - 一个用于切换 tab 的组件,例如 `TabControl`,它通过自定义事件(如 `@tab-click`)将点击事件传递给父组件。 - 一个展示内容的组件,例如 `DetailPage`,它需要提供一个方法(如 `scrollToSection(index)`)用于滚动到指定的锚点位置[^1]。 ```vue <template> <div> <tab-control @tab-click="handleTabClick" /> <detail-page ref="detailPage" /> </div> </template> <script> export default { data() { return { detailPage: null }; }, mounted() { this.detailPage = this.$refs.detailPage; }, methods: { handleTabClick(index) { this.detailPage.scrollToSection(index); } } }; </script> ``` ### 2. 内容区域的滚动逻辑 在 `DetailPage` 组件中,需要实现一个 `scrollToSection(index)` 方法,用于根据传入的索引滚动到对应的内容区域。可以使用 `scrollIntoView` 方法实现平滑滚动[^3]。 ```vue <template> <div> <section ref="sectionA" id="sectionA">内容A</section> <section ref="sectionB" id="sectionB">内容B</section> <section ref="sectionC" id="sectionC">内容C</section> </div> </template> <script> export default { methods: { scrollToSection(index) { const sections = [this.$refs.sectionA, this.$refs.sectionB, this.$refs.sectionC]; if (sections[index]) { sections[index].scrollIntoView({ behavior: 'smooth' }); } } } }; </script> ``` ### 3. 滚动时同步 tab 状态 为了实现滚动时同步 tab 的选中状态,可以在 `DetailPage` 组件中监听滚动事件,并根据当前滚动的位置判断用户正在查看的内容区域,然后通过事件或直接调用父组件的方法更新 tab 的状态[^2]。 ```vue <template> <div @scroll="handleScroll"> <section ref="sectionA" id="sectionA">内容A</section> <section ref="sectionB" id="sectionB">内容B</section> <section ref="sectionC" id="sectionC">内容C</section> </div> </template> <script> export default { props: ['onScroll'], methods: { handleScroll(e) { const scrollTop = e.target.scrollTop; const sections = [this.$refs.sectionA, this.$refs.sectionB, this.$refs.sectionC]; let currentIndex = 0; for (let i = 0; i < sections.length; i++) { const rect = sections[i].getBoundingClientRect(); if (rect.top >= 0) { currentIndex = i; break; } } if (this.onScroll) { this.onScroll(currentIndex); } } } }; </script> ``` 在父组件中,需要接收 `onScroll` 回调并更新 tab 的选中状态: ```vue <template> <div> <tab-control :current-index="currentIndex" /> <detail-page ref="detailPage" :on-scroll="handleScroll" /> </div> </template> <script> export default { data() { return { currentIndex: 0 }; }, methods: { handleScroll(index) { this.currentIndex = index; } } }; </script> ``` ### 4. 优化与注意事项 - **性能优化**:由于滚动事件会频繁触发,建议使用防抖(debounce)或节流(throttle)来减少不必要的计算。 - **兼容性**:确保在不同浏览器中 `scrollIntoView` 和 `getBoundingClientRect` 的兼容性[^3]。 - **可维护性**:将 tab 切换和滚动逻辑封装成独立的组件,便于复用和维护。 通过上述方法,可以在 Vue 中实现一个功能完善且体验良好的锚点组件 tab 切换功能,并实现 tab 与滚动位置的同步。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值