目录
1.C. Increasing Sequence with Fixed OR
2.C. Jellyfish and Green Apple
3.B. Jellyfish and Game
1.C. Increasing Sequence with Fixed OR
Problem - C - Codeforces
题目要求我们构造一个最长的序列,使得任意相邻两个元素按位或等于n,我们对n的二进制分析发现,最长序列就是n的二进制位中1位的个数,元素是依次将其中的某个1替换成0后大小。
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
#include<cstring>
#include<set>
#include<map>
#define int long long
#define TEST int T;cin>>T;while(T--)
#define lowbit(x) x&(-x)
using namespace std;
const int N = 1000010;
const int M = 1e9 + 7;
const int bit=63;
inline void solve() {
int n;cin>>n;vector<int>ans;for(int i=bit;i>=0;i--){if((n>>i)&1){int mask=~(1ll<<i);int k=n&mask;if(k!=0)ans.push_back(k);}}ans.push_back(n);cout<<ans.size()<<"\n";for(auto x:ans)cout<<x<<" ";cout<<"\n";
}
signed main() {TESTsolve();return 0;
}
2.C. Jellyfish and Green Apple
Problem - C - Codeforces
我们可以对部分或完整的苹果分成两个原来的两半,我们可以一直分,直到当前的苹果数量可以整除人数。
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
#include<cstring>
#include<map>
#define ll long long
#define TEST int T;cin>>T;while(T--)
#define lowbit(x) x&(-x)
using namespace std;
const int N = 1000010;
const int M = 1e8 + 7;
int base1 = 131, base2 = 13311;
void solve() {ll n, m;cin >> n >> m;if (n % m == 0) {cout << "0\n";return ;}if ((n<<30)%m) {cout << "-1\n";return;}n%=m;ll ans=0;while(1){ans+=n;n=(n*2)%m;if(n==0) {cout<<ans<<"\n";return ;}}
}
int main() {TESTsolve();return 0;
}
3.B. Jellyfish and Game
Problem - B - Codeforces
当k为奇数时,先手多操作一步。
当k为偶数时,先手和后手操作相同,但后手更有利。
由于二者都是按照最优方案,所以操作的元素肯定是各自数组里面的最大值和最小值。
按照最优贪心即可得到答案。
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
#include<cstring>
#include<map>
#define ll long long
#define TEST int T;cin>>T;while(T--)
#define lowbit(x) x&(-x)
using namespace std;
const int N = 1000010;
const int M = 1e8 + 7;
int base1 = 131, base2 = 13311;
void solve() {ll n, m, k;cin >> n >> m >> k;ll nmin = 1e9, nmax = -1, mmin = 1e9, mmax = -1, sum = 0;for (ll i = 1, x; i <= n; i++) {cin >> x;nmin = min(nmin, x);nmax = max(nmax, x);sum += x;}for (ll i = 1, x; i <= m; i++) {cin >> x;mmin = min(mmin, x);mmax = max(mmax, x);}if (k & 1) {sum += max(0ll, mmax - nmin);} else {if (mmax - nmin > 0) {sum += max(0ll, mmax - nmin);sum -= max(0ll, max(mmax, nmax) - min(nmin,mmin));} else {sum -= max(0ll, nmax - mmin);}}cout<<sum<<"\n";
}
int main() {TESTsolve();return 0;
}