Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

卞秋阳 #867

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions winter-day5/卞秋阳/7-4 二分查找.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <bits/stdc++.h>
using namespace std;
int n,a[10086],x,zl=0,ans=0;
int main()
{ cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
cin>>x;
int l=0,r=n-1;
while(l<=r){
int mid=(l+r)/2;
ans++;
if(x==a[mid]){
cout<<mid<<endl;
zl=1;
break;
}
if(x>a[mid]){l=mid+1;}
if(x<a[mid]){r=mid-1;}
}
if(zl==1)
{cout<<ans;}
if(zl==0)
{
cout<<"-1"<<endl;
cout<<ans;
}
return 0;
}
40 changes: 40 additions & 0 deletions winter-day5/卞秋阳/day5题解.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
**7-1 相邻数对**

先sort,再比较比纯暴力优化了些

**7-3 两个有序序列的中位数**

直接数组模拟就好了,无脑做

##### 二分查找

两道题做法类似放在一起。

模板题。

核心代码:

int l=0,r=n-1; while(l<=r) { int mid=(l+r)/2; ans++; if(x==a[mid]) { cout<<mid<<endl; pd=1; break; } if(x>a[mid]) { l=mid+1; } if(x<a[mid]) { r=mid-1; } }

**7-5 冰岛人**

采用vector<vector>+map对string进行映射+并查集寻找公共祖先处理。

储存数据:

struct people { int sex; int id; }; map<string,people>mp1;

并查集:

int find(int x) { int rt = fa[x]; if (fa[x] != x) rt = find(fa[x]); return rt; }

判断是否为五代以内:

