A. Optimal Path
思路: 先走到最右端再往下走
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
void solve()
{
cin>>n>>m;
int sum=0;
sum+=m*(1+m)/2;
sum+=n*m+n*(n-1)*m/2;
cout<<sum-m<<endl;
}
signed main()
{
io;
cin>>_;
while(_--)
solve();
return 0;
}
B. Palindromic Numbers
思路:第一位小于9的补成 9…99,否则补成1000…1 高精度减法
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
vector<int>sub(vector<int>& A, vector<int>& B)
{
vector<int>C;
int t = 0;
for (int i = 0; i < A.size(); i++)
{
t = A[i] - t;
if (i < B.size())t -= B[i];
C.push_back((t + 10) % 10);
if (t < 0)t = 1;
else t = 0;
}
while (C.size() > 1 && C.back() == 0)C.pop_back();
return C;
}
void solve()
{
string a,b;
cin>>n;
cin>>a;
if(a[0]=='9')
{
for(int i=0;i<=n;i++)b+='1';
}
else
{
for(int i=1;i<=n;i++)b+='9';
}
vector<int>A,B,C;
for (int i = n - 1; i >= 0; i--)A.push_back(a[i] - '0');
for (int i = b.size()-1 ; i >= 0; i--)B.push_back(b[i] - '0');
C=sub(B,A);
for(int i=C.size()-1;i>=0;i--)cout<<C[i];
cout<<endl;
}
signed main()
{
io;
cin>>_;
while(_--)
solve();
return 0;
}
C. Helping the Nature
思路:对于差分数组, 操作一:d1减1,di+1加1 操作二:di加1 操作三:d1 加1 让差分数组除了第一位变成0通过操作1和2 ,然后对差分数组第一位进行操作3
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
const int N=200010;
int a[N],b[N];
void solve()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
b[i]=a[i]-a[i-1];
}
int sum=0;
for(int i=2;i<=n;i++)
{
if(b[i]>0)
{
sum+=b[i];
b[i]=0;
}
else if(b[i]<0)
{
sum-=b[i];
b[1]+=b[i];
b[i]=0;
}
}
sum+=abs(b[1]);
cout<<sum<<endl;
}
signed main()
{
io;
cin>>_;
while(_--)
solve();
return 0;
}
D. River Locks
思路:二分+前缀和 所有水闸都打开不能满足,则输出-1 二分答案
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
const int N=2e5+10;
int a[N];
int s[N];
bool check(int cnt,int x)
{
return x*cnt>=s[n];
}
void solve()
{
cin>>n;
int maxv=0;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)
{
s[i]=s[i-1]+a[i];
maxv=max(maxv,(int)ceil(s[i]*1.0/i));
}
int q;
cin>>q;
while(q--)
{
int x;
cin>>x;
if(x<maxv)
{
cout<<-1<<endl;
continue;
}
else
{
int l=1,r=n;
while(l<r)
{
int mid=l+r>>1;
if(check(mid,x))r=mid;
else l=mid+1;
}
cout<<r<<endl;
}
}
}
signed main()
{
io;
solve();
return 0;
}
|