T1 T640711 快递费用
思路
算出比 多的部分,除以 向上取整,就可以算出超重了多少个 。
然后再根据各个地区用 算出结果。
注意:ceil() 返回浮点数,注意强转 int。
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int w,n;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>w>>n;
if(w<=500){
cout<<20;
return 0;
}
int c=ceil((w*1.0-500)/500);
if(n==1) cout<<20+c*4;
else if(n==2) cout<<20+c*6;
else if(n==3) cout<<20+c*9;
else if(n==4) cout<<20+c*10;
else if(n==5) cout<<20+c*17;
return 0;
}T2 T638889 石头布没有剪刀
思路
不知道是模拟还是贪心(可能一起上吧
就一个 for 循环,此时有条件出布(),那就出布,否则那就只能出布啊。
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
string s;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>s;
int n=s.size();
int x=0,y=0,sc=0; //石头 x,布 y
for(int i=0;i<n;i++){
if(s[i]=='g'){ //对面石头
if(x>=y+1){ //有条件出布
y++;
sc++;
}else{ //没条件出布,只能平局
x++;
}
}
else{ //对面布
if(x>=y+1){ //有条件出布,只能平局
y++;
}else{ //实在没办法了,输一把
x++;
sc--;
}
}
}
cout<<sc;
return 0;
}T3 P2895 [USACO08FEB] Meteor Shower S
思路
我也是写的 bfs 但是总有一些小细节,先来吃一下屎
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxx=5e4+10;
int m;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
bool vis[350][350];
int ma[350][350];
struct NODE{
int x,y,t;
};
bool cmp(NODE x,NODE y){
return x.t<y.t;
}
bool check(int xx,int yy,int t){
if(xx<0 || yy<0 || vis[xx][yy]==1 || (ma[xx][yy]<=t && ma[xx][yy]!=0)) return false;
return true;
}
NODE a[maxx];
void bfs(){
queue<NODE> q;
q.push({0,0,1}); //x,y,t
vis[0][0]=1;
while(!q.empty()){
NODE f=q.front();
q.pop();
if(ma[f.x][f.y]==0){
// cout<<f.x<<" "<<f.y<<" ";
cout<<f.t-1;
exit(0);
}
for(int i=0;i<4;i++){
int xx=f.x+dx[i];
int yy=f.y+dy[i];
// cout<<vis[xx][yy]<<" "<<ma[xx][yy]<<" "<<xx<<" "<<yy<<" "<<f.t<<endl;
if(check(xx,yy,f.t)){
// cout<<xx<<" "<<yy<<endl;
q.push({xx,yy,f.t+1});
vis[xx][yy]=1;
}
}
}
cout<<-1;
return;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>m;
for(int i=1;i<=m;i++){
cin>>a[i].x>>a[i].y>>a[i].t;
ma[a[i].x][a[i].y]=a[i].t;
ma[a[i].x-1][a[i].y]=a[i].t;
ma[a[i].x+1][a[i].y]=a[i].t;
ma[a[i].x][a[i].y-1]=a[i].t;
ma[a[i].x][a[i].y+1]=a[i].t;
}
bfs();
return 0;
}大差不差吧。。。首先就是输入的时候虽说是标记,但是不能是赋值,而是更新这个值,让我们标记的 所对应的 最小。
然后我们采用把没有流星和受它影响的点标记一个无限大,这样的话后面好比较,而且不容易出错。
还有一定要注意这个时间的处理,什么加一减一基本上都是错的,直接正常来,只是注意好哪里是 f.t+1 哪里直接 f.t。
代码
可能主函数有点累赘,又得吃屎了
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxx=5e4+10;
const int N=350;
int m;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
bool vis[N][N];
int ma[N][N];
struct NODE{
int x,y,t;
};
bool cmp(NODE x,NODE y){
return x.t<y.t;
}
bool check(int xx,int yy,int t){
if(xx<0 || yy<0 || xx>=N || yy>=N || vis[xx][yy] || ma[xx][yy]<=t) return false;
return true;
}
NODE a[maxx];
void bfs(){
queue<NODE> q;
q.push({0,0,0});
vis[0][0]=1;
while(!q.empty()){
NODE f=q.front();
q.pop();
if(ma[f.x][f.y]==maxx){
cout<<f.t;
return;
}
for(int i=0;i<4;i++){
int xx=f.x+dx[i];
int yy=f.y+dy[i];
if(check(xx,yy,f.t+1)){
q.push({xx,yy,f.t+1});
vis[xx][yy]=1;
}
}
}
cout<<-1;
return;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>m;
for(int i=0;i<N;i++) for(int j=0;j<N;j++) ma[i][j]=maxx;
for(int i=1;i<=m;i++){
cin>>a[i].x>>a[i].y>>a[i].t;
if(a[i].x>=0 && a[i].x<N && a[i].y>=0 && a[i].y<N) ma[a[i].x][a[i].y]=min(ma[a[i].x][a[i].y],a[i].t);
if(a[i].x-1>=0 && a[i].x-1<N && a[i].y>=0 && a[i].y<N) ma[a[i].x-1][a[i].y]=min(ma[a[i].x-1][a[i].y],a[i].t);
if(a[i].x+1>=0 && a[i].x+1<N && a[i].y>=0 && a[i].y<N) ma[a[i].x+1][a[i].y]=min(ma[a[i].x+1][a[i].y],a[i].t);
if(a[i].x>=0 && a[i].x<N && a[i].y-1>=0 && a[i].y-1<N) ma[a[i].x][a[i].y-1]=min(ma[a[i].x][a[i].y-1],a[i].t);
if(a[i].x>=0 && a[i].x<N && a[i].y+1>=0 && a[i].y+1<N) ma[a[i].x][a[i].y+1]=min(ma[a[i].x][a[i].y+1],a[i].t);
}
bfs();
return 0;
}T4 T644252 篝火问题
思路
可以暴力得 分,但是没时间了。暴力就是直接按照这个方向,一个一个的烟雾去挪动。
但是我们与其动这么多个的分散的雾,不如直接动地图。这样循环立刻就可以少一层。然后用 map 存储有烟的位置。
注意这里 和原点的距离一定不变,很容易就可以推算出来。
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
map<pair<int,int>,int> mp;
int n,r,x,y,c;
string s,ans;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>r>>c>>s;
mp[{0,0}]=1;
int len=s.size();
for(int i=0;i<len;i++){
if(s[i]=='N') x--;
if(s[i]=='S') x++;
if(s[i]=='E') y++;
if(s[i]=='W') y--;
mp[{-x,-y}]=1;
if(mp[{r-x,c-y}]) ans+='1';
else ans+='0';
}
cout<<ans;
return 0;
}