CSU 1089: 羊吃草 1090: Number Transformation 1092: Barricade 1093: Caps Lock 1098: Happy watering

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nameofcsdn/article/details/79966106

1089: 羊吃草

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<set>
using namespace std;
#define N 100100
struct mess
{
    int p;
    int s;
};
mess m1[N],m2[N];
bool cmp(const mess & m1,const mess & m2)
{
    return m1.s>m2.s;
}
bool operator <(const mess & m1,const mess & m2)
{
    return m1.p<m2.p;
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int i,j;
         multiset<mess> sm;
        //memset(m2,0,sizeof(m2));
        for(i=0;i<n;i++)
            scanf("%d%d",&m1[i].p,&m1[i].s);
        for(i=0;i<m;i++)
            scanf("%d%d",&m2[i].p,&m2[i].s);
        sort(m1,m1+n,cmp);
        sort(m2,m2+m,cmp);
        long long cost=0;
        j=0;
        int flag=0;
        for(i=0;i<n;i++)
        {
            while(1)
            {
                if(m2[j].s>=m1[i].s)  
                    sm.insert(m2[j]);    //加入优先队列
                else
                    break;
                j++;
            }
            if(!sm.empty())
            {
                multiset<mess>::iterator pos;
                pos=sm.lower_bound(m1[i]);   //找到满足要求的草料
			if(pos!=sm.end())
			{
				cost+=pos->p;
				sm.erase(pos);
			}
			else
			{
				flag=1;
				break;
			}

            }
		else
		{
			flag=1;
			break;
		}
        }
        if(flag)
            printf("-1\n");
        else printf("%lld\n",cost);
 
    }
    return 0;
}

/**********************************************************************
	Problem: 1089
	User: 3901140225
	Language: C++
	Result: AC
	Time:588 ms
	Memory:5968 kb
**********************************************************************/

1090: Number Transformation

#include<stdio.h>
#include<string.h>
#define Inf 1005
#define min(a,b) a<b?a:b
int prim[200],f[608],dp[1005],p;
void prime()
{
    int i,j;
    memset(f,0,sizeof(f));
    p = 0;
    for(i = 2; i <= 605; i ++)
    {
        if(!f[i])
        {
            prim[p++] = i;
            for(j = 2*i; j <= 605; j += i)
            f[j] = 1;
        }
    }
}
int main()
{
    int s,t;
    int i,j,k;
    prime();
    while(~scanf("%d%d",&s,&t))
    {
        if(s < 3||t - s< 2)
        {
            printf("No path!\n");
            continue;
        }
        for(i = 0; i <= t; i ++)
        dp[i] = Inf;
        dp[s] = 0;
        for(k = s+2; k <= t; k ++)
        for(j = 0; 2*prim[j] < k; j ++)
        {
            dp[k] = min(dp[k - prim[j]] + 1,dp[k]);
        }
        if(dp[t] == Inf)
        printf("No path!\n");
        else
        printf("Need %d step(s)\n",dp[t]);
    }
return 0;
}
/**********************************************************************
	Problem: 1090
	User: 3901140225
	Language: C++
	Result: AC
	Time:52 ms
	Memory:1128 kb
**********************************************************************/

1092: Barricade

#include<iostream>
#include<climits>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int MAXN=1e4+10;
const int MAXM=1e5+10;
const int INF=0x3f3f3f3f;
int head[MAXN],nxt[MAXM];
int odeg[MAXN],dist[MAXN];
bool vis[MAXN];
int n,m,en;

struct edge
{
    int v,cost;
    bool operator <(const edge &r) const
    {
        return cost>r.cost;
    }
}E[MAXM];

void init()
{
    memset(head,0,sizeof(head));
    en=0;
    memset(odeg,0,sizeof(odeg));
    for(int i=1;i<=n;i++) dist[i]=INF;
}

void addedge(int u,int v)
{
    E[++en].v=v;
    nxt[en]=head[u];
    head[u]=en;
    odeg[u]++;
}

void dijkstra(int start)
{
    memset(vis,false,sizeof(vis[0])*(n+5));
    priority_queue<edge> q;
    while(!q.empty()) q.pop();
    edge now,tmp;
    now.v=start;
    now.cost=0;
    q.push(now);
    while(!q.empty())
    {
        now=q.top();
        q.pop();
        if(vis[now.v]) continue;
        vis[now.v]=true;
        dist[now.v]=now.cost;
        for(int i=head[now.v];i;i=nxt[i])
        {
            int to=E[i].v;
            if(!vis[to]&&dist[to]>dist[now.v]+odeg[now.v]-1)
            {
                dist[to]=dist[now.v]+odeg[now.v]-1;
                tmp.cost=dist[to];
                tmp.v=to;
                q.push(tmp);
            }
        }
    }
    if(dist[n]==INF) cout<<"-1"<<endl;
    else cout<<dist[n]<<endl;
}

