【题目链接】:http://codeforces.com/contest/731/problem/D

【题意】



给你n个象形文;

每个象形文由l[i]个数字组成;

你可以把所有的组成象形文的数字同时增加1;

超过c的变成1;

然后让你用这个操作使得n个象形文按照字典序升序

排;

问你最小的操作次数;

【题解】



首先;

得先让这n个字符串,相邻的两个的字符串都符合字典序

即s[i]<=s[i+1];

所以;

可以分别处理出使得这n-1个相邻的关系成立的操作步数;

只考虑第一个不同的位置就好;

因为前面不管怎么改变都是一样的;

比如n-1个相邻关系种的

s[3]和s[4]

如果

s[3][i]和s[4][i]是第一个不同的位置;



则如果s[3][i]< s[4][i]

那么0..c-s4[i]和c-s[3][i]+1..c这个两个区间内的数对应的操作次数;

都能使s[3][i]< s[4][i]成立;



如果s[3][i]>s[4][i]

则c-s[3][i]+1..c-s[4][i]这个区间范围内的数对应的操作次数,都能使得s[3][i]< s[4][i]成立;

求这n-1个区间的交就好;

如果有一个区间能够使得这n-1个关系都满足则输出最小的就好;



【Number Of WA】



0



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 5e5+100;
const int M = 1e6+100; int n,c,l[N],sum[M],sum1[M];
vector <int> G[N]; int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
//init??????
cin >> n >> c;
rep1(i,1,n)
{
cin >> l[i];
int x;
rep1(j,1,l[i])
{
cin >> x;
G[i].pb(x);
}
}
rep1(i,1,n-1)
{
int len = min(l[i],l[i+1]);
int j;
for ( j = 0;j <= len-1;j++)
if (G[i][j]!=G[i+1][j])
break;
if (j==len)
{
if (l[i]>l[i+1])
return cout <<-1<<endl,0;
sum[0]++;
continue;
}
if (G[i][j]<G[i+1][j])
{
sum[0]++;
//s[i][j]<s[i+1][j]
sum[c-G[i+1][j]+1]--;
sum[c-G[i][j]+1]++;
}
else
{
//s[i][j]>s[i+1][j]
sum[c-G[i][j]+1]++;
sum[c-G[i+1][j]+1]--;
}
}
rep1(i,1,c) sum[i] += sum[i-1];
rep1(i,0,c)
if (sum[i]==n-1)
{
cout <<i<<endl;
return 0;
}
cout <<-1<<endl;
return 0;
}

【codeforces 731D】80-th Level Archeology的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【13.77%】【codeforces 734C】Anton and Making Potions

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【51.27%】【codeforces 604A】Uncowed Forces

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  4. 【codeforces 750D】New Year and Fireworks

    time limit per test2.5 seconds memory limit per test256 megabytes inputstandard input outputstandard ...

  5. 【33.33%】【codeforces 608C】Chain Reaction

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  7. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  8. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  9. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

随机推荐

  1. BZOJ 1455 罗马游戏 左偏树

    题目大意:给定n个点,每一个点有一个权值,提供两种操作: 1.将两个点所在集合合并 2.将一个点所在集合的最小的点删除并输出权值 非常裸的可并堆 n<=100W 启示式合并不用想了 左偏树就是快 ...

  2. ssh的tunnel设置+autossh设置

    tunnel设置 一.说明 用于通过ssh转发数据 二.设置 编辑ssh server的'2Fetc/ssh/sshd_config 加入下面: #反向遂道 GatewayPorts  yes #正向 ...

  3. POJ3126——Prime Path

    非常水的一道广搜题(专业刷水题). .. #include<iostream> #include<cstdio> #include<queue> #include& ...

  4. 【JEECG技术博文】Local storage &amp; easyui extensions

    1. Local storage背景 cookie弊端:同域内http请求都会带cookie,添加带宽和流量:有个数和限制大小(约4K). 在HTML5中,本地存储是一个window的属性.包含loc ...

  5. HDUOJ 水果

     /*水果 夏天来了~~好开心啊,呵呵,好多好多水果~~ Joe经营着一个不大的水果店.他觉得生存之道就是经营最受顾客欢迎的水果. 如今他想要一份水果销售情况的明细表,这样Joe就能够非常easy ...

  6. 【JavaScript】在同一个网页中实现多个JavaScript特效

    在网页中,假设出现两次<script type="text/javascript"></script>标签,全部的JavaScipt脚本都不会再生效,仅仅能 ...

  7. MySQL Study之--MySQL体系结构深入解析

    MySQL Study之--MySQL体系结构深入解析 MySQL体系架构 由连接池组件.管理服务和⼯工具组件.sql接口组件.查询分析器组件.优化器组件.缓冲组件.插件式存储引擎.物理⽂文件组成.m ...

  8. hdoj--1864--最大保险额(背包)

    最大报销额 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  9. mvc:view-controller直接转发页面

    在springMVC中,通过@RequestMapping发送请求地址,转发到目标页面,但是,有时候想直接访问页面, 不想通过xxx.jsp直接访问页面,可以通过springmvc.xml配置文件中的 ...

  10. [Java] 总结1.5/1.6/1.7版本的特性

    开发过程中接触到了从jdk1.5---jdk1.7的使用,在不同的阶段,都使用过了jdk的一些新特性,操作起来更加方面啦!特此总结了下,以下是测试代码: JDK1.5新特性: 1.自动装箱与拆箱: I ...