protagonist

Cow Rectangles&Moovie Mooving

2401: Cow Rectangles

时间限制: 1 Sec  内存限制: 64 MB
提交: 19  解决: 7
[提交] [状态] [命题人:admin]

题目描述

The locations of Farmer John's N cows (1 <= N <= 500) are described by distinct points in the 2D plane.  The cows belong to two different breeds: Holsteins and Guernseys.  Farmer John wants to build a rectangular fence with sides parallel to the coordinate axes enclosing only Holsteins, with no Guernseys (a cow counts as enclosed even if it is on the boundary of the fence).  Among all such fences, Farmer John wants to build a fence enclosing the maximum number of Holsteins.  And among all these fences, Farmer John wants to build a fence of minimum possible area.  Please determine this area.  A fence of zero width or height is allowable.

 

输入

The first line of input contains N.  Each of the next N lines describes a cow, and contains two integers and a character. The integers indicate a point (x,y) (0 <= x, y <= 1000) at which the cow
is located. The character is H or G, indicating the cow's breed.  No two cows are located at the same point, and there is always at least one Holstein.

 

输出

Print two integers. The first line should contain the maximum number of Holsteins that can be enclosed by a fence containing no Guernseys, and second line should contain the minimum area enclosed by such a fence.

 

样例输入

5
1 1 H
2 2 H
3 3 G
4 4 H
6 6 H

样例输出

2
1

#include <bits/stdc++.h>
//#define scan(x) scanf("%d",&x);
//#define scan2(x,y) scanf("%d%d",&x,&y);
//#define rg register int
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
 
const int maxn = 1e5 + 10;
 
int n;
int tot1, tot2, x[maxn], y[maxn], lshx[maxn], lshy[maxn];
int realx[maxn], realy[maxn];
char o[maxn], op[8];
int re[600][600], th[600][600];
 
int main() {
#ifndef ONLINE_JUDGE
    freopen("splay.txt", "r", stdin);
#endif
    scanf("%d", &n);
    for (register int i = 1; i <= n; ++i) {
        scanf("%d%d%s", &x[i], &y[i], op + 1);
        o[i] = op[1];
        lshx[++tot1] = x[i];
        lshy[++tot2] = y[i];
        //realx[i]=x[i];
    }
    sort(lshx + 1, lshx + 1 + tot1);
    sort(lshy + 1, lshy + 1 + tot2);
    int max_x = unique(lshx + 1, lshx + 1 + tot1) - lshx - 1;
    int max_y = unique(lshy + 1, lshy + 1 + tot2) - lshy - 1;
    for (register int i = 1; i <= n; ++i) {
        x[i] = lower_bound(lshx + 1, lshx + 1 + max_x, x[i]) - lshx;
        y[i] = lower_bound(lshy + 1, lshy + 1 + max_y, y[i]) - lshy;
    }
    for (register int i = 1; i <= n; ++i) {
        if (o[i] == 'H') {
            re[x[i]][y[i]]++;
        } else {
            th[x[i]][y[i]]++;
        }
    }
    for (register int i = 1; i <= max_x; ++i) {
        for (register int j = 1; j <= max_y; ++j) {
            re[i][j] += re[i - 1][j];
            th[i][j] += th[i - 1][j];
        }
    }
    int point = 0, area = 0;
    for (register int i = 1; i <= max_x; ++i) {
        for (register int j = i; j <= max_x; ++j) {
            int l = 1, r = 1, res = 0;
            while (l <= max_y) {
                while (r <= max_y) {
                    while (l <= max_y && (!(re[j][l] - re[i - 1][l]) || th[j][l] - th[i - 1][l]))++l;
                    res = 0;
                    r = l;
                    while (r <= max_y && !(th[j][r] - th[i - 1][r])) {
                        res += re[j][r] - re[i - 1][r];
                        if (point < res) {
                            point = res;
                            area = (lshx[j] - lshx[i]) * (lshy[r] - lshy[l]);
                        }
                        if (point == res) {
                            area = min(area, (lshx[j] - lshx[i]) * (lshy[r] - lshy[l]));
                        }
                        ++r;
                    }
                    l = r;
                }
            }
        }
    }
    printf("%d\n%d\n", point, area);
    return 0;
}

