Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- input
- 5
- 15 3
- 10 1
- 5 2
- 1 2
- 20 2
- */
- #include<bits/stdc++.h>
- using namespace std;
- int d[100],p[100],j[100];
- int sequence[100];
- int js(int n)
- {
- for(int i =1;i<=n;i++)
- {
- for(int j =1;j<=n;j++)
- {
- if(p[j]<p[j+1])
- {
- swap(p[j],p[j+1]);
- swap(d[j],d[j+1]);
- swap(sequence[j],sequence[j+1]);
- }
- }
- }
- d[0] = j[0] = 0;
- j[1] = 1;
- int k = 1;
- for(int i = 2;i<=n;i++)
- {
- int r = k;
- while(d[i]<d[j[r]] && d[j[r]]!=r)
- {
- r = r-1;
- }
- if(d[i]>=d[j[r]] && d[i]>r)
- {
- for(int q = k;q>=r+1;q--)
- j[q+1] = j[q];
- j[r+1] = i;
- k = k+1;
- }
- }
- return k;
- }
- int main()
- {
- int original_profit_sequence[100],original_deadline_sequence[100];
- int n;
- freopen("in.txt","r",stdin);
- cin>>n;
- for(int i =1;i<=n;i++)
- cin>>p[i]>>d[i];
- cout<<endl;
- for(int i=1;i<=n;i++)
- {
- sequence[i] = i;
- original_profit_sequence[i] = p[i];
- original_deadline_sequence[i] =d[i];
- }
- cout<<"Before Sorting: "<<endl;
- for(int i=1;i<=n;i++)
- {
- cout<<" "<<p[i]<<" "<<d[i]<<endl;
- }
- int k = js(n);
- cout<<"After Sorting: "<<endl;
- for(int i =1;i<=n;i++)
- cout<<p[i]<<" "<<d[i]<<endl;
- cout<<endl;
- cout<<endl;
- cout<<"Added job of original sequence: "<<endl;
- for(int i =1;i<=k;i++)
- {
- cout<<sequence[j[i]]<<" "<<original_profit_sequence[sequence[j[i]]]<<" "<<original_deadline_sequence[sequence[j[i]]]<<endl;
- }
- cout<<endl<<"Sequence after sorting: ";
- for(int i =1;i<=k;i++)
- {
- cout<<j[i]<<" ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement