zoukankan      html  css  js  c++  java
  • wenbao与cf(849D)思维

    http://codeforces.com/contest/848/problem/B

    考虑会碰到的条件,X+Y相等,贪心

     1 #include <iostream>
     2 #include <string.h>
     3 #include <vector>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 const int maxn = 100009;
     8 int g[maxn], p[maxn], t[maxn], n, w, h, x[maxn], y[maxn], co_x[maxn], co_y[maxn];
     9 vector<int> v[maxn*2];
    10 
    11 bool cmp(int x, int y){
    12     if(g[x] != g[y]) return g[x] == 2;
    13     else{
    14         if(g[x] == 1) return p[x] < p[y];
    15         else return p[x] > p[y];
    16     }
    17 }
    18 
    19 int main(){
    20     scanf("%d%d%d", &n, &w, &h);
    21     for(int i = 0; i < n; ++i){
    22         scanf("%d%d%d", &g[i], &p[i], &t[i]);
    23         v[p[i]-t[i]+maxn].push_back(i);
    24     }
    25     for(int i = 0; i < maxn*2; ++i){
    26         int numx = 0, numy = 0;
    27         for(int j = 0; j < v[i].size(); ++j){
    28             int xx = v[i][j];
    29             if(g[xx] == 1) x[numx++] = p[xx];
    30             else y[numy++] = p[xx];
    31         }
    32         sort(x, x+numx);
    33         sort(y, y+numy);
    34         sort(v[i].begin(), v[i].end(), cmp);
    35         for(int j = 0; j < numx; ++j){
    36             co_x[v[i][j]] = x[j];
    37             co_y[v[i][j]] = h;
    38         }
    39         for(int j = 0; j < numy; ++j){
    40             co_x[v[i][numx+j]] = w;
    41             co_y[v[i][numx+j]] = y[numy-j-1];
    42         }
    43     }
    44     for(int i = 0; i < n; ++i){
    45         printf("%d %d
    ", co_x[i], co_y[i]);
    46     }
    47     return 0;
    48 }

     看到别人博客里面提供了另外一种思路,可以将起点和终点排序必然是一一对应的

     1 #include <iostream>
     2 #include <algorithm>
     3 using namespace std;
     4 
     5 const int maxn = 100009;
     6 struct Node{
     7     int x, y, xx, yy, id;
     8 };
     9 
    10 Node a[maxn], b[maxn];
    11 int co_x[maxn], co_y[maxn];
    12 
    13 int main(){
    14     int n, w, h, g, p, t;
    15     scanf("%d%d%d", &n, &w, &h);
    16     for(int i = 0; i < n; ++i){
    17         scanf("%d%d%d", &g, &p, &t);
    18         if(g == 1){
    19             a[i] = (Node){p, -t, p, h, i};
    20         }else{
    21             a[i] = (Node){-t, p, w, p, i};
    22         }
    23         b[i] = a[i];
    24     }
    25     //N1.x+N1.y == N2.x+N2.y 可以相撞
    26     sort(a, a+n, [](Node N1, Node N2){
    27         return (N1.x+N1.y < N2.x+N2.y || N1.x+N1.y == N2.x+N2.y && N1.x-N1.y < N2.x-N2.y);
    28     });
    29     sort(b, b+n, [](Node N1, Node N2){
    30         return (N1.x+N1.y < N2.x+N2.y || N1.x+N1.y == N2.x+N2.y && N1.xx-N1.yy < N2.xx-N2.yy);
    31     });
    32     for(int i = 0; i < n; ++i){
    33         co_x[a[i].id] = b[i].xx;
    34         co_y[a[i].id] = b[i].yy;
    35     }
    36     for(int i = 0; i < n; ++i){
    37         printf("%d %d
    ", co_x[i], co_y[i]);
    38     }
    39     return 0;
    40 }

    只有不断学习才能进步!

  • 相关阅读:
    http编程中的get和post混合使用方式
    SQLServer实现作业依赖(非步骤)
    SQLServer实现两个库的字段长度自动更新
    Python+SQLite数据库实现服务端高并发写入
    sqlite数据库相关使用
    sqlite语法
    VBA关键字总结
    VS2005 .net2.0 TreeView.设置SelectedNodeStyle控制TreeView中选定节点的外观的
    SQLSERVER 2005 如何给sa用户设置空密码?
    解决超过远程连接数而无法连接服务器的问题 踢出已断开用户
  • 原文地址:https://www.cnblogs.com/wenbao/p/7469960.html
Copyright © 2011-2022 走看看