[Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)][C. Playing Piano]

http://codeforces.com/contest/1079/problem/C

题目大意:给出一个数列a[n],让构造一个满足下列条件的数列b[n]:如果a[i]>a[i-1]那么b[i]>b[i-1],如果a[i]<a[i-1]那么b[i]<b[i-1],如果a[i]==a[i-1],那么b[i]!=b[i-1].其中1<=b[i]<=5  1<=a[i]<=2*1e5.

题解:dp[i][j]表示在位置i处取j时是否成立,如果成立则dp[i][j]=1,而这个成立的条件就是这个状态之前的状态成立dp[i-1][k]=1,这样就可以O(5*N)解决这个问题(对于这种要求构造出数列并且新数列的取值很少的题目,直接枚举取值利用dp方式做即可)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 using namespace std;
 7 typedef long long ll;
 8 int a[100005],b[100005];
 9 int dp[100005][6],pre[100005][6];
10 int main(){
11  // freopen("in.txt","r",stdin);
12     int n;
13     int f=1;
14     int tot1=0;
15     int tot2=0;
16     cin>>n;
17     for(int i=1;i<=n;i++){
18         scanf("%d",&a[i]);
19     }
20     for(int i=1;i<=5;i++){
21         dp[1][i]=1;
22     }
23     for(int i=1;i<n;i++){
24         for(int j=1;j<=5;j++){
25             if(!dp[i][j])continue;
26             if(a[i+1]>a[i]){
27                 for(int k=j+1;k<=5;k++){
28                     dp[i+1][k]=1;
29                     pre[i+1][k]=j;
30                 }
31             }
32             else if(a[i+1]<a[i]){
33                 for(int k=1;k<j;k++){
34                     dp[i+1][k]=1;
35                     pre[i+1][k]=j;
36                 }
37             }
38             else{
39                 for(int k=1;k<=5;k++){
40                     if(k==j)continue;
41                     dp[i+1][k]=1;
42                     pre[i+1][k]=j;
43                 }
44             }
45         }
46     }
47     for(int i=1;i<=5;i++){
48         if(dp[n][i]){
49             for(int j=n;j>=1;j--){
50                 b[j]=i;
51                 i=pre[j][i];
52             }
53             f=0;
54             break;
55         }
56     }
57     if(f){printf("-1\n");return 0;}
58     for(int i=1;i<=n;i++){
59         printf("%d",b[i]);
60         char cc=(i==n)?‘\n‘:‘ ‘;
61         printf("%c",cc);
62     }
63     return 0;
64 }

原文地址:https://www.cnblogs.com/MekakuCityActor/p/9983760.html

时间: 2024-11-06 09:33:58

[Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)][C. Playing Piano]的相关文章

Codeforces Round #512 (Div. 2, based on Technocup 2019 Elimination Round 1) C. Vasya and Golden Ticket

C. Vasya and Golden Ticket time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Recently Vasya found a golden ticket - a sequence which consists of nn digits a1a2-ana1a2-an. Vasya considers a ti

Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3

A,有多个线段,求一条最短的线段长度,能过覆盖到所又线段,例如(2,4)和(5,6) 那么我们需要4 5连起来,长度为1,例如(2,10)(3,11),用(3,10) 思路:我们想一下如果题目说的是最长我们肯定是取最小x和最大的y连起来就完事. 但是要求长度最小又得覆盖,那么可以这样想,我们需要把最小的x不断右移到这条线段的y, 最大的左移到x,所以就是最大x-最小y完事 #include <bits/stdc++.h> using namespace std; #define ll long

【cf比赛记录】Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)

比赛传送门 只能说当晚状态不佳吧,有点头疼感冒的症状.也跟脑子没转过来有关系,A题最后一步爆搜没能立即想出来,B题搜索没有用好STL,C题也因为前面两题弄崩了心态,最后,果然掉分了. A:简单数学 B:数学+排序 C:字符串搜索 A // https://codeforces.com/contest/1277/problem/A /* 题意: 给出一个数,求不大于该数的完美整数的个数(完美整数指全是一个数组成的数字,如:111, 333333, 444444, 9, 8888 ...) 分析:

【模拟】 Codeforces Round #434 (Div. 1, based on Technocup 2018 Elimination Round 1) C. Tests Renumeration

题意:有一堆数据,某些是样例数据(假设X个),某些是大数据(假设Y个),但这些数据文件的命名非常混乱.要你给它们一个一个地重命名,保证任意时刻没有重名文件的前提之下,使得样例数据命名为1~X,大数据命名为X+1~X+Y. 先把未使用的名字压进两个栈. 分为三轮:第一轮把占用了对方名字的样例数据以及占用了对方名字的大数据放进两个队列,然后不断反复尝试对这两个队列进行出队操作,每次将占用对方名字的改成一个未被使用的正确名字(从栈里取出),然后将占用的名字压进另一个栈.由于每个数据只会出队一次,所以是

cf 20190307 Codeforces Round #543 (Div. 2, based on Technocup 2019 Final Round)

B. Mike and Children time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Mike decided to teach programming to children in an elementary school. He knows that it is not an easy task to interest

Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1) 题解

A..B略 C 对当前的值排序,再二分答案,然后对于(i%x==0 && i%y==0)放入大的,再放其他的贪心解决即可. #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> #include<map> #define LL long long #define lson rt<<1 #define rson rt<

Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2)

B2 - TV Subscriptions (Hard Version) 遍历,维护一个set和set<pair<int,int>>即可 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn = 1e6+7; 5 const ll mod = 1e9 + 9; 6 #define afdafafafdafaf y1; 7 int ar[maxn]

Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C. Messy 构造

C. Messy You are fed up with your messy room, so you decided to clean it up. Your room is a bracket sequence s=s1s2-sn of length n. Each character of this string is either an opening bracket '(' or a closing bracket ')'. In one operation you can choo

Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) B. Box 贪心

B. Box Permutation p is a sequence of integers p=[p1,p2,-,pn], consisting of n distinct (unique) positive integers between 1 and n, inclusive. For example, the following sequences are permutations: [3,4,1,2], [1], [1,2]. The following sequences are n