CSP-J模拟赛2总结
得分:200
排名:10/13
优势算法:搜索
劣势算法:动态规划
T1 恰饭(meal)
考察范围:c++基本语法
考场得分:100
题目难度:入门
考场上看到这道题就直接写了这个代码,然后AC了。 code:
#include<bits/stdc++.h>
using namespace std;
int main(){
freopen("meal.in","r",stdin);
freopen("meal.out","w",stdout);
int ma=1e9,mb=1e9;
for(int i=1;i<=4;i++){
int x;cin>>x;
ma=min(ma,x);
}
for(int i=1;i<=3;i++){
int x;cin>>x;
mb=min(mb,x);
}
cout<<ma+mb;
return 0;
}T2 卡片(card)
考察范围:搜索
考场得分:100
题目难度:普及
当我看到了“对于 100% 的数据,有 1 ≤ n ≤ 10,1 ≤ k ≤ 4,1 ≤ ai ≤ 99。”之后就开始打了一个暴力搜索。 我们可以枚举所有能构成的数字(字符串),然后用一个Map或者bool数组记录是否出现过。 code:
#include<bits/stdc++.h>
using namespace std;
int n,k;
bool f[100000005];
string s[105];
int turn(string s){
int ret=0;
for(int i=0;i<s.size();i++){
ret*=10;
ret+=(s[i]-'0');
}
return ret;
}
int ans;
bool vis[105];
void dfs(int step,string str,int cnt){
if(cnt+(n-step)<k)return;
if(cnt==k){
int t=turn(str);
if(!f[t]){
f[t]=1;
ans++;
}
return;
}
if(step==n)return;
for(int i=1;i<=n;i++){
if(vis[i])continue;
vis[i]=1;
dfs(step+1,str+s[i],cnt+1);
vis[i]=0;
}
dfs(step+1,str,cnt);
}
int main(){
freopen("card.in","r",stdin);
freopen("card.out","w",stdout);
cin>>n>>k;
for(int i=1;i<=n;i++)cin>>s[i];
dfs(0,"",0);
cout<<ans;
return 0;
}T3 数数(count)
考察范围:动态规划
考场得分:0
题目难度:普及+
考场上尝试写了一些dp,先想到了第一种方法:对于新加入的节点i,枚举一个j将i接在后面,使用线段树维护j前面接的节点的向左/右的方向上的个数,也就是类似于题解中的思路4。 但是在考场上我并没有写出这个代码,所以没有提交(即没有错误code)。 正确的思路的确是dp,用两个数组记录向左和向右的个数,进行转移,详细见代码中的注释。 code:
#include<bits/stdc++.h>
using namespace std;
int n,dp1[6005],dp2[6005],ans;
struct node{
int x,y;
inline bool operator<(const node &p)const{
return x<p.x;//按照x从小到大排序,是因为方便之后计算的时候左边都已知
}
}s[6005];
const int mod=1000000007;
int main(){
freopen("count.in","r",stdin);
freopen("count.out","w",stdout);
cin>>n;
for(int i=1;i<=n;i++)cin>>s[i].x>>s[i].y;
sort(s+1,s+n+1);
for(int i=1;i<=n;i++){
dp1[i]=dp2[i]=1;//向左边和向右边的个数
for(int j=i-1;j>=1;j--){//枚举已知的(即左边的)
//接下来两行用现在正在计算的i和已知的j进行互相推出和完善,用下面的更新上面的
if(s[j].y>s[i].y)dp2[j]=(dp2[j]+dp1[i])%mod;
else dp1[i]=(dp1[i]+dp2[j])%mod;
}
}
for(int i=1;i<=n;i++)ans=((ans+dp1[i])%mod+dp2[i])%mod;//用cnt统计和
ans=(ans-n+mod)%mod;//在18行的时候对于所有的点都加了2,所以现在所有的点都-1
cout<<ans;
return 0;
}T4 划分(partition)
在考场上由于死磕T3没有时间看题。 考后重新和黄思瀚一起合作完成了代码(不借助题解)。 这是卡了我们很久的数据: 数据:
1
8
2 8
1 6
8 7
5 6
3 4
7 4
4 6
611649 402086 402086 402086 854444 154486 583163 104441
154486 402086 611649 402086 854444 402086 104441 583163图:
我们的思路是不断从叶子节点向根寻找使得符合题意,叶子节点的集合使用queue维护,其中是否加入队列取决于维护的数组cntt,用于记录儿子节点的个数。
为了向根节点跳,我们使用dfs1记录深度,在dfs3时只允许向深度浅的节点递归。
其中递归时判断节点是否已经其他链占用或者是否成功,用来判断输出。
code: (by Believe_in_Dreams & HuangSihan)
#include<bits/stdc++.h>
using namespace std;
const int N=500005;
queue<int> q;
bool vis[N];
int problem,all,T,n,mb,x[N],y[N],dp[N],fat[N],cntt[N];
int head[N],ver[N],ne[N],tot;
void add(int x,int y){
ver[++tot]=y;
ne[tot]=head[x];
head[x]=tot;
}
inline int read(){
int ans=0,j=1;
char c=getchar();
while(c>'9' or c<'0'){
if(c=='-')j=-1;
c=getchar();
}
while(c>='0' and c<='9'){
ans=ans*10+c-'0';
c=getchar();
}
return ans*j;
}
void dfs1(int now,int father,int dep){
dp[now]=dep;
for(int i=head[now];i;i=ne[i]){
int y=ver[i];
if(y==father)continue;
cntt[now]++;
fat[y]=now;
dfs1(y,now,dep+1);
}
}
void dfs3(int now,int cnt,int op1,int op2){
if(problem){
return;
}
if(vis[now]){
problem=1;
return;
}
vis[now]=1;
cntt[fat[now]]--;
if(cntt[fat[now]]==0)q.push(fat[now]);
if(op1==op2){
problem=2;
all-=cnt;
return;
}
for(int i=head[now];i;i=ne[i]){
int w=ver[i];
if(dp[w]>=dp[now])continue;
dfs3(w,cnt+1,op1+x[w],op2+y[w]);
}
}
void solve(){
while(q.size())q.pop();
n=read();
tot=0;all=n;
memset(dp,0,sizeof dp);
memset(head,0,sizeof head);
memset(ver,0,sizeof ver);
memset(ne,0,sizeof ne);
memset(vis,0,sizeof vis);
memset(fat,0,sizeof fat);
memset(cntt,0,sizeof cntt);
for(int i=1;i<n;i++){
int a=read(),b=read();
add(a,b);add(b,a);
}
for(int i=1;i<=n;i++)x[i]=read();
for(int i=1;i<=n;i++)y[i]=read();
dfs1(1,0,1);
for(int i=1;i<=n;i++)if(cntt[i]==0)q.push(i);
while(q.size()){
int t=q.front();
q.pop();
if(vis[t])continue;
if(all<=0)break;
problem=0;
dfs3(t,1,x[t],y[t]);
if(problem!=2)break;
}
if(problem!=2 or all>0 or q.size())cout<<"No\n";
else cout<<"Yes\n";
}
int main(){
freopen("partition.in","r",stdin);
freopen("partition.out","w",stdout);
cin>>T;
while(T--)solve();
return 0;
}