7-1 统计工龄 (20 分)
#include<bits/stdc++.h>
#define eb emplace_back
#define PSI pair<string,int>
const int N = 2e5 + 10;
#define x first
#define y second
using namespace std;
set<int>st;
int num[N];
void solve(){
int n;
cin>>n;
int x;
for(int i=1;i<=n;i++){
cin>>x;
st.insert(x);
num[x]++;
}
for(auto i:st){
cout<<i<<":"<<num[i]<<endl;
}
}
int main(){
solve();
return 0;
}
7-2 寻找大富翁 (25 分)
#include <bits/stdc++.h>
using namespace std;
priority_queue<int> p;
int main()
{
int n,m,a;
scanf("%d%d",&n,&m);
int flag=0,flag1=0;
for(int i=0; i<n; i++)
{
scanf("%d",&a);
p.push(a);
}
if(m>n)
{
while(p.size()>=1)
{
if(flag==0)
{
printf("%d",p.top());
p.pop();
flag=1;
}
else
{
printf(" %d",p.top());
p.pop();
}
}
}
else
{
for(int i=0;i<m;i++)
{
if(flag1==0)
{
printf("%d",p.top());
p.pop();
flag1=1;
}
else
{
printf(" %d",p.top());
p.pop();
}
}
}
return 0;
}
7-3 点赞狂魔 (25 分)
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
const int N = 1e7 + 10;
using namespace std;
struct node
{
string s;
int sum;
double ave;
node(string s1,int sum1,double ave1):s(s1),sum(sum1),ave(ave1) {}
};
bool cmp(node x,node y)
{
if(x.sum==y.sum)
return x.ave<y.ave;
return x.sum>y.sum;
}
int main()
{
vector<node>dp;
set<int>s[111];
int i;
string name;
int n;
cin>>n;
for(i=0; i<n; i++)
{
int y;
cin>>name>>y;
int j;
for(j=0; j<y; j++)
{
int k;
cin>>k;
s[i].insert(k);
}
dp.push_back(node(name,s[i].size(),(double)y/(double)s[i].size()));
}
sort(dp.begin(),dp.end(),cmp);
for(i=0; i<3; i++)
{
if(i!=0)
cout<<" ";
if(i<dp.size())
cout<<dp[i].s;
else
cout<<'-';
}
return 0;
}
7-4 插入排序还是归并排序 (25 分)
#include<bits/stdc++.h>
using namespace std;
int a[111],b[111];
int n;
int F()
{
for(int i=0; i<n; i++)
{
if(a[i]!=b[i])
return 0;
}
return 1;
}
int main()
{
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
for(int i=0; i<n; i++)
scanf("%d",&b[i]);
int flag=0;
for(int i=2;; i*=2)
{
for(int j=0; j<n; j+=i)
{
if(j+i<n)
sort(a+j,a+j+i);
else
sort(a+j,a+n);
}
if(flag)
{
printf("Merge Sort\n");
for(int i=0; i<n; i++)
printf("%d%c",a[i]," \n"[i==n-1]);
break;
}
if(F())
flag=1;
if(i>n)
break;
}
if(!flag)
{
printf("Insertion Sort\n");
int t=1;
while(t<n&&b[t-1]<=b[t])
t++;
while(t>0&&b[t-1]>b[t])
{
swap(b[t-1],b[t]);
t--;
}
for(int i=0; i<n; i++)
printf("%d%c",b[i]," \n"[i==n-1]);
}
return 0;
}
7-5 逆序对 (15 分)
#include<bits/stdc++.h>
#define ll long long
const int N = 1e5 + 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);
}
}
while(pos1<=r1) mp[pos++]=dp[pos1++];
while(pos2<=r2) mp[pos++]=dp[pos2++];
for(int i=l1;i<=r2;i++) dp[i]=mp[i-l1];
}
void Merge_sort(int l,int r){
if(l<r){
int mid=l+r>>1;
Merge_sort(l,mid);
Merge_sort(mid+1,r);
Merge(l,mid,mid+1,r);
}
}
void solve(){
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);
}
int main(){
solve();
return 0;
}
7-6 第k数 (20 分)
题面有误 咱也不知道题意
7-7 堆排序 (10 分)
#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;
}
7-8 快速排序 (10 分)
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define eps 1e-7
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
const int M = 111;
using namespace std;
int n;
int a[N];
void dis(int n){
for(int i=1;i<=n;i++){
if(i==n) cout<<a[i]<<endl;
else cout<<a[i]<<" ";
}
}
void q_sort(int l,int r){
int i=l;
int j=r;
int key=a[i];
if(i>=j) return ;
else{
while(i<j){
while(i<j&&a[j]>=key) j--;
a[i]=a[j];
while(i<j&&a[i]<=key) i++;
a[j]=a[i];
}
a[i]=key;
dis(n);
q_sort(l,i-1);
q_sort(i+1,r);
}
}
int main(){
while(cin>>n){
for(int i=1;i<=n;i++) cin>>a[i];
q_sort(1,n);
}
return 0;
}
7-9 归并排序 (20 分)
#include<bits/stdc++.h>
#define eb emplace_back
#define PSI pair<string,int>
const int N = 2e5 + 10;
#define x first
#define y second
using namespace std;
int a[N];
void solve(){
int n;
cin>>n;
for(int i=0;i<n;i++) cin>>a[i];
sort(a,a+n);
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
int main(){
solve();
return 0;
}
7-10 逆序对的数量 (20 分)
#include<bits/stdc++.h>
#define ll long long
const int N = 1e5 + 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);
}
}
while(pos1<=r1) mp[pos++]=dp[pos1++];
while(pos2<=r2) mp[pos++]=dp[pos2++];
for(int i=l1;i<=r2;i++) dp[i]=mp[i-l1];
}
void Merge_sort(int l,int r){
if(l<r){
int mid=l+r>>1;
Merge_sort(l,mid);
Merge_sort(mid+1,r);
Merge(l,mid,mid+1,r);
}
}
void solve(){
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);
}
int main(){
solve();
return 0;
}
|