您的位置:首页 > 其它

短学期 线性复杂度优化 / 离散化

2020-07-12 22:41 141 查看

https://www.luogu.com.cn/problem/P1950

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 1007
#define int long long
using namespace std;
int n, m, now, ans;
int high[N];
signed main()
{
//scanf("%lld%lld", &n, &m);
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
char in;
//scanf(" %c", &in);
cin >> in;
if (in == '.')
++high[j];
else
high[j] = 0;
}
for (int j = 1; j <= m; ++j)
{
now = high[j];
for (int k = j; k <= m; ++k)
{
if (!high[k])
break;
now = min(now, high[k]);
ans += now;
}
}
}
//printf("%lld", ans);
cout << ans << endl;
return 0;
}

思路:以一个方块作为左下角的方块
https://www.luogu.com.cn/problem/P2032

#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;

const int N = 2e6 + 6;
int a[N];
int n, k;
priority_queue< pair<int, int> >q;

int main()
{
ios_base::sync_with_stdio(false);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++)
{
q.push(make_pair(a[i], i));
//按照a[i]由大到小排序
if (i >= k)
{
while (q.top().second <= i - k) q.pop();
//printf("%d\n", q.top().first);
cout << q.top().first << endl;
}
}

return 0;
}

https://www.luogu.com.cn/problem/P1102

#include<iostream>
#include<algorithm>
using namespace std;
int p[200005], n, c;
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> c;
int i, num;
long long ans;//注意
for (i = 0; i < n; i++) {
cin >> p[i];
}
sort(p, p + n);
for (i = 0; i < n; i++) {
ans += ((upper_bound(p, p + n, p[i] + c)-p) - (lower_bound(p, p + n, p[i] + c)-p));
}
cout << ans << endl;
return 0;
}

对于upper_bound来说,返回的是被查序列中第一个大于查找值的指针,也就是返回指向被查值>查找值的最小指针,lower_bound则是返回的是被查序列中第一个大于等于查找值的指针,也就是返回指向被查值>=查找值的最小指针。
https://www.luogu.com.cn/problem/P1147

//用数学方法解一元二次方程
#include<iostream>
#include<cmath>
using namespace std;
int main() {
int M, m, n;
cin >> M;
long long s = 0;
for (m = 1; m <= M / 2; m++) {
s = 1 + 4 * (pow(m, 2) - m + 2 * M);
s = sqrt(s);
n = (s - 1) / 2;
if (m - pow(m, 2) + pow(n, 2) + n == 2 * M&&n>m) {
cout << m << " " << n << endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: