Codeforces Round #604 (Div. 2)D(构造)

构造,枚举起点,如果一个序列成立,那么将它reverse依然成立,所以两个方向(从小到大或从大到小)没有区别,选定一个方向进行探测,直到探测不到以后回头,只要所给数据能成立,那么能探测进去就能探测出来,否则就不能构造。

 1 #define HAVE_STRUCT_TIMESPEC
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 int num[7];
 5 int sum;
 6 int main(){
 7     ios::sync_with_stdio(false);
 8     cin.tie(NULL);
 9     cout.tie(NULL);
10     int a[7]={0};
11     for(int i=1;i<=4;++i)
12         cin>>a[i];
13     for(int i=1;i<=4;++i){
14         num[i]=a[i];
15         sum+=num[i];
16     }
17     for(int i=1;i<=4;++i){
18         for(int i=1;i<=4;++i)
19             num[i]=a[i];
20         vector<int>v;
21         int now=i;
22         for(int i=1;i<=sum;++i){
23             if(!num[now])
24                 break;
25             --num[now];
26             v.push_back(now-1);
27             if(num[now+1])
28                 ++now;
29             else
30                 --now;
31         }
32         if(v.size()==sum){
33             cout<<"YES\n";
34             for(auto it:v)
35                 cout<<it<<" ";
36             return 0;
37         }
38     }
39     cout<<"NO";
40     return 0;
41 }

猜你喜欢

转载自www.cnblogs.com/ldudxy/p/12046850.html