├── 201312 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── 201403 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── 201409 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── 201412 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── 201503 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── 201509 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── 201512 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── 201604 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── 201609 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── 201612 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── 201703 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── 201709 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── 201712 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── 201803 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── 201809 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── 201812 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── 201903 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── 201909 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── 201912 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── 202006 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── 202009 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── 202012 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── 202104 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── 202109 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp └── 5.cpp ├── .github ├── ISSUE_TEMPLATE │ └── issue.yml └── workflows │ └── github-actions-demo.yml ├── .vscode └── settings.json ├── LICENSE ├── README.md └── 算法模板.pdf /.github/ISSUE_TEMPLATE/issue.yml: -------------------------------------------------------------------------------- 1 | name: Bug Report 2 | description: File a bug report 3 | title: "[Bug]: " 4 | labels: ["bug", "triage"] 5 | assignees: 6 | - octocat 7 | body: 8 | - type: markdown 9 | attributes: 10 | value: | 11 | Thanks for taking the time to fill out this bug report! 12 | - type: input 13 | id: contact 14 | attributes: 15 | label: Contact Details 16 | description: How can we get in touch with you if we need more info? 17 | placeholder: ex. email@example.com 18 | validations: 19 | required: false 20 | - type: textarea 21 | id: what-happened 22 | attributes: 23 | label: What happened? 24 | description: Also tell us, what did you expect to happen? 25 | placeholder: Tell us what you see! 26 | value: "A bug happened!" 27 | validations: 28 | required: true 29 | - type: dropdown 30 | id: version 31 | attributes: 32 | label: Version 33 | description: What version of our software are you running? 34 | options: 35 | - 1.0.2 (Default) 36 | - 1.0.3 (Edge) 37 | validations: 38 | required: true 39 | - type: dropdown 40 | id: browsers 41 | attributes: 42 | label: What browsers are you seeing the problem on? 43 | multiple: true 44 | options: 45 | - Firefox 46 | - Chrome 47 | - Safari 48 | - Microsoft Edge 49 | - type: textarea 50 | id: logs 51 | attributes: 52 | label: Relevant log output 53 | description: Please copy and paste any relevant log output. This will be automatically formatted into code, so no need for backticks. 54 | render: shell 55 | - type: checkboxes 56 | id: terms 57 | attributes: 58 | label: Code of Conduct 59 | description: By submitting this issue, you agree to follow our [Code of Conduct](https://example.com) 60 | options: 61 | - label: I agree to follow this project's Code of Conduct 62 | required: true -------------------------------------------------------------------------------- /.github/workflows/github-actions-demo.yml: -------------------------------------------------------------------------------- 1 | name: GitHub Actions Demo 2 | on: [push] 3 | jobs: 4 | Explore-GitHub-Actions: 5 | runs-on: ubuntu-latest 6 | steps: 7 | - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event." 8 | - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!" 9 | - run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}." 10 | - name: Check out repository code 11 | uses: actions/checkout@v2 12 | - run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner." 13 | - run: echo "🖥️ The workflow is now ready to test your code on the runner." 14 | - name: List files in the repository 15 | run: | 16 | ls ${{ github.workspace }} 17 | - run: echo "🍏 This job's status is ${{ job.status }}." 18 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iostream": "cpp", 4 | "ostream": "cpp", 5 | "cstring": "cpp" 6 | } 7 | } -------------------------------------------------------------------------------- /201312/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int N = 1010, M = 10010; 6 | int n; 7 | int a[N]; 8 | int cnt[M]; 9 | 10 | int main() { 11 | cin >> n; 12 | 13 | int ans = 0, max_cnt = 0; 14 | for (int i = 0; i < n; i++) { 15 | cin >> a[i]; 16 | cnt[a[i]] ++; 17 | 18 | if (max_cnt < cnt[a[i]]) { 19 | ans = a[i]; 20 | max_cnt = cnt[a[i]]; 21 | } 22 | if (max_cnt == cnt[a[i]]) { 23 | ans = min(ans, a[i]); 24 | } 25 | } 26 | 27 | cout << ans << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /201312/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | string s;//保存ISBN 号码 9 | cin >> s; 10 | 11 | int check = 0;// 12 | 13 | for(int i = 0, k = 1;k <= 9; i++){ 14 | char x = s[i]; 15 | if(x != '-') 16 | check += (x-'0') * k, k++; 17 | } 18 | 19 | check = check % 11; 20 | if((check == 10 && 'X' == s.back()) || check == s.back() - '0')//校验位正确 21 | cout << "Right" << endl; 22 | else { 23 | if(check == 10) 24 | cout << s.substr(0, 12) + 'X' << endl; 25 | else 26 | cout << s.substr(0, 12) << check << endl; 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /201312/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 1010; 8 | int a[N]; 9 | int n; 10 | 11 | int main() { 12 | cin >> n; 13 | for (int i = 1; i <= n; i++) cin >> a[i]; 14 | 15 | int area = 0; 16 | for (int i = 1; i <= n; i++) { 17 | int mi = a[i]; 18 | for (int j = i; j <= n; j++) { 19 | mi = min(mi, a[j]); 20 | area = max(area, (j - i + 1) * mi); 21 | } 22 | } 23 | 24 | cout << area << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /201312/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef long long LL; 8 | const int N = 1010, MOD = 1e9 + 7; 9 | 10 | int n; 11 | int C[N][N]; 12 | 13 | int main() { 14 | cin >> n; 15 | for (int i = 0; i <= n; i ++ ) 16 | for (int j = 0; j <= i; j ++ ) 17 | if (!j) C[i][j] = 1; 18 | else C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD; 19 | 20 | int res = 0; 21 | for (int i = 2; i <= n - 2; i ++ ) { 22 | res += (LL)C[n - 1][i] * (i - 1) % MOD * (n - i - 1) % MOD; 23 | res %= MOD; 24 | } 25 | 26 | cout << res << endl; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /201312/5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 55; 8 | char g[N][N]; 9 | bool st1[N][N], st2[N][N]; 10 | int m, n; 11 | int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1}; 12 | 13 | // 检查 是否满足 (x1, y1) -> (x2, y2) 14 | bool check(int x1, int y1, int x2, int y2) { 15 | if (g[x1][y1] == '+' || g[x1][y1] == 'S' || g[x1][y1] == 'T') return true; 16 | if (g[x1][y1] == '-') { 17 | if (x2 == x1) return true; 18 | else return false; 19 | } 20 | if (g[x1][y1] == '|') { 21 | if (y2 == y1) return true; 22 | else return false; 23 | } 24 | if (g[x1][y1] == '.') { 25 | if (x2 == x1 + 1 && y2 == y1) return true; 26 | else return false; 27 | } 28 | } 29 | 30 | // S 出发能到达的地方 31 | void dfs1(int x, int y) { 32 | st1[x][y] = true; 33 | for (int i = 0; i < 4; i++) { 34 | int a = x + dx[i], b = y + dy[i]; 35 | if (a < 0 || a >= m || b < 0 || b >= n || g[a][b] == '#') continue; 36 | if (st1[a][b]) continue; 37 | if (check(x, y, a, b)) dfs1(a, b); 38 | } 39 | } 40 | 41 | 42 | // 从 T 走能回溯到的 地方 43 | void dfs2(int x, int y) { 44 | st2[x][y] = true; 45 | for (int i = 0; i < 4; i++) { 46 | int a = x + dx[i], b = y + dy[i]; 47 | if (a < 0 || a >= m || b < 0 || b >= n || g[a][b] == '#') continue; 48 | if (st2[a][b]) continue; 49 | if (check(a, b, x, y)) dfs2(a, b); 50 | } 51 | } 52 | 53 | 54 | int main() { 55 | cin >> m >> n; 56 | int sx, sy, tx, ty; 57 | for (int i = 0; i < m; i ++) { 58 | for (int j = 0; j < n; j++) { 59 | cin >> g[i][j]; 60 | if (g[i][j] == 'S') { 61 | sx = i, sy = j; 62 | } 63 | if (g[i][j] == 'T') { 64 | tx = i, ty = j; 65 | } 66 | } 67 | } 68 | 69 | dfs1(sx, sy); 70 | dfs2(tx, ty); 71 | 72 | int cnt = 0; 73 | for (int i = 0; i < m; i++) { 74 | for (int j = 0; j < n; j++) { 75 | if (st1[i][j] && !st2[i][j]) cnt ++; 76 | } 77 | } 78 | 79 | if (!st2[sx][sy]) puts("I'm stuck!"); 80 | else cout << cnt << endl; 81 | 82 | return 0; 83 | } -------------------------------------------------------------------------------- /201403/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 1010; 8 | 9 | int n; 10 | int s[N]; 11 | 12 | int main() { 13 | cin >> n; 14 | while (n -- ) { 15 | int x; 16 | cin >> x; 17 | s[abs(x)] ++ ; 18 | } 19 | 20 | int res = 0; 21 | for (int i = 1; i <= 1000; i ++ ) 22 | if (s[i] > 1) 23 | res ++ ; 24 | cout << res << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /201403/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 11; 8 | 9 | int n, m; 10 | struct Window { 11 | int x1, y1, x2, y2; 12 | int id; 13 | }w[N]; 14 | 15 | int get(int x, int y) { 16 | for (int i = n; i; i -- ) { 17 | if (x >= w[i].x1 && x <= w[i].x2 && y >= w[i].y1 && y <= w[i].y2) 18 | return i; 19 | } 20 | return -1; 21 | } 22 | 23 | int main() { 24 | cin >> n >> m; 25 | for (int i = 1; i <= n; i ++ ) { 26 | cin >> w[i].x1 >> w[i].y1 >> w[i].x2 >> w[i].y2; 27 | w[i].id = i; 28 | } 29 | 30 | while (m -- ) { 31 | int x, y; 32 | cin >> x >> y; 33 | int id = get(x, y); 34 | if (id == -1) puts("IGNORED"); 35 | else 36 | { 37 | cout << w[id].id << endl; 38 | Window window = w[id]; 39 | for (int i = id; i < n; i ++ ) 40 | w[i] = w[i + 1]; 41 | w[n] = window; 42 | } 43 | } 44 | return 0; 45 | } -------------------------------------------------------------------------------- /201403/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int n; 9 | 10 | int main() { 11 | string choices; 12 | cin >> choices; 13 | cin >> n; 14 | // mp 存储 某一字母是否为带参数选项 15 | map mp; 16 | int len = choices.size(); 17 | for (int i = 0; i < len; i++) { 18 | if (i + 1 < len && choices[i + 1] == ':') mp[choices[i]] = true, i++; 19 | else mp[choices[i]] = false; 20 | } 21 | 22 | 23 | getchar(); 24 | 25 | for (int i = 1; i <= n; i++) { 26 | string cmd; 27 | getline(cin, cmd); 28 | 29 | vector res; 30 | cmd += " "; 31 | size_t pos = cmd.find(" "); 32 | while(pos != cmd.npos) { 33 | string temp = cmd.substr(0, pos); 34 | res.push_back(temp); 35 | //去掉已分割的字符串,在剩下的字符串中进行分割 36 | cmd = cmd.substr(pos+1, cmd.size()); 37 | pos = cmd.find(" "); 38 | } 39 | 40 | cout << "Case " << i << ":"; 41 | for (int i = 0; i < 26; i++) { 42 | string tmp; 43 | for (int j = 1; j < (int)res.size(); j ++) { 44 | if (res[j][0] != '-' || res[j].size() != 2) break; 45 | if (res[j][0] == '-' && !mp.count(res[j][1])) break; 46 | 47 | string choice = res[j]; 48 | if (choice[1] == 'a' + i) { 49 | if (mp[choice[1]]) { 50 | if (j + 1 < (int)res.size()) { 51 | tmp = " " + res[j] + " " + res[j + 1]; 52 | j ++; 53 | } 54 | else ; 55 | } 56 | else tmp = " " + res[j]; 57 | } 58 | else { 59 | if (mp[choice[1]]) j ++; 60 | } 61 | } 62 | cout << tmp; 63 | } 64 | 65 | cout << endl; 66 | 67 | } 68 | 69 | return 0; 70 | } -------------------------------------------------------------------------------- /201403/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define x first 8 | #define y second 9 | 10 | using namespace std; 11 | 12 | typedef pair pii; 13 | typedef long long LL; 14 | const int N = 210, M = N * N; 15 | 16 | struct Point{ 17 | int x, y, id; 18 | }p[N]; 19 | 20 | int n, m, k, r; 21 | map> mp; 22 | int dist[N][N]; 23 | 24 | bool check(Point a, Point b) { 25 | LL dx = a.x - b.x; 26 | LL dy = a.y - b.y; 27 | return dx * dx + dy * dy <= (LL)r * r; 28 | } 29 | 30 | 31 | int bfs() { 32 | queue q; 33 | q.push({1, 0}); 34 | memset(dist, 0x3f, sizeof dist); 35 | dist[1][0] = 0; 36 | 37 | while (q.size()) { 38 | auto t = q.front(); 39 | q.pop(); 40 | 41 | for (int i = 0; i < (int)mp[t.first].size(); i++) { 42 | int x = mp[t.x][i], y = t.y; 43 | if (x > n) y ++ ; 44 | if (y <= k) { 45 | if (dist[x][y] > dist[t.x][t.y] + 1) { 46 | dist[x][y] = dist[t.x][t.y] + 1; 47 | q.push({x, y}); 48 | } 49 | } 50 | } 51 | } 52 | 53 | int res = 1e8; 54 | for (int i = 0; i <= k; i ++ ) 55 | res = min(res, dist[2][i]); 56 | return res - 1; 57 | } 58 | 59 | int main() { 60 | cin >> n >> m >> k >> r; 61 | 62 | for (int i = 1; i <= n + m; i ++ ){ 63 | cin >> p[i].x >> p[i].y; 64 | p[i].id = i; 65 | } 66 | 67 | for (int i = 1; i <= n + m; i ++ ) 68 | for (int j = i + 1; j <= n + m; j ++ ) 69 | if (check(p[i], p[j])) 70 | mp[i].push_back(j), mp[j].push_back(i); 71 | 72 | cout << bfs() << endl; 73 | 74 | return 0; 75 | } -------------------------------------------------------------------------------- /201403/5.cpp: -------------------------------------------------------------------------------- 1 | // 这题不会, 直接cv, 官方数据有误, 只能拿30分 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | const int N = 210, INF = 0x3f3f3f3f; 9 | 10 | int n; 11 | int c[N][3]; 12 | int f[2][N][N][N]; 13 | 14 | int main() 15 | { 16 | cin >> n; 17 | int m = 0; 18 | for (int i = 1; i <= n; i ++ ) 19 | { 20 | int x, y, z, t; 21 | cin >> x >> y >> z >> t; 22 | c[i][0] = x, c[i][1] = z, c[i][2] = min(y, t); 23 | m += x; 24 | } 25 | m = (m + 1) / 2; 26 | memset(f, 0x3f, sizeof f); 27 | f[0][0][0][0] = 0; 28 | for (int u = 1; u <= n; u ++ ) 29 | for (int i = 0; i <= m; i ++ ) 30 | for (int j = 0; j <= m; j ++ ) 31 | for (int k = 0; k <= m; k ++ ) 32 | { 33 | int& v = f[u & 1][i][j][k]; 34 | register int x = c[u][0], y = c[u][1], z = c[u][2], t = u - 1 & 1; 35 | v = f[t][i][j][k] + z; 36 | if (i >= x) v = min(v, f[t][i - x][j][k]); 37 | if (j >= x) v = min(v, f[t][i][j - x][k]); 38 | if (i >= y && k >= y) v = min(v, f[t][i - y][j][k - y]); 39 | if (j >= y && k >= y) v = min(v, f[t][i][j - y][k - y]); 40 | } 41 | 42 | int res = INF; 43 | for (int i = 0; i <= m; i ++ ) 44 | for (int j = 0; j <= m; j ++ ) 45 | for (int k = 0; k <= m; k ++ ) 46 | res = min(res, f[n & 1][i][j][k] + max(i, max(j, k))); 47 | cout << res << endl; 48 | return 0; 49 | } -------------------------------------------------------------------------------- /201409/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 1010; 8 | 9 | int n; 10 | int a[N]; 11 | 12 | int main() { 13 | cin >> n; 14 | for (int i = 0; i < n; i ++ ) cin >> a[i]; 15 | sort(a, a + n); 16 | 17 | int res = 0; 18 | for (int i = 1; i < n; i ++ ) 19 | if (a[i] == a[i - 1] + 1) 20 | res ++ ; 21 | cout << res << endl; 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /201409/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 110; 8 | bool state[N][N]; 9 | int n; 10 | 11 | int main() { 12 | cin >> n; 13 | 14 | for (int i = 1; i <= n; i++) { 15 | int x1, y1, x2, y2; 16 | cin >> x1 >> y1 >> x2 >> y2; 17 | for (int x = x1; x < x2; x ++) { 18 | for (int y = y1; y < y2; y++) { 19 | state[x][y] = true; 20 | } 21 | } 22 | } 23 | 24 | int ans = 0; 25 | for (int i = 0; i <= 105; i++) { 26 | for (int j = 0; j <= 105; j++) { 27 | if (state[i][j]) ans ++; 28 | } 29 | } 30 | 31 | cout << ans << endl; 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /201409/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 110; 8 | 9 | string Lower(string s) { 10 | string res; 11 | for (int i = 0; i < s.size(); i ++ ) 12 | res += tolower(s[i]); 13 | return res; 14 | } 15 | 16 | int main() { 17 | string S; 18 | cin >> S; 19 | int type; 20 | cin >> type; 21 | 22 | int n; 23 | cin >> n; 24 | while (n -- ) { 25 | string line; 26 | cin >> line; 27 | if (type && line.find(S) != -1) cout << line << endl; 28 | else if (!type && Lower(line).find(Lower(S)) != -1) cout << line << endl; 29 | } 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /201409/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define x first 7 | #define y second 8 | 9 | using namespace std; 10 | 11 | typedef pair pii; 12 | 13 | const int N = 1010; 14 | int n, m, k, d; 15 | int g[N][N]; 16 | int dist[N][N]; 17 | queue q; 18 | 19 | int kx[N * N], ky[N * N], kc[N * N]; 20 | int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; 21 | 22 | void bfs() { 23 | while(q.size()) { 24 | pii p = q.front(); 25 | q.pop(); 26 | 27 | int x = p.x, y = p.y; 28 | for (int i = 0; i < 4; i++) { 29 | int a = x + dx[i], b = y + dy[i]; 30 | 31 | if (a < 1 || a > n || b < 1 || b > n || g[a][b] == -2 || g[a][b] == -1) continue; 32 | if (!dist[a][b]) { 33 | dist[a][b] = dist[x][y] + 1; 34 | q.push({a, b}); 35 | } 36 | } 37 | } 38 | } 39 | 40 | 41 | // g[i][j] 42 | // 0 : 正常 -1 : 分店 num : 客户 -2 : 不能经过 43 | int main() { 44 | scanf("%d%d%d%d", &n, &m, &k, &d); 45 | for (int i = 1; i <= m; i++) { 46 | int x, y; 47 | scanf("%d%d", &x, &y); 48 | g[x][y] = -1; 49 | q.push({x, y}); 50 | dist[x][y] = 0; 51 | } 52 | 53 | for (int i = 1; i <= k; i++) { 54 | int x, y, c; 55 | scanf("%d%d%d", &x, &y, &c); 56 | g[x][y] = c; 57 | kx[i] = x, ky[i] = y, kc[i] = c; 58 | } 59 | 60 | for (int i = 1; i <= d; i++) { 61 | int x, y; 62 | scanf("%d%d", &x, &y); 63 | g[x][y] = -2; 64 | } 65 | 66 | bfs(); 67 | 68 | long long ans = 0; 69 | for (int i = 1; i <= k; i++) { 70 | int x = kx[i], y = ky[i], c = kc[i]; 71 | ans += dist[x][y] * c; 72 | } 73 | 74 | cout << ans << endl; 75 | 76 | return 0; 77 | } -------------------------------------------------------------------------------- /201409/5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef long long LL; 8 | const int N = 130, MOD = 1e9 + 7; 9 | 10 | LL n; 11 | int m; 12 | int w[N][N]; 13 | 14 | // 这里将原 n * m 方格 看为 m * n 方格, 便于理解 15 | // x 为 当前列状态, y 为 下一列状态, u 为当前列枚举到的方格 index 16 | void dfs(int x, int y, int u) { 17 | if (u == m) w[x][y] ++ ; 18 | else if (x >> u & 1) dfs(x, y, u + 1); 19 | else { 20 | // 由于 列 从左往右, u(行) 从下往上 枚举, 因此共有四种转移方式 21 | if (u && !(y >> u & 1) && !(y >> u - 1 & 1)) 22 | dfs(x, y + (1 << u) + (1 << u - 1), u + 1); 23 | if (u + 1 < m && !(y >> u & 1) && !(y >> u + 1 & 1)) 24 | dfs(x, y + (1 << u) + (1 << u + 1), u + 1); 25 | if (u + 1 < m && !(x >> u + 1 & 1)) { 26 | if (!(y >> u & 1)) dfs(x, y + (1 << u), u + 2); 27 | if (!(y >> u + 1 & 1)) dfs(x, y + (1 << u + 1), u + 2); 28 | } 29 | } 30 | } 31 | 32 | // 矩阵乘法模板 33 | void mul(int c[][N], int a[][N], int b[][N]) { 34 | static int tmp[N][N]; 35 | memset(tmp, 0, sizeof tmp); 36 | for (int i = 0; i < 1 << m; i ++ ) 37 | for (int j = 0; j < 1 << m; j ++ ) 38 | for (int k = 0; k < 1 << m; k ++ ) 39 | tmp[i][j] = (tmp[i][j] + (LL)a[i][k] * b[k][j]) % MOD; 40 | memcpy(c, tmp, sizeof tmp); 41 | } 42 | 43 | int main() { 44 | cin >> n >> m; 45 | // 构造转移矩阵 46 | for (int i = 0; i < 1 << m; i ++ ) 47 | dfs(i, 0, 0); 48 | 49 | int res[N][N] = {0}; 50 | res[0][(1 << m) - 1] = 1; 51 | 52 | // 快速幂 53 | while (n) { 54 | if (n & 1) mul(res, res, w); 55 | mul(w, w, w); 56 | n >>= 1; 57 | } 58 | cout << res[0][(1 << m) - 1] << endl; 59 | return 0; 60 | } -------------------------------------------------------------------------------- /201412/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 1010; 8 | int cnt[N]; 9 | int n; 10 | 11 | int main() { 12 | cin >> n; 13 | for (int i = 0; i < n; i++) { 14 | int x; 15 | cin >> x; 16 | cnt[x] ++; 17 | cout << cnt[x] << " "; 18 | } 19 | 20 | cout << endl; 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /201412/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 510; 8 | int g[N][N]; 9 | int n; 10 | 11 | int main() { 12 | cin >> n; 13 | 14 | for (int i = n; i >= 1; i--) { 15 | for (int j = 1; j <= n; j ++) { 16 | cin >> g[j][i]; 17 | } 18 | } 19 | 20 | bool flag = false; 21 | for (int diff = -(n - 1); diff <= (n - 1); diff ++) { 22 | if (flag) { 23 | for (int y = n; y > 0; y --) { 24 | int x = y + diff; 25 | if (x < 1 || x > n) continue; 26 | cout << g[x][y] << " "; 27 | } 28 | } 29 | else { 30 | for (int y = 1; y <= n; y ++) { 31 | int x = y + diff; 32 | if (x < 1 || x > n) continue; 33 | cout << g[x][y] << " "; 34 | } 35 | } 36 | flag = !flag; 37 | } 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /201412/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef pair pdi; 9 | 10 | const int N = 5010; 11 | pdi buy[N], sell[N]; 12 | int tot, b, s; 13 | int buy_num, sell_num; 14 | map mp; // 总记录 -> buy 和 sell (+ : buy - : sell) 15 | long long pre1[N], pre2[N]; 16 | double ps[N * 2]; 17 | 18 | int main() { 19 | string op; 20 | double price; 21 | int num; 22 | 23 | for (int i = 0; i < N; i++) { 24 | buy[i] = {10000.0, 0}; 25 | sell[i] = {10000.0, 0}; 26 | } 27 | while(cin >> op) { 28 | tot ++; 29 | if (op == "buy") { 30 | cin >> price >> num; 31 | b ++; 32 | buy_num ++; 33 | buy[b] = {price, num}; 34 | mp[tot] = b; 35 | } 36 | else if (op == "sell") { 37 | cin >> price >> num; 38 | s ++; 39 | sell_num ++; 40 | sell[s] = {price, num}; 41 | mp[tot] = -1 * s; 42 | } 43 | else { 44 | int idx; 45 | cin >> idx; 46 | int row = mp[idx]; 47 | if (row > 0) buy[row] = {10000.0, 0}, buy_num --; 48 | else sell[-1 * row] = {10000.0, 0}, sell_num --; 49 | } 50 | } 51 | 52 | sort(buy + 1, buy + b + 1); 53 | sort(sell + 1, sell + s + 1); 54 | 55 | for (int i = 1; i <= buy_num; i++) { 56 | ps[i] = buy[i].first; 57 | pre1[i] = pre1[i - 1] + buy[i].second; 58 | } 59 | 60 | 61 | for (int i = 1; i <= sell_num; i ++) { 62 | ps[i + buy_num] = sell[i].first; 63 | pre2[i] = pre2[i - 1] + sell[i].second; 64 | } 65 | 66 | sort(ps + 1, ps + buy_num + sell_num + 1); 67 | 68 | double p0; 69 | long long ans = 0; 70 | 71 | int l = 1, r = 1; 72 | for (int i = 1; i <= buy_num + sell_num; i ++) { 73 | double p = ps[i]; 74 | while(l <= buy_num && buy[l].first < p) l ++; 75 | while(r <= sell_num && sell[r].first <= p) r ++; 76 | 77 | long long tmp = min(pre1[buy_num] - pre1[l - 1], pre2[r - 1] - pre2[0]); 78 | // cout << ps[i] << " " << l << " " << r << " " << tmp << endl; 79 | if (tmp >= ans) { 80 | ans = tmp; 81 | p0 = p; 82 | } 83 | } 84 | 85 | printf("%.2f %lld\n", p0, ans); 86 | 87 | return 0; 88 | } -------------------------------------------------------------------------------- /201412/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int n, m; 8 | const int N = 1010; 9 | const int INF = 0x3f3f3f3f; 10 | int g[N][N]; // 邻接矩阵,存储所有边 11 | int dist[N]; // 存储其他点到当前最小生成树的距离 12 | bool st[N]; // 存储每个点是否已经在生成树中 13 | 14 | 15 | // 如果图不连通,则返回INF(值是0x3f3f3f3f), 否则返回最小生成树的树边权重之和 16 | int prim() { 17 | memset(dist, 0x3f, sizeof dist); 18 | 19 | int res = 0; 20 | for (int i = 0; i < n; i ++ ) { 21 | int t = -1; 22 | for (int j = 1; j <= n; j ++ ) 23 | if (!st[j] && (t == -1 || dist[t] > dist[j])) 24 | t = j; 25 | 26 | if (i && dist[t] == INF) return INF; 27 | 28 | if (i) res += dist[t]; 29 | st[t] = true; 30 | 31 | for (int j = 1; j <= n; j ++ ) dist[j] = min(dist[j], g[t][j]); 32 | } 33 | 34 | return res; 35 | } 36 | 37 | int main() { 38 | cin >> n >> m; 39 | memset(g, 0x3f, sizeof g); 40 | 41 | for (int i = 0; i < m; i++) { 42 | int a, b, c; 43 | cin >> a >> b >> c; 44 | g[a][b] = g[b][a] = c; 45 | } 46 | 47 | cout << prim() << endl; 48 | 49 | return 0; 50 | 51 | } -------------------------------------------------------------------------------- /201412/5.cpp: -------------------------------------------------------------------------------- 1 | // 网络流 暂时是CV的, 复习之后重做 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | const int N = 710, M = (N * 3 + 500 * 7 * 2) * 2 + 10, INF = 0x3f3f3f3f; 9 | 10 | int n, m, S, T; 11 | int h[N], e[M], f[M], w[M], ne[M], idx; 12 | int q[N], d[N], pre[N], incf[N]; 13 | bool st[N]; 14 | 15 | int get(int a, int b) 16 | { 17 | if (b == 8) b = 1; 18 | return (a - 1) * 7 + b; 19 | } 20 | 21 | void add(int a, int b, int c, int d) 22 | { 23 | e[idx] = b, f[idx] = c, w[idx] = d, ne[idx] = h[a], h[a] = idx ++ ; 24 | e[idx] = a, f[idx] = 0, w[idx] = -d, ne[idx] = h[b], h[b] = idx ++ ; 25 | } 26 | 27 | bool spfa() 28 | { 29 | int hh = 0, tt = 1; 30 | memset(d, 0x3f, sizeof d); 31 | memset(incf, 0, sizeof incf); 32 | q[0] = S, d[S] = 0, incf[S] = INF; 33 | while (hh != tt) 34 | { 35 | int t = q[hh ++ ]; 36 | if (hh == N) hh = 0; 37 | st[t] = false; 38 | 39 | for (int i = h[t]; ~i; i = ne[i]) 40 | { 41 | int ver = e[i]; 42 | if (f[i] && d[ver] > d[t] + w[i]) 43 | { 44 | d[ver] = d[t] + w[i]; 45 | pre[ver] = i; 46 | incf[ver] = min(incf[t], f[i]); 47 | if (!st[ver]) 48 | { 49 | q[tt ++ ] = ver; 50 | if (tt == N) tt = 0; 51 | st[ver] = true; 52 | } 53 | } 54 | } 55 | } 56 | return incf[T] > 0; 57 | } 58 | 59 | void EK(int& flow, int& cost) 60 | { 61 | flow = cost = 0; 62 | while (spfa()) 63 | { 64 | int t = incf[T]; 65 | flow += t, cost += t * d[T]; 66 | for (int i = T; i != S; i = e[pre[i] ^ 1]) 67 | { 68 | f[pre[i]] -= t; 69 | f[pre[i] ^ 1] += t; 70 | } 71 | } 72 | } 73 | 74 | int main() 75 | { 76 | cin >> n >> m; 77 | S = 0, T = n * 7 + 1; 78 | memset(h, -1, sizeof h); 79 | for (int i = 1; i <= n; i ++ ) 80 | { 81 | int c, d; 82 | for (int j = 1; j <= 7; j ++ ) 83 | { 84 | cin >> c; 85 | add(S, get(i, j), c, 0); 86 | } 87 | for (int j = 1; j <= 7; j ++ ) 88 | { 89 | cin >> c; 90 | add(get(i, j), T, c, 0); 91 | } 92 | cin >> c >> d; 93 | for (int j = 1; j <= 7; j ++ ) 94 | add(get(i, j), get(i, j + 1), c, d); 95 | } 96 | while (m -- ) 97 | { 98 | int a, b, c; 99 | cin >> a >> b >> c; 100 | for (int i = 1; i <= 7; i ++ ) 101 | { 102 | add(get(a, i), get(b, i), INF, c); 103 | add(get(b, i), get(a, i), INF, c); 104 | } 105 | } 106 | int flow, cost; 107 | EK(flow, cost); 108 | cout << cost << endl; 109 | return 0; 110 | } -------------------------------------------------------------------------------- /201503/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 1010; 8 | int g[N][N]; 9 | int m, n; 10 | 11 | int main() { 12 | cin >> m >> n; 13 | for (int i = 0; i < m; i++) { 14 | for (int j = 0; j < n; j++) { 15 | cin >> g[i][j]; 16 | } 17 | } 18 | 19 | for (int i = n - 1; i >= 0; i--) { 20 | for (int j = 0; j < m; j++) { 21 | cout << g[j][i] << " "; 22 | } 23 | cout << endl; 24 | } 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /201503/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef pair pii; 8 | const int N = 1010; 9 | pii p[N]; 10 | int n; 11 | 12 | bool cmp(pii p1, pii p2) { 13 | if (p1.first > p2.first) return true; 14 | else if (p1.first < p2.first) return false; 15 | else { 16 | if (p1.second < p2.second) return true; 17 | else return false; 18 | } 19 | } 20 | 21 | int main() { 22 | cin >> n; 23 | for (int i = 0; i < n; i++) { 24 | int x; 25 | cin >> x; 26 | p[x].first ++; 27 | p[x].second = x; 28 | } 29 | 30 | sort(p, p + N, cmp); 31 | 32 | for (int i = 0; i < n; i++) { 33 | if (p[i].first != 0) 34 | cout << p[i].second << " " << p[i].first << endl; 35 | } 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /201503/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int month[] = { 8 | 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 9 | }; 10 | 11 | // 判断 是否为 闰年 12 | int is_leap(int year) { 13 | if (year % 4 == 0 && year % 100 || year % 400 == 0) 14 | return 1; 15 | return 0; 16 | } 17 | 18 | 19 | // 获取 m 月 天数 20 | int get_month(int y, int m) { 21 | if (m == 2) return month[m] + is_leap(y); 22 | return month[m]; 23 | } 24 | 25 | int main() { 26 | int a, b, c, y1, y2; 27 | cin >> a >> b >> c >> y1 >> y2; 28 | 29 | int days = 0; 30 | for (int y = 1850; y <= y2; y ++ ) { 31 | for (int m = 1; m <= 12; m ++ ) { 32 | if (y >= y1 && m == a) { 33 | bool flag = true; 34 | int w = (1 + days) % 7; 35 | for (int i = 1, j = 0; i <= get_month(y, m); i ++ ) { 36 | if (w == c - 1) { 37 | if ( ++ j == b) { 38 | printf("%04d/%02d/%02d\n", y, m, i); 39 | flag = false; 40 | break; 41 | } 42 | } 43 | w = (w + 1) % 7; 44 | } 45 | if (flag) puts("none"); 46 | } 47 | days += get_month(y, m); 48 | } 49 | } 50 | return 0; 51 | } -------------------------------------------------------------------------------- /201503/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 20010, M = N; 8 | vector> g(N, vector()); 9 | int m, n, ans; 10 | 11 | int dfs(int u) { 12 | int d1 = 0, d2 = 0; 13 | for (int i = 0; i < g[u].size(); i++) { 14 | int idx = g[u][i]; 15 | int d = dfs(idx); 16 | if (d > d1) d2 = d1, d1 = d; 17 | else if (d > d2) d2 = d; 18 | } 19 | 20 | ans = max(ans, d1 + d2); 21 | 22 | return d1 + 1; 23 | } 24 | 25 | int main() { 26 | cin >> n >> m; 27 | for (int i = 2; i <= n; i++) { 28 | int p; 29 | cin >> p; 30 | g[p].push_back(i); 31 | } 32 | 33 | for (int i = n + 1; i <= n + m; i++) { 34 | int p; 35 | cin >> p; 36 | g[p].push_back(i); 37 | } 38 | 39 | dfs(1); 40 | 41 | cout << ans << endl;; 42 | 43 | return 0; 44 | } -------------------------------------------------------------------------------- /201503/5.cpp: -------------------------------------------------------------------------------- 1 | // cv 点分治不会 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define x first 8 | #define y second 9 | 10 | using namespace std; 11 | 12 | typedef long long LL; 13 | typedef pair PII; 14 | const int N = 100010, M = N * 2, INF = 0x3f3f3f3f; 15 | 16 | int n, m; 17 | int pr[N]; 18 | int h[N], e[M], w[M], ne[M], idx; 19 | bool st[N]; 20 | struct Query 21 | { 22 | int a, b; 23 | LL c; 24 | }query[N]; 25 | vector Q[N]; 26 | int bel[N]; 27 | int fp[N][17], fu[N][17], dist[N], cm[N]; 28 | LL cs[N]; 29 | vector qt[N]; 30 | LL cs2[N]; 31 | int minp[N], minu[N], top; 32 | 33 | void add(int a, int b, int c) 34 | { 35 | e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ; 36 | } 37 | 38 | int get_size(int u, int fa) // 求子树大小 39 | { 40 | if (st[u]) return 0; 41 | int res = 1; 42 | for (int i = h[u]; ~i; i = ne[i]) 43 | if (e[i] != fa) 44 | res += get_size(e[i], u); 45 | return res; 46 | } 47 | 48 | int get_wc(int u, int fa, int tot, int& wc) // 求重心 49 | { 50 | if (st[u]) return 0; 51 | int sum = 1, ms = 0; 52 | for (int i = h[u]; ~i; i = ne[i]) 53 | { 54 | int j = e[i]; 55 | if (j == fa) continue; 56 | int t = get_wc(j, u, tot, wc); 57 | ms = max(ms, t); 58 | sum += t; 59 | } 60 | ms = max(ms, tot - sum); 61 | if (ms <= tot / 2) wc = u; 62 | return sum; 63 | } 64 | 65 | void dfs1(int u, int fa, int id) 66 | { 67 | if (st[u]) return; 68 | bel[u] = id, qt[u].clear(); 69 | fp[u][0] = fa, fu[u][0] = u; 70 | for (int i = 1; i < 17; i ++ ) 71 | { 72 | int p = fp[u][i - 1]; 73 | fp[u][i] = fp[p][i - 1]; 74 | if (pr[fu[u][i - 1]] <= pr[fu[p][i - 1]]) fu[u][i] = fu[u][i - 1]; 75 | else fu[u][i] = fu[p][i - 1]; 76 | } 77 | if (pr[fu[u][16]] == pr[u]) // 最小的是自己 78 | { 79 | cs[u] = (LL)dist[u] * pr[u]; 80 | cm[u] = pr[u]; 81 | } 82 | else 83 | { 84 | int x = u; 85 | for (int i = 16; i; i -- ) 86 | if (pr[fu[x][i - 1]] >= pr[u]) 87 | x = fp[x][i - 1]; 88 | 89 | cs[u] = (LL)(dist[u] - dist[x]) * pr[u] + cs[x]; 90 | cm[u] = cm[x]; 91 | } 92 | 93 | for (int i = h[u]; ~i; i = ne[i]) 94 | { 95 | int j = e[i]; 96 | if (j != fa && !st[j]) 97 | { 98 | dist[j] = dist[u] + w[i]; 99 | dfs1(j, u, id); 100 | } 101 | } 102 | } 103 | 104 | void dfs2(int u, int fa) 105 | { 106 | if (st[u]) return; 107 | if (minp[top - 1] < pr[u]) minp[top] = minp[top - 1], minu[top] = minu[top - 1]; 108 | else minp[top] = pr[u], minu[top] = u; 109 | top ++ ; 110 | 111 | for (int i = 0; i < qt[u].size(); i ++ ) 112 | { 113 | int a = qt[u][i].x, k = qt[u][i].y; 114 | if (cm[a] <= minp[top - 1]) query[k].c = cs[a] + (LL)dist[u] * cm[a]; 115 | else 116 | { 117 | int l = 0, r = top - 1; 118 | while (l < r) 119 | { 120 | int mid = l + r >> 1; 121 | if (minp[mid] < cm[a]) r = mid; 122 | else l = mid + 1; 123 | } 124 | int ver = minu[r]; 125 | query[k].c = cs[a] + cs2[u] - cs2[ver] + (LL)dist[ver] * cm[a]; 126 | } 127 | } 128 | 129 | for (int i = h[u]; ~i; i = ne[i]) 130 | { 131 | int j = e[i]; 132 | if (j != fa && !st[j]) 133 | { 134 | cs2[j] = cs2[u] + (LL)minp[top - 1] * w[i]; 135 | dfs2(j, u); 136 | } 137 | } 138 | top -- ; 139 | } 140 | 141 | void calc(int u) 142 | { 143 | if (st[u]) return; 144 | vector q; 145 | q.swap(Q[u]); 146 | get_wc(u, -1, get_size(u, -1), u); 147 | st[u] = true; // 删除重心 148 | 149 | for (int i = 0; i < 17; i ++ ) fp[u][i] = 0, fu[u][i] = u; 150 | dist[u] = cs[u] = 0, cm[u] = pr[u], bel[u] = 0, qt[u].clear(); 151 | for (int i = h[u]; ~i; i = ne[i]) 152 | { 153 | int j = e[i]; 154 | dist[j] = dist[u] + w[i]; 155 | dfs1(j, u, j); 156 | } 157 | 158 | for (int i = 0; i < q.size(); i ++ ) 159 | { 160 | int k = q[i]; 161 | int a = query[k].a, b = query[k].b; 162 | if (b == u) query[k].c = cs[a]; 163 | else if (bel[a] == bel[b]) Q[bel[a]].push_back(k); 164 | else qt[b].push_back({a, k}); 165 | } 166 | 167 | cs2[u] = 0, minp[0] = pr[u], minu[0] = u, top = 1; 168 | for (int i = h[u]; ~i; i = ne[i]) 169 | { 170 | int j = e[i]; 171 | cs2[j] = cs2[u] + (LL)minp[top - 1] * w[i]; 172 | dfs2(j, u); 173 | } 174 | 175 | for (int i = h[u]; ~i; i = ne[i]) 176 | { 177 | int j = e[i]; 178 | if (Q[j].size()) calc(j); 179 | } 180 | } 181 | 182 | int main() 183 | { 184 | scanf("%d%d", &n, &m); 185 | pr[0] = INF; 186 | for (int i = 1; i <= n; i ++ ) scanf("%d", &pr[i]); 187 | memset(h, -1, sizeof h); 188 | for (int i = 0; i < n - 1; i ++ ) 189 | { 190 | int a, b, c; 191 | scanf("%d%d%d", &a, &b, &c); 192 | add(a, b, c), add(b, a, c); 193 | } 194 | 195 | for (int i = 0; i < m; i ++ ) 196 | { 197 | int a, b; 198 | scanf("%d%d", &a, &b); 199 | query[i] = {a, b}; 200 | if (a != b) Q[1].push_back(i); 201 | } 202 | 203 | calc(1); 204 | 205 | for (int i = 0; i < m; i ++ ) 206 | printf("%lld\n", query[i].c); 207 | 208 | return 0; 209 | } -------------------------------------------------------------------------------- /201509/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 1010; 8 | int a[N]; 9 | int n, ans; 10 | 11 | int main() { 12 | cin >> n; 13 | for (int i = 0; i < n; i++) cin >> a[i]; 14 | 15 | int pre = -1; 16 | for (int i = 0; i < n; i++) { 17 | if (a[i] != pre) ans ++, pre = a[i]; 18 | } 19 | 20 | cout << ans << endl; 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /201509/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int month[] = { 8 | 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 9 | }; 10 | 11 | // 判断 是否为 闰年 12 | int is_leap(int year) { 13 | if (year % 4 == 0 && year % 100 || year % 400 == 0) 14 | return 1; 15 | return 0; 16 | } 17 | 18 | // 获取 m 月 天数 19 | int get_month(int y, int m) { 20 | if (m == 2) return month[m] + is_leap(y); 21 | return month[m]; 22 | } 23 | 24 | int main() { 25 | int year, day; 26 | cin >> year >> day; 27 | 28 | int mon = 0, da = 0; 29 | for (int i = 1; i <= 12; i++) { 30 | int d = get_month(year, i); 31 | if (d >= day) { 32 | mon = i; 33 | da = day; 34 | break; 35 | } 36 | else day -= d; 37 | } 38 | 39 | cout << mon << endl; 40 | cout << da << endl; 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /201509/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | const int N = 110; 9 | int n, m; 10 | string a[N]; 11 | map mp; 12 | 13 | void output(string str) { 14 | int n = (int)str.size(); 15 | int l = 0; 16 | string ans; 17 | while(l < n) { 18 | if (str[l] == '{') { 19 | if (l + 2 < n && str[l + 1] == '{' && str[l + 2] == ' ') { 20 | string var; 21 | l += 3; 22 | while(l < n && str[l] != ' ') { 23 | var += str[l]; 24 | l ++; 25 | } 26 | ans += mp[var]; 27 | l += 3; 28 | } 29 | else { 30 | ans += str[l]; 31 | l ++; 32 | } 33 | } 34 | else { 35 | ans += str[l]; 36 | l ++; 37 | } 38 | } 39 | 40 | cout << ans << endl; 41 | } 42 | 43 | int main() { 44 | cin >> m >> n; 45 | getchar(); 46 | 47 | for (int i = 0; i < m; i++) { 48 | getline(cin, a[i]); 49 | } 50 | 51 | for (int i = 0; i < n; i++) { 52 | string var, val; 53 | string line; 54 | getline(cin, line); 55 | 56 | int k = 0, n = line.size(); 57 | while(k < n) { 58 | if (line[k] != ' ') var += line[k], k ++; 59 | else break; 60 | } 61 | 62 | val = line.substr(k + 2, n - k - 3); 63 | mp[var] = val; 64 | } 65 | 66 | for (int i = 0; i < m; i++) { 67 | output(a[i]); 68 | } 69 | 70 | return 0; 71 | } -------------------------------------------------------------------------------- /201509/4.cpp: -------------------------------------------------------------------------------- 1 | // tarjan 算法 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | const int N = 10010, M = 100010; 9 | 10 | int n, m; 11 | int h[N], e[M], ne[M], idx; 12 | int dfn[N], low[N]; 13 | int stk[N], top, ts; 14 | bool in_stk[N]; 15 | int ans; 16 | 17 | void add(int a, int b) { 18 | e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ; 19 | } 20 | 21 | void tarjan(int u) { 22 | dfn[u] = low[u] = ++ ts; 23 | stk[ ++ top] = u, in_stk[u] = true; 24 | for (int i = h[u]; ~i; i = ne[i]) { 25 | int j = e[i]; 26 | if (!dfn[j]) { 27 | tarjan(j); 28 | low[u] = min(low[u], low[j]); 29 | } 30 | else if (in_stk[j]) low[u] = min(low[u], dfn[j]); 31 | } 32 | 33 | if (dfn[u] == low[u]) { 34 | int y, cnt = 0; 35 | do { 36 | y = stk[top -- ]; 37 | in_stk[y] = false; 38 | cnt ++ ; 39 | } while (y != u); 40 | ans += cnt * (cnt - 1) / 2; 41 | } 42 | } 43 | 44 | int main() { 45 | scanf("%d%d", &n, &m); 46 | memset(h, -1, sizeof h); 47 | while (m -- ) { 48 | int a, b; 49 | scanf("%d%d", &a, &b); 50 | add(a, b); 51 | } 52 | 53 | for (int i = 1; i <= n; i ++ ) 54 | if (!dfn[i]) 55 | tarjan(i); 56 | printf("%d\n", ans); 57 | 58 | return 0; 59 | } -------------------------------------------------------------------------------- /201509/5.cpp: -------------------------------------------------------------------------------- 1 | // AC 自动机 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef long long LL; 9 | const int N = 110; 10 | const LL INF = 1e18; 11 | 12 | int n; 13 | LL m; 14 | int tr[N][26], cnt[N], ne[N], idx; 15 | int q[N]; 16 | LL ans[N][N], w[N][N]; 17 | 18 | void insert(char* str) { 19 | int p = 0; 20 | for (int i = 0; str[i]; i ++ ) { 21 | int u = str[i] - 'a'; 22 | if (!tr[p][u]) tr[p][u] = ++ idx; 23 | p = tr[p][u]; 24 | } 25 | cnt[p] ++ ; 26 | } 27 | 28 | void build() { 29 | int hh = 0, tt = -1; 30 | for (int i = 0; i < 26; i ++ ) 31 | if (tr[0][i]) 32 | q[ ++ tt] = tr[0][i]; 33 | while (hh <= tt) { 34 | int t = q[hh ++ ]; 35 | for (int i = 0; i < 26; i ++ ) { 36 | int p = tr[t][i]; 37 | if (!p) tr[t][i] = tr[ne[t]][i]; 38 | else { 39 | ne[p] = tr[ne[t]][i]; 40 | cnt[p] += cnt[ne[p]]; 41 | q[ ++ tt] = p; 42 | } 43 | } 44 | } 45 | } 46 | 47 | void mul(LL c[][N], LL a[][N], LL b[][N]) { 48 | static LL tmp[N][N]; 49 | memset(tmp, -0x3f, sizeof tmp); 50 | for (int i = 0; i <= idx; i ++ ) 51 | for (int j = 0; j <= idx; j ++ ) 52 | for (int k = 0; k <= idx; k ++ ) 53 | tmp[i][j] = max(tmp[i][j], a[i][k] + b[k][j]); 54 | memcpy(c, tmp, sizeof tmp); 55 | } 56 | 57 | int main() { 58 | cin >> n >> m; 59 | char str[N]; 60 | while (n -- ) { 61 | cin >> str; 62 | insert(str); 63 | } 64 | build(); 65 | 66 | memset(w, -0x3f, sizeof w); 67 | for (int i = 0; i <= idx; i ++ ) 68 | for (int j = 0; j < 26; j ++ ) { 69 | int k = tr[i][j]; 70 | w[i][k] = max(w[i][k], (LL)cnt[k]); 71 | } 72 | 73 | for (int i = 1; i <= idx; i ++ ) ans[0][i] = -INF; 74 | while (m) { 75 | if (m & 1) mul(ans, ans, w); 76 | mul(w, w, w); 77 | m >>= 1; 78 | } 79 | LL res = 0; 80 | for (int i = 0; i <= idx; i ++ ) res = max(res, ans[0][i]); 81 | cout << res << endl; 82 | 83 | return 0; 84 | } -------------------------------------------------------------------------------- /201512/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int n; 8 | 9 | int main() { 10 | cin >> n; 11 | 12 | int sum = 0; 13 | while(n) { 14 | sum += n % 10; 15 | n /= 10; 16 | } 17 | 18 | cout << sum << endl; 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /201512/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 33; 8 | 9 | int n, m; 10 | int g[N][N]; 11 | bool st[N][N]; 12 | 13 | int main() 14 | { 15 | cin >> n >> m; 16 | for (int i = 0; i < n; i ++ ) 17 | for (int j = 0; j < m; j ++ ) 18 | cin >> g[i][j]; 19 | 20 | for (int i = 0; i < n; i ++ ) 21 | for (int j = 0; j < m; j ++ ) { 22 | int l = j, r = j, u = i, d = i, x = g[i][j]; 23 | while (l >= 0 && g[i][l] == x) l -- ; 24 | while (r < m && g[i][r] == x) r ++ ; 25 | while (u >= 0 && g[u][j] == x) u -- ; 26 | while (d < n && g[d][j] == x) d ++ ; 27 | st[i][j] = r - l - 1 >= 3 || d - u - 1 >= 3; 28 | } 29 | 30 | for (int i = 0; i < n; i ++ ) { 31 | for (int j = 0; j < m; j ++ ) 32 | if (st[i][j]) cout << 0 << ' '; 33 | else cout << g[i][j] << ' '; 34 | cout << endl; 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /201512/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define x first 7 | #define y second 8 | 9 | using namespace std; 10 | 11 | typedef pair pii; 12 | 13 | const int N = 110; 14 | bool visited[N][N]; 15 | char g[N][N]; 16 | int st[N][N]; 17 | int m, n, q; 18 | 19 | 20 | // st 0 : '.' 1 : '-' 2 : '|' 3 : '+' 21 | void op0(int x1, int y1, int x2, int y2) { 22 | if (y1 == y2) { 23 | int x_min = min(x1, x2), x_max = max(x1, x2); 24 | for (int i = x_min; i <= x_max; i++) { 25 | if (st[i][y1] == 0) st[i][y1] = 2; 26 | else if (st[i][y1] == 1) st[i][y1] = 3; 27 | else continue; 28 | } 29 | } 30 | else { 31 | int y_min = min(y1, y2), y_max = max(y1, y2); 32 | for (int i = y_min; i <= y_max; i++) { 33 | if (st[x1][i] == 0) st[x1][i] = 1; 34 | else if (st[x1][i] == 2) st[x1][i] = 3; 35 | else continue; 36 | } 37 | } 38 | } 39 | 40 | void op1(int sx, int sy, char ch) { 41 | int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; 42 | memset(visited, false, sizeof visited); 43 | queue q; 44 | q.push({sx, sy}); 45 | visited[sx][sy] = true; 46 | 47 | while(q.size()) { 48 | pii p = q.front(); 49 | q.pop(); 50 | int x = p.x, y = p.y; 51 | 52 | g[x][y] = ch; 53 | for (int i = 0; i < 4; i++) { 54 | int a = x + dx[i], b = y + dy[i]; 55 | if (a < 0 || a >= n || b < 0 || b >= m || visited[a][b]) continue; 56 | if (st[a][b] != 0) continue; 57 | 58 | q.push({a, b}); 59 | visited[a][b] = true; 60 | } 61 | } 62 | } 63 | 64 | int main() { 65 | cin >> m >> n >> q; 66 | 67 | for (int i = 0; i < n; i++) { 68 | for (int j = 0; j < m; j++) { 69 | g[i][j] = '.'; 70 | } 71 | } 72 | 73 | while(q --) { 74 | int op; 75 | cin >> op; 76 | if (op == 0) { 77 | int x1, x2, y1, y2; 78 | cin >> x1 >> y1 >> x2 >> y2; 79 | op0(n - 1 - y1, x1, n - 1 - y2, x2); 80 | } 81 | else { 82 | int x, y; 83 | char ch; 84 | cin >> x >> y >> ch; 85 | op1(n - 1 - y, x, ch); 86 | } 87 | } 88 | 89 | for (int i = 0; i < n; i++) { 90 | for (int j = 0; j < m; j++) { 91 | if (st[i][j] == 1) g[i][j] = '-'; 92 | if (st[i][j] == 2) g[i][j] = '|'; 93 | if (st[i][j] == 3) g[i][j] = '+'; 94 | 95 | cout << g[i][j]; 96 | } 97 | cout << endl; 98 | } 99 | 100 | 101 | 102 | return 0; 103 | } -------------------------------------------------------------------------------- /201512/4.cpp: -------------------------------------------------------------------------------- 1 | // 欧拉路径 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | const int N = 10010, M = 100010; 10 | 11 | int n, m; 12 | set g[N]; 13 | int p[N]; 14 | int ans[M], top; 15 | 16 | int find(int x) { 17 | if (p[x] != x) p[x] = find(p[x]); 18 | return p[x]; 19 | } 20 | 21 | void dfs(int u) { 22 | while (g[u].size()) { 23 | int t = *g[u].begin(); 24 | g[u].erase(t), g[t].erase(u); 25 | dfs(t); 26 | } 27 | ans[ ++ top] = u; 28 | } 29 | 30 | int main() { 31 | scanf("%d%d", &n, &m); 32 | for (int i = 1; i <= n; i ++ ) p[i] = i; 33 | while (m -- ) { 34 | int a, b; 35 | scanf("%d%d", &a, &b); 36 | g[a].insert(b), g[b].insert(a); 37 | p[find(a)] = find(b); 38 | } 39 | 40 | int s = 0; 41 | for (int i = 1; i <= n; i ++ ) { 42 | if (find(i) != find(1)) { 43 | puts("-1"); 44 | return 0; 45 | } 46 | else if (g[i].size() % 2) s ++ ; 47 | } 48 | 49 | // 存在欧拉路径 则 奇顶点个数为 0 或 2 且 如果为2,则起点度数为奇数 50 | if (s != 0 && s != 2 || s == 2 && g[1].size() % 2 == 0) { 51 | puts("-1"); 52 | return 0; 53 | } 54 | 55 | dfs(1); 56 | 57 | for (int i = top; i; i --) printf("%d ", ans[i]); 58 | 59 | return 0; 60 | } -------------------------------------------------------------------------------- /201512/5.cpp: -------------------------------------------------------------------------------- 1 | // cv bitset 快速幂 (不开优化只有70分) 2 | 3 | #pragma GCC optimize(2) 4 | #pragma GCC optimize(3) 5 | #pragma GCC optimize("inline") 6 | #pragma GCC optimize("-fgcse-lm") 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #define rint register int 13 | using namespace std; 14 | 15 | typedef unsigned int U; 16 | 17 | const int N = 1000, W = 32; 18 | 19 | 20 | inline int cb(U x){ 21 | x = x ^ (x >> 1); 22 | x = x ^ (x >> 2); 23 | x = x ^ (x >> 4); 24 | x = x ^ (x >> 8); 25 | x = x ^ (x >> 16); 26 | return x; 27 | } 28 | 29 | struct Bit{ 30 | U a[32]; 31 | void inline set1(int x) { 32 | a[x >> 5] |= 1ull << (x & 31); 33 | } 34 | void inline set0(int x) { 35 | a[x >> 5] &= ~(1ull << (x & 31)); 36 | } 37 | int inline get(int x) { 38 | return a[x >> 5] >> (x & 31) & 1; 39 | } 40 | void inline clear() { 41 | memset(a, 0, sizeof a); 42 | } 43 | Bit operator & (const Bit &b) const { 44 | Bit c; 45 | for (rint i = 0; i < 32; i++) 46 | c.a[i] = a[i] & b.a[i]; 47 | return c; 48 | } 49 | bool inline count() { 50 | int res = 0; 51 | for (rint i = 0; i < 32; i++) 52 | res ^= cb(a[i]); 53 | return res & 1; 54 | } 55 | }; 56 | 57 | struct Mat{ 58 | Bit w[N], bw[N]; 59 | int n, m; 60 | Mat inline operator * (const Mat &b) const { 61 | Mat c; c.n = n, c.m = b.m; 62 | for (int i = 0; i < n; i++) { 63 | for (int j = 0; j < c.m; j++) { 64 | if ((w[i] & b.bw[j]).count()) 65 | c.w[i].set1(j), c.bw[j].set1(i); 66 | else c.w[i].set0(j), c.bw[j].set0(i); 67 | } 68 | } 69 | return c; 70 | } 71 | } s, A[30]; 72 | 73 | int m; 74 | 75 | char str[N + 5]; 76 | 77 | int main() { 78 | scanf("%d", &m); 79 | A[0].n = A[0].m = m, s.n = 1, s.m = m; 80 | for (int i = 0; i < m; i++) { 81 | scanf("%s", str); 82 | for (int j = 0; j < m; j++) { 83 | if (str[j] == '1') A[0].w[j].set1(i), A[0].bw[i].set1(j); 84 | } 85 | } 86 | scanf("%s", str); 87 | for (int i = 0; i < m; i++) { 88 | if (str[i] == '1') s.w[0].set1(i), s.bw[i].set1(0); 89 | } 90 | for (int i = 1; i < 30; i++) A[i] = A[i - 1] * A[i - 1]; 91 | int n; scanf("%d", &n); 92 | while (n--) { 93 | int b; scanf("%d", &b); 94 | Mat res = s; 95 | for (int i = 0; i < 30; i++) { 96 | if (b >> i & 1) res = res * A[i]; 97 | } 98 | for (int i = 0; i < m; i++) putchar(res.w[0].get(i) ? '1' : '0'); 99 | puts(""); 100 | } 101 | } -------------------------------------------------------------------------------- /201604/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 1010; 8 | int a[N]; 9 | int n; 10 | 11 | int main() { 12 | cin >> n; 13 | for (int i = 0; i < n; i++ ) cin >> a[i]; 14 | 15 | int ans = 0; 16 | for (int i = 1; i < n - 1; i++) { 17 | if (a[i] - a[i - 1] < 0 && a[i] - a[i + 1] < 0) ans ++; 18 | else if (a[i] - a[i - 1] > 0 && a[i] - a[i + 1] > 0) ans ++; 19 | } 20 | 21 | cout << ans << endl; 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /201604/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define x first 6 | #define y second 7 | 8 | using namespace std; 9 | 10 | typedef pair pii; 11 | 12 | pii p[20]; 13 | char g[20][20]; 14 | char a[5][5]; 15 | int sy, cnt; 16 | 17 | int main() { 18 | for (int i = 0; i < 15; i++) { 19 | for (int j = 0; j < 10; j++) { 20 | cin >> g[i][j]; 21 | } 22 | } 23 | for (int i = 0; i < 4; i++) { 24 | for (int j = 0; j < 4; j++) { 25 | cin >> a[i][j]; 26 | } 27 | } 28 | cin >> sy; 29 | sy -= 1; 30 | 31 | for (int i = 0; i < 4; i++) { 32 | for (int j = 0; j < 4; j++) { 33 | if (a[i][j] == '1') { 34 | p[cnt ++] = {i, j + sy}; 35 | } 36 | } 37 | } 38 | 39 | bool flag = true; 40 | while(true) { 41 | for (int i = 0; i < cnt; i++) { 42 | pii po = p[i]; 43 | int x = po.x, y = po.y; 44 | if (x >= 15 || g[x][y] == '1') { 45 | flag = false; 46 | break; 47 | } 48 | } 49 | 50 | if (!flag) break; 51 | else { 52 | for (int i = 0; i < cnt; i++) { 53 | p[i].x = p[i].x + 1; 54 | } 55 | } 56 | } 57 | 58 | for (int i = 0; i < cnt; i++) { 59 | int x = p[i].x - 1, y = p[i].y; 60 | g[x][y] = '1'; 61 | } 62 | 63 | for (int i = 0; i < 15; i++) { 64 | for (int j = 0; j < 10; j++) { 65 | cout << g[i][j] << " "; 66 | } 67 | cout << endl; 68 | } 69 | 70 | return 0; 71 | } -------------------------------------------------------------------------------- /201604/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int p; 9 | string cur; 10 | 11 | vector walk(string path) { 12 | vector res; 13 | path += string(1, '/'); 14 | 15 | int n = path.size(); 16 | int l = 0, r = 1; 17 | while(r < n) { 18 | while(r < n && path[r] != '/') r ++; 19 | res.push_back(path.substr(l + 1, r - l - 1)); 20 | l = r; 21 | r ++; 22 | } 23 | 24 | return res; 25 | } 26 | 27 | void output(string path) { 28 | vector res; 29 | if (path.size() == 1 && path[0] == '/') { 30 | cout << string(1, '/') << endl; 31 | return ; 32 | } 33 | else if (path[0] == '/') { 34 | res = walk(path); 35 | } 36 | else { 37 | path = cur + string(1, '/') + path; 38 | res = walk(path); 39 | } 40 | 41 | stack stk; 42 | for (auto& str : res) { 43 | if (str == ".") continue; 44 | else if (str == "..") { 45 | if (stk.empty()) continue; 46 | else stk.pop(); 47 | } 48 | else stk.push(str); 49 | } 50 | 51 | vector ans; 52 | while(stk.size()) { 53 | ans.push_back(stk.top()); 54 | stk.pop(); 55 | } 56 | reverse(ans.begin(), ans.end()); 57 | 58 | string outp(1, '/'); 59 | for (auto s : ans) { 60 | outp += s + string(1, '/'); 61 | } 62 | 63 | if (outp.size() == 1) cout << "/" << endl; 64 | else cout << outp.substr(0, outp.size() - 1) << endl; 65 | } 66 | 67 | string pretreat(string path) { 68 | string ans; 69 | int n = path.size(); 70 | 71 | int cur = 0; 72 | while(cur < n) { 73 | if (path[cur] == '/') { 74 | cur ++; 75 | ans += string(1, '/'); 76 | while(path[cur] == '/') cur ++; 77 | } 78 | else { 79 | ans += path[cur]; 80 | cur ++; 81 | } 82 | } 83 | if (ans.size() == 1 && ans[0] == '/') return string(1, '/'); 84 | else if (ans[ans.size() - 1] == '/') return ans.substr(0, ans.size() - 1); 85 | else return ans; 86 | } 87 | 88 | int main() { 89 | cin >> p; 90 | cin >> cur; 91 | cur = pretreat(cur); 92 | getchar(); 93 | 94 | while(p --) { 95 | string tmp; 96 | getline(cin, tmp); 97 | if (tmp.empty()) tmp = cur; 98 | tmp = pretreat(tmp); 99 | output(tmp); 100 | } 101 | 102 | return 0; 103 | } -------------------------------------------------------------------------------- /201604/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define x first 7 | #define y second 8 | 9 | using namespace std; 10 | 11 | typedef pair pii; 12 | 13 | const int N = 110; 14 | bool st[N][N]; 15 | int danger[N][N][2]; 16 | int n, m, t, ans; 17 | 18 | void bfs(int sx, int sy) { 19 | int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; 20 | queue q; 21 | q.push({sx, sy}); 22 | st[sx][sy] = true; 23 | 24 | while(q.size()) { 25 | int size = q.size(); 26 | ans ++; 27 | for (int i = 0; i < size; i++) { 28 | pii p = q.front(); 29 | q.pop(); 30 | 31 | int x = p.x, y = p.y; 32 | 33 | for (int j = 0; j < 4; j++) { 34 | int a = x + dx[j], b = y + dy[j]; 35 | 36 | if (a < 1 || a > n || b < 1 || b > m) continue; 37 | int mi = danger[a][b][0], ma = danger[a][b][1]; 38 | if (ans >= mi && ans <= ma) continue; 39 | if (st[a][b]) continue; 40 | 41 | if (a == n && b == m) return ; 42 | else { 43 | q.push({a, b}); 44 | st[a][b] = true; 45 | } 46 | } 47 | st[x][y] = false; 48 | } 49 | } 50 | } 51 | 52 | int main() { 53 | cin >> n >> m >> t; 54 | while(t --) { 55 | int r, c, a, b; 56 | cin >> r >> c >> a >> b; 57 | danger[r][c][0] = a, danger[r][c][1] = b; 58 | } 59 | 60 | bfs(1, 1); 61 | 62 | cout << ans << endl; 63 | 64 | return 0; 65 | } -------------------------------------------------------------------------------- /201604/5.cpp: -------------------------------------------------------------------------------- 1 | // cv 连通性DP 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | const int N = 510, M = 1 << 6, H = 1e6, INF = 0x3f3f3f3f; 10 | 11 | int n, m, p; 12 | int w[N][6], es[N], ec[N][M]; 13 | int f[2][H], fs[2][H], ft[2]; 14 | char g[N]; 15 | int fa[7]; 16 | int fstate[224695][M]; 17 | 18 | int get(int state, int k) 19 | { 20 | return state >> k * 3 & 7; 21 | } 22 | 23 | void update(int i, int state, int cost) 24 | { 25 | if (state == -1) return; 26 | if (f[i][state] != -1) f[i][state] = min(f[i][state], cost); 27 | else 28 | { 29 | f[i][state] = cost; 30 | fs[i][ft[i] ++ ] = state; 31 | } 32 | } 33 | 34 | void clear(int k) 35 | { 36 | for (int i = 0; i < ft[k]; i ++ ) 37 | f[k][fs[k][i]] = -1; 38 | ft[k] = 0; 39 | } 40 | 41 | int find(int x) 42 | { 43 | if (fa[x] != x) fa[x] = find(fa[x]); 44 | return fa[x]; 45 | } 46 | 47 | int get_state(int state, int k) // 给最小表示法state增加边集k 48 | { 49 | if (k == 1) k = 0; 50 | if (fstate[state][k / 2] >= -1) return fstate[state][k / 2]; 51 | for (int i = 0; i <= p; i ++ ) 52 | if (get(state, i) || (k >> i & 1)) 53 | fa[i] = i; 54 | else fa[i] = -1; 55 | for (int i = 0; i <= p; i ++ ) 56 | if (get(state, i)) 57 | for (int j = i + 1; j <= p; j ++ ) 58 | if (get(state, j) == get(state, i)) 59 | fa[find(j)] = find(i); 60 | 61 | for (int i = 0; i <= p; i ++ ) 62 | if (k >> i & 1) 63 | { 64 | for (int j = i + 1; j <= p; j ++ ) 65 | if (k >> j & 1) 66 | fa[find(j)] = find(i); 67 | break; 68 | } 69 | 70 | if (fa[0] != -1) // 第一个点要么没被选,要么所在集合大小至少为2 71 | { 72 | bool flag = false; 73 | for (int i = 1; i <= p; i ++ ) 74 | if (fa[i] != -1 && find(i) == find(0)) 75 | { 76 | flag = true; 77 | break; 78 | } 79 | if (!flag) return fstate[state][k / 2] = -1; 80 | } 81 | 82 | int res = 0; 83 | for (int i = 1, cnt = 1; i <= p; i ++ ) 84 | if (!get(res, i - 1) && fa[i] != -1) 85 | { 86 | res += cnt << (i - 1) * 3; 87 | for (int j = i + 1; j <= p; j ++ ) 88 | if (!get(res, j - 1) && fa[j] != -1 && find(j) == find(i)) 89 | res += cnt << (j - 1) * 3; 90 | cnt ++ ; 91 | } 92 | return fstate[state][k / 2] = res; 93 | } 94 | 95 | int main() 96 | { 97 | memset(fstate, -0x3f, sizeof fstate); 98 | int T; 99 | scanf("%d", &T); 100 | while (T -- ) 101 | { 102 | scanf("%d%d%d", &n, &m, &p); 103 | memset(w, -1, sizeof w); 104 | scanf("%s", g + 1); 105 | while (m -- ) 106 | { 107 | int a, b, c; 108 | scanf("%d%d%d", &a, &b, &c); 109 | w[a][b - a - 1] = c; 110 | } 111 | memset(es, 0, sizeof es); 112 | memset(ec, 0, sizeof ec); 113 | for (int i = 1; i <= n; i ++ ) 114 | { 115 | for (int j = 0; j < p; j ++ ) 116 | if (w[i][j] != -1) 117 | { 118 | es[i] += 1 << j; 119 | } 120 | for (int j = 0; j < 1 << p; j ++ ) 121 | if ((j & es[i]) == j) 122 | for (int k = 0; k < p; k ++ ) 123 | if (j >> k & 1) 124 | ec[i][j] += w[i][k]; 125 | } 126 | memset(f, -1, sizeof f); 127 | update(1, 0, 0); 128 | int res = INF, maxp; 129 | for (int i = 1; i <= n; i ++ ) 130 | if (g[i] == '1') 131 | maxp = i; 132 | for (int i = 1; i <= n; i ++ ) 133 | { 134 | clear(i + 1 & 1); 135 | for (int j = 0; j < ft[i & 1]; j ++ ) 136 | { 137 | int state = fs[i & 1][j], cost = f[i & 1][state]; 138 | for (int k = 0; k < 1 << p; k ++ ) 139 | { 140 | if ((k & es[i]) != k) continue; 141 | if (g[i] == '1' && !get(state, 0) && !k) continue; 142 | update(i + 1 & 1, get_state(state, k * 2 + 1), cost + ec[i][k]); 143 | } 144 | if (i >= maxp && state == 1) res = min(res, cost); 145 | } 146 | } 147 | printf("%d\n", res); 148 | } 149 | 150 | return 0; 151 | } -------------------------------------------------------------------------------- /201609/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int n; 8 | int a[1010]; 9 | 10 | int main() { 11 | cin >> n; 12 | for (int i = 0; i < n; i++) cin >> a[i]; 13 | 14 | int diff = 0; 15 | for (int i = 1; i < n; i++) diff = max(abs(a[i] - a[i - 1]), diff); 16 | 17 | cout << diff << endl; 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /201609/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int n; 8 | int a[25]; 9 | 10 | int main() { 11 | cin >> n; 12 | for (int i = 1; i <= 20; i++) a[i] = 5; 13 | for (int i = 1; i <= n; i++) { 14 | int num; 15 | cin >> num; 16 | 17 | int j; 18 | for (j = 1; j <= 20; j++) { 19 | if (a[j] >= num) { 20 | for (int k = 1; k <= num; k ++) { 21 | int x = 5 * j - a[j] + k; 22 | cout << x << " "; 23 | } 24 | cout << endl; 25 | a[j] -= num; 26 | break; 27 | } 28 | } 29 | 30 | if (j == 21) { 31 | for (j = 1; j <= 20; j ++) { 32 | if (a[j] > 0 && a[j] <= num) { 33 | for (int k = 1; k <= a[j]; k ++) { 34 | int x = 5 * j - a[j] + k; 35 | cout << x << " "; 36 | } 37 | num -= a[j]; 38 | a[j] = 0; 39 | } 40 | else if(a[j] > 0 && a[j] > num) { 41 | for (int k = 1; k <= num; k ++) { 42 | int x = 5 * j - a[j] + k; 43 | cout << x << " "; 44 | } 45 | a[j]-= num; 46 | num = 0; 47 | } 48 | } 49 | cout << endl; 50 | } 51 | } 52 | 53 | return 0; 54 | } -------------------------------------------------------------------------------- /201609/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define x first 6 | #define y second 7 | 8 | using namespace std; 9 | 10 | typedef pair pii; 11 | 12 | const int N = 1010; 13 | int n; 14 | pii player[2][8]; 15 | 16 | int main() { 17 | cin >> n; 18 | int cnt = 0; 19 | player[0][0] = {0, 30}; 20 | player[1][0] = {0, 30}; 21 | 22 | while(n -- ) { 23 | string op; 24 | cin >> op; 25 | 26 | int idx = cnt % 2; 27 | if (op == "summon") { 28 | int pos, attack, health; 29 | cin >> pos >> attack >> health; 30 | for (int i = pos; i <= 7; i++) { 31 | int x = player[idx][i].x, y = player[idx][i].y; 32 | player[idx][i].x = attack, player[idx][i].y = health; 33 | attack = x, health = y; 34 | } 35 | } 36 | else if (op == "attack") { 37 | int pos1, pos2; 38 | cin >> pos1 >> pos2; 39 | int idx1 = idx, idx2 = 1 - idx; 40 | 41 | player[idx1][pos1].y -= player[idx2][pos2].x; 42 | player[idx2][pos2].y -= player[idx1][pos1].x; 43 | 44 | // check 生命值, 特判是否为 英雄 45 | if (player[idx1][pos1].y <= 0) { 46 | for (int i = pos1; i <= 6; i++) { 47 | player[idx1][i] = player[idx1][i + 1]; 48 | } 49 | player[idx1][7] = {0, 0}; 50 | } 51 | 52 | if (player[idx2][pos2].y <= 0) { 53 | if (pos2 == 0) { 54 | continue; 55 | } 56 | else { 57 | for (int i = pos2; i <= 6; i++) { 58 | player[idx2][i] = player[idx2][i + 1]; 59 | } 60 | player[idx2][7] = {0, 0}; 61 | } 62 | } 63 | } 64 | else { 65 | cnt ++; 66 | } 67 | } 68 | 69 | if (player[0][0].y > 0 && player[1][0].y > 0) cout << "0" << endl; 70 | if (player[0][0].y <= 0) cout << "-1" << endl; 71 | if (player[1][0].y <= 0) cout << "1" << endl; 72 | 73 | int num1 = 0, num2 = 0; 74 | for (int i = 1; i <= 7; i++) { 75 | if (player[0][i].y > 0) num1 ++; 76 | if (player[1][i].y > 0) num2 ++; 77 | } 78 | 79 | cout << player[0][0].y << endl; 80 | cout << num1; 81 | for (int i = 1; i <= num1; i++) cout << " " << player[0][i].y; 82 | cout << endl; 83 | 84 | cout << player[1][0].y << endl; 85 | cout << num2; 86 | for (int i = 1; i <= num2; i++) cout << " " << player[1][i].y; 87 | cout << endl; 88 | 89 | return 0; 90 | } -------------------------------------------------------------------------------- /201609/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 10010, M = 200010, INF = 0x3f3f3f3f; 8 | 9 | int n, m; 10 | int h[N], e[M], w[M], ne[M], idx; 11 | int dist[N], q[N]; 12 | bool st[N]; 13 | 14 | void add(int a, int b, int c) { 15 | e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ; 16 | } 17 | 18 | void spfa() { 19 | int hh = 0, tt = 1; 20 | memset(dist, 0x3f, sizeof dist); 21 | dist[1] = 0; 22 | q[0] = 1; 23 | 24 | while (hh != tt) { 25 | int t = q[hh ++ ]; 26 | if (hh == N) hh = 0; 27 | st[t] = false; 28 | 29 | for (int i = h[t]; ~i; i = ne[i]) { 30 | int j = e[i]; 31 | if (dist[j] > dist[t] + w[i]) { 32 | dist[j] = dist[t] + w[i]; 33 | if (!st[j]) { 34 | q[tt ++ ] = j; 35 | if (tt == N) tt = 0; 36 | st[j] = true; 37 | } 38 | } 39 | } 40 | } 41 | } 42 | 43 | int main() { 44 | scanf("%d%d", &n, &m); 45 | memset(h, -1, sizeof h); 46 | while (m -- ) { 47 | int a, b, c; 48 | scanf("%d%d%d", &a, &b, &c); 49 | add(a, b, c), add(b, a, c); 50 | } 51 | spfa(); 52 | 53 | int res = 0; 54 | for (int a = 2; a <= n; a ++ ) { 55 | int minw = INF; 56 | for (int j = h[a]; ~j; j = ne[j]) { 57 | int b = e[j]; 58 | if (dist[a] == dist[b] + w[j]) 59 | minw = min(minw, w[j]); 60 | } 61 | res += minw; 62 | } 63 | 64 | printf("%d\n", res); 65 | return 0; 66 | } -------------------------------------------------------------------------------- /201609/5.cpp: -------------------------------------------------------------------------------- 1 | // cv 线段树 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define x first 8 | #define y second 9 | 10 | using namespace std; 11 | 12 | typedef pair PII; 13 | const int N = 300010; 14 | 15 | int n, m; 16 | vector xs; 17 | PII p[N]; 18 | int up[N], down[N]; 19 | struct Tree { 20 | int l, r, v, c; 21 | }tr[N * 4]; 22 | int best; 23 | 24 | int get(int x) { 25 | return lower_bound(xs.begin(), xs.end(), x) - xs.begin(); 26 | } 27 | 28 | bool cmp(PII& a, PII& b) { 29 | if (a.y != b.y) return a.y < b.y; 30 | return a.x < b.x; 31 | } 32 | 33 | void pushup(int u) { 34 | Tree &l = tr[u << 1], &r = tr[u << 1 | 1]; 35 | tr[u].v = max(l.v, r.v); 36 | tr[u].c = l.c + r.c; 37 | } 38 | 39 | void build(int u, int l, int r) { 40 | tr[u] = {l, r}; 41 | if (l == r) tr[u].v = tr[u].c = 0; 42 | else { 43 | int mid = l + r >> 1; 44 | build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r); 45 | pushup(u); 46 | } 47 | } 48 | 49 | void update(int u, int x) { 50 | if (tr[u].l == tr[u].r) { 51 | tr[u].v = min(up[x], down[x]); 52 | if (tr[u].v >= best) tr[u].c = 1; 53 | else tr[u].c = 0; 54 | } 55 | else { 56 | int mid = tr[u].l + tr[u].r >> 1; 57 | if (x <= mid) update(u << 1, x); 58 | else update(u << 1 | 1, x); 59 | pushup(u); 60 | } 61 | } 62 | 63 | PII query(int u, int l, int r) { 64 | if (tr[u].l >= l && tr[u].r <= r) return {tr[u].v, tr[u].c}; 65 | else { 66 | int mid = tr[u].l + tr[u].r >> 1; 67 | PII res(-1, 0); 68 | if (l <= mid) res = query(u << 1, l, r); 69 | if (r > mid) { 70 | PII t = query(u << 1 | 1, l, r); 71 | res.x = max(res.x, t.x); 72 | res.y += t.y; 73 | } 74 | return res; 75 | } 76 | } 77 | 78 | int main() { 79 | scanf("%d%d", &n, &m); 80 | for (int i = 0; i < n; i ++ ) { 81 | scanf("%d%d", &p[i].x, &p[i].y); 82 | xs.push_back(p[i].x); 83 | } 84 | sort(xs.begin(), xs.end()); 85 | xs.erase(unique(xs.begin(), xs.end()), xs.end()); 86 | 87 | for (int i = 0; i < n; i ++ ) { 88 | p[i].x = get(p[i].x); 89 | down[p[i].x] ++ ; 90 | } 91 | 92 | sort(p, p + n, cmp); 93 | 94 | build(1, 0, xs.size() - 1); 95 | 96 | for (int i = 0; i < n; i ++ ) { 97 | int j = i; 98 | while (j < n && p[j].y == p[i].y) { 99 | int x = p[j ++ ].x; 100 | up[x] ++, down[x] -- ; 101 | update(1, x); 102 | } 103 | for (int k = i; k + 1 < j; k ++ ) { 104 | if (p[k].x + 1 <= p[k + 1].x - 1) { 105 | PII t = query(1, p[k].x + 1, p[k + 1].x - 1); 106 | best = max(best, min(t.x, min(k - i + 1, j - k - 1))); 107 | } 108 | } 109 | i = j - 1; 110 | } 111 | 112 | if (m == 1) printf("%d\n", best); 113 | else { 114 | memset(down, 0, sizeof down); 115 | memset(up, 0, sizeof up); 116 | 117 | for (int i = 0; i < n; i ++ ) down[p[i].x] ++ ; 118 | 119 | build(1, 0, xs.size() - 1); 120 | 121 | int res = 0; 122 | for (int i = 0; i < n; i ++ ) { 123 | int j = i; 124 | while (j < n && p[j].y == p[i].y) { 125 | int x = p[j ++ ].x; 126 | up[x] ++, down[x] -- ; 127 | update(1, x); 128 | } 129 | for (int k = i; k + 1 < j; k ++ ) { 130 | if (k - i + 1 >= best && j - k - 1 >= best && p[k].x + 1 <= p[k + 1].x - 1) { 131 | PII t = query(1, p[k].x + 1, p[k + 1].x - 1); 132 | res += t.y; 133 | } 134 | } 135 | i = j - 1; 136 | } 137 | printf("%d\n", res); 138 | } 139 | 140 | return 0; 141 | } -------------------------------------------------------------------------------- /201612/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int n; 8 | int a[1010]; 9 | int mp[1010]; 10 | 11 | int main() { 12 | cin >> n; 13 | for (int i = 0; i < n; i++) cin >> a[i]; 14 | 15 | sort(a, a + n); 16 | 17 | bool flag = false; 18 | for (int i = 0; i < n; i++) { 19 | int l = i, r = i; 20 | while(l >= 0 && a[l] == a[i]) l --; 21 | while(r < n && a[r] == a[i]) r ++; 22 | 23 | int mi = l + 1, ma = n - r; 24 | if (mi == ma) { 25 | cout << a[i] << endl; 26 | flag = true; 27 | break; 28 | } 29 | } 30 | if(!flag) puts("-1"); 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /201612/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int get(int n) { 8 | if (n <= 3500) return n; 9 | else { 10 | int a[8] = {0, 1500, 4500, 9000, 35000, 55000, 80000, 1000000000}; 11 | double b[8] = {0, 0.03, 0.1, 0.2, 0.25, 0.3, 0.35, 0.45}; 12 | 13 | double res = 0; 14 | n -= 3500; 15 | for (int i = 1; i <= 7; i ++ ) 16 | if (a[i] <= n) 17 | res += (a[i] - a[i - 1]) * b[i]; 18 | else { 19 | res += (n - a[i - 1]) * b[i]; 20 | break; 21 | } 22 | return n - res + 3500 + 1e-6; 23 | } 24 | } 25 | 26 | int main() { 27 | int n; 28 | cin >> n; 29 | 30 | int l = 0, r = 1e9; 31 | while (l < r) { 32 | int mid = l + r >> 1; 33 | if (get(mid) >= n) r = mid; 34 | else l = mid + 1; 35 | } 36 | 37 | cout << r << endl; 38 | return 0; 39 | } -------------------------------------------------------------------------------- /201612/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef pair pii; 9 | 10 | int p, r, u, q; 11 | map mp; // 各权限 最高等级 12 | map> roles; // 各角色 对应权限等级 13 | map> users; // 各user 对应角色 14 | 15 | int main() { 16 | cin >> p; 17 | // -1 表示无权限等级 18 | for (int i = 1; i <= p; i++) { 19 | string str; 20 | cin >> str; 21 | int n = str.size(); 22 | if (str[n - 2] == ':') { 23 | mp[str.substr(0, n - 2)] = str[n - 1] - '0'; 24 | } 25 | else { 26 | mp[str] = -1; 27 | } 28 | } 29 | 30 | cin >> r; 31 | for (int i = 0; i < r; i++) { 32 | string role; 33 | int num; 34 | cin >> role >> num; 35 | while(num --) { 36 | string str; 37 | cin >> str; 38 | int n = str.size(); 39 | if (str[n - 2] == ':') { 40 | roles[role].push_back({str.substr(0, n - 2), str[n - 1] - '0'}); 41 | } 42 | else { 43 | roles[role].push_back({str, -1}); 44 | } 45 | } 46 | } 47 | 48 | cin >> u; 49 | for (int i = 0; i < u; i++) { 50 | string name; 51 | int num; 52 | cin >> name >> num; 53 | 54 | while(num --) { 55 | string role; 56 | cin >> role; 57 | users[name].push_back(role); 58 | } 59 | } 60 | 61 | cin >> q; 62 | for (int i = 0; i < q; i++) { 63 | string name, str; 64 | cin >> name >> str; 65 | int n = str.size(); 66 | if (n >= 2 && str[n - 2] == ':') { 67 | string query = str.substr(0, n - 2); 68 | int level = str[n - 1] - '0'; 69 | 70 | vector allRoles = users[name]; 71 | int maxL = -1; 72 | for (auto role : allRoles) { 73 | for (pii p : roles[role]) { 74 | if (p.first == query) { 75 | maxL = max(maxL, p.second); 76 | } 77 | } 78 | } 79 | 80 | if (maxL >= level) puts("true"); 81 | else puts("false"); 82 | } 83 | else { 84 | string query = str; 85 | 86 | vector allRoles = users[name]; 87 | int maxL = -2; 88 | for (auto role : allRoles) { 89 | for (pii p : roles[role]) { 90 | if (p.first == query) { 91 | maxL = max(maxL, p.second); 92 | } 93 | } 94 | } 95 | 96 | if (maxL == -2) puts("false"); 97 | else if (maxL == -1) puts("true"); 98 | else cout << maxL << endl; 99 | } 100 | } 101 | 102 | return 0; 103 | } -------------------------------------------------------------------------------- /201612/4.cpp: -------------------------------------------------------------------------------- 1 | // cv DP huffman树 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | const int N = 5010, INF = 1e9; 9 | 10 | int n; 11 | int s[N], f[N][N], p[N][N]; 12 | 13 | int main() 14 | { 15 | scanf("%d", &n); 16 | for (int i = 1; i <= n; i ++ ) 17 | { 18 | scanf("%d", &s[i]); 19 | s[i] += s[i - 1]; 20 | } 21 | 22 | for (int i = 1; i <= n; i ++ ) p[i][i] = i; 23 | for (int len = 2; len <= n; len ++ ) 24 | for (int i = 1; i + len - 1 <= n; i ++ ) 25 | { 26 | int j = i + len - 1; 27 | f[i][j] = INF; 28 | for (int k = p[i][j - 1]; k <= p[i + 1][j]; k ++ ) 29 | { 30 | int t = f[i][k] + f[k + 1][j] + s[j] - s[i - 1]; 31 | if (f[i][j] >= t) 32 | { 33 | f[i][j] = t; 34 | p[i][j] = k; 35 | } 36 | } 37 | } 38 | 39 | printf("%d\n", f[1][n]); 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /201612/5.cpp: -------------------------------------------------------------------------------- 1 | // cv 概率论 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | const int N = 15, M = 1 << N; 9 | 10 | int n, m; 11 | double p[N][N], s1[N], s2[N]; 12 | double f[2][M], w[M][N]; 13 | 14 | int main() 15 | { 16 | scanf("%d%d", &n, &m); 17 | for (int i = 0; i < n; i ++ ) 18 | for (int j = i + 1; j < n; j ++ ) 19 | { 20 | scanf("%lf", &p[i][j]); 21 | p[j][i] = 1 - p[i][j]; 22 | } 23 | 24 | for (int i = 1; i + 1 < 1 << n; i ++ ) 25 | { 26 | memset(s1, 0, sizeof s1); 27 | memset(s2, 0, sizeof s2); 28 | for (int j = 0; j < n; j ++ ) 29 | if (i >> j & 1) 30 | { 31 | for (int k = 0; k < n; k ++ ) 32 | if (!(i >> k & 1)) 33 | s1[j] += p[j][k]; 34 | } 35 | else 36 | { 37 | for (int k = 0; k < n; k ++ ) 38 | if (i >> k & 1) 39 | s2[j] += p[j][k]; 40 | } 41 | 42 | double sum1 = 0, sum2 = 0; 43 | for (int j = 0; j < n; j ++ ) 44 | { 45 | sum1 += s1[j]; 46 | sum2 += s2[j]; 47 | } 48 | for (int j = 0; j < n; j ++ ) 49 | if (i >> j & 1) 50 | { 51 | for (int k = 0; k < n; k ++ ) 52 | if (!(i >> k & 1)) 53 | w[i][j] += s1[j] / sum1 * s2[k] / sum2 * p[k][j]; 54 | } 55 | else 56 | { 57 | for (int k = 0; k < n; k ++ ) 58 | if (i >> k & 1) 59 | w[i][j] += s2[j] / sum2 * s1[k] / sum1 * p[k][j]; 60 | } 61 | } 62 | 63 | for (int k = 1; k <= 1000; k ++ ) 64 | { 65 | memset(f[k & 1], 0, sizeof f[k & 1]); 66 | f[k & 1][(1 << n) - 1] = 1; 67 | for (int i = 1; i + 1 < 1 << n; i ++ ) 68 | for (int j = 0; j < n; j ++ ) 69 | f[k & 1][i] += f[k - 1 & 1][i ^ (1 << j)] * w[i][j]; 70 | } 71 | 72 | while (m -- ) 73 | { 74 | int state = 0; 75 | for (int i = 0; i < n; i ++ ) 76 | { 77 | int x; 78 | scanf("%d", &x); 79 | state += x << i; 80 | } 81 | printf("%.5lf\n", f[0][state]); 82 | } 83 | return 0; 84 | } -------------------------------------------------------------------------------- /201703/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int a[1010]; 8 | int n, k; 9 | 10 | int main() { 11 | cin >> n >> k; 12 | for (int i = 1; i <= n; i++) cin >> a[i]; 13 | 14 | int ans = 0; 15 | 16 | int sum = 0; 17 | for (int i = 1; i <= n; i++) { 18 | sum += a[i]; 19 | if (sum >= k) { 20 | ans ++; 21 | sum = 0; 22 | } 23 | else { 24 | if (i == n) ans ++; 25 | } 26 | } 27 | 28 | cout << ans << endl; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /201703/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int n, m; 8 | int a[1010]; 9 | 10 | int main() { 11 | cin >> n; 12 | cin >> m; 13 | for (int i = 1; i <= n; i++) a[i] = i; 14 | 15 | while(m --) { 16 | int p, q; 17 | cin >> p >> q; 18 | 19 | int idx; 20 | for (int i = 1; i <= n; i++) { 21 | if (a[i] == p) idx = i; 22 | } 23 | 24 | if (q > 0) { 25 | for (int i = 1; i <= q; i++) { 26 | int cur = idx + i - 1; 27 | a[cur] = a[cur + 1]; 28 | } 29 | a[idx + q] = p; 30 | } 31 | else { 32 | q = -1 * q; 33 | for (int i = 1; i <= q; i++) { 34 | int cur = idx - i + 1; 35 | a[cur] = a[cur - 1]; 36 | } 37 | a[idx - q] = p; 38 | } 39 | } 40 | 41 | for (int i = 1; i <= n; i++) cout << a[i] << " "; 42 | cout << endl; 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /201703/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | vector strs; 8 | 9 | int work_link(string str, int i) { 10 | string text, link; 11 | for (i ++; str[i] != ']'; i ++ ) { 12 | char c = str[i]; 13 | if (c == '_') { 14 | text += ""; 15 | i ++ ; 16 | while (str[i] != '_') text += str[i ++ ]; 17 | text += ""; 18 | } 19 | else text += c; 20 | } 21 | 22 | for (i += 2; str[i] != ')'; i ++ ) 23 | link += str[i]; 24 | printf("%s", link.c_str(), text.c_str()); 25 | return i; 26 | } 27 | 28 | int work_em(string str, int i) { 29 | printf(""); 30 | for (i ++; str[i] != '_'; i ++ ) { 31 | char c = str[i]; 32 | if (c == '[') i = work_link(str, i); 33 | else cout << c; 34 | } 35 | printf(""); 36 | return i; 37 | } 38 | 39 | void work_line(string str) { 40 | int k = 0; 41 | while (str[k] == ' ') k ++ ; 42 | str = str.substr(k); 43 | 44 | for (int i = 0; i < str.size(); i++) { 45 | if (str[i] == '_') { 46 | i = work_em(str, i); 47 | } 48 | else if (str[i] == '[') { 49 | i = work_link(str, i); 50 | } 51 | else { 52 | cout << str[i]; 53 | } 54 | } 55 | } 56 | 57 | void work(int a, int b) { 58 | if (strs[a][0] == '#') { 59 | int cnt = 0; 60 | while(strs[a][cnt] == '#') cnt ++; 61 | 62 | printf("", cnt); 63 | work_line(strs[a].substr(cnt)); 64 | printf("\n", cnt); 65 | } 66 | else if (strs[a][0] == '*') { 67 | printf("
    \n"); 68 | for (int i = a; i <= b; i++) { 69 | printf("
  • "); 70 | work_line(strs[i].substr(1)); 71 | printf("
  • \n"); 72 | } 73 | printf("
\n"); 74 | } 75 | else { 76 | printf("

"); 77 | for (int i = a; i <= b; i++) { 78 | work_line(strs[i]); 79 | if (i != b) cout << endl; 80 | } 81 | printf("

\n"); 82 | } 83 | } 84 | 85 | int main() { 86 | string str; 87 | while(getline(cin, str)) strs.push_back(str); 88 | 89 | for (int i = 0; i < strs.size(); i++) { 90 | if (strs[i].empty()) continue; 91 | 92 | int j = i; 93 | while(j < strs.size() && strs[j].size()) j ++; 94 | work(i, j - 1); 95 | i = j - 1; 96 | } 97 | 98 | return 0; 99 | } -------------------------------------------------------------------------------- /201703/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 100010, M = 200010; 8 | int n, m; // n是点数,m是边数 9 | int p[N]; // 并查集的父节点数组 10 | 11 | struct Edge // 存储边 12 | { 13 | int a, b, w; 14 | 15 | bool operator< (const Edge &W)const 16 | { 17 | return w < W.w; 18 | } 19 | }edges[M]; 20 | 21 | int find(int x) // 并查集核心操作 22 | { 23 | if (p[x] != x) p[x] = find(p[x]); 24 | return p[x]; 25 | } 26 | 27 | int kruskal() { 28 | sort(edges, edges + m); 29 | 30 | for (int i = 1; i <= n; i ++ ) p[i] = i; // 初始化并查集 31 | 32 | int res = 0, cnt = 0; 33 | for (int i = 0; i < m; i ++ ) { 34 | int a = edges[i].a, b = edges[i].b, w = edges[i].w; 35 | 36 | a = find(a), b = find(b); 37 | if (a != b) // 如果两个连通块不连通,则将这两个连通块合并 38 | { 39 | p[a] = b; 40 | res = max(res, w); 41 | if (find(1) == find(n)) break; 42 | cnt ++ ; 43 | } 44 | } 45 | 46 | return res; 47 | } 48 | 49 | int main() { 50 | cin >> n >> m; 51 | 52 | for (int i = 0; i < m; i++) { 53 | int a, b, w; 54 | cin >> a >> b >> w; 55 | edges[i] = {a, b, w}; 56 | } 57 | 58 | cout << kruskal() << endl; 59 | 60 | return 0; 61 | } -------------------------------------------------------------------------------- /201703/5.cpp: -------------------------------------------------------------------------------- 1 | // 网络流 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef long long LL; 9 | const int N = 5050; 10 | 11 | int n, m, A, B, Q, X; 12 | int r[N][N], c[N][N]; 13 | LL d[N]; 14 | 15 | int main() 16 | { 17 | scanf("%d%d%d%d%d%d", &n, &m, &A, &B, &Q, &X); 18 | for (int i = 1; i <= n - 1; i ++ ) 19 | for (int j = 1; j <= m; j ++ ) 20 | c[i][j] = X = ((LL)A * X + B) % Q; 21 | for (int i = 2; i <= n - 1; i ++ ) 22 | for (int j = 1; j <= m - 1; j ++ ) 23 | r[i][j] = X = ((LL)A * X + B) % Q; 24 | 25 | for (int j = 1; j <= m; j ++ ) 26 | { 27 | for (int i = 1; i < n; i ++ ) 28 | d[i] += c[i][j]; 29 | for (int i = 2; i < n; i ++ ) 30 | d[i] = min(d[i], d[i - 1] + r[i][j]); 31 | for (int i = n - 2; i; i -- ) 32 | d[i] = min(d[i], d[i + 1] + r[i + 1][j]); 33 | } 34 | LL res = 1e18; 35 | for (int i = 1; i < n; i ++ ) res = min(res, d[i]); 36 | printf("%lld\n", res); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /201709/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int n; 8 | int dp[40]; 9 | 10 | int main() { 11 | cin >> n; 12 | dp[0] = 0, dp[1] = 1, dp[2] = 2, dp[3] = 4, dp[4] = 5, dp[5] = 7; 13 | for (int i = 6; i <= 30; i++) { 14 | dp[i] = max(dp[i - 1] + 1, max(dp[i - 3] + 4, dp[i - 5] + 7)); 15 | } 16 | 17 | cout << dp[n / 10] << endl; 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /201709/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 1010; 8 | int a[N]; 9 | int n, k; 10 | 11 | struct Op { 12 | int id, type, tm; 13 | 14 | bool operator< (const Op& nop) { 15 | if (tm != nop.tm) return tm < nop.tm; 16 | if (type != nop.type) return type < nop.type; 17 | return id < nop.id; 18 | } 19 | }op[N * 2]; 20 | 21 | int main() { 22 | cin >> n >> k; 23 | 24 | for (int i = 1; i <= n; i++) a[i] = i; 25 | int cnt = 0; 26 | while(k --) { 27 | int w, s, c; 28 | cin >> w >> s >> c; 29 | 30 | op[cnt ++] = {w, 1, s}; 31 | op[cnt ++] = {w, 0, s + c}; 32 | } 33 | 34 | sort(op, op + cnt); 35 | 36 | for (int i = 0; i < cnt; i++) { 37 | int id = op[i].id, type = op[i].type; 38 | 39 | if (type == 0) { 40 | for (int j = 1; j <= n; j++) { 41 | if (a[j] == 0) { 42 | a[j] = id; 43 | break; 44 | } 45 | } 46 | } 47 | else { 48 | for (int j = 1; j <= n; j++) { 49 | if (a[j] == id) { 50 | a[j] = 0; 51 | break; 52 | } 53 | } 54 | } 55 | } 56 | 57 | for (int i = 1; i <= n; i++) { 58 | cout << a[i] << " "; 59 | } 60 | 61 | 62 | return 0; 63 | } -------------------------------------------------------------------------------- /201709/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | struct Obj 10 | { 11 | int type; // 1: str, 2: dict 12 | string str; 13 | Obj(){} 14 | Obj(string _str) 15 | { 16 | type = 1; 17 | str = _str; 18 | } 19 | map obj; 20 | }; 21 | 22 | string work_str(string& str, int& k) 23 | { 24 | k ++ ; // 过滤掉'\"' 25 | string res; 26 | while (str[k] != '\"') 27 | if (str[k] == '\\') res += str[k + 1], k += 2; 28 | else res += str[k ++ ]; 29 | k ++ ; // 过滤掉'\"' 30 | return res; 31 | } 32 | 33 | Obj work_obj(string&, int&); 34 | 35 | void work_kv(Obj& obj, string& str, int& k) 36 | { 37 | auto key = work_str(str, k); 38 | while (str[k] != ':') k ++ ; 39 | k ++ ; // 将':'过滤掉 40 | while (str[k] != '\"' && str[k] != '{') k ++ ; 41 | if (str[k] == '\"') obj.obj[key] = Obj(work_str(str, k)); 42 | else obj.obj[key] = work_obj(str, k); 43 | } 44 | 45 | Obj work_obj(string& str, int& k) 46 | { 47 | Obj obj; 48 | obj.type = 2; 49 | while (str[k] != '{') k ++ ; 50 | k ++ ; // 过滤掉 '{' 51 | while (str[k] != '}') 52 | { 53 | if (str[k] == '\"') work_kv(obj, str, k); 54 | else k ++ ; 55 | } 56 | k ++ ; // 过滤掉 '}' 57 | return obj; 58 | } 59 | 60 | void query(Obj o, vector& qs) 61 | { 62 | for (auto& s: qs) 63 | { 64 | if (o.type == 1 || !o.obj.count(s)) 65 | { 66 | puts("NOTEXIST"); 67 | return; 68 | } 69 | auto t = o.obj[s]; 70 | o = t; 71 | } 72 | 73 | if (o.type == 1) printf("STRING %s\n", o.str.c_str()); 74 | else puts("OBJECT"); 75 | } 76 | 77 | int main() 78 | { 79 | int n, m; 80 | cin >> n >> m; 81 | getchar(); 82 | string str, s; 83 | while (n -- ) 84 | { 85 | getline(cin, s); 86 | str += s; 87 | } 88 | 89 | int k = 0; 90 | auto obj = work_obj(str, k); 91 | 92 | while (m -- ) 93 | { 94 | getline(cin, s); 95 | vector qs; 96 | for (int i = 0; i < s.size(); i ++ ) 97 | { 98 | int j = i + 1; 99 | while (j < s.size() && s[j] != '.') j ++ ; 100 | qs.push_back(s.substr(i, j - i)); 101 | i = j; 102 | } 103 | query(obj, qs); 104 | } 105 | return 0; 106 | } -------------------------------------------------------------------------------- /201709/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | const int N = 1010; 9 | vector> g1(N, vector()); 10 | vector> g2(N, vector()); 11 | bool st1[N], st2[N]; 12 | int n, m; 13 | 14 | void bfs(int a, vector>& g, bool st[]) { 15 | queue q; 16 | q.push(a); 17 | st[a] = true; 18 | 19 | while(q.size()) { 20 | int x = q.front(); 21 | q.pop(); 22 | 23 | for (int i = 0; i < g[x].size(); i++) { 24 | int node = g[x][i]; 25 | if (st[node]) continue; 26 | 27 | q.push(node); 28 | st[node] = true; 29 | } 30 | } 31 | } 32 | 33 | int main() { 34 | cin >> n >> m; 35 | 36 | while(m --) { 37 | int a, b; 38 | cin >> a >> b; 39 | g1[a].push_back(b); 40 | g2[b].push_back(a); 41 | } 42 | 43 | int ans = 0; 44 | for (int i = 1; i <= n; i++) { 45 | memset(st1, false, sizeof st1); 46 | memset(st2, false, sizeof st2); 47 | bfs(i, g1, st1); 48 | bfs(i, g2, st2); 49 | 50 | int cnt = 0; 51 | for (int i = 1; i <= n; i++) { 52 | if (st1[i] || st2[i]) { 53 | cnt ++; 54 | } 55 | } 56 | 57 | if (cnt == n) ans ++; 58 | } 59 | 60 | cout << ans << endl; 61 | 62 | return 0; 63 | } -------------------------------------------------------------------------------- /201709/5.cpp: -------------------------------------------------------------------------------- 1 | // 树状数组 线段树 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | typedef long long LL; 10 | const int N = 100010, M = 1000010; 11 | 12 | int n, m; 13 | set pos[M]; 14 | LL tr[N]; 15 | set vs; 16 | struct Query 17 | { 18 | int t, l, r, v; 19 | }q[N]; 20 | int w[N]; 21 | 22 | int lowbit(int x) 23 | { 24 | return x & -x; 25 | } 26 | 27 | void add(int x, int v) 28 | { 29 | for (int i = x; i <= n; i += lowbit(i)) tr[i] += v; 30 | } 31 | 32 | LL query(int x) 33 | { 34 | LL res = 0; 35 | for (int i = x; i; i -= lowbit(i)) res += tr[i]; 36 | return res; 37 | } 38 | 39 | int main() 40 | { 41 | scanf("%d%d", &n, &m); 42 | for (int i = 1; i <= n; i ++ ) 43 | { 44 | scanf("%d", &w[i]); 45 | add(i, w[i]); 46 | } 47 | for (int i = 0; i < m; i ++ ) 48 | { 49 | int t, l, r, v; 50 | scanf("%d%d%d", &t, &l, &r); 51 | if (t == 1) 52 | { 53 | scanf("%d", &v); 54 | q[i] = {t, l, r, v}; 55 | if (v != 1) vs.insert(v); 56 | } 57 | else q[i] = {t, l, r}; 58 | } 59 | for (int i = 1; i <= n; i ++ ) 60 | for (int j = 1; j * j <= w[i]; j ++ ) 61 | if (w[i] % j == 0) 62 | { 63 | if (vs.count(j)) pos[j].insert(i); 64 | if (w[i] / j != j && vs.count(w[i] / j)) 65 | pos[w[i] / j].insert(i); 66 | } 67 | 68 | for (int i = 0; i < m; i ++ ) 69 | if (q[i].t == 1) 70 | { 71 | int v = q[i].v; 72 | auto it = pos[v].lower_bound(q[i].l); 73 | while (it != pos[v].end() && *it <= q[i].r) 74 | { 75 | int k = *it; 76 | it ++ ; 77 | add(k, -w[k] + w[k] / v); 78 | 79 | for (int j = 1; j * j <= w[k]; j ++ ) 80 | if (w[k] % j == 0) 81 | { 82 | pos[j].erase(k); 83 | if (w[k] / j != j) pos[w[k] / j].erase(k); 84 | } 85 | w[k] /= v; 86 | for (int j = 1; j * j <= w[k]; j ++ ) 87 | if (w[k] % j == 0) 88 | { 89 | if (vs.count(j)) pos[j].insert(k); 90 | if (w[k] / j != j && vs.count(w[k] / j)) 91 | pos[w[k] / j].insert(k); 92 | } 93 | } 94 | } 95 | else printf("%lld\n", query(q[i].r) - query(q[i].l - 1)); 96 | return 0; 97 | } -------------------------------------------------------------------------------- /201712/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int a[1010]; 8 | int n; 9 | 10 | int main() { 11 | cin >> n; 12 | for (int i = 1; i <= n; i++) cin >> a[i]; 13 | sort(a + 1, a + 1 + n); 14 | 15 | int ans = 0x3f3f3f3f; 16 | for (int i = 2; i <= n; i++) { 17 | ans = min(ans, a[i] - a[i - 1]); 18 | } 19 | 20 | cout << ans << endl; 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /201712/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int n, k; 8 | int a[1010]; 9 | 10 | int main() { 11 | cin >> n >> k; 12 | int cnt = 0; 13 | for (int i = 1; i <= n; i++) a[i] = i; 14 | 15 | int cur = n, idx = 0; 16 | while(cur > 1) { 17 | cnt ++; 18 | idx = (idx % n) + 1; 19 | while(a[idx] == 0) idx = (idx % n) + 1; 20 | if (cnt % k == 0 || cnt % 10 == k) { 21 | a[idx] = 0; 22 | cur --; 23 | } 24 | } 25 | 26 | int ans; 27 | for (int i = 1; i <= n; i++) { 28 | if (a[i] != 0) { 29 | ans = i; 30 | break; 31 | } 32 | } 33 | 34 | cout << ans << endl; 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /201712/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int n; 9 | int months[13] = { 10 | 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 11 | }; 12 | 13 | struct Timer 14 | { 15 | int year, month, day, hour, minute, week; 16 | Timer(string str) 17 | { 18 | sscanf(str.c_str(), "%04d%02d%02d%02d%02d", &year, &month, &day, &hour, &minute); 19 | } 20 | bool operator< (const Timer& t) const 21 | { 22 | if (year != t.year) return year < t.year; 23 | if (month != t.month) return month < t.month; 24 | if (day != t.day) return day < t.day; 25 | if (hour != t.hour) return hour < t.hour; 26 | return minute < t.minute; 27 | } 28 | int is_leap() 29 | { 30 | if (year % 4 == 0 && year % 100 || year % 400 == 0) 31 | return 1; 32 | return 0; 33 | } 34 | int get_days() 35 | { 36 | if (month == 2) return months[2] + is_leap(); 37 | return months[month]; 38 | } 39 | void next() 40 | { 41 | if ( ++ minute == 60) 42 | { 43 | minute = 0; 44 | if ( ++ hour == 24) 45 | { 46 | hour = 0; 47 | week = (week + 1) % 7; 48 | if ( ++ day > get_days()) 49 | { 50 | day = 1; 51 | if ( ++ month > 12) 52 | { 53 | month = 1; 54 | year ++ ; 55 | } 56 | } 57 | } 58 | } 59 | } 60 | string to_string() 61 | { 62 | char str[20]; 63 | sprintf(str, "%04d%02d%02d%02d%02d", year, month, day, hour, minute); 64 | return str; 65 | } 66 | }; 67 | 68 | struct Task 69 | { 70 | bool minutes[60], hours[24], day_of_month[32], month[13], day_of_week[7]; 71 | string name; 72 | bool check(Timer& t) 73 | { 74 | return minutes[t.minute] && hours[t.hour] && day_of_month[t.day] && 75 | month[t.month] && day_of_week[t.week]; 76 | } 77 | }task[20]; 78 | unordered_map nums; 79 | 80 | void init() 81 | { 82 | string keys[] = { 83 | "jan", "feb", "mar", "apr", "may", "jun", 84 | "jul", "aug", "sep", "oct", "nov", "dec", 85 | "sun", "mon", "tue", "wed", "thu", "fri", 86 | "sat" 87 | }; 88 | int values[] = { 89 | 1, 2, 3, 4, 5, 6, 90 | 7, 8, 9, 10, 11, 12, 91 | 0, 1, 2, 3, 4, 5, 92 | 6 93 | }; 94 | for (int i = 0; i < 19; i ++ ) 95 | nums[keys[i]] = values[i]; 96 | } 97 | 98 | int get(string str) 99 | { 100 | if (str[0] >= '0' && str[0] <= '9') return stoi(str); 101 | string s; 102 | for (auto c: str) s += tolower(c); 103 | return nums[s]; 104 | } 105 | 106 | void work(string str, bool st[], int len) 107 | { 108 | if (str == "*") 109 | { 110 | for (int i = 0; i < len; i ++ ) st[i] = true; 111 | } 112 | else 113 | { 114 | for (int i = 0; i < str.size(); i ++ ) 115 | { 116 | int j = i + 1; 117 | while (j < str.size() && str[j] != ',') j ++ ; 118 | string s = str.substr(i, j - i); 119 | i = j; 120 | int k = s.find('-'); 121 | if (k != -1) 122 | { 123 | int l = get(s.substr(0, k)), r = get(s.substr(k + 1)); 124 | for (int u = l; u <= r; u ++ ) st[u] = true; 125 | } 126 | else st[get(s)] = true; 127 | } 128 | } 129 | } 130 | 131 | int main() 132 | { 133 | init(); 134 | string start, end; 135 | cin >> n >> start >> end; 136 | for (int i = 0; i < n; i ++ ) 137 | { 138 | string minutes, hours, day_of_month, month, day_of_week, name; 139 | cin >> minutes >> hours >> day_of_month >> month >> day_of_week >> name; 140 | work(minutes, task[i].minutes, 60); 141 | work(hours, task[i].hours, 24); 142 | work(day_of_month, task[i].day_of_month, 32); 143 | work(month, task[i].month, 13); 144 | work(day_of_week, task[i].day_of_week, 7); 145 | task[i].name = name; 146 | } 147 | 148 | Timer t("197001010000"), S(start), E(end); 149 | t.week = 4; 150 | 151 | int cnt = 0; 152 | while (t < E) 153 | { 154 | if (!(t < S)) 155 | { 156 | for (int i = 0; i < n; i ++ ) 157 | if (task[i].check(t)) 158 | cout << t.to_string() << ' ' << task[i].name << endl; 159 | } 160 | t.next(); 161 | } 162 | 163 | return 0; 164 | } -------------------------------------------------------------------------------- /201712/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | const int N = 510, M = 200010, INF = 1e6; 9 | 10 | int n, m; 11 | int h[N], e[M], f[M], w[M], ne[M], idx; 12 | int dist[N][1010]; 13 | bool st[N][1010]; 14 | struct Node 15 | { 16 | int x, y, v; 17 | bool operator< (const Node& t) const 18 | { 19 | return v > t.v; 20 | } 21 | }; 22 | 23 | void add(int t, int a, int b, int c) 24 | { 25 | e[idx] = b, f[idx] = t, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ; 26 | } 27 | 28 | void dijkstra() 29 | { 30 | priority_queue heap; 31 | heap.push({1, 0, 0}); 32 | memset(dist, 0x3f, sizeof dist); 33 | dist[1][0] = 0; 34 | while (heap.size()) 35 | { 36 | auto t = heap.top(); 37 | heap.pop(); 38 | 39 | if (st[t.x][t.y]) continue; 40 | st[t.x][t.y] = true; 41 | for (int i = h[t.x]; ~i; i = ne[i]) 42 | { 43 | int x = e[i], y = t.y; 44 | if (f[i]) 45 | { 46 | y += w[i]; 47 | if (y <= 1000) 48 | { 49 | if (dist[x][y] > t.v - t.y * t.y + y * y) 50 | { 51 | dist[x][y] = t.v - t.y * t.y + y * y; 52 | if (dist[x][y] <= INF) 53 | heap.push({x, y, dist[x][y]}); 54 | } 55 | } 56 | } 57 | else 58 | { 59 | if (dist[x][0] > t.v + w[i]) 60 | { 61 | dist[x][0] = t.v + w[i]; 62 | if (dist[x][0] <= INF) 63 | heap.push({x, 0, dist[x][0]}); 64 | } 65 | } 66 | } 67 | } 68 | } 69 | 70 | int main() 71 | { 72 | scanf("%d%d", &n, &m); 73 | memset(h, -1, sizeof h); 74 | while (m -- ) 75 | { 76 | int t, a, b, c; 77 | scanf("%d%d%d%d", &t, &a, &b, &c); 78 | add(t, a, b, c), add(t, b, a, c); 79 | } 80 | 81 | dijkstra(); 82 | int res = INF; 83 | for (int i = 0; i <= 1000; i ++ ) res = min(res, dist[n][i]); 84 | printf("%d\n", res); 85 | 86 | return 0; 87 | } -------------------------------------------------------------------------------- /201712/5.cpp: -------------------------------------------------------------------------------- 1 | // 60分 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef long long LL; 9 | const int N = 100010; 10 | const LL MOD = 1e18; 11 | 12 | int n; 13 | LL v[N]; 14 | int p[N], w[N], f[N]; 15 | LL ans[N]; 16 | 17 | int main() 18 | { 19 | int T; 20 | scanf("%d", &T); 21 | while (T -- ) 22 | { 23 | scanf("%d", &n); 24 | memset(ans, 0, sizeof ans); 25 | for (int i = 1; i <= n; i ++ ) 26 | scanf("%d%d%lld%d", &p[i], &w[i], &v[i], &f[i]); 27 | for (int i = n; i; i -- ) 28 | { 29 | int k = p[i], d = w[i]; 30 | while (k) 31 | { 32 | ans[k] = max(ans[k], v[k] - (LL)(f[k] - d) * (f[k] - d) + ans[i]); 33 | d += w[k]; 34 | k = p[k]; 35 | } 36 | } 37 | LL res = 0; 38 | for (int i = 1; i <= n; i ++ ) 39 | res = (res + ans[i]) % MOD; 40 | printf("%lld\n", res); 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /201803/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int res = 0, x, score = 0; 9 | while (cin >> x, x) { 10 | if (x == 1) res ++, score = 0; 11 | else score += 2, res += score; 12 | } 13 | cout << res << endl; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /201803/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 110; 8 | 9 | int n, L, T; 10 | struct Ball 11 | { 12 | int p, v; 13 | }b[N]; 14 | 15 | int main() 16 | { 17 | cin >> n >> L >> T; 18 | for (int i = 0; i < n; i ++ ) 19 | { 20 | cin >> b[i].p; 21 | b[i].v = 1; 22 | } 23 | while (T -- ) 24 | { 25 | for (int i = 0; i < n; i ++ ) 26 | { 27 | b[i].p += b[i].v; 28 | if (b[i].p == L || !b[i].p) 29 | b[i].v *= -1; 30 | } 31 | for (int i = 0; i < n; i ++ ) 32 | for (int j = i + 1; j < n; j ++ ) 33 | if (b[i].p == b[j].p) 34 | { 35 | b[i].v *= -1; 36 | b[j].v *= -1; 37 | } 38 | } 39 | for (int i = 0; i < n; i ++ ) 40 | cout << b[i].p << ' '; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /201803/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | const int N = 110; 9 | 10 | int n, m; 11 | struct Url 12 | { 13 | string path, name; 14 | }url[N]; 15 | 16 | string get_number(string& str) 17 | { 18 | string res; 19 | for (auto c: str) 20 | if (c >= '0' && c <= '9') 21 | res += c; 22 | else 23 | { 24 | res.clear(); 25 | return res; 26 | } 27 | 28 | // 去掉前导0 29 | int k = 0; 30 | while (k + 1 < res.size() && res[k] == '0') k ++ ; 31 | return res.substr(k); 32 | } 33 | 34 | vector get(string& path, string& str) 35 | { 36 | vector res(1); 37 | int i, j; 38 | for (i = 1, j = 1; i < path.size() && j < str.size();) 39 | { 40 | int u = i + 1, v = j + 1; 41 | while (u < path.size() && path[u] != '/') u ++ ; 42 | while (v < str.size() && str[v] != '/') v ++ ; 43 | string a = path.substr(i, u - i), b = str.substr(j, v - j); 44 | if (a == "") 45 | { 46 | res.push_back(b); 47 | i = u + 1, j = v + 1; 48 | } 49 | else if (a == "") 50 | { 51 | auto t = get_number(b); 52 | if (t.empty()) 53 | { 54 | res.clear(); 55 | return res; 56 | } 57 | res.push_back(t); 58 | i = u + 1, j = v + 1; 59 | } 60 | else if (a == "") 61 | { 62 | res.push_back(str.substr(j)); 63 | return res; 64 | } 65 | else if (a != b) 66 | { 67 | res.clear(); 68 | return res; 69 | } 70 | else i = u + 1, j = v + 1; 71 | } 72 | 73 | if (i - path.size() != j - str.size()) res.clear(); 74 | return res; 75 | } 76 | 77 | 78 | void work(string& str) 79 | { 80 | for (int i = 0; i < n; i ++ ) 81 | { 82 | auto res = get(url[i].path, str); 83 | if (res.size()) 84 | { 85 | cout << url[i].name; 86 | for (int j = 1; j < res.size(); j ++ ) 87 | cout << ' ' << res[j]; 88 | cout << endl; 89 | return; 90 | } 91 | } 92 | puts("404"); 93 | } 94 | 95 | int main() 96 | { 97 | cin >> n >> m; 98 | for (int i = 0; i < n; i ++ ) cin >> url[i].path >> url[i].name; 99 | while (m -- ) 100 | { 101 | string str; 102 | cin >> str; 103 | work(str); 104 | } 105 | 106 | return 0; 107 | } -------------------------------------------------------------------------------- /201803/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 3, INF = 1e8; 8 | 9 | int g[N][N]; 10 | 11 | bool check(int x) 12 | { 13 | for (int i = 0; i < 3; i ++ ) 14 | { 15 | int s = 0; 16 | for (int j = 0; j < 3; j ++ ) 17 | if (g[i][j] == x) 18 | s ++ ; 19 | if (s == 3) return true; 20 | s = 0; 21 | for (int j = 0; j < 3; j ++ ) 22 | if (g[j][i] == x) 23 | s ++ ; 24 | if (s == 3) return true; 25 | } 26 | if (g[0][0] == x && g[1][1] == x && g[2][2] == x) return true; 27 | if (g[2][0] == x && g[1][1] == x && g[0][2] == x) return true; 28 | return false; 29 | } 30 | 31 | int evaluate() 32 | { 33 | int s = 0; 34 | for (int i = 0; i < 3; i ++ ) 35 | for (int j = 0; j < 3; j ++ ) 36 | if (!g[i][j]) 37 | s ++ ; 38 | if (check(1)) return s + 1; 39 | if (check(2)) return -(s + 1); 40 | if (!s) return 0; 41 | return INF; 42 | } 43 | 44 | int dfs(int u) 45 | { 46 | int t = evaluate(); 47 | if (t != INF) return t; 48 | 49 | if (!u) // Alice 50 | { 51 | int res = -INF; 52 | for (int i = 0; i < 3; i ++ ) 53 | for (int j = 0; j < 3; j ++ ) 54 | if (!g[i][j]) 55 | { 56 | g[i][j] = 1; 57 | res = max(res, dfs(1)); 58 | g[i][j] = 0; 59 | } 60 | return res; 61 | } 62 | else // Bob 63 | { 64 | int res = INF; 65 | for (int i = 0; i < 3; i ++ ) 66 | for (int j = 0; j < 3; j ++ ) 67 | if (!g[i][j]) 68 | { 69 | g[i][j] = 2; 70 | res = min(res, dfs(0)); 71 | g[i][j] = 0; 72 | } 73 | return res; 74 | } 75 | } 76 | 77 | int main() 78 | { 79 | int T; 80 | cin >> T; 81 | while (T -- ) 82 | { 83 | for (int i = 0; i < 3; i ++ ) 84 | for (int j = 0; j < 3; j ++ ) 85 | cin >> g[i][j]; 86 | cout << dfs(0) << endl; 87 | } 88 | return 0; 89 | } -------------------------------------------------------------------------------- /201803/5.cpp: -------------------------------------------------------------------------------- 1 | // cv 点分治 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef long long LL; 9 | const int N = 100010, M = N * 2, MOD = 1e9 + 7; 10 | 11 | int n, m, L, R; 12 | int w[N]; 13 | int h[N], father[N], e[M], ne[M], idx; 14 | int depth[N], fa[N][17]; 15 | int path[N], d[N], que[N]; 16 | int pos[N], root[N]; 17 | bool st[N]; 18 | int tr[N]; 19 | struct Node 20 | { 21 | int d, w, id; 22 | bool operator< (const Node& t) const 23 | { 24 | return d < t.d; 25 | } 26 | }q[N], p[N]; 27 | 28 | inline void add(int a, int b) 29 | { 30 | e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ; 31 | } 32 | 33 | inline int lowbit(int x) 34 | { 35 | return x & -x; 36 | } 37 | 38 | inline void update(int x, int v, int k) 39 | { 40 | for (int i = x; i <= k; i += lowbit(i)) tr[i] = (tr[i] + v) % MOD; 41 | } 42 | 43 | inline int query(int x, int k) 44 | { 45 | x = min(x, k); 46 | int res = 0; 47 | for (int i = x; i > 0; i -= lowbit(i)) res = (res + tr[i]) % MOD; 48 | return res; 49 | } 50 | 51 | void bfs() 52 | { 53 | memset(depth, 0x3f, sizeof depth); 54 | depth[0] = 0, depth[1] = 1; 55 | int hh = 0, tt = 0; 56 | que[0] = 1; 57 | while (hh <= tt) 58 | { 59 | int t = que[hh ++ ]; 60 | for (int i = h[t]; ~i; i = ne[i]) 61 | { 62 | int j = e[i]; 63 | if (depth[j] > depth[t] + 1) 64 | { 65 | depth[j] = depth[t] + 1; 66 | que[ ++ tt] = j; 67 | fa[j][0] = t; 68 | for (int k = 1; k <= 16; k ++ ) 69 | fa[j][k] = fa[fa[j][k - 1]][k - 1]; 70 | } 71 | } 72 | } 73 | } 74 | 75 | inline int lca(int a, int b) 76 | { 77 | if (depth[a] < depth[b]) swap(a, b); 78 | for (int k = 16; k >= 0; k -- ) 79 | if (depth[fa[a][k]] >= depth[b]) 80 | a = fa[a][k]; 81 | if (a == b) return a; 82 | for (int k = 16; k >= 0; k -- ) 83 | if (fa[a][k] != fa[b][k]) 84 | { 85 | a = fa[a][k]; 86 | b = fa[b][k]; 87 | } 88 | return fa[a][0]; 89 | } 90 | 91 | void dfs(int u, int fa) 92 | { 93 | d[u] = (d[fa] + path[u]) % MOD; 94 | for (int i = h[u]; ~i; i = ne[i]) 95 | { 96 | int j = e[i]; 97 | if (j == fa) continue; 98 | dfs(j, u); 99 | } 100 | } 101 | 102 | int get_size(int u, int fa) 103 | { 104 | if (st[u]) return 0; 105 | int res = 1; 106 | for (int i = h[u]; ~i; i = ne[i]) 107 | if (e[i] != fa) 108 | res += get_size(e[i], u); 109 | return res; 110 | } 111 | 112 | int get_wc(int u, int fa, int tot, int& wc) 113 | { 114 | if (st[u]) return 0; 115 | int sum = 1, ms = 0; 116 | for (int i = h[u]; ~i; i = ne[i]) 117 | { 118 | int j = e[i]; 119 | if (j == fa) continue; 120 | int t = get_wc(j, u, tot, wc); 121 | ms = max(ms, t); 122 | sum += t; 123 | } 124 | ms = max(ms, tot - sum); 125 | if (ms <= tot / 2) wc = u; 126 | return sum; 127 | } 128 | 129 | void get_dist(int u, int fa, int dist, int sum, int& qt) 130 | { 131 | if (st[u]) return; 132 | q[ ++ qt] = {dist, sum, u}; 133 | for (int i = h[u]; ~i; i = ne[i]) 134 | { 135 | int j = e[i]; 136 | if (j != fa) 137 | get_dist(j, u, dist + 1, (sum + w[j]) % MOD, qt); 138 | } 139 | } 140 | 141 | inline int get(Node a[], int k, int limit, int wu, int& pu) 142 | { 143 | sort(a + 1, a + k + 1); 144 | static int sum[N]; 145 | int res = 0; 146 | for (int i = 1; i <= k; i ++ ) sum[i] = (sum[i - 1] + a[i].w) % MOD; 147 | for (int i = 1, j = k; i < j; i ++ ) 148 | { 149 | while (j > i && a[j].d + a[i].d - 1 > limit) j -- ; 150 | if (j > i && a[j].d + a[i].d - 1 <= limit) 151 | { 152 | res = (res + (LL)sum[j] - sum[i] + (LL)(j - i) * a[i].w - (LL)wu * (j - i)) % MOD; 153 | pu = (pu + j - i) % MOD; 154 | } 155 | } 156 | return res; 157 | } 158 | 159 | int dfs_path(int u, int fa, int dist, int maxd) 160 | { 161 | if (st[u]) return 0; 162 | int res = (query(R + 1 - dist, maxd) - query(L - dist, maxd)) % MOD; 163 | if (dist >= L && dist <= R) res = (res + 1) % MOD; 164 | for (int i = h[u]; ~i; i = ne[i]) 165 | { 166 | int j = e[i]; 167 | if (j != fa) 168 | res = (res + dfs_path(j, u, dist + 1, maxd)) % MOD; 169 | } 170 | path[u] = (path[u] + res) % MOD; 171 | return res; 172 | } 173 | 174 | int calc(int u) 175 | { 176 | if (st[u]) return 0; 177 | get_wc(u, -1, get_size(u, -1), u); 178 | st[u] = true; 179 | 180 | int res = 0, pt = 0; 181 | if (L <= 1 && R >= 1) res = w[u], path[u] = (path[u] + 1) % MOD; 182 | int cnt = 0, maxd = 0; 183 | for (int i = h[u]; ~i; i = ne[i]) 184 | { 185 | int j = e[i], qt = 0; 186 | if (st[j]) continue; 187 | get_dist(j, -1, 2, (w[u] + w[j]) % MOD, qt); 188 | int pR = 0, pL = 0; 189 | res = (res - (LL)(get(q, qt, R, w[u], pR) - get(q, qt, L - 1, w[u], pL))) % MOD; 190 | path[u] = (path[u] - (LL)(pR - pL)) % MOD; 191 | pos[ ++ cnt] = pt + 1; // 每一段开头 192 | root[cnt] = j; // 每一段的根节点 193 | for (int k = 1; k <= qt; k ++ ) 194 | { 195 | if (q[k].d >= L && q[k].d <= R) 196 | { 197 | res = (res + q[k].w) % MOD; 198 | path[u] = (path[u] + 1) % MOD; // 只计算从u到当前点的 199 | } 200 | p[ ++ pt] = q[k]; 201 | maxd = max(maxd, q[k].d); 202 | } 203 | } 204 | 205 | pos[cnt + 1] = pt + 1; // 哨兵 206 | 207 | for (int i = 1; i <= maxd; i ++ ) tr[i] = 0; 208 | for (int i = 1; i <= pt; i ++ ) update(p[i].d, 1, maxd); // 插入树状数组中 209 | for (int i = 1; i <= cnt; i ++ ) 210 | { 211 | int l = pos[i], r = pos[i + 1] - 1; 212 | for (int j = l; j <= r; j ++ ) update(p[j].d, -1, maxd); // 将当前子树中的节点删掉 213 | dfs_path(root[i], u, 2, maxd); 214 | for (int j = l; j <= r; j ++ ) update(p[j].d, 1, maxd); // 将当前子树中的节点添加回来 215 | } 216 | 217 | int pR = 0, pL = 0; 218 | res = (res + (LL)get(p, pt, R, w[u], pR) - get(p, pt, L - 1, w[u], pL)) % MOD; 219 | path[u] = (path[u] + (LL)pR - pL) % MOD; 220 | 221 | for (int i = h[u]; ~i; i = ne[i]) res = (res + calc(e[i])) % MOD; 222 | return res; 223 | } 224 | 225 | int main() 226 | { 227 | int T; 228 | scanf("%d", &T); 229 | while (T -- ) 230 | { 231 | scanf("%d%d%d%d", &n, &m, &L, &R); 232 | memset(h, -1, sizeof h), idx = 0; 233 | memset(path, 0, sizeof path); 234 | for (int i = 1; i <= n; i ++ ) scanf("%d", &w[i]); 235 | for (int i = 2; i <= n; i ++ ) 236 | { 237 | int p; 238 | scanf("%d", &p); 239 | add(i, p), add(p, i); 240 | father[i] = p; 241 | } 242 | memset(st, 0, sizeof st); 243 | int res = calc(1); 244 | 245 | dfs(1, 0); 246 | bfs(); 247 | while (m -- ) 248 | { 249 | int a, b, c; 250 | scanf("%d%d%d", &a, &b, &c); 251 | int p = lca(a, b); 252 | int sum = (d[a] + (LL)d[b] - d[p] * 2 + path[p]) * c % MOD; 253 | res = ((res + sum) % MOD + MOD) % MOD; 254 | printf("%d\n", res); 255 | } 256 | } 257 | 258 | return 0; 259 | } -------------------------------------------------------------------------------- /201809/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int a[1010]; 8 | int b[1010]; 9 | int n; 10 | 11 | int main() { 12 | cin >> n; 13 | for (int i = 1; i <= n; i++) { 14 | cin >> a[i]; 15 | } 16 | 17 | for (int i = 1; i <= n; i++) { 18 | if (i == 1) { 19 | b[i] = (a[i] + a[i + 1]) / 2; 20 | } 21 | else if (i == n) { 22 | b[n] = (a[n] + a[n - 1]) / 2; 23 | } 24 | else b[i] = (a[i - 1] + a[i + 1] + a[i]) / 3; 25 | 26 | cout << b[i] << " "; 27 | } 28 | 29 | cout << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /201809/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define x first 6 | #define y second 7 | 8 | using namespace std; 9 | 10 | typedef pair pii; 11 | 12 | const int N = 2010; 13 | int n; 14 | pii a[N], b[N]; 15 | 16 | int main() { 17 | cin >> n; 18 | for (int i = 0; i < n; i++) { 19 | int st, end; 20 | cin >> st >> end; 21 | a[i] = {st, end}; 22 | } 23 | 24 | for (int i = 0; i < n; i++) { 25 | int st, end; 26 | cin >> st >> end; 27 | b[i] = {st, end}; 28 | } 29 | 30 | int i = 0, j = 0; 31 | int sum = 0; 32 | while(i < n && j < n) { 33 | if (a[i].y <= b[j].x) i ++; 34 | else if (a[i].x >= b[j].y) j ++; 35 | else { 36 | if (a[i].y <= b[j].y) { 37 | if (a[i].x >= b[j].x) sum += (a[i].y - a[i].x); 38 | else sum += (a[i].y - b[j].x); 39 | i ++; 40 | } 41 | else { 42 | if (a[i].x <= b[j].x) sum += (b[j].y - b[j].x); 43 | else sum += (b[j].y - a[i].x); 44 | j ++; 45 | } 46 | } 47 | } 48 | 49 | cout << sum << endl; 50 | 51 | return 0; 52 | } -------------------------------------------------------------------------------- /201809/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | int n, m; 11 | vector strs; 12 | 13 | struct Node 14 | { 15 | int tab; 16 | string tag, id; 17 | int k; // 当前节点最多可以匹配到第几个位置 18 | 19 | Node(string str) 20 | { 21 | int i = 0; 22 | while (str[i] == '.') i ++ ; 23 | tab = i; 24 | while (i < str.size() && str[i] != ' ') 25 | tag += tolower(str[i ++ ]); 26 | i ++ ; // 过滤掉空格 27 | while (i < str.size()) id += str[i ++ ]; 28 | k = 0; 29 | } 30 | bool check(string& word) 31 | { 32 | if (word[0] == '#') return word == id; 33 | return word == tag; 34 | } 35 | }; 36 | 37 | void work(vector& ws) 38 | { 39 | vector res; 40 | stack stk; 41 | for (int i = 0; i < strs.size(); i ++ ) 42 | { 43 | string str = strs[i]; 44 | Node t(str); 45 | while (stk.size() && stk.top().tab >= t.tab) stk.pop(); 46 | if (stk.size()) t.k = stk.top().k; 47 | if (t.k == ws.size()) t.k -- ; 48 | if (t.check(ws[t.k])) 49 | { 50 | t.k ++ ; 51 | if (t.k == ws.size()) res.push_back(i + 1); 52 | } 53 | stk.push(t); 54 | } 55 | cout << res.size(); 56 | for (auto x: res) cout << ' ' << x; 57 | cout << endl; 58 | } 59 | 60 | int main() 61 | { 62 | cin >> n >> m; 63 | getchar(); 64 | string str; 65 | while (n -- ) 66 | { 67 | getline(cin, str); 68 | strs.push_back(str); 69 | } 70 | while (m -- ) 71 | { 72 | getline(cin, str); 73 | stringstream ssin(str); 74 | vector ws; 75 | while (ssin >> str) 76 | { 77 | if (str[0] != '#') 78 | for (auto& c: str) 79 | c = tolower(c); 80 | ws.push_back(str); 81 | } 82 | work(ws); 83 | } 84 | return 0; 85 | } -------------------------------------------------------------------------------- /201809/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 310, M = N * 3; 8 | 9 | int n; 10 | int h[N], e[M], w[M], ne[M], idx; 11 | int dist[N], q[N]; 12 | int b[N]; 13 | bool st[N]; 14 | 15 | void add(int a, int b, int c) 16 | { 17 | e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ; 18 | } 19 | 20 | void spfa() 21 | { 22 | int hh = 0, tt = 1; 23 | memset(dist, -0x3f, sizeof dist); 24 | dist[0] = 0; 25 | q[0] = 0; 26 | while (hh != tt) 27 | { 28 | int t = q[hh ++ ]; 29 | if (hh == N) hh = 0; 30 | st[t] = false; 31 | 32 | for (int i = h[t]; ~i; i = ne[i]) 33 | { 34 | int j = e[i]; 35 | if (dist[j] < dist[t] + w[i]) 36 | { 37 | dist[j] = dist[t] + w[i]; 38 | if (!st[j]) 39 | { 40 | q[tt ++ ] = j; 41 | if (tt == N) tt = 0; 42 | st[j] = true; 43 | } 44 | } 45 | } 46 | } 47 | } 48 | 49 | int main() 50 | { 51 | cin >> n; 52 | memset(h, -1, sizeof h); 53 | for (int i = 1; i <= n; i ++ ) cin >> b[i]; 54 | for (int i = 2; i < n; i ++ ) 55 | { 56 | add(i - 2, i + 1, b[i] * 3); 57 | add(i + 1, i - 2, -(b[i] * 3 + 2)); 58 | } 59 | add(0, 2, b[1] * 2), add(2, 0, -(b[1] * 2 + 1)); 60 | add(n - 2, n, b[n] * 2), add(n, n - 2, -(b[n] * 2 + 1)); 61 | for (int i = 1; i <= n; i ++ ) add(i - 1, i, 1); 62 | spfa(); 63 | 64 | for (int i = 1; i <= n; i ++ ) 65 | cout << dist[i] - dist[i - 1] << ' '; 66 | return 0; 67 | } -------------------------------------------------------------------------------- /201809/5.cpp: -------------------------------------------------------------------------------- 1 | // TODO -------------------------------------------------------------------------------- /201812/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int r, y, g, n; 8 | 9 | int main() { 10 | cin >> r >> y >> g; 11 | cin >> n; 12 | 13 | int ans = 0; 14 | for (int i = 0; i < n; i++) { 15 | int k, t; 16 | cin >> k >> t; 17 | 18 | if (k == 0) ans += t; 19 | else if (k == 1) ans += t; 20 | else if (k == 2) ans += t + r; 21 | else continue; 22 | } 23 | 24 | cout << ans << endl; 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /201812/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef long long LL; 8 | 9 | int main() { 10 | int r, y, g; 11 | cin >> r >> y >> g; 12 | int n; 13 | cin >> n; 14 | 15 | LL res = 0; 16 | while(n --) { 17 | LL k, t; 18 | cin >> k >> t; 19 | 20 | if (k == 0) res += t; 21 | else { 22 | if (k == 1) t = r - t; 23 | else if (k == 2) t = r + g + y - t; 24 | else t = r + g - t; 25 | 26 | t += res; 27 | t %= (r + g + y); 28 | if (t < r) res += r - t; 29 | else if (t >= r + g) res += r + g + y - t + r; 30 | } 31 | } 32 | 33 | cout << res << endl; 34 | return 0; 35 | } -------------------------------------------------------------------------------- /201812/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 100010; 8 | 9 | int n; 10 | struct IP 11 | { 12 | string v; 13 | int k; 14 | bool operator< (const IP& t) const 15 | { 16 | if (v != t.v) return v < t.v; 17 | return k < t.k; 18 | } 19 | bool is_substr(IP& t) 20 | { 21 | if (t.k < k) return false; 22 | if (v.substr(0, k) != t.v.substr(0, k)) return false; 23 | return true; 24 | } 25 | 26 | int get_number(string str) 27 | { 28 | int res = 0; 29 | for (int i = 0; i < 8; i ++ ) 30 | res = res * 2 + str[i] - '0'; 31 | return res; 32 | } 33 | 34 | void print() 35 | { 36 | for (int i = 0; i < 32; i += 8) 37 | { 38 | if (i) printf("."); 39 | printf("%d", get_number(v.substr(i, 8))); 40 | } 41 | printf("/%d\n", k); 42 | } 43 | }ip[N]; 44 | 45 | IP merge(IP& a, IP& b) 46 | { 47 | IP res; 48 | res.k = -1; 49 | if (a.k != b.k) return res; 50 | if (a.v.substr(0, a.k - 1) != b.v.substr(0, b.k - 1)) return res; 51 | res.k = a.k - 1; 52 | res.v = a.v.substr(0, a.k - 1); 53 | while (res.v.size() <= 32) res.v += '0'; 54 | return res; 55 | } 56 | 57 | int main() 58 | { 59 | scanf("%d", &n); 60 | char str[20]; 61 | int d[4]; 62 | for (int i = 0; i < n; i ++ ) 63 | { 64 | scanf("%s", str); 65 | memset(d, 0, sizeof d); 66 | int cnt = 0; 67 | ip[i].k = -1; 68 | for (int j = 0; str[j]; j ++ ) 69 | { 70 | if (str[j] == '/') 71 | { 72 | ip[i].k = atoi(str + j + 1); 73 | break; 74 | } 75 | if (str[j] == '.') continue; 76 | while (str[j] && str[j] != '.' && str[j] != '/') 77 | d[cnt] = d[cnt] * 10 + str[j ++ ] - '0'; 78 | j -- ; 79 | cnt ++ ; 80 | } 81 | for (int j = 0; j < 4; j ++ ) 82 | for (int k = 7; k >= 0; k -- ) 83 | if (d[j] >> k & 1) 84 | ip[i].v += '1'; 85 | else 86 | ip[i].v += '0'; 87 | if (ip[i].k == -1) ip[i].k = cnt * 8; 88 | } 89 | 90 | sort(ip, ip + n); 91 | 92 | int k = 1; 93 | for (int i = 1; i < n; i ++ ) 94 | if (!ip[k - 1].is_substr(ip[i])) 95 | ip[k ++ ] = ip[i]; 96 | n = k; 97 | 98 | k = 1; 99 | for (int i = 1; i < n; i ++ ) 100 | { 101 | ip[k ++ ] = ip[i]; 102 | while (k >= 2) 103 | { 104 | auto t = merge(ip[k - 2], ip[k - 1]); 105 | if (t.k != -1) 106 | { 107 | k -= 2; 108 | ip[k ++ ] = t; 109 | } 110 | else break; 111 | } 112 | } 113 | 114 | n = k; 115 | for (int i = 0; i < n; i ++ ) 116 | ip[i].print(); 117 | return 0; 118 | } -------------------------------------------------------------------------------- /201812/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 50010, M = 100010; 8 | 9 | int n, m; // n是点数,m是边数 10 | int p[N]; // 并查集的父节点数组 11 | 12 | struct Edge // 存储边 13 | { 14 | int a, b, w; 15 | 16 | bool operator< (const Edge &W)const 17 | { 18 | return w < W.w; 19 | } 20 | }edges[M]; 21 | 22 | int find(int x) // 并查集核心操作 23 | { 24 | if (p[x] != x) p[x] = find(p[x]); 25 | return p[x]; 26 | } 27 | 28 | int kruskal() { 29 | sort(edges, edges + m); 30 | 31 | for (int i = 1; i <= n; i ++ ) p[i] = i; // 初始化并查集 32 | 33 | int res = 0, cnt = 0; 34 | for (int i = 0; i < m; i ++ ) { 35 | int a = edges[i].a, b = edges[i].b, w = edges[i].w; 36 | 37 | a = find(a), b = find(b); 38 | if (a != b) // 如果两个连通块不连通,则将这两个连通块合并 39 | { 40 | p[a] = b; 41 | res = max(res, w); 42 | cnt ++ ; 43 | } 44 | } 45 | 46 | return res; 47 | } 48 | 49 | int main() { 50 | int root; 51 | cin >> n >> m >> root; 52 | 53 | int cnt = 0; 54 | for (int i = 1; i <= m; i++) { 55 | int u, v, t; 56 | cin >> u >> v >> t; 57 | edges[cnt ++] = {u, v, t}; 58 | } 59 | 60 | sort(edges, edges + cnt); 61 | 62 | cout << kruskal() << endl; 63 | 64 | return 0; 65 | } -------------------------------------------------------------------------------- /201812/5.cpp: -------------------------------------------------------------------------------- 1 | // 费用流 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | const int N = 210, M = (500 + N) * 2 + 10, INF = 1e6; 9 | 10 | int n, m, S, T; 11 | int h[N], e[M], f[M], w[M], ne[M], idx; 12 | int q[N], dist[N], pre[N], incf[N]; 13 | bool st[N]; 14 | int din[N], dout[N]; 15 | 16 | void add(int a, int b, int c, int d) 17 | { 18 | e[idx] = b, f[idx] = c, w[idx] = d, ne[idx] = h[a], h[a] = idx ++ ; 19 | e[idx] = a, f[idx] = 0, w[idx] = -d, ne[idx] = h[b], h[b] = idx ++ ; 20 | } 21 | 22 | bool spfa() 23 | { 24 | int hh = 0, tt = 1; 25 | memset(dist, 0x3f, sizeof dist); 26 | memset(incf, 0, sizeof incf); 27 | q[0] = S, dist[S] = 0, incf[S] = INF; 28 | while (hh != tt) 29 | { 30 | int t = q[hh ++ ]; 31 | if (hh == N) hh = 0; 32 | st[t] = false; 33 | 34 | for (int i = h[t]; ~i; i = ne[i]) 35 | { 36 | int j = e[i]; 37 | if (f[i] && dist[j] > dist[t] + w[i]) 38 | { 39 | dist[j] = dist[t] + w[i]; 40 | pre[j] = i; 41 | incf[j] = min(f[i], incf[t]); 42 | if (!st[j]) 43 | { 44 | q[tt ++ ] = j; 45 | if (tt == N) tt = 0; 46 | st[j] = true; 47 | } 48 | } 49 | } 50 | } 51 | return incf[T] > 0; 52 | } 53 | 54 | int EK(int tot) 55 | { 56 | int flow = 0, cost = 0; 57 | while (spfa()) 58 | { 59 | int t = incf[T]; 60 | flow += t, cost += t * dist[T]; 61 | for (int i = T; i != S; i = e[pre[i] ^ 1]) 62 | { 63 | f[pre[i]] -= t; 64 | f[pre[i] ^ 1] += t; 65 | } 66 | } 67 | if (flow != tot) return -1; 68 | return cost; 69 | } 70 | 71 | int main() 72 | { 73 | int C, E; 74 | scanf("%d%*d%d", &C, &E); 75 | while (C -- ) 76 | { 77 | memset(h, -1, sizeof h); 78 | idx = 0; 79 | memset(din, 0, sizeof din); 80 | memset(dout, 0, sizeof dout); 81 | scanf("%d%d", &n, &m); 82 | S = 0, T = n + 1; 83 | int down_cost = 0; 84 | while (m -- ) 85 | { 86 | int a, b; 87 | char c; 88 | scanf("%d %d %c", &a, &b, &c); 89 | int down, up; 90 | if (c == 'A') down = 1, up = INF, down_cost += E; 91 | else if (c == 'B') down = up = 1, down_cost += E; 92 | else if (c == 'C') down = 0, up = INF; 93 | else down = 0, up = 1; 94 | add(a, b, up - down, E); 95 | din[b] += down, dout[a] += down; 96 | } 97 | int tot = 0; 98 | for (int i = 1; i <= n; i ++ ) 99 | if (din[i] > dout[i]) 100 | { 101 | add(S, i, din[i] - dout[i], 0); 102 | tot += din[i] - dout[i]; 103 | } 104 | else add(i, T, dout[i] - din[i], 0); 105 | 106 | int c = EK(tot); 107 | if (c != -1) c += down_cost; 108 | printf("%d\n", c); 109 | } 110 | return 0; 111 | } -------------------------------------------------------------------------------- /201903/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N =100010; 8 | int n; 9 | int a[N]; 10 | 11 | int main() { 12 | cin >> n; 13 | for (int i = 1; i <= n; i++) { 14 | cin >> a[i]; 15 | } 16 | 17 | int mid; 18 | float md; 19 | bool flag = true; 20 | if (n % 2 == 0) { 21 | if ((a[n / 2] + a[n / 2 + 1]) % 2) { 22 | md = (float)(a[n / 2] + a[n / 2 + 1]) / 2; 23 | flag = false; 24 | } 25 | else { 26 | mid = (a[n / 2] + a[n / 2 + 1]) / 2; 27 | } 28 | } 29 | else mid = a[n / 2 + 1]; 30 | 31 | if (a[n] >= a[1] && flag) printf("%d %d %d\n", a[n], mid, a[1]); 32 | else if (a[n] >= a[1]) printf("%d %.1lf %d\n", a[n], md, a[1]); 33 | else if (flag) printf("%d %d %d\n", a[1], mid, a[n]); 34 | else printf("%d %.1lf %d\n", a[1], md, a[n]); 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /201903/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | bool check(string str) { 10 | stack st1; 11 | stack st2; 12 | for (int i = 0; i < str.size(); i++) { 13 | if (str[i] >= '0' && str[i] <= '9') st2.push(str[i] - '0'); 14 | else if (str[i] == '+' || str[i] == '-') st1.push(str[i]); 15 | else { 16 | if (str[i] == 'x') { 17 | int pre = st2.top(); 18 | st2.pop(); 19 | int nxt = str[i + 1] - '0'; 20 | st2.push(pre * nxt); 21 | } 22 | else { 23 | int pre = st2.top(); 24 | st2.pop(); 25 | int nxt = str[i + 1] - '0'; 26 | st2.push(pre / nxt); 27 | } 28 | i ++; 29 | } 30 | } 31 | 32 | stack stk1; 33 | stack stk2; 34 | 35 | while(st1.size()) { 36 | stk1.push(st1.top()); 37 | st1.pop(); 38 | } 39 | 40 | while(st2.size()) { 41 | stk2.push(st2.top()); 42 | st2.pop(); 43 | } 44 | 45 | while(stk1.size()) { 46 | char ch = stk1.top(); 47 | stk1.pop(); 48 | 49 | int pre = stk2.top(); 50 | stk2.pop(); 51 | int nxt = stk2.top(); 52 | stk2.pop(); 53 | 54 | if (ch == '+') { 55 | stk2.push(pre + nxt); 56 | } 57 | else { 58 | stk2.push(pre - nxt); 59 | } 60 | } 61 | 62 | if (stk2.top() == 24) return true; 63 | else return false; 64 | } 65 | 66 | int main() { 67 | int n; 68 | cin >> n; 69 | 70 | while(n --) { 71 | string str; 72 | cin >> str; 73 | if (check(str)) puts("Yes"); 74 | else puts("No"); 75 | } 76 | 77 | return 0; 78 | } -------------------------------------------------------------------------------- /201903/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef unsigned int UI; 8 | const int N = 1010, M = 40 * 1024 * 8 + 10; 9 | 10 | int n, s, l; 11 | UI disk[N][M / 8]; 12 | bool st[N]; 13 | char str[M]; 14 | int len; 15 | 16 | inline UI get(char c) 17 | { 18 | if (c <= '9') return c - '0'; 19 | return c - 'A' + 10; 20 | } 21 | 22 | inline char get(UI x) 23 | { 24 | if (x <= 9) return x + '0'; 25 | return x - 10 + 'A'; 26 | } 27 | 28 | inline string u2s(UI x) 29 | { 30 | string res; 31 | for (int i = 7; i >= 0; i -- ) 32 | res += get(x >> (i << 2) & 15); 33 | return res; 34 | } 35 | 36 | inline int get_real_col(int r, int c) 37 | { 38 | r %= n; 39 | r = n - 1 - r; 40 | return (r + 1 + c) % n; 41 | } 42 | 43 | int main() 44 | { 45 | scanf("%d%d%d", &n, &s, &l); 46 | for (int u = 0; u < l; u ++ ) 47 | { 48 | int k; 49 | scanf("%d", &k); 50 | getchar(); 51 | fgets(str, M, stdin); 52 | int sz = strlen(str) - 1; 53 | for (int i = 0; i < sz; i += 8) 54 | { 55 | UI x = 0; 56 | for (int j = 0; j < 8; j ++ ) 57 | x = (x << 4) + get(str[i + j]); 58 | disk[k][i >> 3] = x; 59 | } 60 | st[k] = true; 61 | len = max(len, sz >> 3); 62 | } 63 | 64 | int m; 65 | scanf("%d", &m); 66 | while (m -- ) 67 | { 68 | int b; 69 | scanf("%d", &b); 70 | if (b >= len * (n - 1)) puts("-"); 71 | else 72 | { 73 | int k = b / s; 74 | int row = k / (n - 1), col = get_real_col(row, k % (n - 1)); 75 | int r = row * s + b % s; 76 | if (st[col]) 77 | puts(u2s(disk[col][r]).c_str()); 78 | else if (l == n - 1) 79 | { 80 | UI x = 0; 81 | for (int i = 0; i < n; i ++ ) x ^= disk[i][r]; 82 | puts(u2s(x).c_str()); 83 | } 84 | else puts("-"); 85 | } 86 | } 87 | return 0; 88 | } -------------------------------------------------------------------------------- /201903/4.cpp: -------------------------------------------------------------------------------- 1 | // 90 分 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | const int N =10010; 10 | int n, T; 11 | 12 | bool solve(vector>& g, vector>>& st) { 13 | vector idxs(n, 0); // 维护每行当前走到的位置 14 | 15 | while(true) { 16 | vector nidxs = idxs; 17 | 18 | for (int i = 0; i < n; i++) { 19 | int idx = idxs[i]; 20 | 21 | if (idx == g[i].size()) continue; 22 | 23 | for (int j = 0; j < st[i][idx].size(); j++) { 24 | int nidx = st[i][idx][j]; 25 | int task = stoi(g[i][idx].substr(1)); 26 | 27 | if (idxs[task] == nidx) { 28 | idxs[i] ++; 29 | idxs[task] ++; 30 | break; 31 | } 32 | else continue; 33 | } 34 | } 35 | 36 | if (nidxs == idxs) break; 37 | } 38 | 39 | for (int i = 0; i < n; i++) { 40 | if (idxs[i] != g[i].size()){ 41 | // cout << "line" << i << " : " << idxs[i] << ", " << g[i].size() << endl; 42 | return false; 43 | } 44 | } 45 | 46 | return true; 47 | } 48 | 49 | int main() { 50 | cin >> T >> n; 51 | getchar(); 52 | 53 | while(T --) { 54 | vector> g(n, vector()); // 存储指令 55 | vector>> st(n, vector>()); // 每一个任务 每一条指令能够匹配的 对应任务的指令 idx 56 | for (int i = 0; i < n; i++) { 57 | string line; 58 | getline(cin, line); 59 | stringstream ssin(line); 60 | string str; 61 | while (ssin >> str) g[i].push_back(str); 62 | } 63 | 64 | for (int i = 0; i < n; i++) { 65 | st[i] = vector>(g[i].size(), vector()); 66 | for (int k = 0; k < g[i].size(); k++) { 67 | string str = g[i][k]; 68 | int j = stoi(str.substr(1)); 69 | for (int s = 0; s < g[j].size(); s++) { 70 | int num = stoi(g[j][s].substr(1)); 71 | if (num == i && ((str[0] == 'S' && g[j][s][0] == 'R') || (str[0] == 'R' && g[j][s][0] == 'S'))) { 72 | st[i][k].push_back(s); 73 | } 74 | } 75 | } 76 | } 77 | 78 | if (solve(g, st)) puts("0"); 79 | else puts("1"); 80 | } 81 | 82 | return 0; 83 | } -------------------------------------------------------------------------------- /201903/5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define x first 7 | #define y second 8 | 9 | using namespace std; 10 | 11 | typedef pair PII; 12 | const int N = 10010, M = 20010, INF = 0x3f3f3f3f; 13 | 14 | int n, m, k; 15 | int type[N]; 16 | int h[N], e[M], w[M], ne[M], idx; 17 | int dist[N]; 18 | bool st[N]; 19 | int q[N]; 20 | int ds[N][1010]; 21 | int cnt; 22 | 23 | void add(int a, int b, int c) { 24 | e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ; 25 | } 26 | 27 | void spfa(int start) { 28 | memset(dist, 0x3f, sizeof dist); 29 | dist[start] = 0; 30 | int hh = 0, tt = 1; 31 | q[0] = start; 32 | 33 | while (hh != tt) { 34 | auto t = q[hh ++ ]; 35 | if (hh == N) hh = 0; 36 | st[t] = false; 37 | 38 | for (int i = h[t]; ~i; i = ne[i]) { 39 | int j = e[i]; 40 | if (dist[j] > dist[t] + w[i]) { 41 | dist[j] = dist[t] + w[i]; 42 | if (!st[j]) { 43 | q[tt ++ ] = j; 44 | if (tt == N) tt = 0; 45 | st[j] = true; 46 | } 47 | } 48 | } 49 | } 50 | 51 | for (int i = 1; i <= n; i ++ ) ds[i][cnt] = dist[i]; 52 | } 53 | 54 | int main() { 55 | cin >> n >> m >> k; 56 | 57 | for (int i = 1; i <= n; i ++ ) scanf("%d", &type[i]); 58 | memset(h, -1, sizeof h); 59 | while (m -- ) { 60 | int a, b, c; 61 | scanf("%d%d%d", &a, &b, &c); 62 | add(a, b, c), add(b, a, c); 63 | } 64 | 65 | for (int i = 1; i <= n; i ++ ) { 66 | if (type[i]) { 67 | spfa(i); 68 | cnt ++ ; 69 | } 70 | } 71 | 72 | for (int i = 1; i <= n; i ++ ) { 73 | sort(ds[i], ds[i] + cnt); 74 | int res = 0; 75 | for (int j = 0; j < k && j < cnt; j ++ ) 76 | if (ds[i][j] != INF) 77 | res += ds[i][j]; 78 | 79 | printf("%d\n", res); 80 | } 81 | 82 | return 0; 83 | } -------------------------------------------------------------------------------- /201909/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 1010; 8 | int n, m; 9 | int a[N]; 10 | int change[N]; 11 | 12 | int main() { 13 | cin >> n >> m; 14 | 15 | for (int i = 1; i <= n; i++) { 16 | cin >> a[i]; 17 | for (int j = 1; j <= m; j++) { 18 | int val; 19 | cin >> val; 20 | a[i] += val; 21 | change[i] -= val; 22 | } 23 | } 24 | 25 | int T = 0, diff = 0, idx; 26 | for (int i = 1; i <= n; i++) { 27 | T += a[i]; 28 | if (change[i] > diff) { 29 | diff = change[i]; 30 | idx = i; 31 | } 32 | } 33 | 34 | printf("%d %d %d\n", T, idx, diff); 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /201909/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 1010; 8 | int n; 9 | int a[N]; 10 | bool st[N]; 11 | 12 | int main() { 13 | cin >> n; 14 | 15 | for (int i = 1; i <= n; i++) { 16 | int m; 17 | cin >> m; 18 | for (int j = 1; j <= m; j++) { 19 | int val; 20 | cin >> val; 21 | 22 | if (j == 1) { 23 | a[i] = val; 24 | continue; 25 | } 26 | 27 | if (val > 0) { 28 | if (a[i] != val) st[i] = true; 29 | a[i] = val; 30 | } 31 | else { 32 | a[i] += val; 33 | } 34 | } 35 | } 36 | 37 | int t = 0, d = 0, e = 0; 38 | for (int i = 1; i <= n; i ++) { 39 | t += a[i]; 40 | if (st[i]) d ++; 41 | 42 | if (i <= n - 2 && st[i] && st[i + 1] && st[i + 2]) e ++; 43 | if (i == n - 1 && st[i] && st[n] && st[1]) e ++; 44 | if (i == n && st[n] && st[1] && st[2]) e ++; 45 | } 46 | 47 | printf("%d %d %d\n", t, d, e); 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /201909/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef unsigned char UC; 8 | const int N = 1080, M = 1920; 9 | 10 | int m, n, p, q; 11 | UC g[N][M][3]; 12 | 13 | inline int get(char c) { 14 | if (c <= '9') return c - '0'; 15 | return c - 'a' + 10; 16 | } 17 | 18 | inline char get(int x) { 19 | if (x <= 9) return x + '0'; 20 | return x - 10 + 'A'; 21 | } 22 | 23 | inline void print(char* str) { 24 | for (int i = 0; str[i]; i ++ ) 25 | printf("\\x%c%c", get(str[i] / 16), get(str[i] % 16)); 26 | } 27 | 28 | int main() { 29 | scanf("%d%d%d%d", &m, &n, &p, &q); 30 | char str[100]; 31 | for (int i = 0; i < n; i ++ ) 32 | for (int j = 0; j < m; j ++ ) { 33 | scanf("%s", str); 34 | int len = strlen(str); 35 | if (len == 2) { 36 | int t = get(str[1]); 37 | for (int k = 0; k < 3; k ++ ) 38 | g[i][j][k] = t * 16 + t; 39 | } 40 | else if (len == 4) { 41 | for (int k = 0; k < 3; k ++ ) { 42 | int t = get(str[1 + k]); 43 | g[i][j][k] = t * 16 + t; 44 | } 45 | } 46 | else { 47 | for (int k = 0; k < 3; k ++ ) 48 | g[i][j][k] = get(str[1 + k * 2]) * 16 + get(str[2 + k * 2]); 49 | } 50 | } 51 | 52 | int bg[3] = {0}; 53 | for (int i = 0; i < n / q; i ++ ) { 54 | for (int j = 0; j < m / p; j ++ ) { 55 | int cur[3] = {0}; 56 | for (int x = 0; x < q; x ++ ) 57 | for (int y = 0; y < p; y ++ ) 58 | for (int z = 0; z < 3; z ++ ) 59 | cur[z] += g[i * q + x][j * p + y][z]; 60 | 61 | for (int k = 0; k < 3; k ++ ) cur[k] /= p * q; 62 | 63 | if (cur[0] == bg[0] && cur[1] == bg[1] && cur[2] == bg[2]) ; // pass 64 | else if (!cur[0] && !cur[1] && !cur[2]) print("\033[0m"); 65 | else { 66 | sprintf(str, "\033[48;2;%d;%d;%dm", cur[0], cur[1], cur[2]); 67 | print(str); 68 | } 69 | 70 | for (int k = 0; k < 3; k ++ ) bg[k] = cur[k]; 71 | print(" "); 72 | } 73 | if (bg[0] || bg[1] || bg[2]) { 74 | print("\033[0m"); 75 | for (int k = 0; k < 3; k ++ ) bg[k] = 0; 76 | } 77 | print("\n"); 78 | } 79 | return 0; 80 | } -------------------------------------------------------------------------------- /201909/4.cpp: -------------------------------------------------------------------------------- 1 | // 多路归并 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define x first 10 | #define y second 11 | 12 | using namespace std; 13 | 14 | typedef pair PII; 15 | const int N = 55; 16 | 17 | int m, n; 18 | set g[N]; 19 | unordered_map f[N]; 20 | int sum[N], cnt[N]; 21 | set::reverse_iterator it[N]; 22 | vector ans[N]; 23 | 24 | int main() 25 | { 26 | scanf("%d%d", &m, &n); 27 | for (int i = 1; i <= n; i ++ ) 28 | { 29 | int id, score; 30 | scanf("%d%d", &id, &score); 31 | for (int j = 0; j < m; j ++ ) 32 | { 33 | f[j][id] = score; 34 | g[j].insert({score, -id}); 35 | } 36 | } 37 | 38 | int Q; 39 | scanf("%d", &Q); 40 | while (Q -- ) 41 | { 42 | int t; 43 | scanf("%d", &t); 44 | if (t == 1) 45 | { 46 | int type, id, score; 47 | scanf("%d%d%d", &type, &id, &score); 48 | f[type][id] = score; 49 | g[type].insert({score, -id}); 50 | } 51 | else if (t == 2) 52 | { 53 | int type, id; 54 | scanf("%d%d", &type, &id); 55 | g[type].erase({f[type][id], -id}); 56 | f[type].erase(id); 57 | } 58 | else 59 | { 60 | int tot; 61 | scanf("%d", &tot); 62 | for (int i = 0; i < m; i ++ ) 63 | { 64 | scanf("%d", &sum[i]); 65 | it[i] = g[i].rbegin(); 66 | cnt[i] = 0; 67 | ans[i].clear(); 68 | } 69 | while (tot -- ) 70 | { 71 | int k = -1; 72 | for (int i = 0; i < m; i ++ ) 73 | if (it[i] != g[i].rend() && cnt[i] < sum[i]) 74 | if (k == -1 || it[i]->x > it[k]->x) 75 | k = i; 76 | if (k == -1) break; 77 | ans[k].push_back(-it[k]->y); 78 | cnt[k] ++ ; 79 | it[k] ++ ; 80 | } 81 | for (int i = 0; i < m; i ++ ) 82 | if (ans[i].empty()) puts("-1"); 83 | else 84 | { 85 | for (auto x: ans[i]) 86 | printf("%d ", x); 87 | puts(""); 88 | } 89 | } 90 | } 91 | return 0; 92 | } -------------------------------------------------------------------------------- /201909/5.cpp: -------------------------------------------------------------------------------- 1 | // 树型DP 分组背包 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef long long LL; 9 | const int N = 50010, M = N * 2; 10 | 11 | int n, m, K; 12 | int h[N], e[M], w[M], ne[M], idx; 13 | LL f[N][110]; 14 | bool st[N]; 15 | int sz[N]; 16 | LL ans = 1e18; 17 | 18 | void add(int a, int b, int c) 19 | { 20 | e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ; 21 | } 22 | 23 | void dfs(int u, int fa) 24 | { 25 | f[u][0] = 0; 26 | if (st[u]) f[u][1] = 0; 27 | sz[u] = 1; 28 | for (int i = h[u]; ~i; i = ne[i]) // 枚举物品组 29 | { 30 | int ver = e[i]; 31 | if (ver == fa) continue; 32 | dfs(ver, u); 33 | sz[u] += sz[ver]; 34 | for (int j = min(sz[u], K); j >= 0; j -- ) // 枚举体积 35 | for (int k = 0; k <= min(j, sz[ver]); k ++ ) // 枚举决策 36 | f[u][j] = min(f[u][j], f[u][j - k] + f[ver][k] + (LL)w[i] * k * (K - k)); 37 | } 38 | 39 | ans = min(ans, f[u][K]); 40 | } 41 | 42 | int main() 43 | { 44 | scanf("%d%d%d", &n, &m, &K); 45 | memset(h, -1, sizeof h); 46 | while (m -- ) 47 | { 48 | int x; 49 | scanf("%d", &x); 50 | st[x] = true; 51 | } 52 | for (int i = 0; i < n - 1; i ++ ) 53 | { 54 | int a, b, c; 55 | scanf("%d%d%d", &a, &b, &c); 56 | add(a, b, c), add(b, a, c); 57 | } 58 | 59 | memset(f, 0x3f, sizeof f); 60 | dfs(1, -1); 61 | printf("%lld\n", ans); 62 | return 0; 63 | } -------------------------------------------------------------------------------- /201912/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int n; 8 | int a[4]; 9 | 10 | bool check(int x) { 11 | if (x % 7 == 0) return true; 12 | 13 | while (x) { 14 | int mod = x % 10; 15 | x /= 10; 16 | if (mod == 7) return true; 17 | } 18 | 19 | return false; 20 | } 21 | 22 | int main() { 23 | cin >> n; 24 | 25 | int cnt = 0; 26 | int num = 0; 27 | int idx = 0; 28 | while(num < n) { 29 | cnt ++; 30 | while (check(cnt)) { 31 | cnt ++; 32 | a[idx] ++; 33 | idx = (idx + 1) % 4; 34 | } 35 | 36 | idx = (idx + 1) % 4; 37 | num ++; 38 | } 39 | 40 | for (int i = 0; i < 4; i++) cout << a[i] << endl; 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /201912/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define x first 6 | #define y second 7 | 8 | using namespace std; 9 | 10 | typedef pair PII; 11 | const int N = 1010; 12 | 13 | int n; 14 | PII q[N]; 15 | int ans[5]; 16 | int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0}; 17 | int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1}; 18 | 19 | int main() { 20 | cin >> n; 21 | for (int i = 0; i < n; i ++ ) cin >> q[i].x >> q[i].y; 22 | for (int i = 0; i < n; i ++ ) { 23 | int s[8] = {0}; 24 | for (int j = 0; j < n; j ++ ) 25 | for (int k = 0; k < 8; k ++ ) 26 | if (q[i].x + dx[k] == q[j].x && q[i].y + dy[k] == q[j].y) 27 | s[k] ++ ; 28 | if (s[1] && s[3] && s[5] && s[7]) 29 | ans[s[0] + s[2] + s[4] + s[6]] ++ ; 30 | } 31 | 32 | for (int i = 0; i < 5; i ++ ) cout << ans[i] << endl; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /201912/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define x first 7 | #define y second 8 | 9 | using namespace std; 10 | 11 | typedef unordered_map MPSI; 12 | 13 | MPSI dfs(string& str, int& u) 14 | { 15 | MPSI res; 16 | while (u < str.size()) 17 | { 18 | if (str[u] == '(') 19 | { 20 | u ++ ; // 过滤掉 '(' 21 | auto t = dfs(str, u); 22 | u ++ ; // 过滤掉 ')' 23 | int cnt = 1, k = u; 24 | while (k < str.size() && isdigit(str[k])) k ++ ; 25 | if (k > u) 26 | { 27 | cnt = stoi(str.substr(u, k - u)); 28 | u = k; 29 | } 30 | for (auto c: t) 31 | res[c.x] += c.y * cnt; 32 | } 33 | else if (str[u] == ')') break; 34 | else 35 | { 36 | int k = u + 1; 37 | while (k < str.size() && str[k] >= 'a' && str[k] <= 'z') k ++ ; 38 | auto key = str.substr(u, k - u); 39 | u = k; 40 | int cnt = 1; 41 | while (k < str.size() && isdigit(str[k])) k ++ ; 42 | if (k > u) 43 | { 44 | cnt = stoi(str.substr(u, k - u)); 45 | u = k; 46 | } 47 | res[key] += cnt; 48 | } 49 | } 50 | return res; 51 | } 52 | 53 | MPSI work(string str) 54 | { 55 | MPSI res; 56 | for (int i = 0; i < str.size(); i ++ ) 57 | { 58 | int j = i + 1; 59 | while (j < str.size() && str[j] != '+') j ++ ; 60 | auto item = str.substr(i, j - i); 61 | i = j; 62 | int cnt = 1, k = 0; 63 | while (k < item.size() && isdigit(item[k])) k ++ ; 64 | if (k) cnt = stoi(item.substr(0, k)); 65 | auto t = dfs(item, k); 66 | for (auto c: t) 67 | res[c.x] += c.y * cnt; 68 | } 69 | return res; 70 | } 71 | 72 | int main() 73 | { 74 | int n; 75 | cin >> n; 76 | while (n -- ) 77 | { 78 | string str; 79 | cin >> str; 80 | int k = str.find('='); 81 | auto left = work(str.substr(0, k)), right = work(str.substr(k + 1)); 82 | if (left == right) puts("Y"); 83 | else puts("N"); 84 | } 85 | return 0; 86 | } -------------------------------------------------------------------------------- /201912/4.cpp: -------------------------------------------------------------------------------- 1 | // 可持久化链表 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | typedef vector VI; 12 | const int N = 510, M = 20010; 13 | 14 | int n, m, w, Q; 15 | int h[N], e[M], ne[M], idx; 16 | vector g; 17 | int node[N]; 18 | 19 | struct Op 20 | { 21 | int t, id, pid, hid; 22 | bool operator< (const Op& r) const 23 | { 24 | return t > r.t; 25 | } 26 | }; 27 | priority_queue heap; 28 | 29 | void add(int a, int b) 30 | { 31 | e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ; 32 | } 33 | 34 | void eval() 35 | { 36 | auto t = heap.top(); 37 | heap.pop(); 38 | 39 | auto &a = g[node[t.id]], &b = g[t.hid]; 40 | if (b.size() > a.size() || b.size() == a.size() && b.back() < a.back()) 41 | { 42 | node[t.id] = t.hid; 43 | for (int i = h[t.id]; ~i; i = ne[i]) 44 | if (e[i] != t.pid && e[i] != t.id) 45 | heap.push({t.t + w, e[i], t.id, t.hid}); 46 | } 47 | } 48 | 49 | int main() 50 | { 51 | scanf("%d%d", &n, &m); 52 | g.push_back({0}); 53 | memset(h, -1, sizeof h); 54 | while (m -- ) 55 | { 56 | int a, b; 57 | scanf("%d%d", &a, &b); 58 | add(a, b), add(b, a); 59 | } 60 | 61 | scanf("%d%d", &w, &Q); 62 | getchar(); 63 | char str[100]; 64 | while (Q -- ) 65 | { 66 | fgets(str, 100, stdin); 67 | stringstream ssin(str); 68 | int a[3], cnt = 0; 69 | while (ssin >> a[cnt]) cnt ++ ; 70 | if (cnt == 3) 71 | { 72 | while (heap.size() && heap.top().t <= a[1]) eval(); 73 | g.push_back(g[node[a[0]]]); 74 | g.back().push_back(a[2]); 75 | node[a[0]] = g.size() - 1; 76 | for (int i = h[a[0]]; ~i; i = ne[i]) 77 | if (e[i] != a[0]) 78 | heap.push({a[1] + w, e[i], a[0], node[a[0]]}); 79 | } 80 | else 81 | { 82 | while (heap.size() && heap.top().t <= a[1]) eval(); 83 | printf("%d ", g[node[a[0]]].size()); 84 | for (auto x: g[node[a[0]]]) 85 | printf("%d ", x); 86 | puts(""); 87 | } 88 | } 89 | return 0; 90 | } -------------------------------------------------------------------------------- /201912/5.cpp: -------------------------------------------------------------------------------- 1 | // 线段树 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | typedef __int128 I128; 11 | typedef unsigned long long ULL; 12 | const int N = 1000010, M = 33; 13 | const ULL MOD = 2009731336725594113ull; 14 | 15 | int n, m; 16 | unordered_map v2id; 17 | ULL id2v[M], prod[N][M]; 18 | int g[M][M], id; 19 | ULL U[] = { 20 | 314882150829468584ull, 21 | 427197303358170108ull, 22 | 1022292690726729920ull, 23 | 1698479428772363217ull, 24 | 2006101093849356424ull, 25 | }; 26 | 27 | struct Node 28 | { 29 | int l, r, k; 30 | int s[M]; 31 | }tr[N << 2]; 32 | 33 | void init() 34 | { 35 | queue q; 36 | q.push(1); 37 | v2id[1] = ++ id; 38 | id2v[id] = 1; 39 | while (q.size()) 40 | { 41 | auto t = q.front(); 42 | q.pop(); 43 | for (int i = 0; i < 5; i ++ ) 44 | { 45 | auto r = (I128)t * U[i] % MOD; 46 | if (!v2id.count(r)) 47 | { 48 | v2id[r] = ++ id; 49 | id2v[id] = r; 50 | q.push(r); 51 | } 52 | } 53 | } 54 | for (int i = 1; i <= id; i ++ ) 55 | for (int j = 1; j <= id; j ++ ) 56 | g[i][j] = v2id[(I128)id2v[i] * id2v[j] % MOD]; 57 | for (int i = 1; i <= n; i ++ ) 58 | for (int j = 1; j <= id; j ++ ) 59 | prod[i][j] = (prod[i - 1][j] + id2v[j]) % MOD; 60 | } 61 | 62 | void pushup(int u) 63 | { 64 | auto l = tr[u << 1].s, r = tr[u << 1 | 1].s; 65 | for (int i = 1; i <= id; i ++ ) 66 | tr[u].s[i] = l[i] + r[i]; 67 | } 68 | 69 | void eval(int u, int k) 70 | { 71 | static int tmp[M]; 72 | for (int i = 1; i <= id; i ++ ) tmp[i] = tr[u].s[g[i][k]]; 73 | for (int i = 1; i <= id; i ++ ) tr[u].s[i] = tmp[i]; 74 | if (tr[u].k) tr[u].k = g[tr[u].k][k]; 75 | else tr[u].k = k; 76 | } 77 | 78 | void pushdown(int u) 79 | { 80 | int k = tr[u].k; 81 | if (k) 82 | { 83 | eval(u << 1, k), eval(u << 1 | 1, k); 84 | tr[u].k = 0; 85 | } 86 | } 87 | 88 | void build(int u, int l, int r) 89 | { 90 | tr[u] = {l, r}; 91 | if (l == r) 92 | { 93 | for (int i = 1; i <= id; i ++ ) 94 | tr[u].s[i] = prod[l][i] % 2019; 95 | return; 96 | } 97 | int mid = l + r >> 1; 98 | build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r); 99 | pushup(u); 100 | } 101 | 102 | void update(int u, int l, int r, int k) 103 | { 104 | if (tr[u].l >= l && tr[u].r <= r) eval(u, k); 105 | else 106 | { 107 | pushdown(u); 108 | int mid = tr[u].l + tr[u].r >> 1; 109 | if (l <= mid) update(u << 1, l, r, k); 110 | if (r > mid) update(u << 1 | 1, l, r, k); 111 | pushup(u); 112 | } 113 | } 114 | 115 | int query(int u, int l, int r) 116 | { 117 | if (tr[u].l >= l && tr[u].r <= r) return tr[u].s[1]; 118 | pushdown(u); 119 | int mid = tr[u].l + tr[u].r >> 1, res = 0; 120 | if (l <= mid) res = query(u << 1, l, r); 121 | if (r > mid) res += query(u << 1 | 1, l, r); 122 | return res; 123 | } 124 | 125 | int main() 126 | { 127 | scanf("%d%d", &n, &m); 128 | init(); 129 | build(1, 1, n); 130 | 131 | while (m -- ) 132 | { 133 | int l, r; 134 | scanf("%d%d", &l, &r); 135 | int t = query(1, l, r); 136 | printf("%d\n", t); 137 | update(1, l, r, t % 5 + 2); 138 | } 139 | 140 | return 0; 141 | } -------------------------------------------------------------------------------- /202006/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 1010; 8 | int n, m; 9 | struct Point{ 10 | int x, y, type; 11 | }p[N]; 12 | int a, b, c; 13 | 14 | long long cacl(int x, int y) { 15 | return (long long)(a) * x + (long long)(b) * y + c; 16 | } 17 | 18 | int main() { 19 | cin >> n >> m; 20 | 21 | for (int i = 1; i <= n; i++ ) { 22 | int x, y; 23 | char type; 24 | cin >> x >> y >> type; 25 | if (type == 'A') p[i] = {x, y, 0}; 26 | else p[i] = {x, y, 1}; 27 | } 28 | 29 | for (int i = 1; i <= m; i++) { 30 | cin >> c >> a >> b; 31 | 32 | bool flag = true; 33 | for (int j = 1; j <= n; j++) { 34 | if (cacl(p[j].x, p[j].y) > 0 && p[j].type == 0) continue; 35 | if (cacl(p[j].x, p[j].y) < 0 && p[j].type == 1) continue; 36 | flag = false; 37 | break; 38 | } 39 | 40 | if (flag) { 41 | puts("Yes"); 42 | continue; 43 | } 44 | 45 | for (int j = 1; j <= n; j++) { 46 | if (cacl(p[j].x, p[j].y) < 0 && p[j].type == 0) { 47 | flag = true; 48 | continue; 49 | } 50 | if (cacl(p[j].x, p[j].y) > 0 && p[j].type == 1) { 51 | flag = true; 52 | continue; 53 | } 54 | flag = false; 55 | break; 56 | } 57 | 58 | if (flag) puts("Yes"); 59 | else puts("No"); 60 | } 61 | 62 | return 0; 63 | } -------------------------------------------------------------------------------- /202006/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define x first 6 | #define y second 7 | 8 | using namespace std; 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int N = 500010; 14 | int n, a, b; 15 | pii u[N], v[N]; 16 | 17 | int main() { 18 | cin >> n >> a >> b; 19 | 20 | for (int i = 1; i <= a; i++) { 21 | int idx; 22 | ll val; 23 | cin >> idx >> val; 24 | u[i] = {idx, val}; 25 | } 26 | sort(u + 1, u + a + 1); 27 | 28 | for (int i = 1; i <= b; i++) { 29 | int idx; 30 | ll val; 31 | cin >> idx >> val; 32 | v[i] = {idx, val}; 33 | } 34 | sort(v + 1, v + b + 1); 35 | 36 | ll sum = 0; 37 | int i = 1, j = 1; 38 | while(i <= a && j <= b) { 39 | if (u[i].x < v[j].x) { 40 | i ++; 41 | } 42 | else if (u[i].x == v[j].x) { 43 | sum += u[i].y * v[j].y; 44 | i ++; 45 | j ++; 46 | } 47 | else { 48 | j ++; 49 | } 50 | } 51 | 52 | cout << sum << endl; 53 | } -------------------------------------------------------------------------------- /202006/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | const int N = 20 * 1024 * 1204 + 10; 9 | 10 | char str[N]; 11 | vector strs; 12 | 13 | bool check_blank(string& s) 14 | { 15 | for (auto c: s) 16 | if (c != ' ') return false; 17 | return true; 18 | } 19 | 20 | int get(string& s) 21 | { 22 | if (check_blank(s)) return 0; // 空行 23 | if (s.size() >= 2 && s[0] == '*' && s[1] == ' ') return 1; // 项目 24 | if (s.size() >= 2 && s[0] == ' ' && s[1] == ' ') return 2; // 项目的剩余行 25 | return 3; // 其他 26 | } 27 | 28 | string trim(string s) 29 | { 30 | int i = 0, j = s.size() - 1; 31 | while (i <= j && s[i] == ' ') i ++ ; 32 | while (i <= j && s[j] == ' ') j -- ; 33 | if (i > j) return ""; 34 | return s.substr(i, j - i + 1); 35 | } 36 | 37 | int wc(string& s, int w) 38 | { 39 | int res = 0; 40 | for (int i = 0; i < s.size(); i ++ ) 41 | { 42 | if (s[i] == ' ') continue; 43 | int j = i + 1; 44 | while (j < s.size() && j - i + 1 <= w) j ++ ; 45 | res ++ ; 46 | i = j - 1; 47 | } 48 | return max(res, 1); 49 | } 50 | 51 | int main() 52 | { 53 | int w; 54 | scanf("%d", &w); 55 | getchar(); 56 | while (fgets(str, N, stdin)) 57 | { 58 | strs.push_back(str); 59 | strs.back().pop_back(); // 删除回车 60 | } 61 | 62 | int r = -1, last = 0; // 0表示段落,1表示项目,2表示空行 63 | for (int i = 0; i < strs.size();) 64 | { 65 | int t = get(strs[i]); 66 | if (t == 0) i ++, last = 2; 67 | else if (t == 1) 68 | { 69 | if (last != 1) r ++ ; 70 | last = 1; 71 | string line = trim(strs[i].substr(2)); 72 | i ++ ; 73 | while (i < strs.size()) 74 | { 75 | if (get(strs[i]) == 2) 76 | { 77 | line += ' ' + trim(strs[i].substr(2)); 78 | i ++ ; 79 | } 80 | else break; 81 | } 82 | r += wc(line, w - 3); 83 | } 84 | else 85 | { 86 | r ++ ; 87 | last = 0; 88 | string line = trim(strs[i]); 89 | i ++ ; 90 | while (i < strs.size()) 91 | { 92 | int t = get(strs[i]); 93 | if (t == 2 || t == 3) 94 | { 95 | line += ' ' + trim(strs[i]); 96 | i ++ ; 97 | } 98 | else break; 99 | } 100 | r += wc(line, w); 101 | } 102 | } 103 | printf("%d\n", r); 104 | return 0; 105 | } -------------------------------------------------------------------------------- /202006/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef long long LL; 9 | const int N = 14, MOD = 998244353; 10 | 11 | int n; 12 | string S; 13 | int id[100]; 14 | vector vers{ 15 | 1, 2, 4, 6, 16, 26, 41, 42, 44, 16 | 46, 61, 62, 64, 66 17 | }; 18 | vector> g{ 19 | {2}, {4}, {1, 6, 16}, {6, 4, 64}, {26}, 20 | {46}, {62}, {64}, {61}, {66}, {42}, 21 | {44}, {41}, {46} 22 | }; 23 | int tr[N][N]; 24 | 25 | void init() 26 | { 27 | memset(id, -1, sizeof id); 28 | for (int i = 0; i < N; i ++ ) id[vers[i]] = i; 29 | for (int i = 0; i < N; i ++ ) 30 | for (auto x: g[i]) 31 | tr[i][id[x]] ++ ; 32 | } 33 | 34 | void mul(int c[][N], int a[][N], int b[][N]) 35 | { 36 | static int tmp[N][N]; 37 | memset(tmp, 0, sizeof tmp); 38 | for (int i = 0; i < N; i ++ ) 39 | for (int j = 0; j < N; j ++ ) 40 | for (int k = 0; k < N; k ++ ) 41 | tmp[i][j] = (tmp[i][j] + (LL)a[i][k] * b[k][j]) % MOD; 42 | memcpy(c, tmp, sizeof tmp); 43 | } 44 | 45 | int qmi(int k, int id) 46 | { 47 | if (id == -1) return 0; 48 | int res[N][N] = {0}, w[N][N]; 49 | memcpy(w, tr, sizeof w); 50 | res[0][0] = 1; 51 | 52 | while (k) 53 | { 54 | if (k & 1) mul(res, res, w); 55 | mul(w, w, w); 56 | k >>= 1; 57 | } 58 | return res[0][id]; 59 | } 60 | 61 | string get(string str) 62 | { 63 | string res; 64 | for (int i = 0; i < str.size(); i ++ ) 65 | if (str[i] == '2') res += '1'; 66 | else if (str[i] == '4') res += '2'; 67 | else if (str[i] == '1') 68 | { 69 | if (i + 1 == str.size() || str[i + 1] == '6') res += '4', i ++ ; 70 | else return ""; 71 | } 72 | else 73 | { 74 | if (i + 1 == str.size() || str[i + 1] == '4') res += '6', i ++ ; 75 | else return ""; 76 | } 77 | return res; 78 | } 79 | 80 | int dfs(int k, string& str) 81 | { 82 | if (str.size() <= 2) return qmi(k, id[stoi(str)]); 83 | int res = 0; 84 | for (string s: {"", "1", "6"}) 85 | { 86 | auto t = get(s + str); 87 | if (t.size()) res = (res + dfs(k - 1, t)) % MOD; 88 | } 89 | return res; 90 | } 91 | 92 | int main() 93 | { 94 | init(); 95 | cin >> n >> S; 96 | cout << dfs(n, S) << endl; 97 | return 0; 98 | } -------------------------------------------------------------------------------- /202006/5.cpp: -------------------------------------------------------------------------------- 1 | // 网络流 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef long long LL; 9 | const int N = 20010, M = (10000 * 3 + 100000) * 2 + 10, INF = 1e9; 10 | 11 | int n, m, S, T; 12 | int h[N], e[M], ne[M], idx; 13 | LL f[M]; 14 | int q[N], d[N], cur[N]; 15 | 16 | void add(int a, int b, int c) 17 | { 18 | e[idx] = b, f[idx] = c, ne[idx] = h[a], h[a] = idx ++ ; 19 | e[idx] = a, f[idx] = 0, ne[idx] = h[b], h[b] = idx ++ ; 20 | } 21 | 22 | bool bfs() 23 | { 24 | int hh = 0, tt = 0; 25 | memset(d, -1, sizeof d); 26 | q[0] = S, d[S] = 0, cur[S] = h[S]; 27 | while (hh <= tt) 28 | { 29 | int t = q[hh ++ ]; 30 | for (int i = h[t]; ~i; i = ne[i]) 31 | { 32 | int ver = e[i]; 33 | if (d[ver] == -1 && f[i]) 34 | { 35 | d[ver] = d[t] + 1; 36 | cur[ver] = h[ver]; 37 | if (ver == T) return true; 38 | q[ ++ tt] = ver; 39 | } 40 | } 41 | } 42 | return false; 43 | } 44 | 45 | LL find(int u, LL limit) 46 | { 47 | if (u == T) return limit; 48 | LL flow = 0; 49 | for (int i = cur[u]; ~i && flow < limit; i = ne[i]) 50 | { 51 | cur[u] = i; 52 | int ver = e[i]; 53 | if (d[ver] == d[u] + 1 && f[i]) 54 | { 55 | LL t = find(ver, min(f[i], limit - flow)); 56 | if (!t) d[ver] = -1; 57 | f[i] -= t, f[i ^ 1] += t, flow += t; 58 | } 59 | } 60 | return flow; 61 | } 62 | 63 | LL dinic() 64 | { 65 | LL r = 0, flow; 66 | while (bfs()) while (flow = find(S, 1e18)) r += flow; 67 | return r; 68 | } 69 | 70 | int calc_f(int x, int a, int b, int c) 71 | { 72 | return a * x * x + b * x + c; 73 | } 74 | 75 | int calc(int x1, int x2, int a, int b, int c) 76 | { 77 | int x[5] = {x1, x2}; 78 | if (a) 79 | { 80 | x[2] = -b / (a * 2); 81 | x[3] = x[2] - 1, x[4] = x[2] + 1; 82 | } 83 | int res = -INF; 84 | for (auto v: x) 85 | if (v >= x1 && v <= x2) 86 | res = max(res, calc_f(v, a, b, c)); 87 | return res; 88 | } 89 | 90 | int main() 91 | { 92 | scanf("%d%d", &n, &m); 93 | S = 0, T = n * 2 + 1; 94 | memset(h, -1, sizeof h); 95 | 96 | LL tot = 0; 97 | for (int i = 1; i <= n; i ++ ) 98 | { 99 | int l, r, a, b, c; 100 | scanf("%d%d%d%d%d", &l, &r, &a, &b, &c); 101 | int w1 = calc(l + 1, r - 1, a, b, c); 102 | int w2 = calc(l, r, a, b, c) - w1; 103 | if (w1 > 0) add(S, i, w1), tot += w1; 104 | else add(i, T, -w1); 105 | if (w2 > 0) add(S, n + i, w2), tot += w2; 106 | else add(n + i, T, -w2); 107 | add(n + i, i, INF); 108 | } 109 | while (m -- ) 110 | { 111 | int c, a, b; 112 | scanf("%d%d%d", &c, &a, &b); 113 | if (c == 1) add(b, a, INF); 114 | else add(n + b, a, INF); 115 | } 116 | printf("%lld\n", tot - dinic()); 117 | return 0; 118 | } -------------------------------------------------------------------------------- /202009/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define x first 6 | #define y second 7 | 8 | using namespace std; 9 | 10 | typedef pair pii; 11 | 12 | int n, x, y; 13 | pii p[1010]; 14 | 15 | int calc(int x, int y, int a, int b) { 16 | return ((x - a)*(x - a) + (y - b)*(y - b)); 17 | } 18 | 19 | int main() { 20 | cin >> n >> x >> y; 21 | 22 | for (int i = 1; i <= n; i++) { 23 | int a, b; 24 | cin >> a >> b; 25 | p[i] = {a, b}; 26 | } 27 | 28 | vector dist(n + 1, 0); 29 | for (int i = 1; i <= n; i++) dist[i] = i; 30 | 31 | sort(dist.begin() + 1, dist.end(), [&](int u, int v) { 32 | if (calc(p[u].x, p[u].y, x, y) < calc(p[v].x, p[v].y, x, y)) return true; 33 | else if (calc(p[u].x, p[u].y, x, y) > calc(p[v].x, p[v].y, x, y)) return false; 34 | else return u < v; 35 | }); 36 | 37 | for (int i = 1; i <= 3; i++) cout << dist[i] << endl; 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /202009/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int n, k, t, xl, xr, yd, yu; 8 | 9 | bool check(int x, int y) { 10 | return (x >= xl && x <= xr && y <= yu && y >= yd); 11 | } 12 | 13 | int main() { 14 | cin >> n >> k >> t >> xl >> yd >> xr >> yu; 15 | 16 | int appear = 0, stay = 0; 17 | for (int i = 1; i <= n; i++ ) { 18 | int cnt = 0; 19 | int stay_cnt = 0; 20 | bool flag = false; 21 | for (int j = 1; j <= t; j++) { 22 | int x, y; 23 | cin >> x >> y; 24 | 25 | if (check(x, y)) { 26 | cnt ++; 27 | stay_cnt ++; 28 | if (stay_cnt >= k) flag = true; 29 | } 30 | else stay_cnt = 0; 31 | } 32 | 33 | if (cnt > 0) appear ++; 34 | if (flag) stay ++; 35 | } 36 | 37 | cout << appear << endl; 38 | cout << stay << endl; 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /202009/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | const int N = 3010, M = N * 5; 9 | 10 | int m, n; 11 | int w[N], f[N]; 12 | int h[N], e[M], ne[M], idx; 13 | int q[N], d[N]; 14 | vector in[M], out[M]; 15 | 16 | int get(char* str) { 17 | string names[] = { 18 | "AND", "OR", "NOT", "XOR", "NAND", "NOR" 19 | }; 20 | for (int i = 0; i < 6; i ++ ) 21 | if (names[i] == str) 22 | return i; 23 | return -1; 24 | } 25 | 26 | void add(int a, int b) { 27 | e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ; 28 | d[b] ++ ; 29 | } 30 | 31 | bool topsort() { 32 | int hh = 0, tt = -1; 33 | for (int i = 1; i <= m + n; i ++ ) 34 | if (!d[i]) 35 | q[ ++ tt] = i; 36 | while (hh <= tt) { 37 | int t = q[hh ++ ]; 38 | for (int i = h[t]; ~i; i = ne[i]) { 39 | int j = e[i]; 40 | if ( -- d[j] == 0) 41 | q[ ++ tt] = j; 42 | } 43 | } 44 | return tt == m + n - 1; 45 | } 46 | 47 | int main() { 48 | int T; 49 | scanf("%d", &T); 50 | while (T -- ) { 51 | scanf("%d%d", &m, &n); 52 | memset(h, -1, sizeof h); 53 | idx = 0; 54 | memset(d, 0, sizeof d); 55 | char str[100]; 56 | for (int i = 1; i <= n; i ++ ) { 57 | int cnt; 58 | scanf("%s%d", str, &cnt); 59 | f[m + i] = get(str); 60 | while (cnt -- ) { 61 | scanf("%s", str); 62 | int t = atoi(str + 1); 63 | if (str[0] == 'I') add(t, m + i); 64 | else add(m + t, m + i); 65 | } 66 | } 67 | int Q; 68 | scanf("%d", &Q); 69 | for (int i = 0; i < Q; i ++ ) { 70 | in[i].clear(); 71 | for (int j = 0; j < m; j ++ ) { 72 | int x; 73 | scanf("%d", &x); 74 | in[i].push_back(x); 75 | } 76 | } 77 | for (int i = 0; i < Q; i ++ ) { 78 | out[i].clear(); 79 | int cnt; 80 | scanf("%d", &cnt); 81 | while (cnt -- ) { 82 | int x; 83 | scanf("%d", &x); 84 | out[i].push_back(x); 85 | } 86 | } 87 | if (!topsort()) { 88 | puts("LOOP"); 89 | continue; 90 | } 91 | 92 | for (int i = 0; i < Q; i ++ ) { 93 | for (int j = 0; j < m; j ++ ) w[j + 1] = in[i][j]; 94 | for (int j = m + 1; j <= m + n; j ++ ) 95 | if (f[j] == 0 || f[j] == 5) w[j] = 1; 96 | else w[j] = 0; 97 | for (int j = 0; j < m + n; j ++ ) { 98 | int t = q[j], v = w[t]; 99 | for (int k = h[t]; ~k; k = ne[k]) { 100 | int u = e[k]; 101 | if (f[u] == 0) w[u] &= v; 102 | else if (f[u] == 1) w[u] |= v; 103 | else if (f[u] == 2) w[u] = !v; 104 | else if (f[u] == 3) w[u] ^= v; 105 | else if (f[u] == 4) w[u] |= !v; 106 | else w[u] &= !v; 107 | } 108 | } 109 | for (auto x: out[i]) 110 | printf("%d ", w[m + x]); 111 | puts(""); 112 | } 113 | } 114 | return 0; 115 | } -------------------------------------------------------------------------------- /202009/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | const int N = 110, M = 2010; 9 | 10 | int n, m; 11 | double R; 12 | double o[N], p[M][N]; 13 | double d[M], rd[M]; 14 | double ans[M]; 15 | 16 | inline double sqr(double x) 17 | { 18 | return x * x; 19 | } 20 | 21 | int main() 22 | { 23 | scanf("%d%d%lf", &n, &m, &R); 24 | for (int i = 0; i < n; i ++ ) scanf("%lf", &o[i]); 25 | for (int i = 0; i < m; i ++ ) 26 | { 27 | double s = 0; 28 | for (int j = 0; j < n; j ++ ) 29 | { 30 | scanf("%lf", &p[i][j]); 31 | s += sqr(p[i][j] - o[j]); 32 | } 33 | d[i] = sqrt(s); 34 | rd[i] = sqrt(s - sqr(R)); 35 | } 36 | 37 | for (int i = 0; i < m; i ++ ) 38 | for (int j = 0; j < i; j ++ ) 39 | { 40 | double s = 0; 41 | for (int k = 0; k < n; k ++ ) s += sqr(p[i][k] - p[j][k]); 42 | double c = sqrt(s), a = d[i], b = d[j]; 43 | double p = (a + b + c) / 2; 44 | double area = sqrt(p * (p - a) * (p - b) * (p - c)); 45 | double h = area * 2 / c; 46 | if (h >= R || sqr(b) >= sqr(a) + s || sqr(a) >= sqr(b) + s) 47 | { 48 | ans[i] += c, ans[j] += c; 49 | continue; 50 | } 51 | double angle1 = acos((sqr(a) + sqr(b) - s) / (2 * a * b)); 52 | double angle2 = acos(R / a); 53 | double angle3 = acos(R / b); 54 | double t = (angle1 - angle2 - angle3) * R + rd[i] + rd[j]; 55 | ans[i] += t, ans[j] += t; 56 | } 57 | for (int i = 0; i < m; i ++ ) 58 | printf("%.12lf\n", ans[i]); 59 | return 0; 60 | } -------------------------------------------------------------------------------- /202009/5.cpp: -------------------------------------------------------------------------------- 1 | // AC 自动机 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | const int N = 55, M = 1010, MOD = 998244353; 10 | 11 | int n, m; 12 | int tr[N][26], cnt[N], idx; 13 | int q[N], ne[N]; 14 | struct Node 15 | { 16 | int row, next; 17 | }g[26][N]; 18 | int f[M][N][N]; 19 | vector strs; 20 | 21 | void insert(string& str) 22 | { 23 | int p = 0; 24 | for (char c: str) 25 | { 26 | int u = c - 'a'; 27 | if (!tr[p][u]) tr[p][u] = ++ idx; 28 | p = tr[p][u]; 29 | } 30 | cnt[p] ++ ; 31 | } 32 | 33 | void build() 34 | { 35 | int hh = 0, tt = -1; 36 | for (int i = 0; i < 26; i ++ ) 37 | if (tr[0][i]) 38 | q[ ++ tt] = tr[0][i]; 39 | while (hh <= tt) 40 | { 41 | int t = q[hh ++ ]; 42 | for (int i = 0; i < 26; i ++ ) 43 | { 44 | int p = tr[t][i]; 45 | if (!p) tr[t][i] = tr[ne[t]][i]; 46 | else 47 | { 48 | ne[p] = tr[ne[t]][i]; 49 | cnt[p] += cnt[ne[p]]; 50 | q[ ++ tt] = p; 51 | } 52 | } 53 | } 54 | } 55 | 56 | int main() 57 | { 58 | cin >> n >> m; 59 | for (int i = 0; i < 26; i ++ ) 60 | for (int j = 1; j <= n; j ++ ) 61 | { 62 | string str; 63 | cin >> str; 64 | g[str[0] - 'a'][j] = {i, stoi(str.substr(1))}; 65 | } 66 | string str; 67 | while (cin >> str) 68 | { 69 | insert(str); 70 | strs.push_back(str); 71 | } 72 | 73 | build(); 74 | 75 | f[0][0][1] = 1; 76 | for (int i = 0; i <= m; i ++ ) 77 | { 78 | int sum = 0; 79 | for (int j = 0; j <= idx; j ++ ) 80 | for (int k = 1; k <= n; k ++ ) 81 | { 82 | if (!f[i][j][k]) continue; 83 | sum = (sum + f[i][j][k]) % MOD; 84 | for (auto& s: strs) 85 | { 86 | if (i + s.size() > m) continue; 87 | bool flag = true; 88 | int x = j, y = k; 89 | for (auto c: s) 90 | { 91 | int u = c - 'a'; 92 | auto& t = g[u][y]; 93 | x = tr[x][t.row]; 94 | if (cnt[x]) 95 | { 96 | flag = false; 97 | break; 98 | } 99 | y = t.next; 100 | } 101 | if (flag) 102 | { 103 | auto& v = f[i + s.size()][x][y]; 104 | v = (v + f[i][j][k]) % MOD; 105 | } 106 | } 107 | } 108 | if (i) printf("%d\n", sum); 109 | } 110 | return 0; 111 | } -------------------------------------------------------------------------------- /202012/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int n; 8 | 9 | int main() { 10 | cin >> n; 11 | 12 | int sum = 0; 13 | 14 | for (int i = 1; i <= n; i++) { 15 | int w, score; 16 | cin >> w >> score; 17 | 18 | sum += w * score; 19 | } 20 | 21 | if (sum > 0) cout << sum << endl; 22 | else cout << "0" << endl; 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /202012/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define x first 7 | #define y second 8 | 9 | using namespace std; 10 | 11 | typedef pair pii; 12 | int m; 13 | pii p[100010]; 14 | int les[100010]; 15 | int more[100010]; 16 | map mp; 17 | map info; 18 | int thres[100010]; 19 | 20 | int main() { 21 | cin >> m; 22 | 23 | for (int i = 1; i <= m; i++) { 24 | int thre, result; 25 | cin >> thre >> result; 26 | p[i] = {thre, result}; 27 | mp[thre] ++; 28 | if (result == 0) info[thre].x ++; 29 | else info[thre].y ++; 30 | } 31 | 32 | sort(p + 1, p + m + 1); 33 | 34 | int n = 1; 35 | for (int i = 1; i <= m; i++) { 36 | if (i == 1) thres[n ++] = p[i].x; 37 | else { 38 | if (p[i].x == p[i - 1].x) continue; 39 | else thres[n ++] = p[i].x; 40 | } 41 | } 42 | 43 | n --; 44 | int cnt = 0; 45 | for (int i = 2; i <= n; i++) { 46 | cnt += info[thres[i - 1]].x; 47 | les[i] = cnt; 48 | } 49 | 50 | cnt = 0; 51 | for (int i = n; i >= 1; i--) { 52 | cnt += info[thres[i]].y; 53 | more[i] = cnt; 54 | } 55 | 56 | int sum = 0, ans; 57 | for (int i = 1; i <= n; i++) { 58 | int cur = les[i] + more[i]; 59 | // cout << i << " : " << les[i] << " " << more[i] << endl; 60 | if (cur >= sum) { 61 | sum = cur; 62 | ans = thres[i]; 63 | } 64 | } 65 | 66 | cout << ans << endl; 67 | return 0; 68 | } -------------------------------------------------------------------------------- /202012/3.cpp: -------------------------------------------------------------------------------- 1 | // cv 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | typedef long long LL; 11 | const int N = 2000010; 12 | 13 | int n; 14 | struct Node { 15 | string name; 16 | int id, type; // type == 0: 文件夹;type == 1:文件 17 | mutable LL ld, lr; 18 | mutable LL sd, sr; 19 | bool operator< (const Node& t) const { 20 | return name < t.name; 21 | } 22 | }; 23 | set son[N]; 24 | int idx; 25 | char str[N]; 26 | bool F; 27 | int U; 28 | LL SZ; 29 | 30 | vector get(char* str) { 31 | vector res(1, "root"); 32 | for (int i = 0; str[i]; i ++ ) { 33 | if (str[i] == '/') continue; 34 | string s; 35 | int j = i; 36 | while (str[j] && str[j] != '/') s += str[j ++ ]; 37 | res.push_back(s); 38 | i = j - 1; 39 | } 40 | 41 | return res; 42 | } 43 | 44 | LL dfs_remove(vector& t, int u, int p) { 45 | string name = t[u]; 46 | if (!son[p].count({name})) return -1; 47 | auto it = son[p].find({name}); 48 | if (u == t.size() - 1) { 49 | if (it->type) t[u] = "#file"; 50 | auto sz = it->sr; 51 | son[p].erase(it); 52 | return sz; 53 | } 54 | if (it->type) return -1; 55 | auto sz = dfs_remove(t, u + 1, it->id); 56 | if (sz >= 0) { 57 | it->sr -= sz; 58 | if (t[u + 1] == "#file") it->sd -= sz; 59 | } 60 | return sz; 61 | } 62 | 63 | bool dfs_create(vector& t, int u, int p, LL sz) { 64 | string name = t[u]; 65 | if (u == t.size() - 1) { 66 | if (son[p].count({name})) { 67 | auto it = son[p].find({name}); 68 | if (it->type == 0) return false; 69 | SZ = dfs_remove(t, 0, 0); 70 | Node cur{name, ++ idx, 1, 0, 0, 0, sz}; 71 | son[p].insert(cur); 72 | return true; 73 | } 74 | else { 75 | Node cur{name, ++ idx, 1, 0, 0, 0, sz}; 76 | son[p].insert(cur); 77 | return true; 78 | } 79 | } 80 | else { 81 | if (!son[p].count({name})) { 82 | if (U == -1) U = u; 83 | Node cur{name, ++ idx, 0, 0, 0, 0, 0}; 84 | son[p].insert(cur); 85 | } 86 | 87 | auto it = son[p].find({name}); 88 | if (it->type) return false; 89 | auto res = dfs_create(t, u + 1, it->id, sz); 90 | if (res) { 91 | it->sr += sz; 92 | if (u + 1 == t.size() - 1) it->sd += sz; 93 | 94 | if (it->lr && it->sr > it->lr) F = false; 95 | if (it->ld && it->sd > it->ld) F = false; 96 | } 97 | return res; 98 | } 99 | } 100 | 101 | bool create(char* str, LL sz) { 102 | auto t = get(str); 103 | F = true, U = -1, SZ = -1; 104 | auto res = dfs_create(t, 0, 0, sz); 105 | 106 | auto ans = res && F; 107 | if (res && !F) { 108 | auto t = get(str); 109 | if (U != -1) { 110 | while (t.size() - 1 > U) t.pop_back(); 111 | } 112 | dfs_remove(t, 0, 0); 113 | if (SZ != -1) create(str, SZ); 114 | } 115 | return ans; 116 | } 117 | 118 | bool update(char* str, LL d, LL r) { 119 | auto t = get(str); 120 | int p = 0; 121 | for (int i = 0; i < t.size(); i ++ ) { 122 | string& s = t[i]; 123 | auto it = son[p].find({s}); 124 | if (it == son[p].end()) return false; 125 | if (it->type) return false; 126 | if (i == t.size() - 1) { 127 | if (d && d < it->sd) return false; 128 | if (r && r < it->sr) return false; 129 | it->ld = d, it->lr = r; 130 | } 131 | p = it->id; 132 | } 133 | return true; 134 | } 135 | 136 | int main() { 137 | scanf("%d", &n); 138 | char op[2]; 139 | sprintf(str, "/tmp"); 140 | create(str, 1); 141 | auto t = get(str); 142 | dfs_remove(t, 0, 0); 143 | while (n -- ) { 144 | scanf("%s", op); 145 | bool res; 146 | if (*op == 'C') { 147 | LL sz; 148 | scanf("%s%lld", str, &sz); 149 | res = create(str, sz); 150 | } 151 | else if (*op == 'R') { 152 | scanf("%s", str); 153 | auto t = get(str); 154 | dfs_remove(t, 0, 0); 155 | res = true; 156 | } 157 | else { 158 | LL d, r; 159 | scanf("%s%lld%lld", str, &d, &r); 160 | res = update(str, d, r); 161 | } 162 | if (res) puts("Y"); 163 | else puts("N"); 164 | } 165 | return 0; 166 | } -------------------------------------------------------------------------------- /202012/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define x first 6 | #define y second 7 | 8 | using namespace std; 9 | 10 | typedef pair PII; 11 | const int N = 110, M = 10, S = 1 << M; 12 | 13 | int n, m, k; 14 | int need[N][M]; 15 | int h[N], e[N * 2], w[N * 2], ne[N * 2], idx; 16 | int d[N][M]; 17 | int f[S], state[N]; 18 | 19 | void add(int a, int b, int c) { 20 | e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ; 21 | } 22 | 23 | PII dfs(int u, int fa, int v) { 24 | PII res(0, -1); 25 | if (need[u][v]) res.y = 0; 26 | for (int i = h[u]; ~i; i = ne[i]) { 27 | int j = e[i]; 28 | if (j == fa) continue; 29 | auto t = dfs(j, u, v); 30 | if (t.y != -1) { 31 | res.x += t.x + w[i] * 2; 32 | res.y = max(res.y, t.y + w[i]); 33 | } 34 | } 35 | return res; 36 | } 37 | 38 | bool check(int mid) { 39 | memset(state, 0, sizeof state); 40 | for (int i = 1; i <= n; i ++ ) 41 | for (int j = 0; j < k; j ++ ) 42 | if (d[i][j] <= mid) 43 | state[i] |= 1 << j; 44 | memset(f, 0x3f, sizeof f); 45 | f[0] = 0; 46 | for (int i = 0; i < 1 << k; i ++ ) 47 | for (int j = 1; j <= n; j ++ ) 48 | f[i | state[j]] = min(f[i | state[j]], f[i] + 1); 49 | return f[(1 << k) - 1] <= m; 50 | } 51 | 52 | int main() { 53 | cin >> n >> m >> k; 54 | for (int i = 1; i <= n; i ++ ) 55 | for (int j = 0; j < k; j ++ ) 56 | cin >> need[i][j]; 57 | memset(h, -1, sizeof h); 58 | for (int i = 0; i < n - 1; i ++ ) { 59 | int a, b, c; 60 | scanf("%d%d%d", &a, &b, &c); 61 | add(a, b, c), add(b, a, c); 62 | } 63 | 64 | for (int i = 1; i <= n; i ++ ) 65 | for (int j = 0; j < k; j ++ ) { 66 | auto t = dfs(i, -1, j); 67 | if (t.y != -1) d[i][j] = t.x - t.y; 68 | } 69 | 70 | int l = 0, r = 2e8; 71 | while (l < r) { 72 | int mid = l + r >> 1; 73 | if (check(mid)) r = mid; 74 | else l = mid + 1; 75 | } 76 | 77 | printf("%d\n", r); 78 | return 0; 79 | } -------------------------------------------------------------------------------- /202012/5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef long long LL; 9 | const int N = 80010, MOD = 1e9 + 7; 10 | 11 | int n, m; 12 | struct Q { 13 | int t, l, r, k, a, b, c; 14 | }q[N]; 15 | vector xs; 16 | struct Node { 17 | int l, r, k; 18 | int a[3], b[3], s[3]; 19 | 20 | int get_len() { 21 | return xs[r] - xs[l - 1]; 22 | } 23 | }tr[N * 4]; 24 | 25 | int get(int x) { 26 | return lower_bound(xs.begin(), xs.end(), x) - xs.begin(); 27 | } 28 | 29 | void pushup(int u) { 30 | auto l = tr[u << 1].s, r = tr[u << 1 | 1].s; 31 | for (int i = 0; i < 3; i ++ ) 32 | tr[u].s[i] = (l[i] + r[i]) % MOD; 33 | } 34 | 35 | void rotate(int a[]) { 36 | int t = a[0]; 37 | a[0] = a[1], a[1] = a[2], a[2] = t; 38 | } 39 | 40 | void eval(int u, int k) { 41 | k %= 3; 42 | for (int i = 0; i < k; i ++ ) { 43 | rotate(tr[u].a), rotate(tr[u].b), rotate(tr[u].s); 44 | } 45 | tr[u].k += k; 46 | } 47 | 48 | void eval(int u, int a[], int b[]) { 49 | for (int i = 0; i < 3; i ++ ) { 50 | tr[u].s[i] = ((LL)tr[u].s[i] * a[i] + (LL)tr[u].get_len() * b[i]) % MOD; 51 | int c = (LL)tr[u].a[i] * a[i] % MOD; 52 | int d = ((LL)tr[u].b[i] * a[i] + b[i]) % MOD; 53 | tr[u].a[i] = c, tr[u].b[i] = d; 54 | } 55 | } 56 | 57 | void pushdown(int u) { 58 | eval(u << 1, tr[u].k), eval(u << 1 | 1, tr[u].k); 59 | tr[u].k = 0; 60 | 61 | eval(u << 1, tr[u].a, tr[u].b), eval(u << 1 | 1, tr[u].a, tr[u].b); 62 | for (int i = 0; i < 3; i ++ ) 63 | tr[u].a[i] = 1, tr[u].b[i] = 0; 64 | } 65 | 66 | void build(int u, int l, int r) { 67 | tr[u] = {l, r}; 68 | for (int i = 0; i < 3; i ++ ) tr[i].a[i] = 1; 69 | if (l == r) return; 70 | int mid = l + r >> 1; 71 | build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r); 72 | } 73 | 74 | void update(int u, int l, int r, int k, int a[], int b[]) { 75 | if (tr[u].l >= l && tr[u].r <= r) eval(u, k), eval(u, a, b); 76 | else { 77 | pushdown(u); 78 | int mid = tr[u].l + tr[u].r >> 1; 79 | if (l <= mid) update(u << 1, l, r, k, a, b); 80 | if (r > mid) update(u << 1 | 1, l, r, k, a, b); 81 | pushup(u); 82 | } 83 | } 84 | 85 | vector query(int u, int l, int r) { 86 | if (tr[u].l >= l && tr[u].r <= r) return {tr[u].s[0], tr[u].s[1], tr[u].s[2]}; 87 | pushdown(u); 88 | int mid = tr[u].l + tr[u].r >> 1; 89 | vector res(3); 90 | if (l <= mid) res = query(u << 1, l, r); 91 | if (r > mid) { 92 | auto t = query(u << 1 | 1, l, r); 93 | for (int i = 0; i < 3; i ++ ) 94 | res[i] = (res[i] + t[i]) % MOD; 95 | } 96 | return res; 97 | } 98 | 99 | int main() { 100 | scanf("%d%d", &n, &m); 101 | for (int i = 0; i < m; i ++ ) { 102 | q[i].k = 1; 103 | scanf("%d%d%d", &q[i].t, &q[i].l, &q[i].r); 104 | if (q[i].t == 1) scanf("%d%d%d", &q[i].a, &q[i].b, &q[i].c); 105 | else if (q[i].t == 2) scanf("%d", &q[i].k); 106 | xs.push_back(q[i].l - 1), xs.push_back(q[i].r); 107 | } 108 | sort(xs.begin(), xs.end()); 109 | xs.erase(unique(xs.begin(), xs.end()), xs.end()); 110 | 111 | build(1, 0, xs.size() - 1); 112 | for (int i = 0; i < m; i ++ ) { 113 | int t = q[i].t, l = get(q[i].l - 1) + 1, r = get(q[i].r); 114 | int a[] = {q[i].k, q[i].k, q[i].k}, b[] = {q[i].a, q[i].b, q[i].c}; 115 | if (t == 1 || t == 2) 116 | update(1, l, r, 0, a, b); 117 | else if (t == 3) 118 | update(1, l, r, 1, a, b); 119 | else { 120 | auto t = query(1, l, r); 121 | LL sum = 0; 122 | for (int i = 0; i < 3; i ++ ) 123 | sum += (LL)t[i] * t[i]; 124 | printf("%lld\n", sum % MOD); 125 | } 126 | } 127 | return 0; 128 | } -------------------------------------------------------------------------------- /202104/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 260; 8 | 9 | int n, m, L; 10 | int s[N]; 11 | 12 | int main() { 13 | scanf("%d%d%d", &n, &m, &L); 14 | for (int i = 0; i < n; i ++ ) 15 | for (int j = 0; j < m; j ++ ) { 16 | int x; 17 | scanf("%d", &x); 18 | s[x] ++ ; 19 | } 20 | 21 | for (int i = 0; i < L; i ++ ) 22 | printf("%d ", s[i]); 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /202104/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 610; 8 | 9 | int n, L, r, t; 10 | int s[N][N]; 11 | 12 | int get_sum(int x1, int y1, int x2, int y2) { 13 | return s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1]; 14 | } 15 | 16 | int get_cnt(int x1, int y1, int x2, int y2) { 17 | return (x2 - x1 + 1) * (y2 - y1 + 1); 18 | } 19 | 20 | int main() { 21 | scanf("%d%d%d%d", &n, &L, &r, &t); 22 | for (int i = 1; i <= n; i ++ ) 23 | for (int j = 1; j <= n; j ++ ) { 24 | int x; 25 | scanf("%d", &x); 26 | s[i][j] = x + s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1]; 27 | } 28 | 29 | int res = 0; 30 | for (int i = 1; i <= n; i ++ ) 31 | for (int j = 1; j <= n; j ++ ) { 32 | int x1 = max(1, i - r), y1 = max(1, j - r); 33 | int x2 = min(n, i + r), y2 = min(n, j + r); 34 | if (get_sum(x1, y1, x2, y2) <= t * get_cnt(x1, y1, x2, y2)) 35 | res ++ ; 36 | } 37 | 38 | printf("%d\n", res); 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /202104/3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qzylalala/CSP/7feb088c41eeb1bad0639c7316093a5279b34cba/202104/3.cpp -------------------------------------------------------------------------------- /202104/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef long long LL; 9 | 10 | const int N = 1010, M = 100010, MOD = 1e9 + 7; 11 | 12 | int n; 13 | int a[N]; 14 | int f[N]; 15 | vector q[M]; 16 | bool st[M]; 17 | 18 | int main() { 19 | for (int i = 1; i < M; i ++ ) { 20 | for (int j = i * 2; j < M; j += i) { 21 | q[j].push_back(i); 22 | } 23 | } 24 | 25 | scanf("%d", &n); 26 | for (int i = 0; i < n; i ++ ) scanf("%d", &a[i]); 27 | 28 | f[0] = 1; 29 | for (int i = 1; i < n; i ++ ) { 30 | memset(st, 0, sizeof st); 31 | for (int j = i - 1; j >= 0; j -- ) { 32 | int d = a[i] - a[j], cnt = 0; 33 | for (int k : q[d]) { 34 | if (!st[k]) { 35 | cnt ++ ; 36 | st[k] = true; 37 | } 38 | } 39 | st[d] = true; 40 | f[i] = (f[i] + (LL)f[j] * cnt) % MOD; 41 | } 42 | } 43 | 44 | printf("%d\n", f[n - 1]); 45 | return 0; 46 | } -------------------------------------------------------------------------------- /202104/5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define x first 7 | #define y second 8 | 9 | using namespace std; 10 | 11 | typedef long long LL; 12 | typedef pair PII; 13 | 14 | const int N = 510; 15 | const LL INF = 0x3f3f3f3f3f3f3f3fll; 16 | 17 | int n, m; 18 | int len[N]; 19 | struct Node { 20 | int cid, sum, pid; 21 | }; 22 | vector ps[N]; 23 | vector line[N]; // x表示节点编号;y表示到下一个点的距离 24 | LL dist[N], ans[N]; 25 | int pid[N]; 26 | bool st[N]; 27 | 28 | LL exgcd(LL a, LL b, LL &x, LL &y) { // 扩展欧几里得算法, 求x, y,使得ax + by = gcd(a, b) 29 | if (!b) { 30 | x = 1; y = 0; 31 | return a; 32 | } 33 | LL d = exgcd(b, a % b, y, x); 34 | y -= (a / b) * x; 35 | 36 | return d; 37 | } 38 | 39 | 40 | void dijkstra() { 41 | memset(dist, 0x3f, sizeof dist); 42 | for (int i = 0; i < m; i ++ ) { 43 | int d = 0; 44 | for (int j = 0; j < line[i].size(); j ++ ) { 45 | if (line[i][j].x == 1) { 46 | dist[i] = d; 47 | pid[i] = j; 48 | break; 49 | } 50 | 51 | d += line[i][j].y; 52 | } 53 | } 54 | 55 | for (int i = 0; i < m; i ++ ) { 56 | int t = -1; 57 | for (int j = 0; j < m; j ++ ) 58 | if (!st[j] && (t == -1 || dist[j] < dist[t])) 59 | t = j; 60 | st[t] = true; 61 | 62 | auto& l = line[t]; 63 | auto d = dist[t]; 64 | for (int j = pid[t], k = 0; k < l.size(); j = (j + 1) % l.size(), k ++ ) { 65 | for (auto& c: ps[l[j].x]) { 66 | if (st[c.cid]) continue; // 优化很重要 67 | LL a = d, b = len[t]; 68 | LL x = c.sum, y = len[c.cid]; 69 | LL X, Y; 70 | LL D = exgcd(b, y, X, Y); 71 | if ((x - a) % D) continue; 72 | X = (x - a) / D * X; 73 | y /= D; 74 | X = (X % y + y) % y; 75 | 76 | if (dist[c.cid] > a + b * X) { 77 | dist[c.cid] = a + b * X; 78 | pid[c.cid] = c.pid; 79 | } 80 | } 81 | 82 | d += l[j].y; 83 | } 84 | } 85 | } 86 | 87 | int main() { 88 | scanf("%d%d", &n, &m); 89 | for (int i = 0; i < m; i ++ ) { 90 | int cnt, sum = 0; 91 | scanf("%d", &cnt); 92 | for (int j = 0; j < cnt; j ++ ) { 93 | int ver, t; 94 | scanf("%d%d", &ver, &t); 95 | ps[ver].push_back({i, sum, j}); 96 | line[i].push_back({ver, t}); 97 | sum += t; 98 | } 99 | 100 | len[i] = sum; 101 | } 102 | 103 | dijkstra(); 104 | memset(ans, 0x3f, sizeof ans); 105 | for (int i = 0; i < m; i ++ ) { 106 | if (dist[i] == INF) continue; 107 | LL d = dist[i]; 108 | for (int j = pid[i], k = 0; k < line[i].size(); j = (j + 1) % line[i].size(), k ++ ) { 109 | int ver = line[i][j].x; 110 | ans[ver] = min(ans[ver], d); 111 | d += line[i][j].y; 112 | } 113 | } 114 | 115 | for (int i = 2; i <= n; i ++ ) { 116 | if (ans[i] == INF) puts("inf"); 117 | else printf("%lld\n", ans[i]); 118 | } 119 | 120 | return 0; 121 | } -------------------------------------------------------------------------------- /202109/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 110; 8 | 9 | int n; 10 | int b[N]; 11 | 12 | int main() { 13 | cin >> n; 14 | for (int i = 0; i < n; i ++ ) cin >> b[i]; 15 | 16 | int maxs = 0, mins = 0; 17 | for (int i = 0; i < n; i ++ ) { 18 | maxs += b[i]; 19 | if (!i || b[i] > b[i - 1]) 20 | mins += b[i]; 21 | } 22 | 23 | cout << maxs << endl; 24 | cout << mins << endl; 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /202109/2.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 2.cpp 3 | * @author qzylalala 4 | * @brief 岛屿问题 5 | * @version 0.1 6 | * @date 2022-01-10 7 | * 8 | * @copyright Copyright (c) 2022 9 | * 10 | */ 11 | #include 12 | #include 13 | #include 14 | 15 | using namespace std; 16 | 17 | const int N = 500010, M = 10010; 18 | 19 | int n; 20 | int a[N], cnt[M]; 21 | 22 | int main() { 23 | scanf("%d", &n); 24 | for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]); 25 | n = unique(a + 1, a + n + 1) - a - 1; 26 | a[0] = a[n + 1] = 0; 27 | 28 | for (int i = 1; i <= n; i ++ ) { 29 | int x = a[i - 1], y = a[i], z = a[i + 1]; 30 | if (x < y && z < y) cnt[y] ++ ; 31 | else if (x > y && z > y) cnt[y] -- ; 32 | } 33 | 34 | int res = 0, sum = 0; 35 | for (int i = M - 1; i; i -- ) { 36 | sum += cnt[i]; 37 | res = max(res, sum); 38 | } 39 | 40 | printf("%d\n", res); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /202109/3.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3.cpp 3 | * @author qzylalala 4 | * @brief 脉冲神经网络 SNN, 感兴趣可以看看这里 http://kzyjc.cnjournals.com/html/2021/1/20210101.htm 5 | * @version 0.1 6 | * @date 2022-01-10 7 | * 8 | * @copyright Copyright (c) 2022 9 | * 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | using namespace std; 17 | 18 | const int N = 2010; 19 | const double INF = 1e8; 20 | 21 | int n, s, p, T; 22 | double dt; 23 | int h[N], e[N], D[N], ne[N], idx; 24 | double W[N], v[N], u[N], a[N], b[N], c[N], d[N]; 25 | int r[N], cnt[N]; 26 | double I[1024][N / 2]; 27 | 28 | static unsigned long _next = 1; 29 | 30 | 31 | int myrand(void) { 32 | _next = _next * 1103515245 + 12345; 33 | return((unsigned)(_next/65536) % 32768); 34 | } 35 | 36 | void add(int a, int b, double c, int d) { 37 | e[idx] = b, W[idx] = c, D[idx] = d, ne[idx] = h[a], h[a] = idx ++ ; 38 | } 39 | 40 | int main() { 41 | memset(h, -1, sizeof h); 42 | scanf("%d%d%d%d", &n, &s, &p, &T); 43 | scanf("%lf", &dt); 44 | for (int i = 0; i < n;) { 45 | int rn; 46 | scanf("%d", &rn); 47 | double vv, uu, aa, bb, cc, dd; 48 | scanf("%lf%lf%lf%lf%lf%lf", &vv, &uu, &aa, &bb, &cc, &dd); 49 | for (int j = 0; j < rn; j ++, i ++ ) { 50 | v[i] = vv, u[i] = uu, a[i] = aa, b[i] = bb, c[i] = cc, d[i] = dd; 51 | } 52 | } 53 | 54 | for (int i = n; i < n + p; i ++ ) scanf("%d", &r[i]); 55 | 56 | int mod = 0; 57 | while (s -- ) { 58 | int a, b, d; 59 | double c; 60 | scanf("%d%d%lf%d", &a, &b, &c, &d); 61 | add(a, b, c, d); 62 | mod = max(mod, d + 1); 63 | } 64 | 65 | for (int i = 0; i < T; i ++ ) { 66 | int t = i % mod; 67 | for (int j = n; j < n + p; j ++ ) { 68 | if (r[j] > myrand()) { 69 | for (int k = h[j]; ~k; k = ne[k]) { 70 | int x = e[k]; 71 | I[(t + D[k]) % mod][x] += W[k]; 72 | } 73 | } 74 | } 75 | 76 | 77 | for (int j = 0; j < n; j ++ ) { 78 | double vv = v[j], uu = u[j]; 79 | v[j] = vv + dt * (0.04 * vv * vv + 5 * vv + 140 - uu) + I[t][j]; 80 | u[j] = uu + dt * a[j] * (b[j] * vv - uu); 81 | 82 | if (v[j] >= 30) { 83 | for (int k = h[j]; ~k; k = ne[k]) { 84 | int x = e[k]; 85 | I[(t + D[k]) % mod][x] += W[k]; 86 | } 87 | cnt[j] ++ ; 88 | v[j] = c[j], u[j] += d[j]; 89 | } 90 | } 91 | 92 | memset(I[t], 0, sizeof I[t]); 93 | } 94 | 95 | double minv = INF, maxv = -INF; 96 | int minc = INF, maxc = -INF; 97 | 98 | for (int i = 0; i < n; i ++ ) { 99 | minv = min(minv, v[i]); 100 | maxv = max(maxv, v[i]); 101 | minc = min(minc, cnt[i]); 102 | maxc = max(maxc, cnt[i]); 103 | } 104 | 105 | printf("%.3lf %.3lf\n", minv, maxv); 106 | printf("%d %d\n", minc, maxc); 107 | 108 | return 0; 109 | } -------------------------------------------------------------------------------- /202109/4.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 4.cpp 3 | * @author qzylalala 4 | * @brief 状态DP, 现场做出来了, 感觉挺套路的。 5 | * @version 0.1 6 | * @date 2022-01-10 7 | * 8 | * @copyright Copyright (c) 2022 9 | * 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | using namespace std; 17 | 18 | const int N = 16, M = 1 << N; 19 | 20 | int n, m; 21 | double p[N]; 22 | double f[M][N * 5 + 1]; 23 | 24 | double dp(int state, int coins, int r) { 25 | auto& v = f[state][coins]; 26 | if (v >= 0) return v; 27 | if (coins >= r * m) return v = 0; 28 | 29 | v = 0; 30 | for (int i = 0; i < n; i ++ ) 31 | if (state >> i & 1) 32 | v += p[i] * (dp(state, coins + 1, r) + 1); 33 | else 34 | v += p[i] * (dp(state | (1 << i), coins, r - 1) + 1); 35 | 36 | return v; 37 | } 38 | 39 | int main() { 40 | scanf("%d%d", &n, &m); 41 | for (int i = 0; i < n; i ++ ) scanf("%lf", &p[i]); 42 | 43 | memset(f, -1, sizeof f); 44 | 45 | printf("%.10lf\n", dp(0, 0, n)); 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /202109/5.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 5.cpp 3 | * @author qzylalala 4 | * @brief 动态树, 二分 5 | * @version 0.1 6 | * @date 2022-01-10 7 | * 8 | * @copyright Copyright (c) 2022 9 | * 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #define x first 17 | #define y second 18 | 19 | using namespace std; 20 | 21 | typedef long long LL; 22 | typedef pair PII; 23 | 24 | const int N = 300010; 25 | 26 | int n, p, T; 27 | 28 | struct Node 29 | { 30 | int s[2], p, v; 31 | int rev; 32 | int sum, mul; 33 | }tr[N]; 34 | int stk[N], idx; // 栈 35 | int fa[N], dep[N]; 36 | int A; 37 | vector level[N]; 38 | 39 | void pushrev(int x) 40 | { 41 | swap(tr[x].s[0], tr[x].s[1]); 42 | tr[x].rev ^= 1; 43 | } 44 | 45 | void pushup(int x) 46 | { 47 | tr[x].sum = tr[x].v; 48 | int l = tr[x].s[0], r = tr[x].s[1]; 49 | if (l) tr[x].sum = (tr[x].sum + tr[l].sum) % p; 50 | if (r) tr[x].sum += tr[r].sum; 51 | tr[x].sum %= p; 52 | } 53 | 54 | void pushdown(int x) 55 | { 56 | if (tr[x].rev) 57 | { 58 | pushrev(tr[x].s[0]), pushrev(tr[x].s[1]); 59 | tr[x].rev = 0; 60 | } 61 | 62 | auto &root = tr[x], &left = tr[tr[x].s[0]], &right = tr[tr[x].s[1]]; 63 | if (root.mul != 1) 64 | { 65 | LL mul = root.mul; 66 | left.v = left.v * mul % p; 67 | left.sum = left.sum * mul % p; 68 | left.mul = left.mul * mul % p; 69 | 70 | right.v = right.v * mul % p; 71 | right.sum = right.sum * mul % p; 72 | right.mul = right.mul * mul % p; 73 | 74 | root.mul = 1; 75 | } 76 | } 77 | 78 | bool isroot(int x) // 判断x是否为原树的根节点 79 | { 80 | return tr[tr[x].p].s[0] != x && tr[tr[x].p].s[1] != x; 81 | } 82 | 83 | void rotate(int x) // splay的旋转操作 84 | { 85 | int y = tr[x].p, z = tr[y].p; 86 | int k = tr[y].s[1] == x; 87 | if (!isroot(y)) tr[z].s[tr[z].s[1] == y] = x; 88 | tr[x].p = z; 89 | tr[y].s[k] = tr[x].s[k ^ 1], tr[tr[x].s[k ^ 1]].p = y; 90 | tr[x].s[k ^ 1] = y, tr[y].p = x; 91 | pushup(y), pushup(x); 92 | } 93 | 94 | void splay(int x) // splay操作 95 | { 96 | int top = 0, r = x; 97 | stk[ ++ top] = r; 98 | while (!isroot(r)) stk[ ++ top] = r = tr[r].p; 99 | while (top) pushdown(stk[top -- ]); 100 | while (!isroot(x)) 101 | { 102 | int y = tr[x].p, z = tr[y].p; 103 | if (!isroot(y)) 104 | if ((tr[y].s[1] == x) ^ (tr[z].s[1] == y)) rotate(x); 105 | else rotate(y); 106 | rotate(x); 107 | } 108 | } 109 | 110 | void access(int x) // 建立一条从根到x的路径,同时将x变成splay的根节点 111 | { 112 | int z = x; 113 | for (int y = 0; x; y = x, x = tr[x].p) 114 | { 115 | splay(x); 116 | tr[x].s[1] = y, pushup(x); 117 | } 118 | splay(z); 119 | } 120 | 121 | void makeroot(int x) // 将x变成原树的根节点 122 | { 123 | access(x); 124 | pushrev(x); 125 | } 126 | 127 | int findroot(int x) // 找到x所在原树的根节点, 再将原树的根节点旋转到splay的根节点 128 | { 129 | access(x); 130 | while (tr[x].s[0]) pushdown(x), x = tr[x].s[0]; 131 | splay(x); 132 | return x; 133 | } 134 | 135 | void split(int x, int y) // 给x和y之间的路径建立一个splay,其根节点是y 136 | { 137 | makeroot(x); 138 | access(y); 139 | } 140 | 141 | void link(int x, int y) // 如果x和y不连通,则加入一条x和y之间的边 142 | { 143 | makeroot(x); 144 | if (findroot(y) != x) tr[x].p = y; 145 | } 146 | 147 | int find(int x, int y) 148 | { 149 | int l = 0, r = level[x].size() - 1; 150 | while (l < r) 151 | { 152 | int mid = l + r + 1 >> 1; 153 | if (level[x][mid].x <= y) l = mid; 154 | else r = mid - 1; 155 | } 156 | return level[x][r].y; 157 | } 158 | 159 | int main() 160 | { 161 | scanf("%d%d%d", &n, &p, &T); 162 | 163 | int cur = 0; 164 | for (int i = 1; i <= n; i ++ ) 165 | { 166 | int op; 167 | scanf("%d", &op); 168 | if (op == 1) 169 | { 170 | int x; 171 | scanf("%d", &x); 172 | x ^= A; 173 | if (x > 0) 174 | { 175 | ++ idx; 176 | tr[idx].sum = tr[idx].v = x; 177 | tr[idx].mul = 1; 178 | if (cur) link(cur, idx); 179 | fa[idx] = cur, dep[idx] = dep[cur] + 1; 180 | cur = idx; 181 | level[dep[cur]].push_back({i, cur}); 182 | } 183 | else 184 | { 185 | cur = fa[cur]; 186 | } 187 | } 188 | else if (op == 2) 189 | { 190 | int s, l, r, y; 191 | scanf("%d%d%d%d", &s, &l, &r, &y); 192 | y ^= A; 193 | l = find(l, s), r = find(r, s); 194 | split(l, r); 195 | tr[r].v = tr[r].v * (LL)y % p; 196 | tr[r].sum = tr[r].sum * (LL)y % p; 197 | tr[r].mul = tr[r].mul * (LL)y % p; 198 | } 199 | else 200 | { 201 | int s, l, r; 202 | scanf("%d%d%d", &s, &l, &r); 203 | l = find(l, s), r = find(r, s); 204 | split(l, r); 205 | 206 | printf("%d\n", tr[r].sum); 207 | if (T) A = tr[r].sum; 208 | } 209 | } 210 | 211 | return 0; 212 | } 213 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 历年 CCF-CSP 真题代码 2 | 持续更新历年 CCF-CSP 真题题解 3 | 4 | 可以下载 `算法模板.pdf` ,一般考场是允许待板子的,可以打印下来带着,主要是一些经典题型和模板 5 | 6 | 来 [官网](https://csp.ccf.org.cn/csp/index.action) 刷题 7 | 8 | 来 [Acwing](https://www.acwing.com/activity/content/39/) 看题解 9 | 10 | 11 | 12 | ### 知识点统计(后两题) 13 | 14 | ```markdown 15 | 1. 状态压缩DP 16 | 202012-4 17 | 201409-5 18 | 202109-4 19 | 20 | 2. 线段树 21 | 202012-5 22 | 201912-5 23 | 201709-5 24 | 201609-5 25 | 26 | 3. 计算几何 27 | 202009-4 28 | 29 | 4. AC自动机 30 | 202009-5 31 | 201509-5 32 | 33 | 5. 状态机DP 34 | 202009-5 35 | 201509-5 36 | 201312-4 37 | 38 | 6. 矩阵乘法,快速幂 39 | 202006-4 40 | 201512-5 41 | 42 | 7. 网络流 43 | 202006-5 44 | 201812-5 45 | 201703-5 46 | 201412-5 47 | 48 | 8. 可持久化链表 49 | 201912-4 50 | 51 | 9. 多路归并 52 | 201906-4 53 | 54 | 10. 树型DP 55 | 201906-5 56 | 201503-4 57 | 58 | 11. 最短路 59 | 201903-5 60 | 201806-4 61 | 201712-4(拆点) 62 | 201609-4 63 | 202104-5(+扩展欧几里得) 64 | 65 | 12. 最小生成树 66 | 201812-4 67 | 201703-4 68 | 201412-4 69 | 70 | 13. 点分治 71 | 201803-5 72 | 201503-5 73 | 74 | 14. 区间DP 75 | 201612-4 76 | 77 | 15. 连通性DP 78 | 201604-5 79 | 80 | 16. 欧拉路径 81 | 201512-4 82 | 83 | 17. 强连通分量 84 | 201509-4(tarjan) 85 | 86 | 18. 动态树 87 | 202109-5 88 | ``` 89 | 90 | -------------------------------------------------------------------------------- /算法模板.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qzylalala/CSP/7feb088c41eeb1bad0639c7316093a5279b34cba/算法模板.pdf --------------------------------------------------------------------------------