2402: Moovie Mooving

时间限制: 2 Sec  内存限制: 64 MB
提交: 6  解决: 2
[提交] [状态] [命题人:admin]

题目描述

Bessie is out at the movies.  Being mischievous as always, she has decided to hide from Farmer John for L (1 <= L <= 100,000,000) minutes, during which time she wants to watch movies continuously.She has N (1 <= N <= 20) movies to choose from, each of which has a certain duration and a set of showtimes during the day.  Bessie may enter and exit a movie at any time during one if its showtimes, but she does not want to ever visit the same movie twice, and she cannot switch to another showtime of the same movie that overlaps the current showtime.

Help Bessie by determining if it is possible for her to achieve her goal of watching movies continuously from time 0 through time L.  If it is, determine the minimum number of movies she needs to see to achieve this goal (Bessie gets confused with plot lines if she watches too many movies).

 

输入

The next N lines each describe a movie.  They begin with its integer duration, D (1 <= D <= L) and the number of showtimes, C (1 <= C <= 1000).  The remaining C integers on the same line are each in the range 0..L, and give the starting time of one of the showings of the movie.  Showtimes are distinct, in the range 0..L, and given in increasing order.

 

输出

A single integer indicating the minimum number of movies that Bessie needs to see to achieve her goal.  If this is impossible output -1 instead.

 

样例输入

4 100
50 3 15 30 55
40 2 0 65
30 2 20 90
20 1 0

样例输出

3

 

提示

Bessie should attend the first showing of the fourth movie from time 0 to time 20.  Then she watches the first showing of the first movie from time 20 to time 65.  Finally she watches the last showing of the second movie from time 65 to time 100.

#include <bits/stdc++.h>
//#define scan(x) scanf("%d",&x);
//#define scan2(x,y) scanf("%d%d",&x,&y);
//#define rg register int
#define lowbit(x) x&(-x)
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
 
const int maxn = 1e5 + 10;
 
int n;
int duration,len[maxn],tot[maxn],start[30][maxn];
int dp[(1<<21)+6];
 
inline int search(int cur,int id){
    int l=0,r=tot[id],ans;
    while(l<=r){
        int mid=l+r>>1;
        if(start[id][mid]<=cur){
            ans=mid;
            l=mid+1;
        }
        else{
            r=mid-1;
        }
    }
    return ans;
}
 
int main() {
#ifndef ONLINE_JUDGE
    freopen("splay.txt", "r", stdin);
#endif
    scanf("%d%d",&n,&duration);
    for(register int i=1;i<=n;++i){
        scanf("%d%d",&len[i],&tot[i]);
        for(register int j=1;j<=tot[i];++j){
            scanf("%d",&start[i][j]);
        }
    }
    int c=(1<<n),res=INT_MAX;
    dp[0]=0;
    for(register int i=1;i<c;++i)dp[i]=-1;
    for(register int i=0;i<c;++i){
        if(dp[i]==-1)continue;
        if(dp[i]>=duration){
            int cur=0;
            for(register int j=i;j>0;j-=lowbit(j))++cur;
            res=min(res,cur);
        }
        for(register int j=0;j<n;++j){
            if(i&(1<<j))continue;
            int pos=search(dp[i],j+1);
            //printf("debug pos = %d\n",pos);
            dp[i|(1<<j)]=max(dp[i|(1<<j)],start[j+1][pos]+len[j+1]);
        }
    }
    if(res!=INT_MAX)printf("%d\n",res);
    else puts("-1");
    return 0;
}

 

posted @ 2019-09-12 16:18  czy-power  阅读(372)  评论(0编辑  收藏  举报