A Count Task
队友过的
血泪教训:
子串 substrings:在严格的一段区间里连续
子序列Subsequence:从区间里挑出来的部分
会有爆掉的风险,所以决定以后在乘法、除法前都乘个ll
int num[maxn];
int main() {
IOS;
int t;
cin >> t;
while(t--){
re(num);
string s;
cin >> s;
ll len = s.size();
ll cnt = 0, res = 1;
for (int i = 1; i < len; ++i){
if(s[i] == s[i - 1]){
++res;
}
else {
num[cnt++] = res;
res = 1;
}
}
num[cnt++] = res;
ll ans = 0;
for (int i = 0; i < cnt; ++i){
ans += 1ll * num[i] * (num[i] + 1) / 2;
}
cout << ans << endl;
}
return 0;
}
The puzzle
信誓旦旦绝对是求逆序对,又不会求,准备摆了;队友说不要回回想着逆序对,队友过的;我有个很严重的问题就是喜欢套板子,什么都喜欢归个类再做,这点必须要改,这个想法在后面的CodeForces 437C中也让我吃了大亏。
!这题我做过,我说那么眼熟…还是不会
先学个逆序对,免得下次看什么都是逆序对…
换的是下标
目的:把这个数放在该放的位置上
1.
int a[maxn], b[maxn];
int main() {
IOS;
int t;
cin >> t;
while(t--){
int n;
re(a);
re(b);
cin >> n;
for (int i = 1; i <= n; ++i){
cin >> a[i];
b[a[i]] = i;
}
int ans = 0;
for(int i = 1; i <= n; ++i){
if(a[i] != i){
int t1 = a[i], t2 = b[i];
swap(a[i], a[b[i]]);
b[i] = i;
b[t1] = t2;
// for (int i = 1; i <= n; ++i){
// cout << b[i] << " ";
// }
++ans;
}
}
cout << ans << endl;
}
return 0;
}
while(t--){
int n;
re(a);
re(b);
cin >> n;
for (int i = 1; i <= n; ++i){
cin >> a[i];
b[a[i]] = i;
}
int ans = 0;
for(int i = 1; i <= n; ++i){
while(b[i] != i){
swap(b[i], b[b[i]]);
++ans;
}
}
cout << ans << endl;
}
分治,归并,递归
#include <iostream>
using namespace std;
const int N = 1e6 + 10;
int cmp[N];
long long res = 0;
int q[N];
void merge_sort(int l, int r)
{
if (l >= r) return;
int mid = l + r >> 1;
merge_sort(l, mid), merge_sort(mid + 1, r);
int i = l, j = mid + 1,k = 0;
while (i <= mid && j <= r){
if (q[i] <= q[j]){
cmp[k] = q[i];
++k, ++i;
}
else{
res += mid - i + 1;
cmp[k] = q[j];
++k, ++j;
}
}
while (i <= mid) {
cmp[k] = q[i];
++k, ++i;
}
while (j <= r) {
cmp[k] = q[j];
++k, ++j;
}
for (int i = l, j = 0; i <= r; i ++, j ++) q[i] = cmp[j];
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i ++) scanf("%d", &q[i]);
merge_sort(0, n - 1);
cout << res << endl;
return 0;
}
Overflow
物理题
就离谱,我俩一直调,最后也不知道咋对的,反正就过了
cin还会t掉
题解
当时过的
int n;
scanf("%d", &n);
double x, p;
double m = 0,m1 = 0,m2 = 0;
for (int i = 0; i < n; ++i) {
scanf("%lf%lf", &x, &p);
m1 = x * x * x;
m2 = x * x * x * p;
if(p >= 1)
m += m1;
else
m += m2;
}
double s, h, v;
scanf("%lf%lf%lf", &s, &h, &v);
double ans = v / s;
ans += m / s;
if(ans >= h)
ans = h;
printf("%.2lf\n", ans);
}
return 0;
}
The Child and Toy
一看题,最小生成树!马上去写,写完调了几次不对,重新考虑了一下,嗯是拓扑排序,又去想拓扑排序;时间不够了,去调Overflow
下来一看,是贪心…
原来我一直没读懂题…我又跳读了orz
又理解成连接的边的权了
虽然但是还是想复杂了,考虑的是建图遍历每个点然后对比
简单:
可以知道的是要把每个节点拆下来需要把每个结点的边都拆完,所以这个算法是正确的
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; ++i){
cin >> a[i];
}
int ans = 0;
for (int i = 0; i < m; ++i){
int c, d;
cin >> c >> d;
ans += min(a[c], a[d]);
}
cout << ans;
建图对比:
vector<int> v[maxn];
int val[maxn];
int main() {
IOS;
// freopen("P1908_6.in","r",stdin);//读入数据
// freopen("P1908.out","w",stdout); //输出数据
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; ++i){
cin >> val[i];
}
int ans = 0;
for (int i = 0; i < m; ++i){
int a, b;
cin >> a >> b;
v[b].push_back(a);//b的父节点
}
for (int i = 1; i <= n; ++i){
for(auto w : v[i]){
ans += min(val[w], val[i]);
}
}
cout << ans;
return 0;
}
Sequence Pair Weight
i 前缀和 * 之前的长度
还是有点混
ll a[maxn];
map<ll, ll> mp;
ll val[maxn];
int main() {
IOS;
// freopen("P1908_6.in","r",stdin);//读入数据
// freopen("P1908.out","w",stdout); //输出数据
int t;
cin >> t;
while(t--){
int n;
mp.clear();
cin >> n;
ll ans = 0;
for (int i = 1; i <= n; ++i){
cin >> a[i];
}
for (int i = 1; i <= n; ++i){
ans += mp[a[i]] * (n - i + 1);
mp[a[i]] += i;
}
cout << ans << endl;
}
return 0;
}
其它题先放一下