War(区间贪心)

版权声明:转载请注明出处:https://blog.csdn.net/qq1013459920 https://blog.csdn.net/qq1013459920/article/details/83034273

Description

      War is the reciprical and violent application of force between hostile political entities aimed at bringing about a desired political end-state via armed conflict. War is an interaction in which two or more militaries have a “struggle of wills”. When qualified as a civil war, it is a dispute inherent to a given society, and its nature is in the conflict over modes of governance rather than sovereignty. War is not considered to be the same as mere occupation,murder or genocide because of the reciprical nature of the violent struggle, and the organized nature of the units involved. 
      War is also a cultural entity, and its practice is not linked to any single type of political organisation or society. Rather, as discussed by John Keegan in his “History Of Warfare”, war is a universal phenomenon whose form and scope is defined by the society that wages it. 


      Today we don't want to talk about war, rather than , I want you to tell me which year there was no war.

Input

For each test case, first input one integer N(1<= N <= 100), then two integers p and q (-6000000 <= p <= q <= 6000000) represent the starting year and the ending year in this case. Followed by N wars. 
Each war described as follows: 
A i B i N i (A,B is integer, p <= A, B <= q, N i is a String which is the ith war's name ) 
Represent that the ith War took place between year A and year B. 

Output

Output one number represent which year there was no war in the range p and q, if there are not only one year, output the maximum year which there was no war. If all the year had war, just output "Badly!". 

Sample Input

3

100 200

100 120 RtWar

110 140 WeWar

141 199 QqWar

1

-600 600

-600 600 Cool War

Sample Output

200

Badly!

题意:一个区间(sx, ex),求被n个区间覆盖掉后的最大区间值

如   

100 200

100 120 RtWar

110 140 WeWar

141 199 QqWar

(100,200)被覆盖后区间最大值为200

可能这题的主要判断点在string那里war名字可能包含空格

所以很多有BUG的代码也能AC:

例如:(第一次写的有BUG的AC代码)

具体怎么出BUG的跑下这组数据就知道了:

3
100 120
105 109 aa
111 112 bb
110 120 cc
109

/*
BUG测试:
3
100 120
105 109 aa
111 112 bb
110 120 cc
109
*/
//有BUG的AC代码
#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<ctype.h>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<list>
#define mod 998244353
#define Max 0x3f3f3f3f
#define Min 0xc0c0c0c0
#define mst(a) memset(a,0,sizeof(a))
#define f(i,a,b) for(int i=a;i<b;i++)
using namespace std;
typedef long long ll;
const int maxn=105;
struct Node{
    int a, b;
}war[maxn];

bool cmp(Node x, Node y){
    if(x.b == y.b){
        return x.a < y.a;
    }
    return x.b < y.b;
}
char s[maxn];
int main(){
    ios::sync_with_stdio(false);
    int t, sx, ex;
    while(cin>>t){
        cin>>sx>>ex;
        for(int i = 0; i < t; i++){
            cin>>war[i].a>>war[i].b;
            cin.getline(s, maxn);
        }
        sort(war, war + t, cmp);
        int p1 = war[0].a, p2 = war[0].b;
        int ans = p1 - 1;
        for(int i = 1; i < t; i++){
            if(p2 + 1 >= war[i].b){
                p2 = war[i].b;
            }
            else {
                p1 = war[i].a;
                p2 = war[i].b;
                ans = p1 - 1;
            }
        }
        if(p2 < ex){
            cout<<ex<<endl;
            continue;
        }
        if(ans < sx){
            cout<<"Badly!"<<endl;
        }
        else {
            cout<<ans<<endl;
        }
    }
    return 0;
}

所以我们来讲最简单的思路,也是思路清晰,完全无BUG的AC Code:

#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<ctype.h>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<list>
#define mod 998244353
#define Max 0x3f3f3f3f
#define Min 0xc0c0c0c0
#define mst(a) memset(a,0,sizeof(a))
#define f(i,a,b) for(int i=a;i<b;i++)
using namespace std;
typedef long long ll;
const int maxn=105;
int sx, ex;

struct Node{
    int a, b;
}war[maxn];

bool cmp(Node x, Node y){
    if(x.b == y.b){
        return x.a > y.a;
    }
    return x.b > y.b;
}

int main(){
    ios::sync_with_stdio(false);
    int t;
    char s[maxn];
    while(cin>>t){
        cin>>sx>>ex;
        for(int i = 0; i < t; i++){
            cin>>war[i].a>>war[i].b;
            cin.getline(s, maxn);
        }
        sort(war, war + t, cmp);
        for(int i = 0; i < t; i++){     //遍历每个输入区间
            if(ex > war[i].b){          //输入区间的右边值小于ex,则ex为区间剩余最大值
                break;
            }
            else{
                ex = min(war[i].a - 1, ex);     //符合条件,更新区间最大值
            }
        }
        if(ex < sx){
            cout<<"Badly!"<<endl;
        }
        else{
            cout<<ex<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq1013459920/article/details/83034273