ASC1 G Beautiful People

题意:给你一个二元组,求这个二元组的最长上升子序列,并且记录路径

解题思路:一个值从小到大排序,在这个值相等的情况情况下按从大到小排序(为了不取到第一个值相等),然后对第二个值 求一个最长上升自诩,路径的话求实求最长上升子序列的时候 记录 来自前一个值。

解题代码:

  1 // File Name: e.cpp
  2 // Author: darkdream
  3 // Created Time: 2015年03月21日 星期六 19时58分58秒
  4
  5 #include<vector>
  6 #include<list>
  7 #include<map>
  8 #include<set>
  9 #include<deque>
 10 #include<stack>
 11 #include<bitset>
 12 #include<algorithm>
 13 #include<functional>
 14 #include<numeric>
 15 #include<utility>
 16 #include<sstream>
 17 #include<iostream>
 18 #include<iomanip>
 19 #include<cstdio>
 20 #include<cmath>
 21 #include<cstdlib>
 22 #include<cstring>
 23 #include<ctime>
 24 #define LL long long
 25
 26 using namespace std;
 27 int M , n ;
 28 struct Matrix
 29 {
 30    int mat[40][40];
 31    void clear()
 32    {
 33       memset(mat,0,sizeof(mat));
 34    }
 35    void output()
 36    {
 37        for(int i = 0 ;i < n;i ++)
 38        {
 39          for(int j = 0 ;j < n;j ++)
 40          {
 41            printf("%d ",mat[i][j]);
 42          }
 43          printf("\n");
 44        }
 45    }
 46    Matrix operator *(const Matrix &b) const
 47    {
 48       Matrix ret;
 49       ret.clear();
 50       for(int i =  0 ;i < n;i ++)
 51           for(int j = 0 ;j < n;j ++)
 52           {
 53               for(int s = 0 ; s < n; s ++)
 54               {
 55                 ret.mat[i][j] = (ret.mat[i][j] + mat[i][s] *b.mat[s][j] % M) % M;
 56               }
 57           }
 58       return ret;
 59    }
 60 }a;
 61 char str[1000];
 62 int num[1000];
 63 int len ;
 64 int t;
 65 void judge(int x, int y)
 66 {
 67   //printf("%d %d***\n",x,y);
 68    int tx = x;
 69    int ty = y;
 70    int ttt = 0 ;
 71    int txa[10];
 72    int tya[10];
 73    memset(txa,0,sizeof(txa));
 74    memset(tya,0,sizeof(tya));
 75    while(tx)
 76    {
 77       txa[ttt] = tx % 2;
 78       tx/= 2;
 79       ttt++;
 80    }
 81    ttt =0 ;
 82    while(ty)
 83    {
 84       tya[ttt] = ty % 2;
 85       ty /= 2;
 86       ttt++;
 87    }
 88    for(int i = 1;i < t; i ++)
 89        if(tya[i]+tya[i-1]+txa[i] + txa[i-1] == 4 || tya[i] + tya[i-1] + txa[i] + txa[i-1] == 0 )
 90        {
 91            return;
 92        }
 93    a.mat[x][y] = 1;
 94 }
 95 void jian()
 96 {
 97    for(int i= 0 ;i <= len/2;i ++)
 98     {
 99         swap(num[i],num[len-i]);
100     }
101    for(int i = 0 ;i <= len ;i ++)
102    {
103         if(num[i] == 0 )
104         {
105            num[i] = 9 ;
106         }else{
107            num[i]-- ;
108            break;
109         }
110    }
111      /*for(int i = len ;i >= 0 ;i --)
112          printf("%d",num[i]);
113      printf("\n");*/
114    if(num[len] == 0 )
115        len --;
116    /*
117      for(int i = len ;i >= 0 ;i --)
118          printf("%d",num[i]);
119      printf("\n");*/
120 }
121 void chu()
122 {
123      int tmp = 0 ;
124      for(int i = len ;i >= 0 ;i --)
125      {
126          int k = (tmp * 10 + num[i]);
127          num[i] = k/2;
128          tmp = k % 2;
129      }
130      if(num[len] == 0 )
131          len --;
132      /*
133      for(int i = len ;i >= 0 ;i --)
134          printf("%d",num[i]);
135      printf("\n");*/
136 }
137 Matrix Pow(Matrix a)
138 {
139    Matrix ret;
140    ret.clear();
141    for(int i= 0 ;i < n;i ++)
142       ret.mat[i][i] = 1;
143    Matrix tmp = a;
144    while(len != -1)
145    {
146       // printf("%d ***\n",len);
147         if(num[0] % 2 == 1)
148         {
149             ret = ret * tmp ;
150             //ret.output();
151         }
152         tmp = tmp*tmp ;
153         chu();
154    }
155    return ret;
156 }
157 int main(){
158     freopen("nice.in","r",stdin);
159     freopen("nice.out","w",stdout);
160
161     scanf("%s %d %d",str,&t,&M);
162     len = strlen(str);
163     for(int i=  0 ;i < len ;i ++)
164     {
165       num[i] = str[i] - ‘0‘;
166     }
167     len --;
168     n = (1 << t);
169     for(int i = 0 ;i <n ;i ++)
170     {
171       for(int j = 0 ;j < n;j ++)
172       {
173          judge(i,j);
174       }
175     }
176    // a.output();
177     jian();
178     a = Pow(a);
179     //a.output();
180     int sum = 0 ;
181     for(int i = 0 ;i < n;i ++)
182         for(int j = 0 ;j < n;j ++)
183     {
184         sum = (sum + a.mat[i][j]) % M;
185     }
186     printf("%d\n",sum);
187
188 return 0;
189 }

时间: 2024-08-07 16:48:46

ASC1 G Beautiful People的相关文章

AC_Dream 1216 G - Beautiful People

题意:有n个人每人有一个力气值Si,美丽值Bi,满足Bi>Bj&&Si>Sj 或者 Bi<Bj&&Si<Sj 的人可以一起参见晚会,问最多有多少人可以一起参见晚会.思路: 我们根据S从小到大将所有人排序,然后看B最长的上升子序列的长度求出来即可! 在排序中优先对S排序,S相等的则对B进行由大到小的排序,why? 也就是对于S相同的,我们先选取B最大的值插入LIS中,因为比如 S1=1, B1 = 1 S1=1, B1 = 2, S1=1, B1 =

ASC(1)G(最长上升子序列)

G - Beautiful People Time Limit: 10000/5000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)     Special Judge SubmitStatus Problem Description The most prestigious sports club in one city has exactly N members. Each of its members is stron

ASC(1)G(上升时间最长的序列)

G - Beautiful People Time Limit: 10000/5000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)     Special Judge SubmitStatus Problem Description The most prestigious sports club in one city has exactly N members. Each of its members is stron

程序员是否必须会算法

本章的标题既然是"程序员与算法",就必然要涉及一个基本问题,那就是"程序员是否必须会算法".这是一个充满争议的问题,虽然并不像"生存还是毁灭"之类的选择那样艰难而沉重,但也绝不是一个轻松的话题.朋友们在我的"算法系列"博客专栏上发表的评论和回复,并不都是我所期待的赞美和鼓励,也常常会有一些冷言冷语.比如,"穷举也算是算法吗"或者"请你说明一下算法在XX系统中能起到什么作用". 有一次,一

[转] Python内存管理机制

转自:https://www.cnblogs.com/51try-again/p/11099999.html 一.引用计数 1.变量与对象 变量赋值的时候才创建,它可以指向(引用)任何类型的对象 python里每一个东西都是对象,它们的核心就是一个结构体:PyObject 变量必须先赋值,再引用. 比如,你定义一个计数器,你必须初始化成0,然后才能自增. 每个对象都包含两个头部字段(类型标识符和引用计数器) 关系图如下: 变量名没有类型,类型属于对象(因为变量引用对象,所以类型随对象),在Pyt

G - Mike and gcd problem

G - Mike and gcd problem Mike has a sequence A?=?[a1,?a2,?...,?an] of length n. He considers the sequence B?=?[b1,?b2,?...,?bn] beautiful if the gcd of all its elements is bigger than 1, i.e. . Mike wants to change his sequence in order to make it be

bnu 34982 Beautiful Garden(暴力)

题目链接:bnu 34982 Beautiful Garden 题目大意:给定一个长度为n的序列,问说最少移动多少点,使得序列成等差序列,点的位置能够为小数. 解题思路:算是纯暴力吧.枚举等差的起始和中间一点,由于要求定中间一点的位置.所以这一步是o(n3);然后用o(n)的算法确定说须要移动几个来保证序列等差. #include <cstdio> #include <cstring> #include <vector> #include <algorithm&g

codeforces 336D Vasily the Bear and Beautiful Strings(组合数学)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Vasily the Bear and Beautiful Strings Vasily the Bear loves beautiful strings. String s is beautiful if it meets the following criteria: String s only consists of characters 0 and 1, at that

HDU Redraw Beautiful Drawings 推断最大流是否唯一解

点击打开链接 Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1660    Accepted Submission(s): 357 Problem Description Alice and Bob are playing together. Alice is crazy about