http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4101

题意:数组a通过交换一对数字,得到了b数组,给出x=和y=和b数组,问有多少对l,r(l<=r)能满足条件

C++版本一

题解:规律+数学

1、

2、

3、,则

4、,则不合法数据;

5、,则不合法数据;

6、,对于某一个位置i,可以推出应该交换的位置,其中不一定在范围内,不一定在范围内;

7、对于每个再完成上述操作后,放进vector

8、可以查询vector是否操作 ,当然直接询问;

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=200000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r;
ll x,y;
ll ans,cnt,flag,temp,X,Y;
ll b[N];
vector<int>v[M];
int main()
{
#ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout);
#endif//ios::sync_with_stdio(false);//cin.tie(0);//cout.tie(0);scanf("%d",&t);while(t--){scanf("%d%lld%lld",&n,&x,&y);X=Y=0;for(int i=1;i<=100000;i++)v[i].clear();for(int i=1;i<=n;i++){scanf("%lld",&b[i]);X+=i*b[i];Y+=i*b[i]*b[i];}//cout<<X<<" "<<Y<<endl;ll needx=x-X,needy=y-Y;ans=0;if(needx==0&&needy==0){//needx==0&&needy==0时,任意两个相同的元素都可以交换for(int i=1;i<=n;i++){ans+=v[b[i]].size();v[b[i]].push_back(i);}}else if(needx==0){ans=0;}else if(needy%needx==0){//needx和needy必须倍数关系//cout<<needx<<" "<<needy<<endl;ll need=needy/needx;//cout<<need<<endl;for(int i=1;i<=n;i++){if(need-2*b[i]&&1<=need-b[i]&&need-b[i]<=100000){int j=i-needx/(need-2*b[i]);vector<int>::iterator it=lower_bound(v[need-b[i]].begin(),v[need-b[i]].end(),j);//cout<<need-b[i]<<" "<<i-needx/(need-2*b[i])<<endl;if(it!=v[need-b[i]].end()&&*it==j){//it不能是end(),不然会段错误ans++;}}v[b[i]].push_back(i);}}cout<<ans<<endl;}#ifdef DEBUGprintf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif//cout << "Hello world!" << endl;return 0;
}

C++版本二

题解:

原博客

#include<bits/stdc++.h>
#define ll long long
#define Map map<ll,ll>::iterator
#define MAXN 100005
#define se second
using namespace std;
map<ll,ll>cnt;
ll X1,Y1,X2,Y2,a[MAXN];
int T,n;
int main(){cin>>T;while(T--){cnt.clear();scanf("%d%lld%lld",&n,&X1,&Y1);X2=Y2=0;for(ll i=1;i<=n;i++){scanf("%lld",&a[i]);cnt[a[i]]++;X2+=a[i]*i;Y2+=a[i]*a[i]*i;}ll dx=X2-X1,dy=Y2-Y1;if(dx==0){if(dy!=0){puts("0");continue;}ll ans=0;for(Map it=cnt.begin();it!=cnt.end();it++){ans+=((it->se)-1)*(it->se)/2;}printf("%lld\n",ans);continue;}if(dy%dx){puts("0");continue;}//cout<<dx<<" "<<dy<<endl;ll dt=dy/dx,ans=0;//cout<<dt<<endl;for(ll i=1;i<=n;i++){ll Aj=dt-a[i];if((Aj-a[i])==0)continue;ll j=(dx+(Aj-a[i])*i)/(Aj-a[i]);if(j<=i||j>n)continue;if(a[j]==Aj)ans++;}printf("%lld\n",ans);}
}

Element Swapping相关推荐

  1. 【数学】Element Swapping

    Element Swapping Time Limit: 1 Second Memory Limit: 65536 KB DreamGrid has an integer sequence a1,a2 ...

  2. 【2019浙江省赛 - B】Element Swapping(思维,数学)

    题干: DreamGrid has an integer sequence  and he likes it very much. Unfortunately, his naughty roommat ...

  3. The 16th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple

    Problem A Vertices in the Pocket 比赛地址:http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?proble ...

  4. 2021ccpc桂林小结

    2021ccpc桂林 D - Assumption is All You Need 一题只差痛失打铜,这题队友交给了我是我不争气 cf复现链接 题目 JB holds the belief that ...

  5. 2021第7届中国大学生程序设计竞赛CCPC桂林站, 签到题5题

    文章目录 A.Hero Named Magnus I. PTSD G. Occupy the Cities E. Buy and Delete D.Assumption is All You Need ...

  6. Cannot locate BeanDefinitionParser for element [scoped-proxy]

    指定使用 CGLIB 而不使用 JDK 生成代理对象:注意:此两个标签必须同时出现,不然会报:Cannot locate BeanDefinitionParser for element [scope ...

  7. Codeforces 903E Swapping Characters

    题目大意 考虑一个未知的长为 $n$($2\le n\le 5000$)由小写英文字母构成的字符串 $s$ .给出 $k$($1\le k\le 2500$,$nk\le 5000$)个字符串 $s_ ...

  8. element ui 图片控件 排序_JAVA全栈面试前端基础之四 Vue+Element框架快速开发

    本文主要包括以下4个内容 什么是ElementUI Vue 中引入ElementUI Vue+elementUI构建后台管理系统 开源项目推荐 1.什么是ElementUI 官网:https://el ...

  9. element ui需要引入样式吗_ElementUI 修改默认样式的几种办法

    ElementUI 是一套ui组件库,目前最新版本 react 和 vue 等主流框架都有支持.该库默认主题色是天蓝色,若用于项目开发,难免遇到要需求修改其默认样式的情况,本文就基于 react 和 ...

最新文章

  1. 这么全的数组去重,你怕不怕?
  2. OpenResty 概要及原理科普
  3. 1. ELK 之elasticsearch 简介、获取、安装
  4. Codeforces Round #703 (Div. 2) D . Max Median 二分 +思维
  5. android 底部动画,Android实现360手机助手底部的动画菜单
  6. 青蛙学Linux—Zabbix Agent的主动模式和被动模式
  7. IP trie树接口
  8. Spark DataSource API
  9. idea中 mybatis 的 mapper.xml 新建没有 头文件
  10. Linux fstab配置文件详解
  11. iOS 使用pods报错问题 pod --version
  12. 【推荐】数据治理资料合集
  13. 利用google map 查询全国城市坐标
  14. python 64式: 第28式、分布式锁与群组管理__3、tooz应用之分布式锁
  15. Linux OOM killer(转)
  16. python-优矿-期权合成期货策略
  17. win10安装账户卡住_简单几步解决win10安装卡死在用户名的问题
  18. systemd 简介
  19. onload 属性的作用
  20. 基于加密机制的WebService访问与通信安全

热门文章

  1. 外网ip怎么查_无公网IP的情况下,搞定群晖并实现远程Nas访问
  2. 北大计算机学院 教授 湖南人,北大湘籍教授邹恒甫简历
  3. 拾取物品怎么实现_Dynamo如何实现图层的效果
  4. closecmd python_如何在python中禁止控制台/ cmd错误消息
  5. d盘莫名其妙被占空间 win10_Win10不好用?你不可不知的Win10技巧
  6. docker 查看虚拟网卡_最简单的免费虚拟化方案:Hyper-V Server + Windows Admin Center
  7. python typing typescript_将 python 数据转化为 TypeScript 格式
  8. 绝地求生12月18日服务器信息,绝地求生12月18号几点更新维护完 2019绝地求生12月18日更新维护开服时间...
  9. shiny 发布部署到公开网站 ,供别人访问
  10. pytorch 实现线性回归