bool pd(int x,int y) { int cnt=1; while(x!=fa[x]&&y!=fa[y]) { x=fa[x]; y=fa[y]; ++cnt; if(cnt==5) { // cout<<"cnt"<<cnt<<endl; return 1; } if(x==y) { // cout<<"cnt"<<cnt<<endl; return 0; } } return 0; }

**7-7 愤怒的牛**

先sort排序,后二分答案:



17 changes: 17 additions & 0 deletions winter-day5/卞秋阳/两个有序序列的中位数.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include<bits/stdc++.h>
using namespace std;
int a[100010];
int sum=0,n;
int main()
{
cin>>n;
for(int i=1;i<=n*2;i++)
{
cin>>a[i];
}
sort(a+1,a+1+n+n);
cout<<a[n];



}
46 changes: 46 additions & 0 deletions winter-day5/卞秋阳/二分查找 (2).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include<bits/stdc++.h>
using namespace std;
int c,d,flag=0,g;
int a[110],b[110];
int main()
{
while(cin>>c)
{


for(int i=0;i<c;i++)
{
cin>>a[i];
}
cin>>d;
for(int i=0;i<d;i++)
{
cin>>b[i];
}
sort(a,a+c);
for(int i=0;i<c-1;i++){
cout<<a[i]<<" ";
}
cout<<a[c-1]<<endl;

for(int i=0;i<d;i++)
{
for(int j=0;j<c;j++)
{

if(b[i]==a[j])
{ if(i!=0) cout<<" ";
cout<<j+1;flag++;
}

}
if(flag==0&&i==0) cout<<0;
if(flag==0&&i!=0) cout<<" "<<0;
flag=0;


}
cout<<endl;
}

}
96 changes: 96 additions & 0 deletions winter-day5/卞秋阳/冰岛人.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <bits/stdc++.h>
using namespace std;
int fa[100001];
int n,m;
string s1,s2,s3,s4;
struct people
{
int sex;
int id;
};
map<string,people>mp1;
int find(int x);
bool pd(int x,int y)
{
int cnt=1;
while(x!=fa[x]&&y!=fa[y])
{
x=fa[x];
y=fa[y];
++cnt;
if(cnt==5)
{

return 1;
}
if(x==y)
{

return 0;
}
}
return 0;
}

int main()
{
cin>>n;
for(int i=1;i<=100005;++i)
{
fa[i]=i;
}
vector< vector<string> >a(n);
int q=0;
for(int i=0;i<n;++i)
{
cin>>s1>>s2;
mp1[s1].id=++q;
a[i].push_back(s1);
a[i].push_back(s2);
}
for(int i=0;i<n;++i)
{
s1=a[i][0];
s2=a[i][1];
if(s2.size()>4&&s2.substr(s2.size()-4,4)=="sson")
{
mp1[s1].sex=1;
fa[mp1[s1].id]=mp1[s2.substr(0,s2.size()-4)].id;
}
if(s2.size()>7&&s2.substr(s2.size()-7,7)=="sdottir")
{
mp1[s1].sex=0;
fa[mp1[s1].id]=mp1[s2.substr(0,s2.size()-7)].id;
}
if(s2[s2.size()-1]=='m')
{
mp1[s1].sex=1;
}
if(s2[s2.size()-1]=='f')
{
mp1[s1].sex=0;
}
}
cin>>m;
for(int i=1;i<=m;i++)
{
cin>>s1>>s2>>s3>>s4;
if (!mp1[s1].id||!mp1[s3].id)
cout<<"NA"<<endl;
else if(mp1[s1].sex==mp1[s3].sex)
cout<<"Whatever"<<endl;
else if(find(mp1[s1].id)!=find(mp1[s3].id))
cout<<"Yes"<<endl;
else if(pd(mp1[s1].id,mp1[s3].id))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}
int find(int x)
{
int rt = fa[x];
if (fa[x] != x) rt = find(fa[x]);
return rt;
}
44 changes: 44 additions & 0 deletions winter-day5/卞秋阳/愤怒的牛.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int t,n,c,a[100001];
int pd(int x)
{
int now=a[1];
int pos=1;
for(int i=2;i<=n;i++)
{
if(a[i]-now>=x)
{
now=a[i];
pos++;
}
if(pos>=c)
return 1;
}
return 0;
}
int main()
{
scanf("%d%d", &n,&c);
for(int i=1;i<=n;i++)
{
scanf("%d", &a[i]);
}
sort(a+1,a+n+1);
int l=1;
int r=a[n];
while(l<=r)
{
int mid=(l+r)/2;
if(pd(mid)==1)
{
l=mid+1;
}
else
r=mid-1;
}
cout<<r<<endl;
return 0;
}
21 changes: 21 additions & 0 deletions winter-day5/卞秋阳/相邻数对.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include<bits/stdc++.h>
using namespace std;
int a[1010];
int sum=0,n;
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
sort(a+1,a+1+n);
for(int i=0;i<n;i++)
{
if(a[i+1]-a[i]==1) sum++;
}
cout<<sum;



}
25 changes: 25 additions & 0 deletions winter-day6/卞秋阳/Day6题解.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## **Day6题解**

**7-1 直接插入排序**

就是最基本的模板,在每次循环中加一次输出就可

**7-2 成绩排序**

结构体加sort函数可秒杀

**7-3 统计工龄**

木桶排序,比较典型的还有投选票那种

**7-4 最接近神的人**

树状数组

**7-5 堆排序**

先把最大堆建好,在拿出来

**7-6 快速排序**

水了水了hhh
36 changes: 36 additions & 0 deletions winter-day6/卞秋阳/堆排序.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
int n;
int a[maxn]={};
void adjust(int a[],int s,int m);
void heapsort(int a[],int n)
{
for(int i=n/2;i>=1;i--) adjust(a,i,n);
for (int i = 1; i <= n; i++) cout<<a[i]<<" ";
cout<<endl;
for(int i=1;i<=n-1;i++){
swap(a[1],a[n+1-i]);
adjust(a,1,n-i);
for(int j=1;j<=n;j++) cout<<a[j]<<" ";
cout<<endl;
}
}

int main()
{ cin >> n;
for (int i = 1; i <=n; i++) cin>>a[i];
heapsort(a,n);
return 0;
}
void adjust(int a[],int s,int m)
{
int t=a[s];
for(int j=2*s;j<=m;j*=2){
if(j<m&&a[j]<a[j+1]) j++;
if(t>=a[j]) break;
a[s]=a[j];
s=j;
}
a[s]=t;
}
27 changes: 27 additions & 0 deletions winter-day6/卞秋阳/成绩排序.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e4+5;
int n;
struct stu
{ string name;
int mark;
}a[maxn];
bool cmp1(stu a, stu b);
bool cmp2(stu a, stu b);
int main()
{
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i].name >> a[i].mark;
sort(a, a + n, cmp1);
sort(a, a + n, cmp2);
for (int i = 0; i < n; i++) cout<< a[i].name <<" "<< a[i].mark<<endl;
return 0;
}
bool cmp1(stu a, stu b)
{
return a.mark > b.mark;
}
bool cmp2(stu a, stu b)
{
return a.mark == b.mark && a.name < b.name;
}
Loading