Topcoder 636 B [violent]

Source: Internet
Author: User

A arrange is provided, and some numbers are accidentally erased. But there are N element pairs that know that this sequence meets the requirements of I <J & A [I] <A [J, then let you restore this arrangement. Then, there are several arrays that meet the conditions.


Analysis: It is very simple and violent. Fill in the erased value in the brute force mode, then check the number of elements, and compare them.


Code:

#include <cstdlib>#include <cctype>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <string>#include <iostream>#include <sstream>#include <map>#include <set>#include <queue>#include <stack>#include <fstream>#include <numeric>#include <iomanip>#include <bitset>#include <list>#include <stdexcept>#include <functional>#include <utility>#include <ctime>using namespace std;#define PB push_back#define MP make_pair#define REP(i,n) for(i=0;i<(n);++i)#define FOR(i,l,h) for(i=(l);i<=(h);++i)#define FORD(i,h,l) for(i=(h);i>=(l);--i)typedef vector<int> VI;typedef vector<string> VS;typedef vector<double> VD;typedef long long LL;typedef pair<int,int> PII;class SortishDiv2{        public:        int ways(int sortedness, vector <int> seq)        {            int ans = 0;            int vis[200],dis[200];            int pps[200];            memset(vis,0,sizeof(vis));            memset(dis,0,sizeof(dis));            for(int i=0;i<seq.size();i++)            {                dis[seq[i]]=1;                if(seq[i]==0)                {                    vis[i]=1;                    continue;                }                for(int j=i+1; j<seq.size();j++)                {                    if(seq[i]==0)                        continue;                    if(seq[i]<seq[j])                        ans++;                }            }            sortedness -= ans;            int count = 0;            vector<int> cc,wei;            for(int i=1;i<=seq.size();i++){                if(dis[i]==0){                    cc.push_back(i);                }                if(vis[i-1])                    wei.push_back(i-1);            }            if(cc.size()==0)                return sortedness==0;            do            {                ans = 0;                for(int i=0;i<seq.size();i++)                    pps[i] = seq[i];                for(int i = 0;i<wei.size();i++)                {                    for(int k=0;k<seq.size();k++)                    {                        if(pps[k]==0)                            continue;                        if(k>wei[i] && pps[k]>cc[i] || k<wei[i] && pps[k]<cc[i]){                            ans++;                        }                    }                    pps[wei[i]]=cc[i];                }                if(ans == sortedness)                    count++;            }while(next_permutation(cc.begin(),cc.end()));            cc.clear();            wei.clear();            return count;        }};int main(){    freopen("Input.txt","r",stdin);        SortishDiv2 a;        int n;        vector<int> v;        while(~scanf("%d",&n))        {            for(int i=0;i<n;i++)            {                int x;                scanf("%d",&x);                v.push_back(x);            }            cout<<a.ways(n,v)<<endl;            v.clear();        }        return 0;}


Topcoder 636 B [violent]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.