DAY1
航班时间
这是一道printf和scanf的题目
#include<iostream>
using namespace std;
int get_time()
{
int h1,h2,m1,m2,s1,s2,d=0;
scanf("%d:%d:%d %d:%d:%d (+%d)",&h1,&m1,&s1,&h2,&m2,&s2,&d);
int time=d*24*3600+h2*3600+m2*60+s2-h1*3600-m1*60-s1;
return time;
}
int main()
{
int t;cin>>t;
while(t--)
{
int time1=get_time();
int time2=get_time();
int time=(time1+time2)/2;
printf("%02d:%02d:%02d\n",time/3600,time/60%60,time%60);
}
}
全球变暖
#include<iostream>
using namespace std;
const int N=1010;
int n,flag,ans;
char g[N][N];
bool st[N][N];
int d[4][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}}; //四个方向
void dfs(int i,int j)
{
st[i][j]=1;
if(g[i-1][j]=='#'&&g[i][j-1]=='#'&&g[i][j+1]=='#'&&g[i+1][j]=='#')
flag=1;
for(int k=0;k<4;k++)
{
int nx=i+d[k][0],ny=j+d[k][1];
if(g[nx][ny]=='#'&&!st[nx][ny])
dfs(nx,ny);
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cin>>g[i][j];
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(!st[i][j]&&g[i][j]=='#')
{
flag=0;
dfs(i,j);
if(!flag)
ans++;
}
}
cout<<ans<<endl;
}
DAY2
区间选点
注意看数据范围
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int N=1e5+10;
#define pii pair<int,int>
vector<pii>segs;
bool cmp(pii a,pii b)
{
return a.second<b.second;
}
int main()
{
int n;cin>>n;
while(n--)
{
int l,r;cin>>l>>r;
segs.push_back({l,r});
}
int ed=-1e10,st=-1e9;
int ans=0;
sort(segs.begin(),segs.end(),cmp);
for(auto &seg:segs)
{
if(ed<seg.first)
{
ed=seg.second;
ans++;
}
}
cout<<ans<<endl;
}
合并果实(哈夫曼树)
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define ll long long
priority_queue<int,vector<int>,greater<int>>heap;
int main()
{
int n;cin>>n;
while(n--)
{
int x;cin>>x;
heap.push(x);
}
ll sum=0;
while(heap.size()>1)
{
ll a=heap.top();heap.pop();
ll b=heap.top();heap.pop();
sum+=a+b;
heap.push(a+b);
}
cout<<sum<<endl;;
}
DAY3
正则问题
#include<iostream>
#include<stack>
#include<string>
using namespace std;
stack<char>st;
void count()
{
int cnt=0,maxn=0;
while(!st.empty()&&st.top()!='(')
{
char ch=st.top();st.pop();
if(ch=='x')
cnt++;
else
{
maxn=max(cnt,maxn);
cnt=0;
}
}
maxn=max(cnt,maxn);
if(st.size())st.pop();
for(int j=0;j<maxn;j++)
st.push('x');
}
int main()
{
string a;
cin>>a;
for(int i=0;i<a.size();i++)
{
char ch=a[i];
if(ch!=')')
st.push(ch);
else
count();
}
count();
cout<<st.size()<<endl;
}
完全二叉树
完全二叉树的最后一行不一定是满枝
#include<iostream>
using namespace std;
#define ll long long
const int N=1e5+10;
int a[N];
int main()
{
int n;cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
ll ans,sum=0,tmp=-1e17,depth=1;
for(int i=1;i<=n;i*=2)
{
for(int j=i;j<=2*i-1&&j<=n;j++)
sum+=a[j];
if(sum>tmp)
{
ans=depth;
tmp=sum;
}
depth++;
sum=0;
}
cout<<ans<<endl;
}