A. Add Candies
题解:
让所有数被加成 n * (n + 1) / 2即可
代码:
/*
* @Author : Nightmare
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define PII pair<int,int>
#define ls 2 * rt
#define rs 2 * rt + 1
#define gcd(a,b) __gcd(a,b)
#define eps 1e-6
#define lowbit(x) (x&(-x))
#define N 105
#define M 10005
#define mod 1000000007
#define inf 0x3f3f3f3f
void solve(){
int n; cin >> n;
cout << n << '\n';
for(int i = 1 ; i <= n ; i ++) cout << i << ' '; cout << '\n';
}
signed main(){
#ifndef ONLINE_JUDGE
freopen("D:\\in.txt", "r", stdin);
#endif
int T; cin >> T; while(T--) solve();
#ifndef ONLINE_JUDGE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
B. Numbers Box
题解:
首先如果有0,那么不管有多少个负数都是可以全变为正数的,否则判断负数的个数为奇数还是偶数即可。
代码:
/*
* @Author : Nightmare
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define PII pair<int,int>
#define ls 2 * rt
#define rs 2 * rt + 1
#define gcd(a,b) __gcd(a,b)
#define eps 1e-6
#define lowbit(x) (x&(-x))
#define N 105
#define M 10005
#define mod 1000000007
#define inf 0x3f3f3f3f
int n, m, a[N][N];
void solve(){
scanf("%d %d", &n, &m); bool zero = false; int fu = 0;
for(int i = 1 ; i <= n ; i ++)
for(int j = 1 ; j <= m ; j ++)
scanf("%d", &a[i][j]), zero |= (a[i][j] == 0), fu += (a[i][j] < 0 ? 1 : 0);
int sum = 0, mi = 1e9;
for(int i = 1 ; i <= n ; i ++)
for(int j = 1 ; j <= m ; j ++)
sum += abs(a[i][j]), mi = min(mi, abs(a[i][j]));
if(zero) cout << sum << '\n';
else if(fu & 1) cout << sum - 2 * mi << '\n';
else cout << sum << '\n';
}
signed main(){
#ifndef ONLINE_JUDGE
freopen("D:\\in.txt", "r", stdin);
#endif
int T; cin >> T; while(T--) solve();
#ifndef ONLINE_JUDGE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
C. Knapsack
题解:
首先筛去 > w的数,其实如果有数在(w + 1) / 2 ≤ x ≤ w直接输出这个数。
然后就将所有 < (w + 1) / 2 的数筛选出来了,因为这些数中最大的数也就(w + 1) / 2 - 1是小于等于 w - (w + 1) / 2的,所以直接加到满足范围即可,不会超出 w 的范围。
代码:
/*
* @Author : Nightmare
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define PII pair<int,int>
#define ls 2 * rt
#define rs 2 * rt + 1
#define gcd(a,b) __gcd(a,b)
#define eps 1e-6
#define lowbit(x) (x&(-x))
#define N 200005
#define M 10005
#define mod 1000000007
#define inf 0x3f3f3f3f
int n, w, a[N];
void solve(){
vector<PII> b;
cin >> n >> w;
for(int i = 1 ; i <= n ; i ++) cin >> a[i];
for(int i = 1 ; i <= n ; i ++){
if(a[i] < (w + 1) / 2) b.emplace_back(a[i], i);
else if(a[i] <= w){ cout << 1 << '\n' << i << '\n'; return ; }
}
int sum = 0; vector<int> ans;
for(auto &cur : b){
if(sum < (w + 1) / 2){
sum += cur.first;
ans.push_back(cur.second);
}else{
break;
}
}
if(sum < (w + 1) / 2){ puts("-1"); return ; }
cout << ans.size() << '\n';
for(int i = 0 ; i < ans.size() ; i ++) cout << ans[i] << (i == ans.size() - 1 ? "\n" : " ");
}
signed main(){
#ifndef ONLINE_JUDGE
freopen("D:\\in.txt", "r", stdin);
#endif
int T; cin >> T; while(T--) solve();
#ifndef ONLINE_JUDGE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
D. Catching Cheaters
题解:
设dp[i][j]表示以S[i]结束的子串和以T[j]结束的子串,
4
∗
L
C
S
−
∣
C
∣
−
∣
D
∣
4 * LCS - |C| - |D|
4∗LCS−∣C∣−∣D∣的最大值
如果S[i] == T[j],LCS增加1,|C|增加1,|D|增加1,增加的贡献为2,则
d
p
[
i
]
[
j
]
=
m
a
x
(
d
p
[
i
−
1
]
[
j
−
1
]
+
2
,
0
)
dp[i][j] = max(dp[i - 1][j - 1] + 2, 0)
dp[i][j]=max(dp[i−1][j−1]+2,0)
否则LCS不变,|C|增加1 或者 |D|增加1,增加的贡献为-1,则
d
p
[
i
]
[
j
]
=
m
a
x
(
m
a
x
(
d
p
[
i
−
1
]
[
j
]
,
d
p
[
i
]
[
j
−
1
]
)
−
1
,
0
)
dp[i][j] = max(max(dp[i - 1][j], dp[i][j - 1]) - 1, 0)
dp[i][j]=max(max(dp[i−1][j],dp[i][j−1])−1,0)
容易发现,如果dp[i][j] = 0,可以表示为我们舍弃前面的字符,重新进行选择,也就是对应选择子串了。
代码:
/*
* @Author : Nightmare
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define PII pair<int,int>
#define ls 2 * rt
#define rs 2 * rt + 1
#define gcd(a,b) __gcd(a,b)
#define eps 1e-6
#define lowbit(x) (x&(-x))
#define N 5005
#define M 10005
#define mod 1000000007
#define inf 0x3f3f3f3f
char s[N], t[N]; int n, m, ans, dp[N][N];
void solve(){
scanf("%d %d", &n, &m);
scanf("%s %s", s, t);
for(int i = 1 ; i <= n ; i ++){
for(int j = 1 ; j <= m ; j ++){
if(s[i - 1] == t[j - 1]) dp[i][j] = max(dp[i - 1][j - 1] + 2, 0);
else dp[i][j] = max(max(dp[i - 1][j], dp[i][j - 1]) - 1, 0);
ans = max(ans, dp[i][j]);
}
}
cout << ans << '\n';
}
signed main(){
#ifndef ONLINE_JUDGE
freopen("D:\\in.txt", "r", stdin);
#endif
solve();
#ifndef ONLINE_JUDGE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
E. Xor Tree
题解:
考虑两个集合S0和S1,表示以当前二进制位为最高有效位的两个集合,容易发现,如果集合S0的数量 >= 2,那么S0中的数选择的最小异或一定是在S0集合中选出的,S1同理,那么这样两个集合中的数就会没有联系,相当于没有连边。
我们可以让其中一个集合中的数的个数减少到1个,让它强制与另外一个集合产生联系。
接下来就只需要递归计算出让某个儿子的集合中相互连边的最小操作数即可。
代码:
/*
* @Author : Nightmare
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define PII pair<int,int>
#define ls 2 * rt
#define rs 2 * rt + 1
#define gcd(a,b) __gcd(a,b)
#define eps 1e-6
#define lowbit(x) (x&(-x))
#define N 200005
#define M 200005 * 30
#define mod 1000000007
#define inf 0x3f3f3f3f
int n, tot, a[N], ch[M][2], sz[M];
void insert(int x){
int rt = 0;
for(int i = 29 ; i >= 0 ; i --){
int k = (x >> i) & 1;
if(!ch[rt][k]) ch[rt][k] = ++tot;
rt = ch[rt][k];
++ sz[rt];
}
}
int dfs(int rt){
int lson = ch[rt][0], rson = ch[rt][1];
if(lson && rson) return min(sz[lson] - 1 + dfs(rson), sz[rson] - 1 + dfs(lson));
// 将lson集合或者rson集合中的数删除至一个,另外一个集合相互存在关系
if(lson) return dfs(lson); // 计算出让lson集合中的数相互存在关系,需要执行删数操作的最小值
if(rson) return dfs(rson); // 同理计算出rson集合
return 0;
}
void solve(){
scanf("%d", &n);
for(int i = 1 ; i <= n ; i ++) scanf("%d", &a[i]), insert(a[i]);
printf("%d\n", dfs(0));
}
signed main(){
#ifndef ONLINE_JUDGE
freopen("D:\\in.txt", "r", stdin);
#endif
solve();
#ifndef ONLINE_JUDGE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}