『CCF题解』2014年09月【A】【B】【C】【D】

  1. 相邻数对【基础】

    思路:排序即可

    AC代码:

    #include<bits/stdc++.h>
    using namespace std;
    int main(void){
        int n;
        cin >> n;
        int a[n];
        for(int i = 1; i <= n; i++){
            cin >> a[i];
        }
        sort(a+1,a+1+n);
        int cnt = 0;
        for(int i = 1; i <= n-1; i++){
            if(abs(a[i]-a[i+1])==1){
                cnt++;
            }
        }
        cout<<cnt<<endl;
        return 0;
    }
    
  2. 画图【暴力】

    思路:暴力打标记,暴力判断

    AC代码:

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int maxn = 1e2 + 5;
    bool mp[maxn][maxn];
    int main(void) {
        int n;
        cin >> n;
        for(int i = 1; i <= n; i++){
            int x1,y1,x2,y2;
            cin >> x1 >> y1 >> x2 >> y2;
            for(int i = x1; i < x2; i++){
                for(int j = y1; j < y2; j++){
                    mp[i][j] = 1;
                }
            }
        }
        int cnt = 0;
        for(int i = 0; i <= 100; i++){
            for(int j = 0; j <= 100; j++){
                if(mp[i][j]) cnt++;
            }
        }
        cout<<cnt<<endl;
        return 0;
    }
    
  3. 字符串匹配【暴力查找】

    思路:使用string中的find函数,暴力匹配查找即可。

    AC代码:

    #include<bits/stdc++.h>
    using namespace std;
    int main(void)
    {
        string s;
        string str;
        int n,m;
        cin >> s;
        cin >> n >> m;
        string tmp;
        if(n == 0){
        	for(int i = 0; i < s.size(); i++) s[i] = toupper(s[i]);
    	}
        for(int i = 0; i < m; i++)
        {
            cin >> str;
            tmp = str;
            if(n == 0){
       			for(int i = 0; i < str.size(); i++) str[i] = toupper(str[i]);
    		}
            if(str.find(s)!=str.npos) cout << tmp << endl;
        }
        return 0;
    }
    
  4. 最优配餐【多源BFS模板】

    思路:多起点BFS模板,没有啥好说的

    AC代码:

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int maxn = 1e3 + 5;
    int n,m,k,d;
    int mp[maxn][maxn];
    int vis[maxn][maxn];
    struct Node{
        int x,y;
        ll dis;
        Node(int _x,int _y, ll _dis):x(_x),y(_y),dis(_dis){}
    };
    queue<Node> q;
    int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
    ll bfs(){
        ll ans = 0;
        int cnt = 0;
        while(!q.empty()){
            Node tmp = q.front();
            q.pop();
            for(int i = 0; i < 4; i++){
                int tx = tmp.x+dir[i][0];
                int ty = tmp.y+dir[i][1];
                if(vis[tx][ty]||tx<=0||tx>n||ty<=0||ty>n) continue;
                vis[tx][ty] = 1;
                if(mp[tx][ty]){
                    ans += (mp[tx][ty]*(tmp.dis+1));
                    cnt++;
                    if(cnt>=k) return ans;
                }
                q.push(Node(tx,ty,tmp.dis+1));
            }
        }
    }
    int main(void) {
        cin >> n >> m >> k >> d;
        for(int i = 1; i <= m; i++){
            int a,b;
            cin >> a >> b;
            vis[a][b] = 1;
            q.push(Node(a,b,0));
        }
        for(int i = 1; i <= k; i++){
            int a,b,c;
            cin >> a >> b >> c;
            mp[a][b] = c;
        }
        for(int i = 1; i <= d; i++){
            int x,y;
            cin >> x >> y;
            vis[x][y] = 1;
        }
        cout<<bfs()<<endl;
        return 0;
    }
    
发布了14 篇原创文章 · 获赞 4 · 访问量 1624

猜你喜欢

转载自blog.csdn.net/wxd1233/article/details/105640829