Greedy algorithm special training 1

Greedy special training
This week, I started to brush questions on Luogu, but recently Luogu can't register an account, so I have been using the same account with my roommate. I didn't brush many questions, but I also encountered some interesting questions. This week I mainly brush greedy questions and review the sort function by the way.
1 Magic
Ac code

#include <iostream>
#include<algorithm>
using namespace std;
const long long int maxn=100000+1;
struct node{
int pre;
int date;
};
bool cmp(node a,node b){
return a.pre+a.date>b.pre+b.date;}
int main()
{
int t;
cin>>t;
while(t--){
  int n,m;
  cin>>n>>m;
  node a[maxn];
for(int i=0;i<n;i++)
cin>>a[i].pre>>a[i].date;
sort(a,a+n,cmp);
int flag=-1;
for(int i=0;i<n;i++)
if(a[i].pre<m){m+=a[i].date;if(m<=0){flag=1;break;}}
else {flag=1;break;}
if(flag==1){
    cout<<"-1s"<<endl;
}else {
cout<<"+1s"<<endl;}
}
}

Title:
cjwssb apologized to you after knowing it was a misunderstanding. You're going to start magic with him in order to make him laugh. But you don'T have much time, but what's worse is that you still need to complete n magic tasks. Assuming that your current time is T, each task needs to have a certain limit. Ti means that you can complete this task only when your T is strictly greater than ti. It doesn'T take time to complete the task. When you complete the i-th task, your time T will be added with bi. At this time, it is necessary to ensure that T is greater than 0 at any time. Can you complete these n magic tasks? If you can, output + 1s; if not, output - 1s.
The essence of this problem is
bool cmp(node a,node b){
return a.pre+a.date>b.pre+b.date;}
This ranking is critical. When you see the question, it is obvious that you can think of calculating bi greater than 0 first, which can ensure enough time. One idea is to use two structures, one with bi greater than 0 and the other with bi less than 0. It is classified and discussed, but this method is more troublesome and not as concise as this method.
The specific reasoning process is:
Time t may be greater than t1, but adding b1 causes t to be less than t2, while t plus b2 is greater than t1, so the formula satisfying the equation is
1 T+b1<t2
2 T+b2>t1
3 t1-b2<t2-b1
Both T1 + B1 < T2 + B2
Therefore, the priority of t2 plus b2 should be calculated, and the special solution is found to be valid.
2 PLES COCI2011-2012#1

#include <iostream>
#include<algorithm>
using namespace std;
const long long int maxn=100000+5;
int a[maxn];
int a1[maxn];
int a2[maxn];
int a3[maxn];
int main()
{
int n;
int s=0;
int s1=0;
int s2=0;
int s3=0;
cin>>n;
for(int i=0;i<n;i++){//a to a3 a1 to a2
int k;
cin>>k;
if(k>0){a[s]=k;s++;}
else {a2[s2]=-k;s2++;}
}
for(int i=0;i<n;i++){
int k;
cin>>k;
if(k>0){a1[s1]=k;s1++;}//female
else {a3[s3]=-k;s3++;}
}
int count=0;
sort(a,a+s);
sort(a1,a1+s1);
sort(a2,a2+s2);
sort(a3,a3+s3);
for(int i=0,j=0;i<s&&j<s3;j++)
if(a[i]<a3[j]){i++;count++;}

for(int i=0,j=0;i<s1&&j<s2;j++)
if(a1[i]<a2[j]){i++;count++;}

cout<<count<<endl;

}

Meaning:
Ask what is the largest number of partners if you follow everyone's wishes.
This problem is obviously greedy, but I always have a lot of data when doing this problem. The specific idea is to use four arrays to store the numbers of boys greater than 0 and less than 0 and girls greater than 0 and less than 0.
But one thing to note is the order of i and j.
For example, the first for loop means that boys must find higher than themselves. The two arrays are sorted from small to large. Whether it is true or not, girls have to choose a higher one. The reason is that if it is not true, boys can't choose a higher one.
Summary: this week mainly focuses on the popularization / improvement of greedy topics, and the goal next week is also mainly to brush greedy topics and search topics.

Tags: Algorithm

Posted on Sat, 02 Oct 2021 15:06:22 -0400 by kendallkamikaze