Educational Codeforces Round 52 (Rated for Div. 2)【A,B,C】【C题:贪心+约束差分模板】
水题。仔细看题
1 #include<bits/stdc++.h> 2 3 using namespace std; 4 #define int long long 5 signed main(){ 6 int _; 7 cin>>_; 8 while(_--){ 9 int s,a,b,c; 10 cin>>s>>a>>b>>c; 11 int ans=0; 12 ans=(s/(a*c))*a+(s/(a*c))*b+(s%(a*c))/c; 13 cout<<ans<<'\n'; 14 } 15 16 return 0; 17 }
思路:找规律
1 #include<bits/stdc++.h> 2 3 using namespace std; 4 #define int long long 5 signed main(){ 6 7 int n,m; 8 cin>>n>>m; 9 10 int ans1=n-min(m*2,n); 11 int ans2=0; 12 for(int i=0;;i++){ 13 int cnt=(i-1)*(i)/2; 14 if(cnt>=m){ 15 ans2=i;break; 16 } 17 } 18 cout<<ans1<<" "<<n-ans2; 19 return 0; 20 }
思路:约束差分进行O(n)的处理。然后贪心。
1 #include<bits/stdc++.h> 2 3 using namespace std; 4 #define int long long 5 #define N 666666 6 int H[N];int vis[N];//vis数组的定义:高度为I的有VIS[I]个 7 signed main(){ 8 int n,k; 9 cin>>n>>k; 10 int maxn=0; 11 for(int i=1;i<=n;i++){ 12 scanf("%lld",&H[i]); 13 maxn=max(maxn,H[i]); 14 vis[0]++; 15 vis[H[i]+1]--; 16 } 17 for(int i=1;i<=N;i++){ 18 vis[i]+=vis[i-1]; 19 } 20 int ans=0;int sum=0; 21 /* for(int i=1;i<=maxn;i++){// 22 cout<<vis[i]<<" "; 23 } 24 cout<<'\n'; 25 */ 26 for(int i=maxn;i>=1;i--){ 27 if(vis[i]>=n){ 28 if(sum) 29 ans++; 30 break; 31 } 32 if(sum+vis[i]<=k){ 33 sum+=vis[i]; 34 }else{ 35 ans++; 36 sum=vis[i]; 37 } 38 } 39 cout<<ans; 40 return 0; 41 }