【CF1015C】Songs Compression(贪心)
题意:
给定n和m,n组(a[i],b[i]),每一组a[i]可以压缩为b[i],求最少只需要压缩几个,使得m可以存下所有数据,无解输出-1
思路:按差贪心,排序
1 #include<cstdio> 2 #include<cstring> 3 #include<string> 4 #include<cmath> 5 #include<iostream> 6 #include<algorithm> 7 #include<map> 8 #include<set> 9 #include<queue> 10 #include<vector> 11 using namespace std; 12 typedef long long ll; 13 typedef unsigned int uint; 14 typedef unsigned long long ull; 15 typedef pair<int,int> PII; 16 typedef vector<int> VI; 17 #define fi first 18 #define se second 19 #define MP make_pair 20 21 const int N=110000; 22 23 PII a[N]; 24 int n,m; 25 26 27 int read() 28 { 29 int v=0,f=1; 30 char c=getchar(); 31 while(c<48||57<c) {if(c=='-') f=-1; c=getchar();} 32 while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar(); 33 return v*f; 34 } 35 36 bool cmp(const PII &a,const PII &b) 37 { 38 return a.fi-a.se>b.fi-b.se; 39 } 40 41 int main() 42 { 43 //freopen("1.in","r",stdin); 44 //freopen("1.out","w",stdout); 45 scanf("%d%d",&n,&m); 46 ll s=0; 47 for(int i=1;i<=n;i++) 48 { 49 int x,y; 50 scanf("%d%d",&x,&y); 51 a[i]=MP(x,y); 52 s+=x; 53 } 54 sort(a+1,a+n+1,cmp); 55 int ans=0; 56 for(int i=1;i<=n;i++) 57 { 58 if(s>m) {s-=a[i].fi-a[i].se; ans++;} 59 else break; 60 } 61 if(s>m) printf("-1"); 62 else printf("%d",ans); 63 return 0; 64 }
null