Codeforces Round #401 (Div. 2) A,B,C,D,E
0.5 seconds
256 megabytes
standard input
standard output
Bomboslav likes to look out of the window in his room and watch lads outside playing famous shell game. The game is played by two persons: operator and player. Operator takes three similar opaque shells and places a ball beneath one of them. Then he shuffles the shells by swapping some pairs and the player has to guess the current position of the ball.
Bomboslav noticed that guys are not very inventive, so the operator always swaps the left shell with the middle one during odd moves (first, third, fifth, etc.) and always swaps the middle shell with the right one during even moves (second, fourth, etc.).
Let's number shells from 0 to 2 from left to right. Thus the left shell is assigned number 0, the middle shell is 1 and the right shell is 2. Bomboslav has missed the moment when the ball was placed beneath the shell, but he knows that exactly n movements were made by the operator and the ball was under shell x at the end. Now he wonders, what was the initial position of the ball?
The first line of the input contains an integer n (1 ≤ n ≤ 2·109) — the number of movements made by the operator.
The second line contains a single integer x (0 ≤ x ≤ 2) — the index of the shell where the ball was found after n movements.
Print one integer from 0 to 2 — the index of the shell where the ball was initially placed.
4
2
1
1
1
0
In the first sample, the ball was initially placed beneath the middle shell and the operator completed four movements.
- During the first move operator swapped the left shell and the middle shell. The ball is now under the left shell.
- During the second move operator swapped the middle shell and the right one. The ball is still under the left shell.
- During the third move operator swapped the left shell and the middle shell again. The ball is again in the middle.
- Finally, the operators swapped the middle shell and the right shell. The ball is now beneath the right shell.
思路:模6,自己手模拟一下即可;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=;
int a[]={,,,,,};
int b[]={,,,,,};
int c[]={,,,,,};
int main()
{
int n;
int x;
scanf("%d%d",&n,&x);
n%=;
if(!n)n=;
x++;
if(a[n-]==x)
printf("0\n");
else if(b[n-]==x)
printf("1\n");
else
printf("2\n");
return ;
}
2 seconds
256 megabytes
standard input
standard output
After the fourth season Sherlock and Moriary have realized the whole foolishness of the battle between them and decided to continue their competitions in peaceful game of Credit Cards.
Rules of this game are simple: each player bring his favourite n-digit credit card. Then both players name the digits written on their cards one by one. If two digits are not equal, then the player, whose digit is smaller gets a flick (knock in the forehead usually made with a forefinger) from the other player. For example, if n = 3, Sherlock's card is 123 and Moriarty's card has number 321, first Sherlock names 1 and Moriarty names 3 so Sherlock gets a flick. Then they both digit 2 so no one gets a flick. Finally, Sherlock names 3, while Moriarty names 1 and gets a flick.
Of course, Sherlock will play honestly naming digits one by one in the order they are given, while Moriary, as a true villain, plans to cheat. He is going to name his digits in some other order (however, he is not going to change the overall number of occurences of each digit). For example, in case above Moriarty could name 1, 2, 3 and get no flicks at all, or he can name 2, 3 and 1 to give Sherlock two flicks.
Your goal is to find out the minimum possible number of flicks Moriarty will get (no one likes flicks) and the maximum possible number of flicks Sherlock can get from Moriarty. Note, that these two goals are different and the optimal result may be obtained by using different strategies.
The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of digits in the cards Sherlock and Moriarty are going to use.
The second line contains n digits — Sherlock's credit card number.
The third line contains n digits — Moriarty's credit card number.
First print the minimum possible number of flicks Moriarty will get. Then print the maximum possible number of flicks that Sherlock can get from Moriarty.
3
123
321
0
2
2
88
00
2
0
First sample is elaborated in the problem statement. In the second sample, there is no way Moriarty can avoid getting two flicks.
题意:两个人,n个数字,第一个人出数字的顺序不变,第二个人的随意,出的数字小于另一个人的算赢;
求第二个人赢得最小次数,第一个人赢的最大次数;
思路:标记,贪心;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=;
char a[N];
char b[N];
int flaga[];
int flagb[];
int x[];
int y[];
int n;
void slove1()
{
int ans=n;
for(int i=;i<;i++)
x[i]=flaga[i],y[i]=flagb[i];
for(int i=;i<=;i++)
{
for(int j=i;j>=;j--)
{
int minn=min(y[i],x[j]);
ans-=minn;
x[i]-=minn;
y[i]-=minn;
}
}
printf("%d\n",ans);
}
void slove2()
{
int ans=;
for(int i=;i<;i++)
x[i]=flaga[i],y[i]=flagb[i];
for(int i=;i<=;i++)
{
for(int j=i+;j<=;j++)
{
int minn=min(x[i],y[j]);
ans+=minn;
x[i]-=minn;
y[j]-=minn;
}
}
printf("%d\n",ans);
}
int main()
{ scanf("%d",&n);
scanf("%s%s",a,b);
for(int i=;i<n;i++)
flaga[a[i]-'']++,flagb[b[i]-'']++;
slove1();
slove2();
return ;
}
1 second
256 megabytes
standard input
standard output
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables.
Now she has a table filled with integers. The table consists of n rows and m columns. By ai, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if ai, j ≤ ai + 1, j for all i from 1 ton - 1.
Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that ai, j ≤ ai + 1, j for all i from l to r - 1 inclusive.
Alyona is too small to deal with this task and asks you to help!
The first line of the input contains two positive integers n and m (1 ≤ n·m ≤ 100 000) — the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table.
Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for ai, j (1 ≤ ai, j ≤ 109).
The next line of the input contains an integer k (1 ≤ k ≤ 100 000) — the number of task that teacher gave to Alyona.
The i-th of the next k lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n).
Print "Yes" to the i-th line of the output if the table consisting of rows from li to ri inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No".
5 4
1 2 3 5
3 1 3 2
4 5 2 3
5 5 3 2
4 4 3 4
6
1 1
2 5
4 5
3 5
1 3
1 5
Yes
No
Yes
Yes
Yes
No
In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
题意:给你n*m的一个矩阵;k个询问,取出l行-r行,找出是否有一列是非递减的序列;
思路:有点dp的思想,对于一列dp[i]=(a[i]>=a[i-1]? dp[i-1],i);
dp[i]表示从dp[i]行到当前行是非递减序列;
对于每一行取一个最小值minn,表示从minn行-i行开始能找到一个非递减的序列;
详见代码;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=;
vector<int>v[N];
vector<int>flag[N];
int minn[N];
int n,m;
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
minn[i]=inf;
v[i].push_back();
for(int j=;j<=m;j++)
{
int x;
scanf("%d",&x);
v[i].push_back(x);
}
}
minn[]=;
for(int i=;i<=m;i++)
flag[].push_back();
for(int i=;i<=n;i++)
{
flag[i].push_back();
for(int j=;j<=m;j++)
{
if(v[i][j]>=v[i-][j])
{
flag[i].push_back(flag[i-][j]);
minn[i]=min(minn[i],flag[i][j]);
}
else
{
flag[i].push_back(i);
minn[i]=min(minn[i],i);
}
}
}
int q;
scanf("%d",&q);
while(q--)
{
int l,r;
scanf("%d%d",&l,&r);
if(minn[r]<=l)
printf("Yes\n");
else
printf("No\n");
}
return ;
}
2 seconds
256 megabytes
standard input
standard output
Vasya is an administrator of a public page of organization "Mouse and keyboard" and his everyday duty is to publish news from the world of competitive programming. For each news he also creates a list of hashtags to make searching for a particular topic more comfortable. For the purpose of this problem we define hashtag as a string consisting of lowercase English letters and exactly one symbol '#' located at the beginning of the string. The length of the hashtag is defined as the number of symbols in it without the symbol '#'.
The head administrator of the page told Vasya that hashtags should go in lexicographical order (take a look at the notes section for the definition).
Vasya is lazy so he doesn't want to actually change the order of hashtags in already published news. Instead, he decided to delete some suffixes (consecutive characters at the end of the string) of some of the hashtags. He is allowed to delete any number of characters, even the whole string except for the symbol '#'. Vasya wants to pick such a way to delete suffixes that the total number of deleted symbols is minimum possible. If there are several optimal solutions, he is fine with any of them.
The first line of the input contains a single integer n (1 ≤ n ≤ 500 000) — the number of hashtags being edited now.
Each of the next n lines contains exactly one hashtag of positive length.
It is guaranteed that the total length of all hashtags (i.e. the total length of the string except for characters '#') won't exceed 500 000.
Print the resulting hashtags in any of the optimal solutions.
3
#book
#bigtown
#big
#b
#big
#big
3
#book
#cool
#cold
#book
#co
#cold
4
#car
#cart
#art
#at
#
#
#art
#at
3
#apple
#apple
#fruit
#apple
#apple
#fruit
Word a1, a2, ..., am of length m is lexicographically not greater than word b1, b2, ..., bk of length k, if one of two conditions hold:
- at first position i, such that ai ≠ bi, the character ai goes earlier in the alphabet than character bi, i.e. a has smaller character than bin the first position where they differ;
- if there is no such position i and m ≤ k, i.e. the first word is a prefix of the second or two words are equal.
The sequence of words is said to be sorted in lexicographical order if each word (except the last one) is lexicographically not greater than the next word.
For the words consisting of lowercase English letters the lexicographical order coincides with the alphabet word order in the dictionary.
According to the above definition, if a hashtag consisting of one character '#' it is lexicographically not greater than any other valid hashtag. That's why in the third sample we can't keep first two hashtags unchanged and shorten the other two.
题意:n个字符串,删掉最少的后缀使得n个字符串按字典序从小到大排序;
思路:从后往前贪心,对于当前要删的字符串进行二分,找到最长满足条件的字符串;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=5e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=;
int n;
string a[N];
string ans[N];
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
cin>>a[i];
ans[n]=a[n];
for(int i=n-;i>=;i--)
{
string c=ans[i+];
int l=,r=a[i].size(),q;
while(l<=r)
{
int mid=(l+r)>>;
string d=a[i].substr(,mid);
if(d<=c)
{
q=mid;
l=mid+;
}
else
r=mid-;
}
ans[i]=a[i].substr(,q);
}
for(int i=;i<=n;i++)
cout<<ans[i]<<endl;
return ;
}
1 second
256 megabytes
standard input
standard output
Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings.
There are n rings in factory's stock. The i-th ring has inner radius ai, outer radius bi and height hi. The goal is to select some subset of rings and arrange them such that the following conditions are satisfied:
- Outer radiuses form a non-increasing sequence, i.e. one can put the j-th ring on the i-th ring only if bj ≤ bi.
- Rings should not fall one into the the other. That means one can place ring j on the ring i only if bj > ai.
- The total height of all rings used should be maximum possible.
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of rings in factory's stock.
The i-th of the next n lines contains three integers ai, bi and hi (1 ≤ ai, bi, hi ≤ 109, bi > ai) — inner radius, outer radius and the height of the i-th ring respectively.
Print one integer — the maximum height of the tower that can be obtained.
3
1 5 1
2 6 2
3 7 3
6
4
1 2 1
1 3 3
4 6 2
5 7 1
4
In the first sample, the optimal solution is to take all the rings and put them on each other in order 3, 2, 1.
In the second sample, one can put the ring 3 on the ring 4 and get the tower of height 3, or put the ring 1 on the ring 2 and get the tower of height 4.
思路:线段树维护区间最大值;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=;
struct linetree
{
ll maxx[N*];
void pushup(int pos)
{
maxx[pos]=max(maxx[pos<<],maxx[pos<<|]);
}
void build(int l,int r,int pos)
{
if(l==r)
{
maxx[pos]=;
return;
}
int mid=(l+r)>>;
build(l,mid,pos<<);
build(mid+,r,pos<<|);
pushup(pos);
}
void update(int p,ll c,int l,int r,int pos)
{
if(l==r)
{
maxx[pos]=max(maxx[pos],c);
return;
}
int mid=(l+r)>>;
if(p<=mid)
update(p,c,l,mid,pos<<);
else
update(p,c,mid+,r,pos<<|);
pushup(pos);
}
ll query(int L,int R,int l,int r,int pos)
{
if(L<=l&&r<=R)
{
return maxx[pos];
}
int mid=(l+r)>>;
ll maxxx=;
if(L<=mid)
maxxx=max(maxxx,query(L,R,l,mid,pos<<));
if(R>mid)
maxxx=max(maxxx,query(L,R,mid+,r,pos<<|));
return maxxx;
}
};
linetree tree;
int n;
int s[N<<],cnt;
struct is
{
int a,b;
ll h;
bool operator <(const is &c)const
{
if(b!=c.b)
return b<c.b;
return a<c.a;
}
}a[N];
int getpos(int x)
{
int pos=lower_bound(s,s+cnt,x)-s;
return pos+;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d%d%lld",&a[i].a,&a[i].b,&a[i].h);
s[cnt++]=a[i].a;
s[cnt++]=a[i].b;
}
sort(a+,a+n+);
sort(s,s+cnt);
cnt=unique(s,s+cnt)-s;
tree.build(,cnt+,);
for(int i=;i<=n;i++)
{
ll h=a[i].h;
ll maxxx=tree.query(getpos(a[i].a)+,getpos(a[i].b),,cnt+,);
//cout<<h<<" "<<maxxx<<endl;
tree.update(getpos(a[i].b),maxxx+h,,cnt+,);
}
printf("%lld\n",tree.query(,cnt,,cnt+,));
return ;
}
Codeforces Round #401 (Div. 2) A,B,C,D,E的更多相关文章
- Codeforces Round #401 (Div. 2) 离翻身就差2分钟
Codeforces Round #401 (Div. 2) 很happy,现场榜很happy,完全将昨晚的不悦忘了.终判我校一片惨白,小董同学怒怼D\E,离AK就差一个C了,于是我AC了C题还剩35 ...
- Codeforces Round #401 (Div. 2) C Alyona and Spreadsheet —— 打表
题目链接:http://codeforces.com/contest/777/problem/C C. Alyona and Spreadsheet time limit per test 1 sec ...
- Codeforces Round #401 (Div. 2) D Cloud of Hashtags —— 字符串
题目链接:http://codeforces.com/contest/777/problem/D 题解: 题意:给出n行字符串,对其进行字典序剪辑.我自己的想法是正向剪辑的,即先对第一第二个字符串进行 ...
- Codeforces Round #401 (Div. 2)
和FallDream dalao一起从学长那借了个小号打Div2,他切ABE我做CD,我这里就写下CD题解,剩下的戳这里 AC:All Rank:33 小号Rating:1539+217->17 ...
- D Cloud of Hashtags Codeforces Round #401 (Div. 2)
Cloud of Hashtags [题目链接]Cloud of Hashtags &题意: 给你一个n,之后给出n个串,这些串的总长度不超过5e5,你要删除最少的单词(并且只能是后缀),使得 ...
- C Alyona and Spreadsheet Codeforces Round #401(Div. 2)(思维)
Alyona and Spreadsheet 这就是一道思维的题,谈不上算法什么的,但我当时就是不会,直到别人告诉了我,我才懂了的.唉 为什么总是这么弱呢? [题目链接]Alyona and Spre ...
- Codeforces Round #401 (Div. 2) A B C 水 贪心 dp
A. Shell Game time limit per test 0.5 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #401 (Div. 1) C(set+树状数组)
题意: 给出一个序列,给出一个k,要求给出一个划分方案,使得连续区间内不同的数不超过k个,问划分的最少区间个数,输出时将k=1~n的答案都输出 比赛的时候想的有点偏,然后写了个nlog^2n的做法,T ...
- 【贪心】Codeforces Round #401 (Div. 2) D. Cloud of Hashtags
从后向前枚举字符串,然后从左向右枚举位. 如果该串的某位比之前的串的该位小,那么将之前的那串截断. 如果该串的某位比之前的串的该位大,那么之前那串可以直接保留全长度. 具体看代码. #include& ...
随机推荐
- C# Global.asax文件里实现通用防SQL注入漏洞程序(适应于post/get请求)
可使用Global.asax中的Application_BeginRequest(object sender, EventArgs e)事件来实现表单或者URL提交数据的获取,获取后传给SQLInje ...
- xcode7/ios9中 低版本app运行时,屏幕上下出现黑边的问题
xcode从低版本升级至 7.0或更高版本后,某些低版本app再次编译运行后,发现app在设备上运行时,会在上端和底部 出现黑边的现象.这导致app的展示界面跟缩水了一样,变得十分丑陋. 对于这一问题 ...
- java设计模式----迭代子模式
顺序访问聚集中的对象,主要用于集合中.一是需要遍历的对象,即聚集对象,二是迭代器对象,用于对聚集对象进行遍历访问. 迭代子模式为遍历集合提供了统一的接口方法.从而使得客户端不需要知道聚集的内部结构就能 ...
- 关于Properties的用法的详细解释
如果不熟悉 java.util.Properties类,那么现在告诉您它是用来在一个文件中存储键-值对的,其中键和值是用等号分隔的.(如清单 1 所示).最近更新的java.util.Properti ...
- 【node】------module.exports&&exports之间的区别------【巷子】
1.再讲module.exports 与exports之间的区别的时候我们先来回顾一下js里面的引用传递 001.引用传递 var arr = [10,20,30]; var newarr = arr ...
- Jenkins可持续集成
Jenkins 平台安装部署 基于Java开发的持续集成工具,需要安装Java JDK软件 (1).Jenkins稳定版下载地址:wget http://updates.jenkins-ci.org ...
- SQL Server 存储过程生成流水号
SQL Server利用存储过程生成流水号 USE BiddingConfig SET QUOTED_IDENTIFIER ON SET ANSI_NULLS ON GO -- =========== ...
- 南京网络赛E-AC Challenge【状压dp】
Dlsj is competing in a contest with n (0 < n \le 20)n(0<n≤20) problems. And he knows the answe ...
- JavaScript原型链的理解
JavaScript中的每一个对象都有prototype属性,我们称之为原型,而原型的值也是一个对象,因此它有自己的原型,这样就串联起来形成了一条原型链.原型链的链头是object,它的prototy ...
- uchome 缓存生成
一.uchome的缓存目录 ---------data此目录要有777权限 (1)模板文件缓存机制 1:在要显示的页面通过include template($name) 语句来包含被编译后的模板文件 ...