ACM注意事项

 
2.lower_bound

在从小到大的排序数组中,

lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

 unique函数返回第一个重复元素的位置,必须排序完才有用。

在从大到小的排序数组中,重载lower_bound()和upper_bound()。

lower_bound( begin,end,num,greater<type>() ):从数组的begin位置到end-1位置二分查找第一个小于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

upper_bound( begin,end,num,greater<type>() ):从数组的begin位置到end-1位置二分查找第一个小于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

注意事项:如果不存在,则返回end - begin;

set中用greater表示从大到小排序,有限队列里面用graeter表示小的先出来。

#include<bits/stdc++.h>
using namespace std;


int main() {
    set<int, greater<int> > s;
    s.insert(1);
    s.insert(2);
    set<int, greater<int> > :: iterator it;
    for(it = s.begin(); it != s.end(); it++) {
        printf("%d
", *it);
    }
    priority_queue<int, vector<int>, greater<int> > q;
    q.push(1);
    q.push(2);
    printf("%d
", q.top());
    return 0;
}

 multiset的使用

https://blog.csdn.net/sodacoco/article/details/84798621

string 中erase使用

https://www.cnblogs.com/yaoyueduzhen/p/4366267.html

struct rec{
    int x,y;
};
struct cmp{
    bool operator()(const rec&a,const rec&b){
        return a.x<b.x||a.x==b.x&&a.y<b.y;
    }
};
multiset<rec,cmp>h;



struct Point{
  int a,b;
  bool operator <(const Point& rhs)const{
    return a<rhs.a||(a==rhs.a && b <rhs.b);
  }
};
multiset<Point> S;
multiset<Point>::iterator it;

binary_search(arr[],arr[]+size ,  indx) 二分查找函数,找到返回1,没找到返回0.

set中的lower_bound与upper_boound函数用法,equal_range函数是看这个数组内有多少相同的元素。

// g++ -o setLowerUpperBoundTest setLowerUpperBoundTest.cpp

#include <set>
#include <iostream>

using namespace std ;

typedef set<int> SET_INT;

int main() 
{
  SET_INT s1;
  SET_INT::iterator i;
  cout << "s1.insert(5)" << endl;
  s1.insert(5);
  cout << "s1.insert(10)" << endl;
  s1.insert(10);
  cout << "s1.insert(15)" << endl;
  s1.insert(15);
  cout << "s1.insert(20)" << endl;
  s1.insert(20);
  cout << "s1.insert(25)" << endl;
  s1.insert(25);

  cout << "s1 -- starting at s1.lower_bound(12)" << endl;
// prints: 15,20,25
  for (i=s1.lower_bound(12);i!=s1.end();i++)
     cout << "s1 has " << *i << " in its set." << endl;

  cout << "s1 -- starting at s1.lower_bound(15)" << endl;
// prints: 15,20,25
  for (i=s1.lower_bound(15);i!=s1.end();i++)
     cout << "s1 has " << *i << " in its set." << endl;

  cout << "s1 -- starting at s1.upper_bound(12)" << endl;
// prints: 15,20,25
  for (i=s1.upper_bound(12);i!=s1.end();i++)
     cout << "s1 has " << *i << " in its set." << endl;

  cout << "s1 -- starting at s1.upper_bound(15)" << endl;
// prints: 20,25
  for (i=s1.upper_bound(15);i!=s1.end();i++)
     cout << "s1 has " << *i << " in its set." << endl;

  cout << "s1 -- s1.equal_range(12)" << endl;
// does not print anything
  for (i=s1.equal_range(12).first;i!=s1.equal_range(12).second;i++)
     cout << "s1 has " << *i << " in its set." << endl;

  cout << "s1 -- s1.equal_range(15)" << endl;
// prints: 15
  for (i=s1.equal_range(15).first;i!=s1.equal_range(15).second;i++)
     cout << "s1 has " << *i << " in its set." << endl;
}
/* Output in windows
s1.insert(5)
s1.insert(10)
s1.insert(15)
s1.insert(20)
s1.insert(25)
s1 -- starting at s1.lower_bound(12)
s1 has 15 in its set.
s1 has 20 in its set.
s1 has 25 in its set.
s1 -- starting at s1.lower_bound(15)
s1 has 15 in its set.
s1 has 20 in its set.
s1 has 25 in its set.
s1 -- starting at s1.upper_bound(12)
s1 has 15 in its set.
s1 has 20 in its set.
s1 has 25 in its set.
s1 -- starting at s1.upper_bound(15)
s1 has 20 in its set.
s1 has 25 in its set.
s1 -- s1.equal_range(12)
s1 -- s1.equal_range(15)
s1 has 15 in its set.
*/
 
4.蔡勒公式 
适合于1582年(中国明朝万历十年)10月15日之后的情形 公式 w = y + y/4 + c/4 - 2*c + 26 * (m+1)/10 + d - 1; m如果是1 2 月份 y要倒退1年 m += 12 y是年份的后两位 y = year%100 c是世纪 c = year/100  
 
<wiz_code_mirror>
 
 
#include <cstring>
#include <cstdio>
using namespace std;
char a[7][10] = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};
int main()
{
 int y, m, d;
 while(scanf("%d %d %d", &y, &m, &d) != EOF)
 { 
  if(m < 3)
  {
   y--;
   m += 12;
  }
  int c = y / 100;
  y = y % 100;
  int w = y + y/4 + c/4 - 2*c + 26 * (m+1)/10 + d - 1;
  printf("%s
", a[(w%7+7)%7]);
 }
 return 0;
}
 
 
 
5.ASCII吗
.''的ASCII吗为0, a----97, z------122, A -------65,Z---------90
 
6.pow函数
pow函数很慢
 
<wiz_code_mirror>
 
 
 
 
 
void init() {
    pw[0] = 1;
    for(int i = 1; i < 15; i++) {
        pw[i] = pw[i - 1] * 10;
    }
}
 
 
可以试着先把要算的先乘出来。
7.全排列 next_permutation(),prev_permutation().,按照字典序排的序。
<wiz_code_mirror>
 
 
 
 
 
while (next_permutation(str.begin(), str.end()))
{
k++;
cout << str << endl;
if (k == 5)
{
break;
}
 
 
如果到头了就返回false;同样end不参与;
 
 
 
8.第一抽屉原理 (前缀和)
原理1: 把多于n+k个的物体放到n个抽屉里,则至少有一个抽屉里的东西不少于两件。
原理2 :把多于mn(m乘以n)+1(n不为0)个的物体放到n个抽屉里,则至少有一个抽屉里有不少于(m+1)的物体。
原理3 :把无穷多件物体放入n个抽屉,则至少有一个抽屉里 有无穷个物体。
第二抽屉原理
把(mn-1)个物体放入n个抽屉中,其中必有一个抽屉中至多有(m—1)个物体(例如,将3×5-1=14个物体放入5个抽屉中,则必定有一个抽屉中的物体数少于等于3-1=2)。 --------------------- 本文来自 数数1234 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/l2580258/article/details/51052631?utm_source=copy 
 
9.判断回文数
bool palindromes(int start, int enda){ 
for(int i = start, j = enda; i < j; i++, j--)
{ 
if(ch[i] != ch[j])
   {
    return false;
   }
}
 return true; 
}

10.atan2与atan函数使用

printf("%lf %lf %lf %lf ", atan2(1, 1), atan2(-1, 1), atan2(-1, -1), atan2(1, -1));
printf("%lf %lf %lf %lf ", atan(1.0/1), atan(-1.0/1), atan(-1.0/-1), atan(1.0/-1));

主要是int_128的输入与输出。

#include <iostream>
#define ll __int128
using namespace std;
 
inline __int128 read() {
    __int128 x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}
  
  
inline void print(__int128 x)
{
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9)
        print(x / 10);
    putchar(x % 10 + '0');
}
 
 
ll exgcd(ll a, ll b, ll &x, ll &y) {
    if (!b) {
        x = 1;
        y = 0;
        return a;
    }
    ll d = exgcd(b, a % b, x, y);
    ll z = x;
    x = y;
    y = z - y * (a / b);
    return d;
}
 
int main() {
    int n;
    scanf("%d",&n);
    ll val=read();
     
        ll a, b, x, y, k;
        ll lcm = a, ans = b;
        bool flag = 1;
        --n;
        while (n--) {
            a=read(),b=read();
            b = (b - ans % a + a) % a;
            ll d = exgcd(lcm, a, x, y);
            if (b % d) flag = 0;
            else k = x * (b / d) % a;
            ans += k * lcm;
            lcm = lcm / d * a;
            ans = (ans % lcm + lcm) % lcm;
        }
         
        if(!flag) puts("he was definitely lying");
        else if(ans>val) puts("he was probably lying");
        else print(ans);
  
    return 0;
}
原文地址:https://www.cnblogs.com/downrainsun/p/9733640.html