#include<iostream> #include<algorithm> usingnamespace std; intmain(){ int n; cin >> n; int a[1000]; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); int ans = 0; for (int i = 0; i < n - 2; i++) { for (int j = i + 1; j < n - 1; j++) { int k = j + 1; while (k < n && a[i] + a[j] > a[k]) { k++; } ans += k - j - 1; } } cout << ans << endl;
usingnamespace std; intssort(int a, int b){ returnabs(b) > abs(a); } intmain(){ int a; int b[1010]; while (cin >> a) { for (int i = 0; i < a; i++) { cin >> b[i]; } sort(b,b+a,ssort); for (int i = 0; i < a; i++) { cout << b[i] << " "; } cout << endl; } return0; }
#include<iostream> usingnamespace std; int a[20];//放外面不需要赋零 intmain(){ int m; cin >> m; for (int i = 0; i < m; i++) { cin >> a[i]; } int sum = 0; for (int j = 0; j < m - 1; j++) { for (int k = j+1; k < m; k++) { int tmp = (min(a[j], a[k]) * (k - j)); if (tmp > sum)sum = tmp; } } cout << sum<<endl; return0; }
1197: 区间和统计
题目描述
给定一个包含n个元素的数组,数组中元素ai保证 -1000 < ai < 1000。在数组中,从l到r(l <= r)的所有数叫做数组的一个[l, r]子区间,你需要求出,所有子区间中,和为k的区间一共有多少个。
#include<iostream> #include<algorithm> usingnamespace std; longlong a[1001][1001]; intmain(){ int M, N, Q; cin >> M >> N >> Q; for (int i = 0; i < M; i++) for (int j = 0; j < N; j++) cin >> a[i][j]; for (int k = 0; k < Q; k++) { int sum = 0; int num1, num2, num3, num4; cin >> num1 >> num2 >> num3 >> num4; for (int i1 = min(num1, num3); i1 <= max(num1, num3); i1++) for (int j1 = min(num2, num4); j1 <= max(num2, num4); j1++) sum += a[i1][j1];//最大值最小值转化为标准型去计算 cout << sum << endl; } return0; }
#define int long long usingnamespace std; int a[3010]; int b[5000000];//注意开对数组容量
inttmp(int a, int b){ return b < a;//从大到小是< } signedmain(){ int m, n; while (cin >> m >> n) { for (int i = 0; i < m; i++) { cin >> a[i]; } int uu = 0; for (int j = 0; j < m; j++) { for (int k = j + 1; k < m; k++) { b[uu] = a[j] + a[k]; uu++; } } sort(b, b + m*(m-1)/2, tmp);//先出结果再排序,放心,够用 for (int u = 0; u < n; u++) { cout << b[u] << " "; }cout << endl; } return0; }
1238: Greed
题目描述
Jafar has n cans of cola. Each can is described by two integers: remaining volume of cola ai and can’s capacity bi (ai ≤ bi).
Jafar has decided to pour all remaining cola into just 2 cans, determine if he can do this or not!
Jafar has n cans of cola. Each can is described by two integers: remaining volume of cola ai and can’s capacity bi (ai ≤ ≤ bi). Jafar has decided to pour all remaining cola into just 2 cans, determine if he can do this or not!
#include<iostream> #define int long long usingnamespace std; bool a[10000009]; int b[1010]; signedmain(){ int N; cin >> N; for (int i = 0; i < N; i++) { cin >> b[i]; a[b[i]] = 1; } int ans = 0; for (int i = 0; i < N - 1; i++) { for (int j = i + 1; j < N; j++) { if (a[b[i] + b[j]] == 1) { ans++; a[b[i] + b[j]] = 0; } } } cout << ans << endl; return0; }
constint MAXN = 100005; int n, m; int a[MAXN], sum[MAXN];
boolcheck(int x){ int cnt = 1, pre = 0; for (int i = 1; i <= n; i++) { if (sum[i] - sum[pre] > x) { cnt++; pre = i - 1; } } return cnt <= m; }
intmain(){ while (cin >> n >> m) { memset(sum, 0, sizeof(sum)); int l = 0, r = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; sum[i] = sum[i-1] + a[i]; r += a[i]; } while (l < r) { int mid = (l + r) / 2; if (check(mid)) r = mid; else l = mid + 1; } cout << l << endl; } return0; }
booldfs(int* a, int idx, int sum, int* visited, int n, int cnt, int target){ if (cnt == 3) { returntrue; } if (sum < 0) { returnfalse; } if (sum == 0) { returndfs(a, 0, target, visited, n, cnt + 1, target); } for (int i = idx; i < n; i++) { if (visited[i] || a[i] > sum) { continue; } visited[i] = 1; if (dfs(a, i + 1, sum - a[i], visited, n, cnt, target)) { returntrue; } visited[i] = 0; } // 当已经找到三组数字之和等于目标和时,还需要判断最后一组是否符合要求 if (cnt == 2 && sum == target) { returntrue; } returnfalse; }
boolsolve(int* a, int n){ int sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; } if (sum % 4 != 0) { returnfalse; } int visited[25] = { 0 }; returndfs(a, 0, sum / 4, visited, n, 0, sum / 4); }
intmain(){ int t; cin >> t; while (t--) { int n; cin >> n; int a[25]; for (int i = 0; i < n; i++) { cin >> a[i]; } if (solve(a, n)) { cout << "yes" << endl; } else { cout << "no" << endl; } } return0; }
1265: prime circle
题目描述
A ring is compose of n circles as shown in diagram. Put natural number 1, 2, …, n into each circle separately, and the sum of numbers in two adjacent circles should be a prime.
Note: the number of first circle should always be 1.
输入
n (0 < n < 20). (multi test case)
输出
The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order.
You are to write a program that completes above process.
题目描述 A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime.
Note: the number of first circle should always be 1. 输入 n (0 < n < 20). (multi test case) 输出 The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order.
You are to write a program that completes above process.
structPoint { int a, b; booloperator< (const Point& other) const { return b < other.b; } } p[N];
int n, m; int cnt; char a[8][8]; voiddfs(int u, int last_b) { if (u == n) { cnt++; return; } for (int i = u; i < m; i++) { if (p[i].b >= last_b) { int tmp = p[i].b; p[i].b = last_b; dfs(u + 1, tmp); p[i].b = tmp; } } }
intmain() { while ((cin >> m >> n)&&(m != -1) && (n != -1)) { for (int i = 0; i < m; i++) { for (int j = 0,q=0; j < m; j++,q++) { cin >> a[i][j]; if (a[i][j] == '#')p[q].a = i, p[q].b = j; } } sort(p, p + m); cnt = 0; dfs(0, -1); cout << cnt << endl; } return0; }
voidbfs(){ memset(visited, false, sizeof(visited)); queue<Node> q; Node start = { 0, 0, 0 }; q.push(start); visited[0][0] = true; while (!q.empty()) { Node cur = q.front(); q.pop(); int x = cur.x, y = cur.y, step = cur.step; if (x == MAXN - 1 && y == MAXN - 1) { // 到达终点,结束搜索 cout << "(0, 0)" << endl; int pre_x = MAXN - 1, pre_y = MAXN - 1; vector<pair<int, int>> path; // 记录路径 while (pre_x != 0 || pre_y != 0) { path.push_back({ pre_x, pre_y }); int tmp_x = pre_x, tmp_y = pre_y; pre_x = pre[pre_x][pre_y][0]; pre_y = pre[tmp_x][tmp_y][1]; } for (int i = path.size() - 1; i >= 0; i--) { cout << "(" << path[i].first << ", " << path[i].second << ")" << endl; } return; } for (int i = 0; i < 4; i++) { int next_x = x + dir[i][0], next_y = y + dir[i][1]; if (next_x < 0 || next_x >= MAXN || next_y < 0 || next_y >= MAXN) continue; // 越界 if (maze[next_x][next_y] == 1 || visited[next_x][next_y]) continue; // 墙或者已访问过 Node next_node = { next_x, next_y, step + 1 }; pre[next_x][next_y][0] = x; // 记录路径 pre[next_x][next_y][1] = y; q.push(next_node); visited[next_x][next_y] = true; } } }
intmain(){ for (int i = 0; i < MAXN; i++) { for (int j = 0; j < MAXN; j++) { cin >> maze[i][j]; } } bfs(); return0; }
1270: Find a way
题目描述
<span style=”font-family:Merriweather, Full-Width-Quoration-Marks, -apple-system, blinkMacSystemFont, “font-size:15px;background-color:rgba(210, 210, 255, 0.5);”>Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
<span style=”font-family:Merriweather, Full-Width-Quoration-Marks, -apple-system, blinkMacSystemFont, “font-size:15px;background-color:rgba(210, 210, 255, 0.5);”>For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
intmain() { while (cin >> n >> m) { int sy, sx, ty, tx, res = INF; for (int i = 0; i < n; i ++ ) for (int j = 0; j < m; j ++ ) { cin >> g[i][j]; if (g[i][j] == 'Y') sx = i, sy = j; if (g[i][j] == 'M') tx = i, ty = j; }
for (int i = 0; i < n; i ++ ) for (int j = 0; j < m; j ++ ) { if (g[i][j] == '@') { int d1 = bfs(sx, sy, i, j); int d2 = bfs(tx, ty, i, j); res = min(res, d1 + d2); } }
constint MAXN = 405; constint INF = 1e9; int n, m, sx, sy; int dis[MAXN][MAXN]; int dx[8] = {-1, -2, -2, -1, 1, 2, 2, 1}; int dy[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
structnode { int x, y; };
boolcheck(int x, int y){ return x >= 1 && x <= n && y >= 1 && y <= m; }
voidbfs(){ queue<node> q; q.push({sx, sy}); dis[sx][sy] = 0; while (!q.empty()) { int x = q.front().x, y = q.front().y; q.pop(); for (int i = 0; i < 8; i++) { int nx = x + dx[i], ny = y + dy[i]; if (check(nx, ny) && dis[nx][ny] == INF) { dis[nx][ny] = dis[x][y] + 1; q.push({nx, ny}); } } } }
intmain(){ cin >> n >> m >> sx >> sy; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { dis[i][j] = INF; } } bfs(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (dis[i][j] == INF) { cout << "-1 "; } else { cout << dis[i][j] << " "; } } cout << endl; } return0; }
int n, m; char matrix[MAXN][MAXN]; // 输入的矩阵 bool vis[MAXN][MAXN]; // 标记每个点是否访问过 int cnt; // 细胞数量计数器
// 判断一个点是否在矩阵内 boolinMatrix(int x, int y){ return x >= 0 && x < n&& y >= 0 && y < m; }
// BFS遍历整个细胞 voidbfs(int sx, int sy){ queue<pair<int, int>> q; q.push({ sx, sy }); vis[sx][sy] = true; while (!q.empty()) { auto p = q.front(); int x = p.first, y = p.second; q.pop(); for (int i = 0; i < 4; i++) { // 四个方向搜索 int nx = x + dx[i], ny = y + dy[i]; if (inMatrix(nx, ny) && !vis[nx][ny] && matrix[nx][ny] != '0') { q.push({ nx, ny }); vis[nx][ny] = true; } } } }
intmain(){ cin >> n >> m; for (int i = 0; i < n; i++) { cin >> matrix[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (matrix[i][j] != '0' && !vis[i][j]) { // 如果是细胞且未访问过 cnt++; bfs(i, j); // BFS搜索整个细胞 } } } cout << cnt << endl; return0; }
int n; char maze[MAXN][MAXN]; bool vis[MAXN][MAXN];
int dx[] = { -1, 0, 1, 0 }; // 上右下左 int dy[] = { 0, 1, 0, -1 };
intbfs(int sx, int sy){ queue<pair<int, int>> q; q.push({ sx, sy }); vis[sx][sy] = true; int cnt = 1;
while (!q.empty()) { auto p = q.front(); int x = p.first, y = p.second;
q.pop();
for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue; if (vis[nx][ny]) continue; if (maze[x][y] != maze[nx][ny]) { vis[nx][ny] = true; q.push({ nx, ny }); cnt++; } } }
return cnt; }
intmain(){ int m; cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> maze[i][j]; } } memset(vis, false, sizeof(vis)); for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; x--; y--; // 转换成从0开始编号的坐标 cout << bfs(x, y) << endl; memset(vis, false, sizeof(vis)); // 注意清空标记数组 } return0; }
#include<iostream> #include<vector> #include<cstring> usingnamespace std; constint N = 10010; vector<int> g[N]; // 存储图 bool vis[N]; // 标记是否已经访问 voiddfs(int u){ cout << u << ' '; // 输出当前节点编号 vis[u] = true; // 标记已经访问 for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (!vis[v]) { // 如果节点v还没有被访问 dfs(v); // 继续访问节点v } } } intmain(){ int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; g[u].push_back(v); // 添加无向边 g[v].push_back(u); } dfs(1); // 从节点1开始遍历 cout << endl; return0; }
int n, m, s, t, w; int dis[MAXN]; bool vis[MAXN]; vector<pair<int, int>> G[MAXN];
intprim(){ memset(dis, INF, sizeof(dis)); memset(vis, false, sizeof(vis)); dis[s] = 0; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q; q.push(make_pair(0, s)); while (!q.empty()) { int u = q.top().second; q.pop(); if (vis[u]) continue; vis[u] = true; for (int i = 0; i < G[u].size(); ++i) { int v = G[u][i].first, w = G[u][i].second; if (!vis[v] && dis[v] > w) { dis[v] = w; q.push(make_pair(dis[v], v)); } } } int ans = 0; for (int i = 1; i <= n; ++i) { if (dis[i] == INF) return-1; ans += dis[i]; } return ans; }
intmain(){ int T; cin >> T; while (T--) { cin >> n >> m; for (int i = 1; i <= n; ++i) { G[i].clear(); } for (int i = 1; i <= m; ++i) { cin >> s >> t >> w; G[s].push_back(make_pair(t, w)); G[t].push_back(make_pair(s, w)); } int ans = prim(); cout << ans << endl; } return0; }
voidmerge(int x, int y){ // 合并两个集合 int rootX = find(x); int rootY = find(y); if (rootX != rootY) { parent[rootX] = rootY; } }
intcount(){ // 统计集合的数量 int cnt = 0; for (int i = 1; i < parent.size(); i++) { if (parent[i] == i) { cnt++; } } return cnt; } };
intmain(){ int n, m; while (cin >> n >> m) { UnionFind uf(n); // 初始化并查集 for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; uf.merge(u, v); // 合并u和v所在的集合 } cout << uf.count() - 1 << endl; // 输出最少需要的道路数 } return0; }
constint N = 105, K = 105, INF = 0x3f3f3f3f; int n, k, m, s, t; int c[N], dist[N]; //某个国家的文化,到某个国家的距离 int a[K][K], e[N][N]; //两国之间是否排斥,两国之间的距离 bool h[K]; //已经学过的文化
//到国家x的距离是d voiddfs(int x, int d){ if (d >= dist[x] || d >= dist[t]) return; dist[x] = d; if (x == t) return;
for (int i = 1; i <= n; i ++) { //i国 if (e[x][i] == INF) continue; //x,i两国之间没有路 if (h[c[i]]) continue; //已学过该国文化
bool flag = false; for (int j = 1; j <= k; j ++) { //j文化 if (h[j] && a[c[i]][j] == 1) { //学过j文化且i国的文化排斥j文化 flag = true; break; } } if (flag) continue;
int n, m, s, t; // n: 点的数量,m: 边的数量,s: 起点,t: 终点 int dis[105]; // dis[i] 表示从起点 s 到 i 的最短距离 bool vis[105]; // vis[i] 表示 i 是否已经访问过 int G[105][105]; // G[i][j] 表示 i 到 j 的边的长度
for (int i = 1; i <= n; i++) { int u = -1, min_dis = INF; for (int j = 1; j <= n; j++) { if (!vis[j] && dis[j] < min_dis) { u = j; min_dis = dis[j]; } } if (u == -1) break; // 所有点都已经访问过 vis[u] = true; // 标记 u 已经访问过 for (int v = 1; v <= n; v++) { if (!vis[v] && G[u][v] != INF && dis[u] + G[u][v] < dis[v]) { dis[v] = dis[u] + G[u][v]; } } }
}
intmain(){ cin >> n >> m >> s >> t; memset(G, INF, sizeof(G)); // 初始化 G 数组为 INF for (int i = 1; i <= n; i++) G[i][i] = 0; // 自己到自己的距离为 0 for (int i = 1; i <= m; i++) { int u, v, w; cin >> u >> v >> w; G[u][v] = G[v][u] = w; // 无向图,所以需要同时更新 G[u][v] 和 G[v][u] }
constint MAXN = 3005; string dp[MAXN]; string reverseStr(string str) { int n = str.length();
for (int i = 0; i < n / 2; i++) swap(str[i], str[n - i - 1]); return str; }
string add(string a, string b){ string res; int carry = 0; // 进位 int len1 = a.size(), len2 = b.size(); a = reverseStr(a); b = reverseStr(b); int i = 0, j = 0; while (i < len1 || j < len2) { int sum = carry; if (i < len1) { sum += a[i] - '0'; i++; } if (j < len2) { sum += b[j] - '0'; j++; } carry = sum / 10; sum %= 10; res += (sum + '0'); //cout << carry << " " << res << endl; } if (carry) { res += (carry + '0'); } returnreverseStr(res); }
#include<iostream> #define int long long usingnamespace std;
intmaxSum(int a[], int n){ int sum = 0; int b = 0; for (int i = 0; i < n; i++) { if (b > 0) b += a[i]; else b = a[i]; if (b > sum) sum = b; } return sum; } signedmain(){ int M; cin >> M; while (M--) { int N; cin >> N; int* a = newint[N]; for (int i = 0; i < N; i++) { cin >> a[i]; }
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter ‘e’. He was a member of the Oulipo group. A quote from the book:
Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : stir son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un non-dit : la vision, l’avision d’un oubli commandant tout, où s’abolissait la raison : tout avait l’air normal mais…
Perec would probably have scored high (or rather, low) in the following contest. People are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given “word” as possible. Our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. These competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive ‘T’s is not unusual. And they never use spaces.
So we want to quickly find out how often a word, i.e., a given string, occurs in a text. More formally: given the alphabet {‘A’, ‘B’, ‘C’, …, ‘Z’} and two finite strings over that alphabet, a word W and a text T, count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. Occurrences may overlap.
输入
The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:
One line with the word W, a string over {‘A’, ‘B’, ‘C’, …, ‘Z’}, with 1 ≤ |W| ≤ 20,000 (here |W| denotes the length of the string W). One line with the text T, a string over {‘A’, ‘B’, ‘C’, …, ‘Z’}, with |W| ≤ |T| ≤ 2,000,000.
输出
For every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T.
for (int i = 2; i <= sqrt(n); i++) { if (is_prime[i]) { for (int j = i * i; j <= n; j += i) { is_prime[j] = false; } } } for (int i = 2; i <= n; i++) { if (is_prime[i]) { sum += i; } } cout << sum << endl; return0; }
#include<iostream> usingnamespace std; intmain(){ int A, B; while (cin >> A >> B && !(A == 0 && B == 0)) { int ans = 1; while (B > 0) { if (B & 1) { ans = (ans * A) % 1000; } A = (A * A) % 1000; B >>= 1; } cout << ans << endl; } return0; }
#include<iostream> #include<algorithm> #include<cmath> usingnamespace std; #define int long long intPow(int a, int b, int m){ a %= m; int res = 1; while (b > 0) { if (b & 1)res = res * a % m; a = a * a % m; b >>= 1; } return res; } signedmain(){ int a, b, p; cin >> a >> b >> p; int k = Pow(a,b,p); cout << a << "^" << b << " mod " << p << "=" << k << endl; return0;
}
问题 G: XORinacci
题目描述
Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that he named XORinacci numbers. He defined them as follows: