hdu -War

http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?pid=1001&ojid=0&cid=5510&hide=0

Problem 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!

分析:

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
class war
{
public:
    int st;
    int et;
};
bool cmp(war a,war b)
{
    if(a.et!=b.et)
        return a.et>b.et;
    else
        return a.st>b.st;
}
war a[150];
int main()
{
    int n;
    while(cin>>n)
    {
        memset(a,0,sizeof(a));
        string tmp;
        int res=0;
        int flag=0;
        int p,q;
        cin>>p>>q;
        for(int i=0; i<n; i++)
        {
            cin>>a[i].st>>a[i].et;
            getline(cin,tmp);
        }
        sort(a,a+n,cmp);
        if(a[0].et<q)
        {
            cout<<q<<endl;
            continue;
        }
        for(int i=0; i<n; i++)
        {
            if(a[i].et>=res)
            {
                if(res>=a[i].st)
                    res=a[i].st;
            }
            else
            {
                flag=1;
                break;
            }
        }
        if(flag==1)
            cout<<res-1<<endl;
        else
            cout<<"Badly!"<<endl;
    }
    return 0;
}


另:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
struct ss
{
    int st,en;
} s[101];
int cmp(ss a,ss b)
{
    if(a.en!=b.en)
        return a.en<b.en;
    else
        a.st<b.st;

}
int main()
{
    int n,i,sum,max,st,en;
    char c[100];
    while(cin>>n)
    {
        max=-6000001;
        cin>>st>>en;
        for(i=0; i<n; i++)
        {
            cin>>s[i].st>>s[i].en;
            gets(c);
        }
        sort(s,s+n,cmp);
        if(s[0].st-st>0)
            max=s[0].st-1;
        for(i=1; i<n; i++)
        {
            if(s[i].st-1<=s[i-1].en)
            {
                s[i].st=s[i-1].st;
            }
            else
                max=s[i].st-1;
        }
        if(s[n-1].en-en<0)
            max=en;
        if(max==-6000001) cout<<"Badly!"<<endl;
        else cout<<max<<endl;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值