4 条题解

  • 42
    @ 2023-7-23 21:05:20

    ` 点个赞吧~~❤️

    #include <bits/stdc++.h>
    using namespace std;
    long long n,m,a[1000010],d,t,c[1000010],p,b[1000010];
    bool cmp(int x, int y)
    {
        return c[x] < c[y];
    }
    int main()
    {
        cin >> n >> m;
        for(int i=1;i<=n;i++)
        {
            cin >> a[i];
            b[i]=i;
        }
        for(int i=1;i<=n;i++)
        {
            cin >> c[i];
        }
        sort(b + 1, b + n + 1, cmp);
        p = 1;
        for (int i = 1; i <= m; i++) 
        {
            cin >> t >> d;
            if (a[t] >= d) 
            {
                a[t] -= d;
                cout << c[t] * d << endl;
            } 
            else 
            {
                long long sum = c[t] * a[t];
                d -= a[t];
                a[t] = 0;
                while (p <= n && d > 0)
                {
                    int cur = b[p];
                    if (a[cur] >= d) 
                    {
                        sum += c[cur] * d;
                        a[cur] -= d;
                        d = 0;
                    } 
                    else 
                    {
                        sum += c[cur] * a[cur];
                        d -= a[cur];
                        a[cur] = 0;
                        p++;
                    }
                }
                if (d > 0)
                    cout << 0 << endl;
                else
                    cout << sum << endl;
            }
        }
    }
    

    已 A ~ C ~

  • 15
    @ 2024-2-19 20:16:54

    我来了!!! 这道题我一试就错,但样例全对,我摸不着头脑,然后试着试着就AC了!!!我便挖掘原先错哪了,然后发现除了花费的变量也需要开long long!!!这真是俗话所说“不开long long见祖宗” 所以在这里特此提醒,随手long long是好习惯! 好了 下面步入正题 由于没看到优先队列的题解,我便来做一个 我首先使用struct将初始位置和对应价格封装,再用字符串重载,以价格为比较,然后按照输入顺序依次输入 然后遍历客人需求 具体流程参见代码注释 我这里的id相当于老师代码里的t数组,存储的是原先的编号,省了不少事 好了,大体就这样,上大家等待已久的代码

    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    struct Cus
    {
    	ll c,id;//位置和价格 
    };
    struct cmp
    {
    	bool operator()(Cus x,Cus y)
    	{
    		return x.c > y.c;
    	}
    };
    priority_queue<Cus,vector<Cus>,cmp> q;
    int main()
    {
    	ll n,m,a1[100005],c1[100005];
    	cin >> n >> m;
    	for (int i = 1;i <= n;i++)
    	{
    		cin >> a1[i];
    	}
    	for (int i = 1;i <= n;i++)
    	{
    		Cus h;
    		cin >> c1[i];
    		h.c = c1[i];
    		h.id = i;
    		q.push(h);
    	}
    	ll d,t;
    	unsigned ll cost = 0;
    	for (int i = 1;i <= m;i++)
    	{
    		cost = 0;
    		bool flag = 0;//表示是否付款 
    		cin >> t >> d;
    		if (a1[t] >= d)//数量够 
    		{
    			a1[t] -= d;
    			cost += c1[t] * d;
    			flag = 1;
    		}
    		else//不够 
    		{
    			d -= a1[t];
    			cost += a1[t] * c1[t];
    			a1[t] = 0;//先把所有这个菜买光 
    			while(!q.empty())//看其他菜 
    			{
    				if (a1[q.top().id] >= d)//如果这一个数量够 
    				{
    					a1[q.top().id] -= d;
    					cost += d * q.top().c;//买还所需要的数量 
    					flag = 1;//可以付帐 
    					break;
    				}
    				else
    				{
    					d -= a1[q.top().id];//把这个菜买光 
    					cost += a1[q.top().id] * q.top().c;
    					a1[q.top().id] = 0;//卖光了 
    					q.pop();//将此菜删除 
    				}
    			}
    		}
    		if (flag == 1)//付帐了 
    		{
    			cout << cost << endl;
    		}
    		else//走了 
    		{
    			cout << 0 << endl;
    		}
    	}
    	return 0;
    }
    

    编写代码出现的小插曲: 刚开始是存数量和价格,后来发现要操作数量,把数量挪了出来, 这样便成了int类型的优先队列,把结构体和重载都删了,后来发现要加上id,就得重新改回原来的框架!!!在此温馨提示大家删代码时一定要慎重,慎重,慎重!!! 最后一条建议,码了这么多字非常不易,别忘点个赞支持一下吧********************************* 本次分享到此结束,感谢大家配合 See you!

    • 10
      @ 2023-7-6 16:17:51

      根据题意模拟即可,但是细节比较多。

      可以把菜品的序号按价格从低到高排序(也可用优先队列处理),然后依次处理每一个客人,如果他点的菜剩余数量是够的,那么直接满足他的要求即可。

      否则就需要找到还有剩余的最低价的菜品,提供给他,直到给他的菜品数量达到要求,或者菜品全部用完。

      cmp函数
      
      bool cmp(int x, int y)
      {
          return c[x] < c[y];
      }
      
      核心代码
      
      sort(b + 1, b + n + 1, cmp);
      p = 1;
      for (int i = 1; i <= m; i++) {
          cin >> t >> d;
          if (a[t] >= d) {
              a[t] -= d;
              cout << c[t] * d << endl;
          } else {
              long long sum = c[t] * a[t];
              d -= a[t];
              a[t] = 0;
              while (p <= n && d > 0) {//p表示当前考虑的最低价菜品的下标
                  int cur = b[p];
                  if (a[cur] >= d) {
                      sum += c[cur] * d;
                      a[cur] -= d;
                      d = 0;
                  } else {
                      sum += c[cur] * a[cur];
                      d -= a[cur];
                      a[cur] = 0;
                      p++;
                  }
              }
              if (d > 0)
                  cout << 0 << endl;
              else
                  cout << sum << endl;
          }
      }
      
      • @ 2023-7-22 22:16:29

        求一下关于数组b的解析o(TヘTo),都看得懂,就是不知道怎么处理数组b的初始化(这个数组是干什么的呢?)

      • @ 2023-7-23 15:52:27

        @ b数组就是第i道菜的编号,按每道菜的价格排序

      • @ 2023-7-23 16:52:44

        @啊!thanks

    • -25
      @ 2023-7-23 20:56:45

      代码拿走,不用谢

      
      
      #include <bits/stdc++.h>
      #define ll long long
      using namespace std;
      struct node
      {
      int id;
      ll sum,cost;
      }q[100005];
      bool cmp(node a,node b)
      {
      if(a.cost!=b.cost)return a.cost<b.cost;
      return a.id<b.id;
      }
      int t[100005];
      int st=1;
      ll ans=0;
      bool p=0;
      int main()
      {
      int n,m;
      cin>>n>>m;
      for(int i=1;i<=n;++i)
      {
      q[i].id=i;
      cin >> q[i].sum;
      }
      for(int i = 1;i<=n;++i)
      {
      cin >> q[i].cost;
      }
      sort(q+1, q+n+1, cmp);
      for(int i = 1;i<=n;++i)
      {
      t[q[i].id]=i;
      }
      for(int i = 1;i<=m;++i)
      {
      ans=0;
      int x;
      ll y;
      cin >> x >> y;
      if(p)
      {
      cout << 0 << endl;
      continue;
      }
      if(y<=q[t[x]].sum)
      {
      q[t[x]].sum -= y;
      ans+=q[t[x]].cost*y;
      while(q[st].sum == 0) st++;
      }
      else
      {
      y-=q[t[x]].sum;
      ans+=q[t[x]].cost * q[t[x]].sum;
      q[t[x]].sum = 0;
      while(y)
      {
      ll temp=min(y,q[st].sum);
      ans+=q[st].cost*temp;
      y-=temp;
      q[st].sum-=temp;
      while(q[st].sum==0)
      {
      st++;
      if(st>n)
      {
      p=1;
      if(y)
      {
      cout << 0 << endl;
      }
      else
      {
      cout << ans << endl;
      }
      break;
      }
      }
      if(p) break;
      }
      }
      if(!p) cout << ans << endl;
      }
      
      }
      
      
      • @ 2023-12-9 11:15:00

        你需要注意一下格式啊[捂脸]

    • 1

    信息

    ID
    248
    时间
    1000ms
    内存
    256MiB
    难度
    4
    标签
    (无)
    递交数
    859
    已通过
    411
    上传者