这两天几个比赛都在一起,谁顶的住啊,打(签到)了6场,结果还有没有中奖,太真实了ヽ(`Д´)ノ︵ ┻━┻ ┻━┻
A-串串
https://ac.nowcoder.com/acm/contest/558/A
思路:除了暴力,没思路。。。
B-重复
https://ac.nowcoder.com/acm/contest/558/B
思路:签到,用map去重即可。。
#include<iostream>
#include<map>
using namespace std;
typedef long long LL;
int n,m;
map<string,int> imap;
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m;
string str;
for(int i=0;i<n;++i)
{
cin>>str;
imap[str]=1;
}
cout<<imap.size()<<endl;
return 0;
}
C-二元
https://ac.nowcoder.com/acm/contest/558/C
思路:优先队列,遍历a_i的最小值,再从中找最大b_i.
先将(a_i,b_i)按照a_i由小到大排序,在将前k大a_i对应的b_i加入优先队列Q(小的先出队),这样遍历a_i时,将b_i加入队列Q,再出队列将最小的b_i淘汰,更新ans即可
Code:
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
typedef pair<int,int> pr;
const int MAX_N=1e5+5;
int n,m;
pr a[MAX_N];
priority_queue<int,vector<int>,greater<int> > Q;
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=0;i<n;++i)
cin>>a[i].first>>a[i].second;
sort(a,a+n);
int r=n-1,M1,M2=a[n-1].second;
while(r>=0&&n-r<=m){
Q.push(a[r].second);
M1=a[r].first; M2=min(M2,a[r].second);
--r;
}
int ans=M1+M2;
while(r>=0){
Q.push(a[r].second); Q.pop();
ans=max(ans,a[r].first+Q.top());
--r;
}
cout<<ans<<endl;
return 0;
}
D-寻找
不会-.-
E-区间
https://ac.nowcoder.com/acm/contest/558/E
链接:https://ac.nowcoder.com/acm/contest/558/E
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
小猫在研究序列。
小猫在研究单调性。
给定一个长度为N的序列a1,a2,…,aN,请你选出一个最长的区间[l,r](1≤l≤r≤N),满足al≤al+1≤…≤ar。
如果有多个,请输出l最小的。
输入描述:
第一行一个正整数T,表示数据组数。 每组数据的第一行一个正整数N。 接下来一行N个正整数a1,a2,…,aN。
输出描述:
T行,每行两个正整数l,r,表示选出的区间。
示例1
输入
复制
4 5 1 2 3 4 5 5 5 4 3 2 1 5 5 3 4 1 2 5 3 4 5 1 2
输出
复制
1 5 1 1 2 3 1 3
备注:
1≤T,N,ai≤1000
思路:按题目要求遍历一遍即可
Code:
#include<iostream>
using namespace std;
typedef long long LL;
const int MAX_N=1e3+5;
int n,T;
int dp[MAX_N];
int main()
{
ios::sync_with_stdio(false);
cin>>T;
while(T--){
cin>>n;
int Max=0,Sum=1,p=n,pre=-1;
for(int i=1,x;i<=n;++i)
{
cin>>x;
if(pre!=-1){
if(x>=pre){
++Sum;
}else{
if(Sum>Max){
Max=Sum;
p=i-1;
}
Sum=1;
}
}
pre=x;
}
if(Max<Sum){
Max=Sum;
p=n;
}
cout<<p-Max+1<<" "<<p<<endl;
}
return 0;
}
F-点对
https://ac.nowcoder.com/acm/contest/558/F
思路:Flody..
Code :
#include<iostream>
using namespace std;
typedef long long LL;
const int MAX_N=1e3+5;
int n,m;
int G[MAX_N][MAX_N];
void Flody();
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m;
int u,v;
for(int i=1;i<=m;++i)
{
cin>>u>>v;
G[u][v]=1;
}
Flody();
int ans=0;
for(int i=1;i<=n;++i)
for(int j=i+1;j<=n;++j)
if(G[i][j]&&G[j][i]) ++ans;
cout<<ans<<endl;
return 0;
}
void Flody()
{
for(int k=1;k<=n;++k)
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
if(G[i][k]&&G[k][j]) G[i][j]=1;
}
G-路径
https://ac.nowcoder.com/acm/contest/558/G
思路:树形dp,输入u,v,w的v我一直默认为根节点,结果坑了我无数遍,一直想不通哪里错了。。。
M1[u],M2[u]分别记录以u为树节点的边数分别为奇数和偶数的最大价值
ans=max{M1[u]+M2[v]+w,M2[u]+M1[v]+w};
Code:
#include<iostream>
#include<vector>
using namespace std;
typedef long long LL;
typedef pair<int,LL> pr;
const int MAX_N=1e5+5;
int n;
LL ans;
LL M1[MAX_N],M2[MAX_N];
vector<pr> e[MAX_N];
void DFS(int u,int pre);
int main()
{
ios::sync_with_stdio(false);
cin>>n;
int u,v,w;
for(int i=1;i<n;++i)
{
//Warning: v点不一定为子节点
cin>>u>>v>>w;
e[u].push_back({v,w});
e[v].push_back({u,w});
}
DFS(1,0);
cout<<ans<<endl;
return 0;
}
void DFS(int u,int pre)
{
M1[u]=-1e16; M2[u]=0;
int v,w;
for(auto c:e[u])
if(c.first!=pre){
v=c.first; w=c.second;
DFS(v,u);
ans=max(ans,M1[u]+M2[v]+w);
ans=max(ans,M2[u]+M1[v]+w);
M1[u]=max(M1[u],M2[v]+w);
M2[u]=max(M2[u],M1[v]+w);
}
}
H-分离
https://ac.nowcoder.com/acm/contest/558/H
思路:签到题。
Code:
#include<iostream>
#include<map>
using namespace std;
typedef long long LL;
int n,m;
int main()
{
ios::sync_with_stdio(false);
cin>>n;
string str,ss;
for(int i=0;i<n;++i)
{
cin>>str;
m=str.length();
ss="";
for(int i=0;i<m;i+=2)
ss+=str[i];
cout<<ss<<endl;
}
return 0;
}
I-选择
https://ac.nowcoder.com/acm/contest/558/I
思路:简单DP,对当前x有选和不选则,很明显dp[i]=max(dp[i-1],dp[i-2]+x);
Code:
#include<iostream>
#include<cstring>
using namespace std;
const int MAX_N=105;
int n,T;
int dp[MAX_N];
int main()
{
ios::sync_with_stdio(false);
cin>>T;
while(T--){
memset(dp,0,sizeof(dp));
cin>>n;
for(int i=2,x;i<=n+1;++i)
{
cin>>x;
dp[i]=max(dp[i-1],dp[i-2]+x);
}
cout<<dp[n+1]<<endl;
}
return 0;
}
J-相聚
https://ac.nowcoder.com/acm/contest/558/J
思路:基础的DFS
Code:
#include<iostream>
#include<cstring>
using namespace std;
typedef long long LL;
const int MAX_N=1e2+5;
const int MAX_M=1e2+5;
const int f[][2]={-1,0, 1,0, 0,-1, 0,1};
int n,m,T;
char G[MAX_N][MAX_M];
int boo[MAX_N][MAX_M];
void DFS(int x,int y);
int main()
{
ios::sync_with_stdio(false);
cin>>T;
while(T--){
memset(boo,0,sizeof(boo));
cin>>n>>m;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
cin>>G[i][j];
int ans=0;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
if(G[i][j]=='1'&&!boo[i][j]){
DFS(i,j); ++ans;
}
cout<<ans<<endl;
}
return 0;
}
void DFS(int x,int y)
{
int xi,yi;
for(int i=0;i<4;++i)
{
xi=x+f[i][0]; yi=y+f[i][1];
if(xi>=1&&xi<=n&&yi>=1&&yi<=m&&G[xi][yi]=='1'&&!boo[xi][yi]){
boo[xi][yi]=true;
DFS(xi,yi);
}
}
}