- F. Yet Another Problem About Pairs Satisfying an Inequality
- code
- G. Good Key, Bad Key
- code
题意:给一个长度为
n
n
n的数组
a
[
n
]
a[n]
a[n],求满足
a
i
<
i
<
a
j
<
j
a_i
记录这个数组每个元素是否小于下标的情况
for(int i=1;i<=n;i++)s[i]=i>a[i];
对其进行前缀和处理
for(int i=1;i<=n;i++)s[i]+=s[i-1];
然后从前往后枚举每一个 a i a_i ai
for(int i=1;i<=n;i++) if(i>a[i])res+=s[a[i]-1];code
#includeG. Good Key, Bad Keyusing namespace std; const double pi = acos(-1); const double eps=1e-5; #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 #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;}//最低位1及其后面的0构成的数值 void read(__int128 &x){char c = getchar();while (c != '-' & (c < '0' | c > '9'))c = getchar();if (c == '-') {c = getchar();while (c >= '0' & c <= '9') {x = x * 10 + c - '0';c = getchar();}x = 0 - x;} else {while (c >= '0' & c <= '9') {x = x * 10 + c - '0';c = getchar();}}} void out(__int128 x) {string c = "";while (x) {c += x % 10 + '0';x /= 10;}reverse(c.begin(), c.end());cout << c << endl;} 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 _; int n; const int N=2e5+10; int a[N]; int s[N]; void solve() { cin>>n; for(int i=1;i<=n;i++)cin>>a[i]; for(int i=1;i<=n;i++)s[i]=i>a[i]; for(int i=1;i<=n;i++)s[i]+=s[i-1]; int res=0; for(int i=1;i<=n;i++) { if(i>a[i])res+=s[a[i]-1]; } cout< io; cin>>_; while(_--) solve(); return 0; }
题意:
一个人刚开始的金币是0,有
n
n
n个箱子可以按照顺序打开,箱子内的金币数量是
a
i
a_i
ai,打开箱子需要钥匙,钥匙一共有两种,一种是good需要花费
k
k
k购买,一种是bad是免费的,但是会将包括该箱子在内的其后边的所有箱子的金币除以2,人的金币可以为负数,问这个人在打开
n
n
n个箱子后的最大金币数。
思路:
动态规划,题目描述的所有
a
i
<
=
2
31
a_i<=2^{31}
ai<=231,虽然箱子的个数
1
0
5
10^{5}
105,但是最每个箱子最多只能衰减不超过31次。
f[i][j]表示第i个箱子衰退j次后所获得金币的最大值
把我们就得先预处理每个箱子所含有的金币数的衰退状态res[i][j]
for(int i=1;i<=n;i++)
{
int m=a[i];
int cnt=0;
while(m)
{
res[i][++cnt]=m/2;
m/=2;
}
}
紧接着就是动态规划的过程,详细看code
code#includeusing namespace std; const double pi = acos(-1); const double eps=1e-5; #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 #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;}//最低位1及其后面的0构成的数值 void read(__int128 &x){char c = getchar();while (c != '-' & (c < '0' | c > '9'))c = getchar();if (c == '-') {c = getchar();while (c >= '0' & c <= '9') {x = x * 10 + c - '0';c = getchar();}x = 0 - x;} else {while (c >= '0' & c <= '9') {x = x * 10 + c - '0';c = getchar();}}} void out(__int128 x) {string c = "";while (x) {c += x % 10 + '0';x /= 10;}reverse(c.begin(), c.end());cout << c << endl;} 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 _; int n,k; const int N=2e5+10; int a[N]; int f[N][35]; int res[N][35]; void solve() { cin>>n>>k; for(int i=1;i<=n;i++)cin>>a[i]; for(int i=0;i<=n;i++) for(int j=0;j<=31;j++)f[i][j]=res[i][j]=0; for(int i=1;i<=n;i++)//预处理衰退状态 { int m=a[i]; int cnt=0; while(m) { res[i][++cnt]=m/2; m/=2; } } f[1][0]=a[1]-k;//0状态表示没有衰退,也就是good f[1][1]=a[1]/2; for(int i=2;i<=n;i++) { f[i][0]=f[i-1][0]+a[i]-k;//good for(int j=1;j<=min(31ll,i);j++) f[i][j]=f[i-1][j]+res[i][j]-k;//good for(int j=1;j<=min(31ll,i);j++) f[i][j]=max(f[i][j],f[i-1][j-1]+res[i][j]);//bad } int res=-ll_INF; for(int i=0;i<=30;i++) res=max(res,f[n][i]); for(int i=n;i>=30;i--)//加特判 res=max(res,f[i][30]); cout< io; cin>>_; while(_--) solve(); return 0; }