int main()
{
    ios::sync_with_stdio(false);
    int u,v;
    while(cin>>n>>m)
    {
        init();
        for(int i=1;i<=m;i++)
        {
            cin>>u>>v;
            addedge(u,v);
        }
        dijkstra(1);
    }
    return 0;
}
/**********************************************************************
	Problem: 1092
	User: 3901140225
	Language: C++
	Result: AC
	Time:196 ms
	Memory:3476 kb
**********************************************************************/

1093: Caps Lock

#include<stdio.h>

#include<string.h>

char b1[1001][101];
int main()
{
    int n,i,j,k1,k2,k3,k4,len;    
    while(scanf("%d",&n)!=EOF)
    {
        int s=0;
        int d;
       int kk;
        k1=0;k2=0;k3=0;k4=0;
        for(i=0;i<n;i++)
        {
            d=0;
            scanf("%s",&b1[i]);
            len=strlen(b1[i]);
            for(j=0;j<len;j++)
                if(b1[i][j]<92&&b1[i][j+1]>92||b1[i][j]>92&&b1[i][j+1]<92||b1[i][j+1]=='\0')d++; //计算一个字符串的段数
                s+=d; //总段数           
                if(b1[i][0]>92&&b1[i][len-1]>92)k1++; //记录各类型的个数;

            else               if(b1[i][0]>92&&b1[i][len-1]<92)k2++;
            else               if(b1[i][0]<92&&b1[i][len-1]>92)k3++;
            else            if(b1[i][0]<92&&b1[i][len-1]<92)k4++;

        }
        if(k1>1) //自连
        {
            s-=(k1-1);
            k1=1;
        }

        if(k4>1)
        {
            s-=(k4-1);
            k4=1;
        }
            kk=k2<k3?k2:k3;
            if(k2!=k3)
            {
               s=s-2*kk; //若有2,3类字符串s2,s3,则个数较多的放前面,如s2s3s2s3s2,则s3的首尾被连接一次,共减少2*kk次

                  if(k4!=0)s--;

                  if(k1!=0)s--;
                  if(k2>k3)
                    s--; //此时2类放前,则首字母是小写,与计算机相连
            } 

            else 
            {
                if(k2!=0)//2,3类间隔相连,2类放前
                {
                    s=s-(k2+k3-1); //除了最后一个字符串,其它每个的尾字母都被连接一次

                    if(k4!=0)s--;

                  if(k1!=0)s--;

                  s-=1; //2类的首字母与计算机相连
                }

                else  //2,3类都不存在,
                {
                if(s==1) //说明只有字符串aaa或BBBB,前者与计算机相连
                    {
                        if(k1==0)s=1;
                        else s=0;
                    }
                    else          if(s==2)s=1; //说明只有aaa和BBB这两类字符串
                    else     if(k1==1)s-=1;//可能是aaBBaa或BBaaBB或这两者都有,若有1类,当然1类放前,与计算机相连
                }
            }
            printf("%d\n",s);
    }
    return 0;
}
/**********************************************************************
	Problem: 1093
	User: 3901140225
	Language: C++
	Result: AC
	Time:12 ms
	Memory:1216 kb
**********************************************************************/

1098: Happy watering

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
struct note
{
	int h,d;
	bool operator <  (const struct note &tmp)const
	{
		return h>tmp.h;
	}
};
int main()
{
	//freopen("in.txt","r",stdin);
	int n,k;
	while(scanf("%d%d",&n,&k)!=EOF)
	{
		note tmp;
		int tmpmax=0;
		int ans = 0x3f3f3f3f;
		priority_queue<note> q;
		for(int i=0;i<n;i++)
		{
			scanf("%d%d",&tmp.h,&tmp.d);
			tmpmax = max(tmp.h,tmpmax);
			q.push(tmp);
		}
		k++;
		while(k--)
		{
			tmp = q.top();
			q.pop();
			ans = min(tmpmax-tmp.h,ans);
			if(ans==0)
				break;
			tmp.h+=tmp.d;
			tmpmax = max(tmpmax,tmp.h);
			q.push(tmp);
		}
		printf("%d\n",ans);
	}
}

/**********************************************************************
	Problem: 1098
	User: 3901140225
	Language: C++
	Result: AC
	Time:116 ms
	Memory:3644 kb
**********************************************************************/



阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页