按要求if else 即可
/*
悲观看待成功,乐观看待失败。
author:leimingze
*/
#include
using namespace std;
const double pi = acos(-1);
const double eps=1e-7;
const int base=131;
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#define x first
#define y second
#define int long long
#define lb long double
#define pb push_back
#define endl '\n'//交互题删掉此
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define rep(i,x,n) for(int i=x;i<=n;i++)
#define dwn(i,n,x) for(int i=n;i>=x;i--)
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
int Mod(int a,int mod){return (a%mod+mod)%mod;}
int lowbit(int x){return x&-x;}//最低位1及其后面的0构成的数值
int qmi(int a, int k, int p){int res = 1 % p;while (k){if (k & 1) res = Mod(res * a , p);a = Mod(a * a , p);k >>= 1;}return res;}
int inv(int a,int mod){return qmi(a,mod-2,mod);}
int lcm(int a,int b){return a*b/__gcd(a,b);}
int n;
void solve()
{
int a,b;
cin>>a>>b;
if(b>0)cout<<abs(a)<<endl;
else cout<<-abs(a)<<endl;
}
signed main()
{
io;
int _;_=1;
//cin>>_;
while(_--)solve();
return 0;
}
高精度加法,只不过对于第i
位的是i+1
进制
/*
悲观看待成功,乐观看待失败。
author:leimingze
*/
#include
using namespace std;
const double pi = acos(-1);
const double eps=1e-7;
const int base=131;
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#define x first
#define y second
#define int long long
#define lb long double
#define pb push_back
#define endl '\n'//交互题删掉此
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define rep(i,x,n) for(int i=x;i<=n;i++)
#define dwn(i,n,x) for(int i=n;i>=x;i--)
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
int Mod(int a,int mod){return (a%mod+mod)%mod;}
int lowbit(int x){return x&-x;}//最低位1及其后面的0构成的数值
int qmi(int a, int k, int p){int res = 1 % p;while (k){if (k & 1) res = Mod(res * a , p);a = Mod(a * a , p);k >>= 1;}return res;}
int inv(int a,int mod){return qmi(a,mod-2,mod);}
int lcm(int a,int b){return a*b/__gcd(a,b);}
int n,m;
const int N=2e5+10;
int a[N],b[N];
void solve()
{
cin>>n>>m;
rep(i,1,n)cin>>a[i];
rep(i,1,m)cin>>b[i];
vector<int>A,B,C;
dwn(i,n,1)A.pb(a[i]);
dwn(i,m,1)B.pb(b[i]);
int t=0;
for(int i=0;i<B.size()||i<A.size();i++)
{
if(i<A.size())t+=A[i];
if(i<B.size())t+=B[i];
C.pb(t%(i+2)),t/=(i+2);
}
int maxv=max(A.size(),B.size());
while(t)C.pb(t%(maxv+1)),t/=(maxv+1);
while(C.size()>1&&C.back()==0)C.pop_back();
reverse(all(C));
for(auto x:C)cout<<x<<' ';
cout<<endl;
}
signed main()
{
io;
int _;_=1;
//cin>>_;
while(_--)solve();
return 0;
}
这一题相对较难。tag:双指针+贪心
假设最优区间值域范围是[l,r]
,设v
是小于l
的个数,u
是大于r
的个数
那么有两种情况:
l
的的元素进行一次操作,操作次数+v
;把大于r
的元素进行一次操作,操作次数+v+u
r
的元素进行一次操作,操作次数+u
,把小于l
的的元素进行一次操作,操作次数+v+u
两种情况取min(u,v)+u+v
最后双指针更新一下res
/*
悲观看待成功,乐观看待失败。
author:leimingze
*/
#include
using namespace std;
const double pi = acos(-1);
const double eps=1e-7;
const int base=131;
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#define x first
#define y second
#define int long long
#define lb long double
#define pb push_back
#define endl '\n'//交互题删掉此
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define rep(i,x,n) for(int i=x;i<=n;i++)
#define dwn(i,n,x) for(int i=n;i>=x;i--)
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
int Mod(int a,int mod){return (a%mod+mod)%mod;}
int lowbit(int x){return x&-x;}//最低位1及其后面的0构成的数值
int qmi(int a, int k, int p){int res = 1 % p;while (k){if (k & 1) res = Mod(res * a , p);a = Mod(a * a , p);k >>= 1;}return res;}
int inv(int a,int mod){return qmi(a,mod-2,mod);}
int lcm(int a,int b){return a*b/__gcd(a,b);}
int n,k;
const int N=1e5+10;
int a[N];
void solve()
{
cin>>n>>k;
rep(i,1,n)cin>>a[i];
sort(a+1,a+1+n);
int j=1;
int res=ll_INF;
rep(i,1,min(n,k+1))
{
j=max(i,j);
while((i-1)+(n-j)+min(i-1,n-j)>k)j++;
res=min(res,a[j]-a[i]);
}
cout<<res<<endl;
}
signed main()
{
io;
int _;_=1;
//cin>>_;
while(_--)solve();
return 0;
}
模拟题,二分找一下在哪个区间,每个区间分成四段模拟一下
屎山代码
/*
悲观看待成功,乐观看待失败。
author:leimingze
*/
#include
using namespace std;
const double pi = acos(-1);
const double eps=1e-7;
const int base=131;
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#define x first
#define y second
#define int long long
#define lb long double
#define pb push_back
#define endl '\n'//交互题删掉此
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define rep(i,x,n) for(int i=x;i<=n;i++)
#define dwn(i,n,x) for(int i=n;i>=x;i--)
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
int Mod(int a,int mod){return (a%mod+mod)%mod;}
int lowbit(int x){return x&-x;}//最低位1及其后面的0构成的数值
int qmi(int a, int k, int p){int res = 1 % p;while (k){if (k & 1) res = Mod(res * a , p);a = Mod(a * a , p);k >>= 1;}return res;}
int inv(int a,int mod){return qmi(a,mod-2,mod);}
int lcm(int a,int b){return a*b/__gcd(a,b);}
int n;
void solve()
{
cin>>n;
while(n--)
{
int x;
cin>>x;
int l=1,r=2e9;
while(l<r)
{
int mid=l+r>>1;
if(mid*(2*mid-1)>=x)r=mid;
else l=mid+1;
}
int sum=r*(2*r-1)-(r-1)*(2*(r-1)-1);//该周期个数
int now=x-(r-1)*(2*(r-1)-1);//该周期的第几个
int mid1=1+r*(2*r-1)-(r-1)*(2*(r-1)-1)>>1;
int mid2=1+mid1>>1;
int mid3=r*(2*r-1)-(r-1)*(2*(r-1)-1)+mid1>>1;
int h=mid2-1;
if(now<=mid2)cout<<now-1<<endl;
else if(now<=mid1)
{
now-=mid2;
cout<<h-now<<endl;
}
else if(now<=mid3)
{
now-=mid1;
cout<<-now<<endl;
}
else if(now<=r*(2*r-1)-(r-1)*(2*(r-1)-1))
{
now-=mid3;
cout<<now-h<<endl;
}
}
}
signed main()
{
io;
int _;_=1;
//cin>>_;
while(_--)solve();
return 0;
}