Class 1 -- 知识点串讲

  • int类型的取值范围:-2147483648 - 2147483647,上界是2*109,超出会循环(变负号);
  • 浮点数比大小:

(int)a=1   (实际上)a=1.00001

     b=1           b=0.99999

然而比大小时的精度不需要那么高,也就是说可以忽略极小的误差。这是我们需要设置eps(ε)。一般是保留的位数+3:比如,保留三位小数,eps=1e-6;然而注意double只能保证前十五个小数位的精度,所以:double eps=1e-15,不能再大了。

 

根据数轴理解→→→→→→→→→→→→→↓

a==bfabs(a-b)<eps
a>ba-b>eps
a≥ba-b>-eps
a<ba-b<-eps
a≤ba-b<eps
  • 一个浮点数陷阱:
1 //输入有l,输出没有
2 scanf("%lf");
3 printf("%f");
  • 关于位运算

1.& , | , ~  , ^ , << , >>

   and,or,not,xor,lsh,rsh

2.位运算的优先度很低,所以记得要加括号。平时写的时候最好用位运算。

3.作用:

①判断奇偶(原因是二进制奇数个位一定是1):

1 int a;
2 if(a & 1)  //奇数
3 else //偶数

乘除2的幂:

1 int a;
2 a>>1;   //即除2。二进制每右移一位就是除2
3 a<<1;  //即乘2.不过要小心溢出。如果溢出的话首位的符号位就会消失,出现变负等情况

③如何用位运算求得一个整数除以16的余数?

除以16,即24,即将二进制被除数a右移4位。那么根据异或运算的性质,a&15得到的值即为a除以16得到的余数。

 

  • 字符串的末尾一定会有'\0'(ASCII常见数值:'\0':0  '0':48  'A':65  'a':97)
  • 关于定义函数式的形参:

以swap(int &a,int &b)为例,有引用说明a、b和调用时一样,没有引用则说明a、b是新定义的。(举例:)

 1 int a = b; //a和b不是一个内存,所以修改a时b不会变

2 int & a = b; //a和b是同一个内存,所以修改a时b跟着变。 

  • 递归的注意事项:调用开销(内存、栈)和边界(会导致死循环)

  • 哪些错误会导致程序在运行时崩溃?(runtime error)
  1. 除零
  2. 模零
  3. 数组越界
  4. 爆栈
  5. 空指针

  • 关于STL:
  1. algorithm:

①sort():   O(nlogn)

1 sort(a,a+n);   //正常版本
2 
3 int cmp(const int i,const int j) {return c[i]>c[j];}   //强行从大到小
4 sort(a,a+n,cmp);
5 
6 sort(a.begin(),a.end());   //利用STL

②lower_bound() && upper_bound()     O(logn)

unique:unique函数可以去掉一个有序数列中的重复元素,也就是说要先sort再用   O(n)

           1 unique(a,a+n); 

2.vector(向量):

1 #include <vector>
2 
3     vector <int> a;
4     int x;
5     a.push_back(x);   //在尾部推入一个新的元素
6     cout<<a[i]<<endl;
7     a.size();   //求个数
8     sort(a.begin(),a.end());   //注意此处a.end()指最后一个数的后面一位。这样a.end()-a.begin()就是数组长度
9     a.back();  //最后一个元素的位置
10 //P.s:a.begin()和a.end()返回的不是数值,因此不能直接输出;而a.front()和a.back()可以

3.queue && stack && priority_queue

4.set(集合):

 本质:平衡树

 1 #include <set>
 2 
 3     set <string> s;
 4     string s0;
 5     s.insert(s0);           //压入一个字符串   复杂度:O(logn)  (一个树高)
 6     s.count(s0);           //查找set中某个元素出现的次数   复杂度:O(logn)
 7     /*P.s:由于set具有集合的性质(互异性),所以不会有重复的元素在set中出现。因此s.count(s0)其实就是判断集合中有无s0这个元素*/
 8     s.empty();             //判断是否为空
 9     s.begin();s.end();  //不会自行判断集合是否为空。所以使用前最好先用s.empty()
10     s.clear()                //清空集合
11     lower_bound && upper_bound   //详情请见之前博客

5.map(红黑树):

直接上例题好了:

Greedy Gift Givers

