1. 程式人生 > >CSU 1919: 不醉不歸 1922: Irony Ring 1923: Mysterious Block D 1924: 那些年寮裡的崽兒們

CSU 1919: 不醉不歸 1922: Irony Ring 1923: Mysterious Block D 1924: 那些年寮裡的崽兒們

#include<iostream>
#include<string.h>
using namespace std;

int dp0[500][500],dp1[500][500],s[500][500];
bool mp[500][500];

int main()
{
	int N,M,K;
	while (~scanf("%d%d%d",&N,&M,&K)&&N&&M&&K)
	{
		int ans=0;
		memset(mp,0,sizeof(mp));
		memset(dp0,-1,sizeof(dp0));
		memset
(dp1,-1,sizeof(dp1)); for(int i=1;i<=N;i++) for(int j=1;j<=M;j++) { scanf("%d",&s[i][j]); s[i][j]+=s[i][j-1]; } int n; scanf("%d",&n); while (n--) { int x,y; scanf("%d%d",&x,&y); mp[x][y]=1; } dp0[0][0]=0; for(int i=1;i<=N;i++) { dp0[i][0
]=dp0[i-1][0]; for(int j=1;j<=K&&j<=i*M;j++) { dp0[i][j]=dp0[i-1][j]; dp1[i][j]=max(dp0[i][j-1],dp1[i-1][j]); for(int k=1,p=1;k<=j&&p<=M;k++) //k表示在第i個架子花k元錢,p表示目前已經取到第p個瓶子 for(;p<=M;p++) if (mp[i][p]) //當第i個架子的第p個是再來一瓶的瓶子的時候,取完之後有獎碼,p可以繼續迴圈而價格不多花錢 { if
(dp0[i-1][j-k]>=0) dp1[i][j]=max(dp1[i][j],dp0[i-1][j-k]+s[i][p]); //在前i-1個架子中花j-k元,而在第i個架子上花k元剛好取到第p個瓶子,且那個再來一瓶放在之後取 if (dp1[i-1][j-k+1]>=0) dp1[i][j]=max(dp1[i][j],dp1[i-1][j-k+1]+s[i][p]); //在前i-1個架子花j-k+1元且之前沒有拿的獎碼放在第i個架子上用,所以用j-k+1元就可取p個瓶子 if (dp0[i-1][j-k+1]>=0) dp0[i][j]=max(dp0[i][j],dp0[i-1][j-k+1]+s[i][p]); //由於當前遇到了一個再來一瓶,所以我們可以把這個獎碼用在前i-1行,相當於在前i-1行可以多1元錢 } else //當第i個架子第p個不是再來一瓶的時候 { if (dp0[i-1][j-k]>=0) dp0[i][j]=max(dp0[i][j],dp0[i-1][j-k]+s[i][p]); //直接從之前的繼承 if (dp1[i-1][j-k+1]>=0) dp0[i][j]=max(dp0[i][j],dp1[i-1][j-k+1]+s[i][p]); //把之前的獎碼用掉 p++; break; //沒有再來一瓶,所以處理完畢後就要跳出,得多花錢 } } if (i==N) ans=max(ans,max(dp0[i][K],dp1[i][K])); } printf("%d\n",ans); } return 0; }
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<stack>
using namespace std;
#define LL long long
const int maxn=100010;
struct node
{
    LL r,R,h;
    bool operator<(const node &res)const
    {
        if(R==res.R) return r>res.r;
        else return R>res.R;
    }
}a[maxn];
stack<node> s;

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        while(!s.empty()) s.pop();
        for(int i=0;i<n;i++) scanf("%lld%lld%lld",&a[i].r,&a[i].R,&a[i].h);
        sort(a,a+n);
        LL MAX=0,ans=0;
        for(int i=0;i<n;i++)
        {
            while(!s.empty()&&s.top().r>=a[i].R)
            {
                ans-=s.top().h;
                s.pop();
            }
            ans+=a[i].h;
            s.push(a[i]);
            MAX=max(MAX,ans);
        }
        printf("%lld\n",MAX);
    }
    return 0;
} 
#include <queue>
#include <stack>
#include <ctime>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;
typedef long double LB;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;

void debug() { cout << endl; }
template<typename T, typename ...R> void debug (T f, R ...r) { cout << "[" << f << "]"; debug (r...); }


const int MAXN = 111 + 2;
const int HASH_MAX = 1000007;
const int HASH_SIZ = 1e5 + 5;
const int BASE = 131;
struct HashMap {
    struct Edge {
        ULL d[4];
        int next;
        Edge() {}
        Edge(ULL dx[], int nxt) : next(nxt) { for(int i = 0; i < 4; ++i) d[i] = dx[i]; }
    } edge[HASH_SIZ];
    int head[HASH_MAX], tot;
    void hash_init() {
        tot = 0;
        memset(head, -1, sizeof(head));
    }
    bool hash_query(ULL dx[4]) {
        sort(dx, dx + 4);
        ULL x = (dx[0] & dx[1] & dx[2] & dx[3]);

        ULL u = x % HASH_MAX;
        for(int i = head[u]; ~i; i = edge[i].next) {
            bool equ = true;
            for(int j = 0; j < 4; ++j) {
                if(dx[j] != edge[i].d[j]) { equ = false; break; }
            }
            if(equ) return true;
        }
        return false;
    }
    ULL hash_add(ULL dx[4]) {
        sort(dx, dx + 4);
        ULL x = (dx[0] & dx[1] & dx[2] & dx[3]);

        ULL u = x % HASH_MAX;
        for(int i = head[u]; ~i; i = edge[i].next) {
            bool equ = true;
            for(int j = 0; j < 4; ++j) {
                if(dx[j] != edge[i].d[j]) { equ = false; break; }
            }
            if(equ) return x;
        }
        edge[tot] = Edge(dx, head[u]);
        head[u] = tot ++;
        return x;
    }

} hash_xxw;

int n, m;
char maze[MAXN][MAXN];
bool vis[MAXN][MAXN];
void dfs(int x, int y, int& xx, int& yy) {
    if(x >= n || y >= m) return;
    if(vis[x][y] || maze[x][y] == '#') return;
    vis[x][y] = true;
    xx = max(x, xx), yy = max(y, yy);
    dfs(x + 1, y, xx, yy);
    dfs(x, y + 1, xx, yy);
}

ULL up(int sx, int sy, int tx, int ty) {
    ULL ret = 0;
    for(int i = sx; i <= tx; ++i) {
        for(int j = sy; j <= ty; ++j) {
            ret = ret * BASE + (maze[i][j] == '+');
        }
    }
    return ret;
}

ULL right(int sx, int sy, int tx, int ty) {
    ULL ret = 0;
    for(int j = sy; j <= ty; ++j) {
        for(int i = tx; i >= sx; --i) {
            ret = ret * BASE + (maze[i][j] == '+');
        }
    }
    return ret;
}
ULL down(int sx, int sy, int tx, int ty) {
    ULL ret = 0;
    for(int i = tx; i >= sx; --i) {
        for(int j = ty; j >= sy; --j) {
            ret = ret * BASE + (maze[i][j] == '+');
        }
    }
    return ret;
}
ULL left(int sx, int sy, int tx, int ty) {
    ULL ret = 0;
    for(int j = ty; j >= sy; --j) {
        for(int i = sx; i <= tx; ++i) {
            ret = ret * BASE + (maze[i][j] == '+');
        }
    }
    return ret;
}


int main() {
#ifdef ___LOCAL_WONZY___
    freopen ("input.txt", "r", stdin);
#endif // ___LOCAL_WONZY___
    while(~scanf("%d %d", &n, &m)) {
        memset(vis, false, sizeof(vis));
        for(int i = 0; i < n; ++i) {
            scanf("%s", maze[i]);
        }
        int xx, yy;
        ULL dx[4];
        hash_xxw.hash_init();
        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < m; ++j) {
                if(maze[i][j] == '#' || vis[i][j]) continue;
                xx = i, yy = j;
                dfs(i, j, xx, yy);
                dx[0] = up(i, j, xx, yy);
                dx[1] = right(i, j, xx, yy);
                dx[2] = down(i, j, xx, yy);
                dx[3] = left(i, j, xx, yy);
                hash_xxw.hash_add(dx);
            }
        }
        printf("%d\n", hash_xxw.tot);
    }
#ifdef ___LOCAL_WONZY___
    cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif // ___LOCAL_WONZY___
    return 0;
}
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
using namespace std;


int main()
{
    int n;
    int u=1;
    while(scanf("%d",&n)!=EOF)
    {
        map<string,int> p;
        string a[100];
        string t;
        int index=0;
        for(int i=0;i<n;i++)
        {
            cin>>t;
            if(p[t])
            {
                p[t]++;
            }else{
               a[index++]=t;
               p[t]++;
            }
        }
        printf("Case %d:\n",u++);
        for(int i=0;i<index;i++)
        {
            cout<<a[i]<<" "<<p[a[i]]<<endl;
        }
    }
    return 0;
}