noSorryMaker

360

itdef
Andre-MarieAmpere

MazelTovy
liuser

mcoldmayor
see_1

CrescentMoon

noSorryMaker
8小时前

区间合并算法

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

typedef pair<int, int> PII;

void m(vector<PII> &segs)
{
vector<PII> res;

sort(segs.begin(), segs.end());

int st = -2e9, ed = -2e9;                 // 第一个维护的区间在最左边无穷远初
for (int i = 0; i < segs.size(); i ++)
if (ed < segs[i].first)
{
if (st != -2e9) res.push_back({st, ed});
st = segs[i].first;
ed = segs[i].second;
}
else ed = max(ed, segs[i].second);
if (ed < 2e9) res.push_back({st, ed});

segs = res;
}

int main()
{
int n;
cin >> n;

vector<PII> segs;
while(n --)
{
int l, r;
cin >> l >> r;
segs.push_back({l, r});
}

m(segs);

cout << segs.size();

return 0;
}



noSorryMaker
10小时前

离散化算法

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef pair<int, int> PII;

const int N = 300010;

int n, m;
int a[N], s[N];

vector<int> alls;

int findx(int x)
{
int l = 0, r = alls.size() - 1;
while(l < r)
{
int mid = l + r >> 1;
if (alls[mid] >= x) r = mid;
else l = mid + 1;
}
return r + 1;
}

int main()
{
cin >> n >> m;

for (int i = 0; i < n; i ++)
{
int x, c;
cin >> x >> c;

alls.push_back(x);
}

for (int i = 0; i < m; i ++)
{
int l, r;
cin >> l >> r;
query.push_back({l, r});

alls.push_back(l);
alls.push_back(r);
}

// 去重
sort(alls.begin(), alls.end());
alls.erase( unique(alls.begin(), alls.end()), alls.end() );

// 插入
for (int i = 0; i < n; i ++)
{
}

// 前缀和
for (int i = 1; i <= alls.size(); i ++)
s[i] = s[i - 1] + a[i];

// 查询
for (int i = 0; i < m; i ++)
{
int l = findx(query[i].first);
int r = findx(query[i].second);

cout << s[r] - s[l - 1] << endl;
}

return 0;
}



noSorryMaker
11小时前

lowbit()函数

#include <iostream>

using namespace std;

int lowbit(int x)
{
return x & - x;   // ~ x + 1
}

int main()
{
int n;
cin >> n;
while(n --)
{
int x;
cin >> x;

int cnt = 0;
while (x)
{
x -= lowbit(x);   // 每次减去 x 的最后一位 1 直到减到 0
cnt ++;
}
cout << cnt << ' ';
}

return 0;
}



noSorryMaker
13小时前
#include <iostream>

using namespace std;

int fact(int n)
{
if (n == 1) return 1;
return n * fact(n - 1);
}

int main()
{
int n;
cin >> n;

cout << fact(n) << endl;

return 0;
}


noSorryMaker
14小时前
#include <iostream>

using namespace std;

int main()
{
int n;
cin >> n;
cout << n / 365 << " ano(s)"<< endl;
cout << n % 365 / 30 << " mes(es)" << endl;
cout << n % 365 % 30 << " dia(s)" << endl;  // 这个可不能直接 n % 30 哈
return 0;
}



noSorryMaker
15小时前

取余操作不能用在浮点数, 所以先把数转换为整数, 再用取余操作

#include <cstdio>

int main()
{
double m;
scanf("%lf", &m);
int n = m * 100;

printf("NOTAS:\n");
printf("%d nota(s) de R$100.00\n", n / 10000); n %= 10000; printf("%d nota(s) de R$ 50.00\n", n / 5000); n %= 5000;
printf("%d nota(s) de R$20.00\n", n / 2000); n %= 2000; printf("%d nota(s) de R$ 10.00\n", n / 1000); n %= 1000;
printf("%d nota(s) de R$5.00\n", n / 500); n %= 500; printf("%d nota(s) de R$ 2.00\n", n / 200); n %= 200;

printf("MOEDAS:\n");
printf("%d moeda(s) de R$1.00\n", n / 100); n %= 100; printf("%d moeda(s) de R$ 0.50\n", n / 50); n %= 50;
printf("%d moeda(s) de R$0.25\n", n / 25); n %= 25; printf("%d moeda(s) de R$ 0.10\n", n / 10); n %= 10;
printf("%d moeda(s) de R$0.05\n", n / 5); n %= 5; printf("%d moeda(s) de R$ 0.01\n", n / 1); n %= 1;

return 0;
}



noSorryMaker
15小时前

setprecision(n)

    cout << fixed << setprecision(n) << f;
// 小数点后面显示 n 位小数
// 只用setprecision(n)是控制保留几位有效数字的
// 此语句在前面只写一次，对后面的输出也有效


#include <bits/stdc++.h>
using namespace std;
int main(){
long long s, t;
cin >> s >> t;
cout << fixed << setprecision(3) << s * t / 12.0;

// 数据太大用 long long 存 只在输出时转化为浮点数
}

#include <bits/stdc++.h>
using namespace std;
int a, b;
int main() {
double ans = 0;
scanf("%d%d", &a, &b);
printf("%.3lf", a * 1.000 / 12 * b); //先除再乘，以免double溢出
return 0;
}


noSorryMaker
15小时前
printf("%d:%d:%d", n / 3600, n % 3600 / 60, n % 60);

#include <iostream>

using namespace std;

int main()
{
int t;
cin >> t;
printf("%d:%d:%d", t/3600, (t%3600)/60, (t%3600)%60);
return 0;
}



noSorryMaker
15小时前

呜呜呜 梦开始的地方啊

#include<stdio.h>
#include<math.h>
main()
{
double a, b, c;
scanf("%lf %lf %lf", &a, &b, &c);

double delta = b * b - 4 * a * c;
if (delta < 0 || a == 0) printf("Impossivel calcular");
else
{
double r1 = ( (-1) * b + sqrt(delta) ) / (2 * a);
double r2 = ( (-1) * b - sqrt(delta) ) / (2 * a);
printf("R1 = %.5lf\n", r1);
printf("R2 = %.5lf", r2);
}
return 0;
}



noSorryMaker
16小时前

统一到最小单位作差法

#include <iostream>

using namespace std;

int main()
{
int a, b, c, d;
cin >> a >> b >> c >> d;

int t1, t2;
t1 = a * 60 + b;
t2 = c * 60 + d;

int t = t2 - t1;
if (t <= 0) t += 24 * 60;

cout << "O JOGO DUROU " << t / 60 <<" HORA(S) E " << t % 60 << " MINUTO(S)";
return 0;
}



直接算 注意%得0的情况

#include <iostream>
using namespace std;

int main()
{
int a, b, c, d;
cin >> a >> b >> c >> d;

int hour, minute;
hour = (c - a + 24) % 24;
if (!hour && d <= b) hour = 24;
if(d < b) hour --;
minute = (d - b + 60) % 60;

cout << "O JOGO DUROU " << hour <<" HORA(S) E " << minute << " MINUTO(S)";
return 0;
}