首页 > 代码库 > Summer training #7

Summer training #7

B:读懂题意模拟

技术分享
#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#define foror(i,a,b) for(i=a;i<b;i++)
#define foror2(i,a,b) for(i=a;i>b;i--)
#define EPS 1.0e-6
#define PI acos(-1.0)
#define INF 3000000000
#define MOD 1000000009
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
//using ll = long long;
//using ull= unsigned long long;
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
typedef long long ll;
int main()
{
 //freopen("in.txt", "r", stdin);
 //freopen("out.txt", "w", stdout);
 int n;
 int m1,m2,y1,y2;
 cin >> n;
 double ans;
 while(n--)
 {
   scanf("%d %d %d %d",&m1,&y1,&m2,&y2);
   //cout<< a << b << c << d<<endl;
   if(y1==y2)
        ans=(m2-m1)/2.0/(13-m1)+EPS;
   else
        ans=0.5+(y2-y1-1)+1.0/12*(m2-1)+EPS;
   printf("%.4lf\n",ans);
 }
  return 0;
}
View Code

C:给你一个起点和终点 每个点有补给但是只能拿一次 每条路有经过的cost 问你能不能到终点 能的话最少cost多少 DFS + 剪枝

(对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝.)

技术分享
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;
map<string, int> pos;
struct Edge {
    int v, c, next;
} edge[3000];
int head[3000], cnt;
int vis[3000], amm[3000], aim[3000],sum[3000];
int cs, n, m, ans;
string s, t;

void addedge (int u, int v, int c) {
    edge[++cnt].v = v, edge[cnt].c = c;
    edge[cnt].next = head[u];
    head[u] = cnt;
}
void dfs (int x, int s, int k) {
    if(k>=ans) return;
    if (aim[x]) {
        ans = min (ans, k);
        return ;
    }
    if (!vis[x]) s += amm[x], vis[x] = 1;
    if(vis[x]&&sum[x]>=s) return;
    sum[x]=max(sum[x],s);
    for (int i = head[x]; i; i = edge[i].next) {
        int v = edge[i].v, c = edge[i].c;
        if (s >= c)  dfs (v, s - c, k + c);
    }
    vis[x] = 0;
}
int main() {
    scanf ("%d", &cs);
    while (cs--) {
        pos.clear();
        memset (head, 0, sizeof head);
        memset(sum,0,sizeof sum);
        memset(vis,0,sizeof vis);
        cnt = 1, ans = 0x7fffffff;
        scanf ("%d %d", &n, &m);
        for (int i = 1; i <= n; i++) {
            cin >> s;
            pos[s] = i;
            cin >> amm[i] >> s;
            if (s[0] == ‘y‘) aim[i] = 1;
            else aim[i] = 0;
        }
        for (int i = 1, x; i <= m; i++) {
            cin >> s >> t >> x;
            int u = pos[s], v = pos[t];
            addedge (u, v, x);
            addedge (v, u, x);
        }
        dfs (1, 0, 0);
        if (ans != 0x7fffffff) printf ("%d\n", ans);
        else puts ("No safe path");
    }
}
View Code

D

E:如果有 就先去掉特殊的两个 然后DFS

技术分享
#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#define foror(i,a,b) for(i=a;i<b;i++)
#define foror2(i,a,b) for(i=a;i>b;i--)
#define EPS 1.0e-6
#define PI acos(-1.0)
#define INF 3000000000
#define MOD 1000000009
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
//using ll = long long;
//using ull= unsigned long long;
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
typedef long long ll;
int num[10];
int flag=0;
void dfs(int number)
{
 if(number==4||flag==1)
 {
   flag=1;
   return ;
 }
 for(int i=1;i<=7;i++)
 {
        if(num[i]>=1&&num[i+1]>=1&&num[i+2]>=1)
        {
        num[i]-=1;
        num[i+1]-=1;
        num[i+2]-=1;
        dfs(number+1);
        num[i]+=1;
        num[i+1]+=1;
        num[i+2]+=1;
        }
 }
 for(int i=1;i<=9;i++)
 {
        if(num[i]>=3)
        {
        num[i]-=3;
        dfs(number+1);
        num[i]+=3;
        }
 }
}
void init()
{
        for(int i=1;i<=9;i++)
        {
        if(num[i]<2)
        continue;
        num[i]-=2;
        dfs(0);
        num[i]+=2;
        if(flag==1)
        break;
        }
}
int main()
{
 //freopen("in.txt", "r", stdin);
 //freopen("out.txt", "w", stdout);
 int n;
 cin >> n;
 while(n--)
 {
        flag=0;
        mem(num,0);
        int a[20];
        for(int i=1;i<=14;i++)
        {
        scanf("%d",&a[i]);
        num[a[i]]++;
        }
        init();
       if(flag==1)
        printf("Vulnerable\n");
       else
        printf("Immune\n");
 }
  return 0;
}
View Code

F:BFS 注意!!一次outbreak只能在BFS中的每个点发生一次

