UASCO Photo

Photo

TimeLimit: 2 Second   MemoryLimit: 64 Megabyte

Totalsubmit: 33   Accepted: 13  

Description

FJ wants to take pictures of his N cows (2 <= N <= 1,000,000,000), which
are standing in a line and conveniently numbered 1..N.  Each photograph can
capture a consecutive range of cows from the lineup, and FJ wants to make
sure that each cow appears in at least one photo.  

Unfortunately, there are K unfriendly pairs of cows (1 <= K <= 1000) that
each refuse to be in the same photograph.  Given the locations of these
unfriendly pairs, please determine the minimum number of photos FJ needs to
take.

Input

* Line 1: Two space-separated integers, N and K.

* Lines 2..K+1: Line i+1 contains two integers, A_i and B_i, stating
       that the cows in positions A_i and B_i are unfriendly and
       therefore cannot be in the same photograph.

Output

* Line 1: A single integer, specifying the minimum number of photos FJ
       needs to take.
FJ can take 3 photos:
- One ranging from 1 to 2.
- One ranging from 3 to 5.
- One ranging from 6 to 7.

Sample Input

7 3
1 3
2 4
5 6

Sample Output

3

Source

USACO

 
思路:
 
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
int can_be[1010][1010];
int map[10010];
int the_last_sum;
int n,k;
int a,b;
struct Node
{
   int x;
   int posi;
}v[10010];
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        memset(can_be,0,sizeof(can_be));
        memset(map,0,sizeof(map));
        memset(v,0,sizeof(v));
        int m = 1;
        for(int i = 1;i <= k * 2;i ++)
        {
              scanf("%d%d",&a,&b);
              v[i].x = a;v[i].posi = m;
              i ++;
              v[i].x = b;v[i].posi = m;
              m ++;
        }
        for(int i = 1;i <= k * 2;i ++)
            for(int j = i + 1;j <= k * 2;j ++)
            {
                int temp,flag;
                if(v[i].x > v[j].x)
                {
                    temp = v[i].x;
                    v[i].x = v[j].x;
                    v[j].x = temp;
                    flag = v[i].posi;
                    v[i].posi = v[j].posi;
                    v[j].posi = flag;
                }
            }
       int the_can_flag = 1;
       the_last_sum = 1;
       for(int i = 1;i <= k * 2;i ++)
       {
           if(can_be[the_can_flag][v[i].posi] == 1)
           {
               the_last_sum ++;
               the_can_flag ++;
           }
           can_be[the_can_flag][v[i].posi] = 1;
       }
       printf("%d
",the_last_sum);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/GODLIKEING/p/3426708.html