#include<iostream>
#include<iomanip>
#include<cmath>
#include<string.h>
#include<string>
#include <ctime>
#include<cstdlib>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
//p3156
//查询,可以考虑用顺序表,用动态数组
// int n,m;
// int main()
// {
// cin>>n>>m;
// int *arr = new int[n];//开辟空间;
// for(int i =0;i<n;i++)cin>>arr[i];//初始化
// for(int i =0;i<m;i++)
// {
// int find;
// cin>>find;//输入查询位置
// cout<<arr[find-1]<<endl;//输出
// }
// }
// p3613
//查询,顺序表
//1.先开辟寄包柜的空间
//2.格子的空间动态存储
// int n,q;
// int x,y,k;
// int main()
// {
// cin>>n>>q;
// vector<vector<int>> arr(n+1);//开辟一维空间
// for(int i =0;i<q;i++)
// {
// int opt;
// cin>>opt;
// if(opt==1)//存
// {
// cin>>x>>y>>k;
// if(arr[x].size()<y+1)//空间不够
// arr[x].resize(y+1);
// arr[x][y] = k;
// }
// else//读取
// {
// cin>>x>>y;
// cout<<arr[x][y]<<endl;
// }
// }
// }
// p1449
// char ch;//存入字符
// int s = 0;//存入数据
// int x,y;//前两个栈顶元素
// stack<int> n;//建立栈
// int main()
// {
// do
// {
// cin>>ch;
// if(ch-'0'>=0&&ch-'0'<=9)//判断数字
// {
// s = s*10+ch-'0';//存数字
// }
// else if(ch=='.')
// {
// n.push(s);//入栈
// s = 0;//清理
// }
// else if(ch!='@')
// {
// x = n.top();n.pop();
// y = n.top();n.pop();
// if(ch=='+')x = y+x;
// else if(ch=='-')x = y-x;
// else if(ch=='*')x = y*x;
// else if(ch=='/')x = y/x;
// n.push(x);
// }
// } while (ch!='@');
// cout<<n.top();
// }
//p1996
// int n,m;
// int ptr = 1;//记录位置
// queue<int> q;
// int main()
// {
// cin>>n>>m;
// for(int i =1;i<=n;i++)q.push(i);//初始化入队
// while(!q.empty())//有剩余
// {
// int item = q.front();//记录元素
// q.pop();//出队
// if(ptr==m)//数到数字m
// {
// ptr =1;//清理
// cout<<item<<' ';
// }
// else
// {
// q.push(item);//入队
// ptr++;
// }
// }
// }
// p1160
// int n,k,p,m,tot;
// int first;//记录首位置
// int inde[100005];//记录每个位置节点编号
// struct node
// {
// int key;
// int prior,next;
// }arr[100005];//开辟空间
// void Insert_next(int x,int y)
// {
// int now = inde[x];//获得该节点
// tot++;
// arr[tot].key = y;arr[tot].prior = now;arr[tot].next = arr[now].next;
// if(arr[now].next!=0)arr[arr[now].next].prior = tot;
// arr[now].next = tot;
// inde[y] = tot;
// }
// void Insert_prior(int x,int y)
// {
// int now = inde[x];//获得该节点
// tot++;
// arr[tot].key = y;arr[tot].prior = arr[now].prior;arr[tot].next = now;
// arr[arr[now].prior].next = tot;
// arr[now].prior = tot;
// inde[y] = tot;
// }
// void delete_item(int x)
// {
// int now = inde[x];
// if(now==0)return;
// int pr = arr[now].prior,nxt = arr[now].next;
// arr[pr].next = nxt;
// arr[nxt].prior = pr;
// inde[x] = 0;
// }
// int main()
// {
// cin>>n;
// Insert_next(0,1);
// for(int i =2;i<=n;i++)
// {
// cin>>k>>p;
// if(p==0)Insert_prior(k,i);//插入左边
// else Insert_next(k,i);//插入右边
// }
// cin>>m;
// for(int i =0;i<m;i++)
// {
// int item;
// cin>>item;
// delete_item(item);//删除
// }
// //找到第一个元素
// int now = arr[0].next;
// while(now)
// {
// cout<<arr[now].key<<' ';
// now = arr[now].next;
// }
// }
// p1540
// 队列
// queue<int> q;//创建队列
// int n,m;
// int my_count,ans;
// int arr[100005];
// int main()
// {
// cin>>n>>m;
// for(int i =0;i<m;i++)
// {
// int item;
// cin>>item;//单词
// if(arr[item])continue;//内存有
// if(my_count<n)//没有满
// {
// q.push(item);
// my_count++;
// ans++;//入栈
// arr[item] = 1;
// }
// else
// {
// q.push(item);
// arr[q.front()] = 0;
// arr[item] = 1;
// q.pop();//出栈
// ans++;//入栈
// }
// }
// cout<<ans;
// }
// p2058
//队列:存24h内的船(70分,明天去看错点)
// long long n,ans;
// long long arr[100000];//计数数组
// struct LNode
// {
// long long t,k;//时间
// vector<long long>con;//国家
// };
// queue <LNode> q;//创建列队
// int main()
// {
// LNode fir;
// cin>>n;
// vector<LNode>a(n);//开辟数组
// for(long long i =0;i<n;i++)
// {
// cin>>a[i].t>>a[i].k;
// for(long long j =0;j<a[i].k;j++)
// {
// a[i].con.resize(a[i].k);//开辟空间
// cin>>a[i].con[j];//输入国家
// if(arr[a[i].con[j]]==0)ans++;//判断新国家
// arr[a[i].con[j]]++;
// }
// q.push(a[i]);//入队
// //判断时间
// while(a[i].t-q.front().t>=86400)//超过时间
// {
// fir = q.front();//首队时间
// q.pop();//弹出第一个
// for(long long j =0;j<fir.k;j++)
// {
// if(--arr[fir.con[j]]==0)ans--;
// }
// }
// cout<<ans<<endl;
// }
// }
//p1241(56分)看不懂要求了^-^
//栈
// stack<char> S;//建立栈
// string arr;
// bool is_bal(string a,int len)
// {
// if(len==0)return true;//空串
// for(int i =0;i<len;i++)
// {
// if(a[i]=='('||a[i]=='[')
// {
// S.push(a[i]);//压如栈
// }
// else
// {
// if(S.empty())return false;
// char a = S.top();//头
// if(arr[i]==')'&&a!='(')return false;
// else if(arr[i]==']'&&a!='[')return false;
// }
// }
// return S.empty();
// }
// void print_s(string a,int len)
// {
// for(int i =0;i<len;i++)
// {
// char ch_1 = a[i],ch_2 = a[i+1];//两个元素
// if(ch_1=='('&&ch_2==')')
// {
// cout<<"()";
// i++;
// }
// else if(ch_1=='['&&ch_2==']')
// {
// cout<<"[]";
// i++;
// }
// else if(ch_1=='('||ch_1==')')cout<<"()";
// else if(ch_1=='['||ch_1==']')cout<<"[]";
// }
// }
// int main()
// {
// cin>>arr;//输入字符
// int len = arr.length();
// if(is_bal(arr,len))cout<<arr;//原封不动输出
// else
// {
// //输出成对括号
// print_s(arr,len);
// }
// }
//p4387
// 将输入序列按顺序入栈
// 如果出现对应值出栈,没有继续入栈
// 最后如果栈空则正确
// (这道题很简单啊!!!)
// int q,n;
// int arr[1000000];
// int brr[1000000];
// int main()
// {
// cin>>q;
// for(int i =0;i<q;i++)
// {
// cin>>n;
// stack<int>S;//建立栈
// for(int j =0;j<n;j++)cin>>arr[j];
// for(int j =0;j<n;j++)cin>>brr[j];//记录数据
// int k =0;//记录目标
// for(int j =0;j<n;j++)
// {
// S.push(arr[j]);//先存数据
// while(!S.empty()&&S.top()==brr[k])//出现目标
// {
// k++;
// S.pop();//出栈
// }
// }
// if(S.empty())cout<<"Yes"<<endl;
// else cout<<"No"<<endl;
// }
// }
//p2234
// 1.计数数组:把之前的数据全放入
// 2.找值:如果计数数组的值大于1,则是0,否则,从最大最小值进行二分查找
long long n,ans;
int arr[8000000];
long long Min,Max;
int find(int x)
{
arr[x] = 1;
if(x<Min)return Min-x;
if(x>Max)return x-Max;//考虑特殊情况
long long i =1;
while(arr[x+i]==0&&arr[x-i]==0)i++;//试一试
return i;
}
int main()
{
cin>>n;
long long item;
cin>>item;//第一个特殊考虑
long long an = item+1000000;
arr[an] = 1;
ans = abs(item);
Min = Max = an;//初始化最大最小
for(int i =1;i<n;i++)
{
cin>>item;
an = item+1000000;
if(arr[an]>0)continue;//不用进行运算
else
{
//找到绝对值最小的位置
ans +=find(an);
if(an>Max)Max = an;
if(an<Min)Min = an;
}
}
cout<<ans;
}
洛谷——《线性表》
于 2024-02-19 21:58:53 首次发布