c--歌曲压缩 C - Songs Compression

这题只要了解sort这个函数就ok;

其余的就无了

Ivan has nn songs on his phone. The size of the ii-th song is a_iai​ bytes. Ivan also has a flash drive which can hold at most mm bytes in total. Initially, his flash drive is empty.

Ivan wants to copy all nn songs to the flash drive. He can compress the songs. If he compresses the ii-th song, the size of the ii-th song reduces from a_iai​ to b_ibi​ bytes (b_i < a_ibi​<ai​).

Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most mm. He can compress any subset of the songs (not necessarily contiguous).

Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to mm).

If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress.

Input

The first line of the input contains two integers nn and mm (1 \le n \le 10^5, 1 \le m \le 10^91≤n≤105,1≤m≤109) — the number of the songs on Ivan's phone and the capacity of Ivan's flash drive.

The next nn lines contain two integers each: the ii-th line contains two integers a_iai​ and b_ibi​ (1 \le a_i, b_i \le 10^91≤ai​,bi​≤109, a_i > b_iai​>bi​) — the initial size of the ii-th song and the size of the ii-th song after compression.

Output

If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress.

Sample 1

InputcopyOutputcopy
4 21
10 8
7 4
3 1
5 4
2

Sample 2

InputcopyOutputcopy
4 16
10 8
7 4
3 1
5 4
-1

Note

In the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to 8 + 7 + 1 + 5 = 21 \le 218+7+1+5=21≤21. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal 8 + 4 + 3 + 5 = 20 \le 218+4+3+5=20≤21. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to 10 + 4 + 3 + 5 = 22 > 2110+4+3+5=22>21).

In the second example even if Ivan compresses all the songs the sum of sizes will be equal 8 + 4 + 1 + 4 = 17 > 168+4+1+4=17>16.

#include<cstdio>
#include<algorithm>
using namespace std;
long long n,m,ans=0,cnt=0,x,y,flag=0,a[10000]; 
int main()
{
	scanf("%lld%lld",&n,&m);
	  for(int i=1;i<=n;i++)
	  {
	  	scanf("%lld &lld",&x,&y);
	  	a[i]=x-y; ans+=x;cnt+=y;
	  }
	  if(cnt>m)
	  {
	  	printf("-1\n");
	  	return 0;
	  }
	  sort(a+1,a+1+n);
	  for(int i=n;i>=1;i--)
	  {
	  	if(ans<=m) break;
	  	else
	  	{
	  		ans=ans-a[i];
	  		flag++;
		  }
	  }
	  printf("%d\n",flag);
	  return 0;
}

<think>嗯,用户遇到了FileNotFoundError,说找不到'songs.csv'文件。首先,我得想这个错误的常见原因。通常,这种情况是因为文件路径不正确或者文件确实不存在。我需要一步步引导用户检查这些可能性。 首先,应该确认文件是否真的存在于指定的路径中。用户可能把文件放在项目目录之外,或者文件名拼写错误,比如大小写不一致或者后缀名不对。比如,用户可能写成了'Songs.csv'或者'songs.CSV',而实际文件是'songs.csv'。这时候需要提醒用户仔细检查文件名和扩展名。 接下来,文件路径的问题。如果用户直接写'pandas.read_csv('songs.csv')',这默认是在当前工作目录下查找。但有时候当前目录可能不是用户所想的那个。这时候应该建议用户打印当前工作目录,比如用os.getcwd(),然后确认文件是否在那个位置。如果不在,可能需要使用绝对路径或者调整相对路径。 比如,如果文件在子目录data里,应该写成'data/songs.csv'。或者,如果用户的项目结构比较复杂,可能需要使用更高级的路径处理,比如Pathlib模块,这样能更直观地处理不同操作系统的路径问题。 另外,用户可能没有解压文件,或者文件是压缩的。比如,示例中的引用提到读取的是' movie_data.csv.zip',但用户的问题里没有提到压缩文件。不过,如果他们的文件是压缩过的,可能需要用适当的参数,比如compression='zip'。不过这里用户的问题里是'songs.csv',可能不需要,但如果有类似情况可能需要考虑。 还有可能权限问题,但这种情况在个人电脑上较少见,更多是在服务器环境。不过可以提一下检查文件是否有读取权限。 然后,代码示例部分,我需要给出正确的读取方式,包括如何处理相对路径和绝对路径,以及使用Pathlib的例子。同时,要提醒用户处理可能的大小写问题,尤其是在区分大小写的系统上,比如Linux。 另外,用户提到的引用[1]和引用[2]中的例子,他们用pandas.read_csv时指定了index_col和parse_dates,但用户的问题可能不需要这些参数,除非他们需要处理时间序列数据。不过当前的问题主要是文件找不到,所以重点应放在路径和文件存在性上。 总结一下,解决方案的步骤应该是: 1. 检查文件名和扩展名是否正确。 2. 确认文件路径是否正确,当前工作目录是否对。 3. 使用绝对路径或更可靠的相对路径方法。 4. 使用Pathlib处理路径。 5. 检查文件是否被其他程序占用,或者权限问题。 然后,相关问题部分,可以包括如何处理其他读取错误,比如编码问题,解析日期列,或者读取Excel文件等。这样用户如果有后续问题,可以继续参考。</think>### 解决`pandas.read_csv`的`FileNotFoundError` #### 1. **检查文件路径与名称** - **文件名正确性**:确保文件名(包括大小写)和扩展名与代码中的完全一致。例如,`songs.csv` ≠ `Songs.CSV`[^1]。 - **文件路径验证**:如果未指定完整路径,文件需位于当前工作目录。可通过以下代码验证当前路径: ```python import os print(os.getcwd()) # 输出当前工作目录 ``` #### 2. **使用绝对路径** 若文件不在当前目录,需提供完整路径: ```python import pandas as pd # Windows示例 df = pd.read_csv(r'C:\Users\YourName\Documents\songs.csv') # macOS/Linux示例 df = pd.read_csv('/home/yourname/documents/songs.csv') ``` #### 3. **处理相对路径** 若文件位于子目录(如`data/`),需调整路径: ```python df = pd.read_csv('data/songs.csv') # 假设文件在项目下的data子目录 ``` #### 4. **使用`pathlib`简化路径操作**(推荐) ```python from pathlib import Path file_path = Path(__file__).parent / 'songs.csv' # 自动处理操作系统差异 df = pd.read_csv(file_path) ``` #### 5. **其他可能原因** - 文件被其他程序占用(如Excel打开时无法读取) - 文件权限问题(确保有读取权限) - 文件未解压(若文件是压缩包需先解压) --- ### 示例代码 ```python import pandas as pd from pathlib import Path try: file_path = Path(__file__).parent / 'songs.csv' df = pd.read_csv(file_path) print("文件读取成功!") except FileNotFoundError: print("错误:文件未找到,请检查路径或文件名") ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不是你的奥奥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值