技术分享
#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#define foror(i,a,b) for(i=a;i<b;i++)
#define foror2(i,a,b) for(i=a;i>b;i--)
#define EPS 1.0e-6
#define PI acos(-1.0)
#define INF 3000000000
#define MOD 1000000009
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
//using ll = long long;
//using ull= unsigned long long;
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
typedef long long ll;
char s[110][110];
char visit[110][110];
queue<pair<int,int> > que;
int dir[4][2];
int n,m;
void bfs()
{
 for(int i=0;i<=101;i++)
 mem(visit[i],0);
 pair<int,int> a;
 while(!que.empty())
 {
        a=que.front();
        que.pop();
        int r=a.first;
        int c=a.second;
        if(s[r][c]==D)
        {
        for(int i=0;i<4;i++)
        {
        int r1=r+dir[i][0];
        int c1=c+dir[i][1];
        if(r1>=0&&r1<n&&c1>=0&&c1<m&&s[r1][c1]!=X)
        {
        if(s[r1][c1]==D&&!visit[r1][c1])
        {
        pair<int,int> b;
        b.first=r1,b.second=c1;
        que.push(b);
        visit[r1][c1]=1;
        }
        else if(s[r1][c1]>=A&&s[r1][c1]<D)
        {
        s[r1][c1]=s[r1][c1]+1;
        }
        }
        }
        visit[r][c]=1;
        }
        else if(s[r][c]>=A&&s[r][c]<D)
        {
        s[r][c]=s[r][c]+1;
        }
 }
}
int main()
{
 //freopen("in.txt", "r", stdin);
 //freopen("out.txt", "w", stdout);
dir[0][0]=-1,dir[0][1]=0;
dir[1][0]=1,dir[1][1]=0;
dir[2][0]=0,dir[2][1]=1;
dir[3][0]=0,dir[3][1]=-1;
 int t;
 cin >> t;
 while(t--)
 {
        for(int i=0;i<=101;i++)
        mem(visit[i],0);
        int r,c;
        scanf("%d %d",&m,&n);
        for(int i=0;i<n;i++)
        scanf("%s",s[i]);
        int time;
        scanf("%d",&time);
        while(time--)
        {
        scanf("%d %d",&c,&r);
        pair<int,int> a;
        a.first=r,a.second=c;
        que.push(a);
        bfs();
        /*for(int i=0;i<n;i++)
        {
                for(int j=0;j<m;j++)
                printf("%c",s[i][j]);
                printf("\n");
        }
        cout<<endl;*/
        }
        for(int i=0;i<n;i++)
        {
                for(int j=0;j<m;j++)
                printf("%c",s[i][j]);
                printf("\n");
        }
 }
  return 0;
}
View Code

G:水

技术分享
#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#define foror(i,a,b) for(i=a;i<b;i++)
#define foror2(i,a,b) for(i=a;i>b;i--)
#define EPS 1.0e-8
#define PI acos(-1.0)
#define INF 3000000000
#define MOD 1000000009
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
//using ll = long long;
//using ull= unsigned long long;
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
typedef long long ll;
int a[25];
int main()
{
 //freopen("in.txt", "r", stdin);
 //freopen("out.txt", "w", stdout);
 int n;
 cin >> n;
 while(n--)
 {
        for(int i=0;i<20;i++)
                cin>>a[i];
        for(int i=19;i>0;i--)
        {
        if(i!=0)
        a[i-1]+=a[i]/2;
        a[i]=a[i]%2;
        }
        cout<<a[0];
        for(int i=1;i<20;i++)
        cout<<" "<<a[i];
        cout<<endl;
 }
  return 0;
}
View Code

F:以分号为界 每个一行 按照题意输出 substr的应用

技术分享
#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#define foror(i,a,b) for(i=a;i<b;i++)
#define foror2(i,a,b) for(i=a;i>b;i--)
#define EPS 1.0e-8
#define PI acos(-1.0)
#define INF 3000000000
#define MOD 1000000009
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
//using ll = long long;
//using ull= unsigned long long;
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
typedef long long ll;
string a,b;
int now=0;
int pop=0;
void doit()
{
 int cur=0;
 string ans;
 for(int i=0;i<b.size();i++)
 {
        if(b[i]==;)
        {
        ans=b.substr(cur,i-cur+1);
        if(pop==0)
        printf("%d: ",now-1);
        else
        printf("%d: ",now);
        cout<<ans<<endl;
        cur=i+1;
        }
 }
 b=b.substr(cur,b.size()-cur+1);
}
int main()
{
 //freopen("in.txt", "r", stdin);
 //freopen("out.txt", "w", stdout);
  int t;
  cin >> t;
        now=0;
        b=a="";
        while(getline(cin,a))
        {
        now++;
        if(a=="END OF CASE")
        {
        pop=1;
        now=0;
        b="";
        continue;
        }
        b+=a;
        doit();
        }
  return 0;
}
View Code

J

Summer training #7