月色的糖果询问
题意分析
每次询问都给N个糖果,每个糖果有不同的糖分,每次给出的糖果有Q次询问,询问的东西就是糖分,加起来的糖分大于问的糖分,输出糖果最少的数量
正确思路
这一题因为是求最少,所以可以枚举出最小值,每次一个一个加上,但是这样子会TLE,我们只能用一个前缀和数组记录最大的前缀,然后再找出前缀和的对于那个糖分最合适的数量
正确代码
C++ 复制 #include <bits/stdc++.h>
// #define int long long
using namespace std ;
int a[150005 ],t,n,q,b[150005 ];
int main (){
// ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>> t;
while (t-- ){
cin>> n>> q;
for (int i= 1 ;i<= n;i++ ){
cin>> a[i];
}
sort (a+ 1 ,a+ 1 + n,greater <int >());
for (int i= 1 ;i<= n;i++ ){b[i]= b[i- 1 ]+ a[i];}//前缀和
while (q-- ){
int x;
cin>> x;
int ans= lower_bound (b+ 1 ,b+ n+ 1 ,x)- b;//二分答案
if (ans<= n){
cout<< ans<< ' \n ' ;
}else {
cout<<- 1 << ' \n ' ;
}
}
}
return 0 ;
}
Nova Poshta
题意分析
有两台货车,可以运不同的吨数,有5个货物,每个货物有不同的重量,分四种输出,如果两个人合伙都不能把东西运走,那就输出They cannot do it. 如果两个人都可以独自完成订单,输出They both can do it. 如果只有其中一个人可以完成,另一个人不能完成,就输出他们其中一个人,如果订单需要他们合作完成,就输出他们分别需要运货的编号
正确思路
先判前面两种,然后就进行DFS选和不选,是给这第一个人还是给第二个人,如果条件允许的话,就输出选的编号
正确代码
C++ 复制 #include <bits/stdc++.h>
// #define int long long
using namespace std ;
int v,p,m[10 ],ans1[10000 ],ans2[10000 ];
void dfs (int x ,int sum1 ,int sum2 ,int cnt1 ,int cnt2 ){
if (x== 6 ){
if (sum1<= v && sum2<= p){//符合条件
cout<< "They need to work together!" << ' \n ' ;
cout<< "Vasyl: " ;
for (int i= 1 ;i<= cnt1;i++ ){
cout<< ans1[i]<< ' ' ;
}
cout<< ' \n ' ;
cout<< "Petro: " ;
for (int i= 1 ;i<= cnt2;i++ ){
cout<< ans2[i]<< ' ' ;
}
exit (0 );
}
return ;
}
ans1[cnt1+ 1 ]= x;
dfs (x+ 1 ,sum1+ m[x],sum2,cnt1+ 1 ,cnt2);//选和不选
ans2[cnt2+ 1 ]= x;
dfs (x+ 1 ,sum1,sum2+ m[x],cnt1,cnt2+ 1 );
}
int main (){
// ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>> m[1 ]>> m[2 ]>> m[3 ]>> m[4 ]>> m[5 ];
cin>> v>> p;
int ans= m[1 ]+ m[2 ]+ m[3 ]+ m[4 ]+ m[5 ];
if (ans<= v && ans<= p){//先判前三种
cout<< "They both can do it!" ;
return 0 ;
}else if (ans<= v && ans> p){
cout<< "Vasyl can do it!" ;
return 0 ;
}else if (ans> v && ans<= p){
cout<< "Petro can do it!" ;
return 0 ;
}else {
dfs (1 ,0 ,0 ,0 ,0 );
}
cout<< "They can not do it!" ;
return 0 ;
}
月色的离开路线
题意分析
给一个N×M的方格,有点和井号,井号不能穿过,点是可以走的,E是出口,求每一个点到出口的最短距离,需要给出路线
正确思路
因为从每一个点到出口DFS肯定会TLE,所以可以反着来,从出口回到每一个点,到每一个点就倒着把自己过来的方向记录下来,最后输出那个方向数组
正确代码
C++ 复制 #include <bits/stdc++.h>
#define int long long
using namespace std ;
int n,m;
char a[1005 ][1005 ],b[1005 ][1005 ];
struct P {
int x,y;
};
queue< P> q;
int vis[1005 ][1005 ];
int d[4 ][2 ]= {
{1 ,0 },{- 1 ,0 },{0 ,1 },{0 ,- 1 }
};
char d1[4 ]= {'^' ,'v' ,'<' ,'>' };//反着记录
void bfs (){
while (q.size ()){
P now= q.front ();
q.pop ();
int x= now.x,y= now.y;
for (int i= 0 ;i< 4 ;i++ ){
int dx= x+ d[i][0 ],dy= y+ d[i][1 ];
if (dx< 1 || dx> n || dy< 1 || dy> m || vis[dx][dy]== 1 || a[dx][dy]== '#' ){
continue ;
}
vis[dx][dy]= 1 ;
q.push ({dx,dy});
b[dx][dy]= d1[i];
}
}
}
signed main (){
ios ::sync_with_stdio (false ),cin.tie (0 ),cout.tie (0 );
cin>> n>> m;
for (int i= 1 ;i<= n;i++ ){
for (int j= 1 ;j<= m;j++ ){
cin>> a[i][j];
b[i][j]= a[i][j];
if (a[i][j]== 'E' ){//多起点
q.push ({i,j});
vis[i][j]= 1 ;
}
}
}
bfs ();
for (int i= 1 ;i<= n;i++ ){
for (int j= 1 ;j<= m;j++ ){
cout<< b[i][j];
}
cout<< ' \n ' ;
}
return 0 ;
}