A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts of money. Each of these friends might or might not give some money to any or all of the other friends. Likewise, each friend might or might not receive money from any or all of the other friends. Your goal in this problem is to deduce how much more money each person gives than they receive.

The rules for gift-giving are potentially different than you might expect. Each person sets aside a certain amount of money to give and divides this money evenly among all those to whom he or she is giving a gift. No fractional money is available, so dividing 3 among 2 friends would be 1 each for the friends with 1 left over -- that 1 left over stays in the giver's "account".

In any group of friends, some people are more giving than others (or at least may have more acquaintances) and some people have more money than others.

Given a group of friends, no one of whom has a name longer than 14 characters, the money each person in the group spends on gifts, and a (sub)list of friends to whom each person gives gifts, determine how much more (or less) each person in the group gives than they receive.

IMPORTANT NOTE

The grader machine is a Linux machine that uses standard Unix conventions: end of line is a single character often known as '\n'. This differs from Windows, which ends lines with two characters, '\n' and '\r'. Do not let your program get trapped by this!

PROGRAM NAME: gift1

INPUT FORMAT

Line 1:The single integer, NP
Lines 2..NP+1:Each line contains the name of a group member
Lines NP+2..end:NP groups of lines organized like this:
The first line in the group tells the person's name who will be giving gifts.
The second line in the group contains two numbers: The initial amount of money (in the range 0..2000) to be divided up into gifts by the giver and then the number of people to whom the giver will give gifts, NGi (0 ≤ NGi ≤ NP-1).
If NGi is nonzero, each of the next NGi lines lists the name of a recipient of a gift.

 

SAMPLE INPUT (file gift1.in)

5
dave
laura
owen
vick
amr
dave
200 3
laura
owen
vick
owen
500 1
dave
amr
150 2
vick
owen
laura
0 2
amr
vick
vick
0 0

OUTPUT FORMAT

The output is NP lines, each with the name of a person followed by a single blank followed by the net gain or loss (final_money_value - initial_money_value) for that person. The names should be printed in the same order they appear starting on line 2 of the input.

All gifts are integers. Each person gives the same integer amount of money to each friend to whom any money is given, and gives as much as possible that meets this constraint. Any money not given is kept by the giver.

SAMPLE OUTPUT (file gift1.out)

dave 302
laura 66
owen -359
vick 141
amr -150

OUTPUT EXPLANATION

Five names: dave, laura, owen, vick, amr Let's keep a table of the gives (money) each person 'has':

 

davelauraowenvickamr
00000
First, 'dave' splits 200 among 'laura', 'owen', and 'vick'. That comes to 66 each, with 2 left over
-200+26666660
Second, 'owen' gives 500 to 'dave':
-198+5006666-500660
Third, 'amr' splits 150 between 'vick' and 'owen':
30266-434+7566+75-150
Fourth, 'laura' splits 0 between 'amr' and 'vick'; no changes:
30266-359141-150
Finally, 'vick' gives 0 to no one:
davelauraowenvickamr
30266-359141-150

 1 #include <iostream>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <cstdlib>
 6 #include <algorithm>
 7 #include <map>
 8 using namespace std;
 9 string sn[15];
10 int main()
11 {
12     freopen("gift1.in","r",stdin);
13     freopen("gift1.out","w",stdout);
14     int n;
15     scanf("%d",&n);
16     map <string,int> a;
17     for(int i=1;i<=n;i++)
18     {
19         string s;
20         cin>>s;
21         a[s]=0;
22         sn[i]=s;
23     }
24     for(int i=1;i<=n;i++)
25     {
26         string s0;
27         cin>>s0;
28         int m,x;
29         scanf("%d%d",&m,&x);
30         int c,sum=0;
31         if(x!=0) c=m/x;   //这里注意不要除零!!! 
32         else continue;
33         for(int j=1;j<=x;j++)
34         {
35             string s1;
36             cin>>s1;
37             a[s1]+=c;
38             sum+=c;
39         }
40         a[s0]-=sum;
41     }
42     for(int i=1;i<=n;i++)
43     {
44         cout<<sn[i];
45         printf(" %d\n",a[sn[i]]);
46     }
47     return 0;
48 }
USACO 1.1.2

 

转载于:https://www.cnblogs.com/lulala/p/7186520.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值