• Codeforces Gym102219 2019 ICPC Malaysia National J. Kitchen Plates (暴力,拓扑排序)


    • 题意:给你5个\(A,B,C,D,E\)大小关系式,升序输出它们,如果所给的大小矛盾,输出\(impossible\).

    • 题意:当时第一眼想到的就是连边然后排序,很明显是拓扑排序(然而我不会qwq,之后再补),但貌似可以直接暴力来写,用二维数组来记录两个数之间的大小关系,如果一维\(>\)二维就记录true,然后我们要对题目所给的关系进行合并,将所有大小关系弄清楚,三个for就可以搞定了,之后判断一下是否有矛盾存在,最后再统计一下大小然后再按顺序输出即可(这题的输出其实没怎么看懂,之前写的桶排输出不知道为什么不给过qaq).

    • 代码:

      #include <iostream>
      #include <cstdio>
      #include <cstring>
      #include <cmath>
      #include <algorithm>
      #include <stack>
      #include <queue>
      #include <vector>
      #include <map>
      #include <set>
      #include <unordered_set>
      #include <unordered_map>
      #define ll long long
      #define fi first
      #define se second
      #define pb push_back
      #define me memset
      const int N = 1e6 + 10;
      const int mod = 1e9 + 7;
      using namespace std;
      typedef pair<int,int> PII;
      typedef pair<long,long> PLL;
       
      string s;
      int v[100][100];
      int cnt[N];
      map<int,int> mp;
      vector<int> rest;
      int main() {
          ios::sync_with_stdio(false);cin.tie(0);
          for(int i=1;i<=5;++i){
              cin>>s;
              //初始化记录
               for(int j=0;j<s.size();++j){
                   if(s[1]=='>') v[s[0]-'A'][s[2]-'A']=1;
                   else v[s[2]-'A'][s[0]-'A']=1;
               }
          }
          //合并
          for(int k=0;k<5;++k){
              for(int i=0;i<5;++i){
                  for(int j=0;j<5;++j){
                      if(v[i][k] && v[k][j]) v[i][j]=1;
                  }
              }
          }
       
          for(int i=0;i<5;++i){
              for(int j=0;j<5;++j){
                  if(v[i][j] && v[j][i]){
                      puts("impossible");
                      return 0;
                  }
              }
          }
          //统计大小
          for(int i=0;i<5;++i){
              for(int j=0;j<5;++j){
                  if(v[i][j]) cnt[i]++;
              }
          }
          for(int len=0;len<5;++len){
              for(int num=0;num<5;++num){
                  if(cnt[num]==len) printf("%c",num+'A');
              }
          }
       
          return 0;
      }
      
    • :拓扑排序的裸题,直接写就行了

      #include <iostream>
      #include <cstdio>
      #include <cstring>
      #include <cmath>
      #include <algorithm>
      #include <stack>
      #include <queue>
      #include <vector>
      #include <map>
      #include <set>
      #include <unordered_set>
      #include <unordered_map>
      #define ll long long
      #define fi first
      #define se second
      #define pb push_back
      #define me memset
      const int N = 1e6 + 10;
      const int mod = 1e9 + 7;
      const int INF = 0x3f3f3f3f;
      using namespace std;
      typedef pair<int,int> PII;
      typedef pair<ll,ll> PLL;
      
      string s;
      vector<char> v;
      vector<char> out[N];
      int in[N];
      
      int main() {
          ios::sync_with_stdio(false);cin.tie(0);
          for(int i=1;i<=5;++i){
              cin>>s;
               if(s[1]=='>'){
                   in[s[0]]++;
                   out[s[2]].pb(s[0]);
               }
               else{
                   in[s[2]]++;
                   out[s[0]].pb(s[2]);
               }
          }
          queue<char> q;
          for(char i='A';i<='E';++i){
              if(in[i]==0) q.push(i);
          }
          while(!q.empty()){
              char tmp=q.front();
              q.pop();
              v.pb(tmp);
              for(auto w:out[tmp]){
                  if(w!=-1){
                      in[w]--;
                      if(in[w]==0) q.push(w);
                  }
                  w=-1;
              }
          }
          if(v.size()!=5) puts("impossible");
          else{
              for(auto w:v) printf("%c",w);
          }
          return 0;
      }
      
  • 相关阅读:
    深入理解sizeof
    trie树详解
    高精度计算
    编写高效的Android代码
    Android Architecture
    AIDL Android中的远程接口
    性能测试常见术语
    软件与软件测试相关
    注解实现Springmvc+jsp步骤
    非注解实现SpringMvc+JSP (一般用不到 主要用于了解研究底层)
  • 原文地址:https://www.cnblogs.com/lr599909928/p/12904846.html
Copyright © 2020-2023  润新知