- The document contains code and explanations for solving optimization problems using dynamic programming, including calculating minimum costs using a 2D array to store results.
- It describes applying dynamic programming to problems involving finding minimum costs for tasks that can be split into subtasks, with the overall cost determined by combining subtask costs.
- The code provided shows initializing a 2D array and using nested for loops to iterate through values, calculate minimum costs based on previous results, and store them in the 2D array to build up an optimal solution.
- The document contains code and explanations for solving optimization problems using dynamic programming, including calculating minimum costs using a 2D array to store results.
- It describes applying dynamic programming to problems involving finding minimum costs for tasks that can be split into subtasks, with the overall cost determined by combining subtask costs.
- The code provided shows initializing a 2D array and using nested for loops to iterate through values, calculate minimum costs based on previous results, and store them in the 2D array to build up an optimal solution.
10. オブジェクト形式マクロ
#define MAX 128
● コンパイル前の処理(プリプロセッサ)
● プログラム中のMAXが128に置換される
● MAXIMAとか描いても128IMAにはならない
int array[MAX]; // => int array[128];
int r = MAXIMA; // => int r = MAXIMA;
11. 関数形式マクロ
#define SQR(x) x * x
● 関数っぽく置換される
● 本当にただ「置き換えるだけ」
int area = SQR(side); // => int area = side * side;
int x = SQR(a + b); // => int x = a + b * a + b;
12. 関数形式マクロ
#define SQR(x) (x) * (x)
● 引数を()でくくると防げる
● 副作用があると相変わらず爆死
int x = SQR(a + b); // => int x = (a + b) * (a + b);
int y = SQR(++j); // => int x = (++j) * (++j);
13. インライン関数
inline int sqr(int x){ return (x * x * x); }
● 関数にinlineをつけるとインライン展開される
● 関数呼び出しのオーバーヘッドが0になる
● マクロと同様だが型安全、副作用にも比較的強い
● オプションをつけないとインライン展開されないことがある
int y = sqr(++j); // => 期待通りに動く
22. in
template <class T = int>
in(){ T x; cin >> x; return (x); }
● 宣言と同時に読み込みたい
● テンプレートなので色んな型に対応できる
● stdinより遅いので大量の入力には注意
int a = in(), b = in();
string s = in<string>();
26. usingエイリアス
using template <class T> vec = vector<T>;
using ll = long long;
● usingでエイリアスが設定できる
● テンプレートが使えるtypedef
● C++11以降でないと使えない
vec<ll> v;
27. 力が欲しいか…
#define int long long
● intでオーバーフローした時の切り替え用
● 強すぎる力にmain関数が耐えられない
● メモリ食い過ぎに注意(普通そんなに食わないけど)
int x; // => long long x;
int fact(int n) // => long long fact(long long n)