Monopoly(ccpc大学生程序竞赛补题)(前缀和加二分)

11 篇文章 0 订阅
8 篇文章 0 订阅

http://acm.hdu.edu.cn/showproblem.php?pid=7130

第一步先求出前缀和和总和。
判断sum值在不同情况下的处理情况。
当sum==0,所有的数据只会在第一轮中出现一次。即若不在第一次中出现就不会出现。
当sum<0时,将前缀和全部反转,并标记已翻转。
将前缀和存入对应的取模后的map中进行存储,按照前缀和大小进行排序。然后二分查找若能找不到对应的取模值则输出-1,找到对应取模值后将前缀和值在数组中的位置取出。若取出值为最后,即为找不到对应值(只是对应的取模值相同)。找到后将在第几轮中乘以n加上前缀和的序号即为最后值。

#include<iostream>
#include<map>
#include<vector>
#include<stdio.h>
#include<algorithm>
using namespace std;
#define ll long long
ll a[200005],pre[200005];
map<ll,int > vs;
map<ll,vector<pair<ll,int>>> shu;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        int flag=0;
        ll sum=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%lld",&a[i]);
            pre[i]=pre[i-1]+a[i];
            sum+=a[i];
        }
        if(sum<0)
        {
            sum=-sum;
            flag=1;
            for(int i=1; i<=n; i++)
                pre[i]=-pre[i];
        }
        if(sum==0)
        {
            vs.clear();
            for(int i=1; i<=n; i++)
                if(vs.find(pre[i])==vs.end())
                {
                    vs[pre[i]]=i;
                }
            while(m--)
            {
                ll x;
                scanf("%lld",&x);
                if(x==0)
                {
                    cout<<0<<endl;
                    continue;
                }
                if(vs.find(x)==vs.end())
                    cout<<-1<<endl;
                else
                    cout<<vs[x]<<endl;
            }
            continue;
        }

        shu.clear();
        for(int i=1; i<=n; i++)
        {
            ll res=(pre[i]%sum+sum)%sum;
            shu[res].push_back({-pre[i],i});
        }
        for(auto it=shu.begin(); it!=shu.end(); it++)
        {
            sort(it->second.begin(),it->second.end());
        }
        while(m--)
        {
            ll x;
            cin>>x;
            if(flag==1)
                x=-x;
            if(x==0)
            {
                cout<<0<<endl;
                continue;
            }
            ll xr=(x%sum+sum)%sum;
            if(shu.find(xr)==shu.end())
            {
                cout<<-1<<endl;
                continue;
            }
            pair<ll,int> ask= {-x,0};

            int pos=lower_bound(begin(shu[xr]),end(shu[xr]),ask)-shu[xr].begin();
            if(pos==shu[xr].size())
                cout<<-1<<endl;
            else
            {
                ll ans=shu[xr][pos].second;
                ans+=(x+shu[xr][pos].first)/sum*n;
                cout<<ans<<endl;
            }
        }
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
- Privacy disclosure: This refers to the unauthorized sharing or exposure of an individual's personal information without their consent. For example, if a company sells or shares its customer data with third-party advertisers without the customers' knowledge or permission, that would be a privacy disclosure. - Digital divide: This describes the unequal access to technology and digital resources among different groups of people. For example, individuals living in rural or low-income areas may not have access to high-speed internet or the latest technology, leading to a digital divide between them and those with better access. - Data dictatorship: This refers to a situation where a government or other powerful entity has complete control over the use and dissemination of data. For example, a government may use surveillance technology to monitor its citizens' activities without their knowledge or consent, effectively creating a data dictatorship. - Data monopoly: This describes a situation where a single entity or a small group of entities have complete control over access to and use of a particular type of data. For example, a company that owns a large portion of a particular type of data, such as search engine data, may have a data monopoly. - Threat of human’s subject status: This refers to the potential for technology and data to reduce or eliminate human agency and autonomy, effectively turning individuals into passive subjects rather than active agents. For example, if a company uses AI to make decisions for its customers without their input or control, the customers may feel that their subject status has been threatened.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Prime me

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值