目录
7-1 统计工龄
给定公司N名员工的工龄,要求按工龄增序输出每个工龄段有多少员工。
输入格式:
输入首先给出正整数N(≤105),即员工总人数;随后给出N个整数,即每个员工的工龄,范围在[0, 50]。
输出格式:
按工龄的递增顺序输出每个工龄的员工个数,格式为:“工龄:人数”。每项占一行。如果人数为0则不输出该项。
输入样例:
8
10 2 0 5 7 2 5 2
输出样例:
0:1
2:3
5:2
7:1
10:1
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first
#define yy second
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
int a[N];
int b[N];
set<int>s;
int main()
{
int n;
cin >> n;
for(int i = 1; i <= n; i ++)
{
cin >> a[i];
s.insert(a[i]);
b[a[i]] ++;
}
for(auto it : s)
{
cout << it << ":" << b[it] << endl;
}
return 0;
}
7-2 寻找大富翁
胡润研究院的调查显示,截至2017年底,中国个人资产超过1亿元的高净值人群达15万人。假设给出N个人的个人资产值,请快速找出资产排前M位的大富翁。
输入格式:
输入首先给出两个正整数N(≤106)和M(≤10),其中N为总人数,M为需要找出的大富翁数;接下来一行给出N个人的个人资产值,以百万元为单位,为不超过长整型范围的整数。数字间以空格分隔。
输出格式:
在一行内按非递增顺序输出资产排前M位的大富翁的个人资产值。数字间以空格分隔,但结尾不得有多余空格。
输入样例:
8 3
8 12 7 3 20 9 5 18
输出样例:
20 18 12
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first
#define yy second
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
int a[N];
int main()
{
int n, m;
cin >> n >> m;
for(int i = 0; i < n; i ++)
{
cin >> a[i];
}
stable_sort(a, a + n, greater<int>());
for(int i = 0; i < min(n, m); i ++)
{
cout << a[i] << " \n"[i == min(n, m) - 1];
}
return 0;
}
7-3 点赞狂魔
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first
#define yy second
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
struct node
{
string name;
int k;
int wao;
}a[N];
bool cmp(node a, node b)
{
if(a.wao == b.wao)
return a.k < b.k;
return a.wao > b.wao;
}
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; i ++)
{
cin >> a[i].name;
int k;
cin >> k;
a[i].k = k;
set<int>s;
while(k --)
{
int x;
cin >> x;
s.insert(x);
}
a[i].wao = s.size();
}
sort(a, a + n, cmp);
if(n == 1)
{
cout << a[0].name << " - -\n";
}
else if(n == 2)
{
cout << a[0].name << " " << a[1].name << " -\n";
}
else
{
for(int i = 0; i < 3; i ++)
{
cout << a[i].name << " \n"[i == 2];
}
}
return 0;
}
7-4 插入排序还是归并排序
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first
#define yy second
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
int a[N], b[N];
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; i ++)
cin >> a[i];
for(int i = 0; i < n; i ++)
cin >> b[i];
int i = 0, j = 0;
for(i = 0; i < n - 1 && b[i] <= b[i + 1]; i ++);
for(j = i + 1; j < n && a[j] == b[j]; j ++);
if(j == n)
{
puts("Insertion Sort");
sort(a, a + i + 2);
}
else
{
puts("Merge Sort");
int k = 1, flag = 1;
while(flag)
{
flag = 0;
for(int i = 0; i < n; i ++)
{
if(a[i] != b[i])
flag = 1;
}
k = k*2;
for(int i = 0; i < n/k; i ++)
{
sort(a + i*k, a + (i + 1)*k);
}
sort(a + n/k * k, a + n);
}
}
for(int i = 0; i < n; i ++)
{
cout << a[i] << " \n"[i == n - 1];
}
return 0;
}
7-5 插入排序还是堆排序
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first
#define yy second
#define endl '\n'
int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
int a[N], num[N];
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
cin >> num[i];
if (num[1] > num[2]) // heapsort
{
int step = -1;
for (int i = 3; i <= n; i++)
{
if (num[i] > num[1])
{
step = i;
break;
}
}
swap(num[1], num[step - 1]);
step -= 2;
int max = -1;
int i = 1;
for (; i * 2 <= step;)
{
if (i * 2 == step)
{
max = step;
}
else
{
max = num[i * 2] < num[i * 2 + 1] ? i * 2 + 1 : i * 2;
}
if (num[i] < num[max])
{
swap(num[i], num[max]);
i = max;
}
else
break;
}
puts("Heap Sort");
for (int i = 1; i <= n; i++)
cout << num[i] << " \n"[i == n];
}
else // insertsort
{
bool flag = 0;
for (int i = 2; i <= n; i++)
{
for (int j = i; j > 1; j--)
{
if (a[j] < a[j - 1])
swap(a[j], a[j - 1]);
else
break;
}
if (flag == 1)
{
puts("Insertion Sort");
for (int j = 1; j <= n; j++)
{
cout << a[j] << " \n"[j == n];
}
return 0;
}
for (int j = 1; j <= n; j++)
{
if (num[j] != a[j])
{
flag = 0;
break;
}
flag = 1;
}
}
}
return 0;
}
另一种写法
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first
#define yy second
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
int n, a[N], b[N], h[N], cnt, m[N];
void down(int x)
{
int t = x;
if (x * 2 <= cnt && h[x * 2] > h[t])
t = x * 2;
if (x * 2 + 1 <= cnt && h[x * 2 + 1] > h[t])
t = x * 2 + 1;
if (x != t)
{
swap(h[x], h[t]);
down(t);
}
}
int main()
{
cin >> n;
for(int i = 1; i <= n; i ++)
cin >> a[i];
for(int i = 1; i <= n; i ++)
cin >> b[i];
int j = 1;
while(b[j] <= b[j + 1]) j ++;
j ++;
int flag = 0;
for(int i = j; i <= n; i ++)
{
if(a[i] != b[i])
flag = 1;
}
if(flag)
{
puts("Heap Sort");
for(int i = 1; i <= n; i ++)
{
m[i] = max(m[i - 1], b[i]);
}
int j;
for(j = n; j >= 1; j --)
{
if(m[j] != b[j])
break;
}
swap(b[1], b[j]);
for(int i = 1; i < j; i ++)
{
h[++ cnt] = b[i];
}
for(int i = cnt/2; i; i --)
down(i);
for(int i = 1; i < j; i ++)
cout << h[i] << " ";
for(int i = j; i <= n; i ++)
cout << b[i] << " \n"[i == n];
}
else
{
puts("Insertion Sort");
sort(a + 1, a + 1 + j);
for(int i = 1; i <= n; i ++)
cout << a[i] << " \n"[i == n];
}
return 0;
}
7-6 逆序对
//归并排序解法
#include <bits/stdc++.h>
#define ll long long
const int N = 1e6 + 10;
using namespace std;
int dp[N]; //存放元素
int mp[N]; //存放归并排序后的元素
ll ans;
void Merge(int l1, int r1, int l2, int r2)
{
int pos = 0;
int pos1 = l1;
int pos2 = l2;
while (pos1 <= r1 && pos2 <= r2)
{
if (dp[pos1] <= dp[pos2])
mp[pos++] = dp[pos1++];
else
{
mp[pos++] = dp[pos2++];
ans += (r1 - pos1 + 1); // 当前逆序对数量
}
}
//如果mid前面的元素数目比mid后面的元素数目少,那么后面就剩下了很多的元素;
while (pos1 <= r1)
mp[pos++] = dp[pos1++]; //把后面余下的mid前面元素放进mp中;
while (pos2 <= r2)
mp[pos++] = dp[pos2++]; //把后面余下的mid前面元素放进mp中;
for (int i = l1; i <= r2; i++)
dp[i] = mp[i - l1]; //用mp数组对dp数组进行更新;
}
void Merge_sort(int l, int r)
{
if (l < r)
{
// cout << l << " " << r << endl;
int mid = l + r >> 1;
Merge_sort(l, mid); // 左边递归到最小
Merge_sort(mid + 1, r); // 右边递归到最小
//cout << l << " " << mid << " " << mid + 1 << " " << r << endl;
Merge(l, mid, mid + 1, r);
}
}
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &dp[i]);
Merge_sort(0, n - 1);
printf("%lld\n", ans);
return 0;
}
//树状数组解法
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first
#define yy second
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
int n, c[N], ans;
int lowbit(int x)
{
return x&(-x);
}
void modify(int x, int d)
{
for(int i = x; i <= n; i += lowbit(i))
c[i] += d;
}
int get(int x)
{
int sum = 0;
for(int i = x; i >= 1; i -= lowbit(i))
sum += c[i];
return sum;
}
signed main()
{
cin >> n;
for(int i = 1; i <= n; i ++)
{
int x;
cin >> x;
ans += get(n) - get(x);
modify(x, 1);
}
cout << ans << endl;
return 0;
}
7-7 堆排序
#include <bits/stdc++.h>
using namespace std;
int n, N = 1000;
int a[10000];
void HeapAdjust(int a[], int s, int m)
{ //筛选法调整堆
int x = a[s];
for (int j = 2 * s; j <= m; j *= 2)
{
if (j < m && a[j] < a[j + 1])
j++;
if (x >= a[j])
break;
a[s] = a[j];
s = j;
}
a[s] = x;
}
void CreateHeap(int a[])
{ //建初堆
for (int i = n / 2; i > 0; i--)
HeapAdjust(a, i, n);
}
void HeapSort(int *a)
{ //堆排序实现
CreateHeap(a);
for (int i = n; i > 1; i--)
{
swap(a[1], a[i]); //交换堆顶记录和子序列最后一个记录
HeapAdjust(a, 1, i - 1);
for (int i = 1; i <= n; i++)
{
if (i > 1)
cout << " ";
cout << a[i];
}
cout << endl;
}
}
int main()
{
while (cin >> n)
{
for (int i = 1; i <= n; i++)
cin >> a[i];
HeapSort(a);
}
return 0;
}
另一种写法
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first
#define yy second
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
int n, a[N], cnt;
void down(int x)
{
int t = x;
if(a[x * 2] > a[t] && x * 2 <= cnt)
t = x * 2;
if(a[x * 2 + 1] > a[t] && x * 2 + 1 <= cnt)
t = x * 2 + 1;
if(t != x)
{
swap(a[t], a[x]);
down(t);
}
}
int main()
{
while(cin >> n)
{
for(int i = 1; i <= n; i ++) cin >> a[i];
cnt = n;
for(int i = n/2; i >= 1; i --)
down(i);
for(int i = n; i > 1; i --)
{
swap(a[1], a[i]);
cnt = i - 1;
down(1);
for(int j = 1; j <= n; j ++)
cout << a[j] << " \n"[j == n];
}
}
return 0;
}
7-8 石子合并
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first
#define yy second
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e3 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
int n, a[N], sum[N];
int dp[N][N];
int main()
{
cin >> n;
for(int i = 1; i <= n; i ++)
{
cin >> a[i];
sum[i] = sum[i - 1] + a[i];
}
mem(dp, 0x3f);
for(int i = 0; i <= n; i ++) dp[i][i] = 0;
for(int len = 1; len < n; len ++)
{
for(int i = 1; i <= n; i ++)
{
int j = i + len;
if(j > n) continue;
for(int k = i; k < j; k ++)
{
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + sum[j] - sum[i - 1]);
}
}
}
cout << dp[1][n] << endl;
return 0;
}
7-9 第k小
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first
#define yy second
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
int a[N];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for(int i = 0; i < n; i ++)
{
cin >> a[i];
}
sort(a, a + n);
cout << a[m - 1] << endl;
return 0;
}
7-10 快速排序的过程
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first
#define yy second
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
int a[N], n;
void q_sort(int l, int r)
{
int i, j, temp;
if(l > r) return;
temp = a[l];
i = l, j = r;
while(i != j)
{
while(a[j] >= temp && i < j)
j --;
while(a[i] <= temp && i < j)
i ++;
if(i < j) swap(a[i], a[j]);
}
a[l] = a[i];
a[i] = temp;
for(int i = 1; i <= n; i ++)
cout << a[i] << " ";
cout << endl;
q_sort(l, i - 1);
q_sort(i + 1, r);
}
int main()
{
cin >> n;
for(int i = 1; i <= n; i ++)
cin >> a[i];
q_sort(1, n);
return 0;
}