codeforces 357
C 题意:
###n个勇士编号1-n,m个回合对战,每个回合由仍留在游戏里的编号Li~Ri的人参加,胜者为Xi,输的人退出游戏。
###求一个a1-an的序列,若ai为胜者,则ai=0,否则ai=打败ai的勇士的编号。
思路:
解法一:使用set,输的人擦除。
#include<bits/stdc++.h>
using namespace std;
set<int> s;
set<int>::iterator it,lp;
int ans[]={},del[];
int main() {
int n,m,l,r,win;
scanf("%d%d",&n,&m);
for(int i=;i<=n;++i) s.insert(i);
while(m--) {
scanf("%d%d%d",&l,&r,&win);
lp=s.lower_bound(l);
int cnt=;
for(it=lp;*it<=r&&it!=s.end();++it) {
if(*it==win) continue;
ans[*it]=win;
del[cnt++]=*it;
}
for(int i=;i<cnt;++i) {
s.erase(del[i]);
}
}
for(int i=;i<=n;++i) {
printf("%d ",ans[i]==i?:ans[i]);
}
return ;
}
D 题意:http://codeforces.com/problemset/problem/357/D
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=;
char a[maxn],b[maxn];
ll cnt[maxn][]={};
ll gcd(ll x, ll y) {
return y==?x:gcd(y,x%y);
}
int main() {
ll n,m,g,alen,blen,len,ans=;
scanf("%I64d%I64d",&n,&m);
scanf("%s%s",a,b);
alen=strlen(a);
blen=strlen(b);
g=gcd(alen,blen);
len=alen*blen/g; for(int i=;i<alen;++i)
++cnt[i%g][a[i]-'a']; for(int i=;i<blen;++i)
ans+=cnt[i%g][b[i]-'a']; ans=n*alen-n*alen/len*ans;
printf("%I64d\n",ans);
return ;
}
E 题意:有n个车厢,每个车厢有4个人,其中ai个学生,交换最少的人使得每个车厢的学生数为0或3或4,无法达成输出-1。
思路:贪心。统计车厢内学生为1,2,3,4的数目cnt[1],cnt[2],cnt[3],cnt[4]。
目标为最少的步数,过程中应先配出cnt[3],配不出3时考虑4。
首先考虑cnt[1]不为0时,尽可能让cnt[1]与cnt[2]配对,这样可以同时减少cnt[1]与cnt[2]的数目使二者符合要求,明显最优,即1 2 -> 3。
然后考虑让三个1抱团 1 1 1 -> 3 ;剩下一个1时考虑 1 3 -> 4;剩下两个1时先考虑1 1 3 3 -> 4 4 ,再考虑1 1 -> 2。
这些过程后若cnt[1]!=0,则无解。
考虑cnt[2]不为0时,2 2 2 -> 3 3;剩下一个2时先考虑 2 4 -> 3 3,再考虑 2 3 3 -> 4 4;剩下两个2时考虑 2 2 -> 4。
#include<bits/stdc++.h>
using namespace std;
int cnt[]={};
int main() {
int n,x,ans=,t;
scanf("%d",&n);
for(int i=;i<n;++i) {
scanf("%d",&x);
++cnt[x];
}
if(!cnt[]&&!cnt[]) {
puts("");
return ;
}
if(cnt[]) {
if(cnt[]) { // 1 2 -> 3
t=min(cnt[],cnt[]);
ans+=t;
cnt[]-=t;
cnt[]-=t;
cnt[]+=t;
}
if(cnt[]) {
// 1 1 1 -> 3
ans+=cnt[]/*;
cnt[]+=cnt[]/;
cnt[]%=;
if(cnt[]==) {
if(cnt[]>=) ans+=,cnt[]-=,cnt[]+=; // 1 1 3 3 -> 4 4
else ++ans,++cnt[]; // 1 1 -> 2
}
else if(cnt[]==) {
if(cnt[]) ++ans,--cnt[],++cnt[]; // 1 3 -> 4
else if(cnt[]&&cnt[]<) {
puts("-1");
return ;
}
else cnt[]-=,ans+=2,cnt[3]+=3; // 1 4 4 -> 3 3 3
}
cnt[]=;
}
}
if(cnt[]) {
// 2 2 2 -> 3 3
ans+=cnt[]/*;
cnt[]+=cnt[]/*;
cnt[]%=;
if(cnt[]==) ans+=,cnt[]=,++cnt[]; // 2 2 -> 4
else if(cnt[]==) {
if(cnt[]) ++ans,cnt[]=,--cnt[],cnt[]+=; // 2 4 -> 3 3
else if(cnt[]>=) ans+=,cnt[]=,cnt[]-=,cnt[]+=; // 2 3 3 -> 4 4
else {
puts("-1");
return ;
}
}
}
if(!cnt[]&&!cnt[]) printf("%d\n",ans);
else puts("-1");
return ;
}
codeforces 357的更多相关文章
- Codeforces Round #357 (Div. 2) E. Runaway to a Shadow 计算几何
E. Runaway to a Shadow 题目连接: http://www.codeforces.com/contest/681/problem/E Description Dima is liv ...
- Codeforces Round #357 (Div. 2) D. Gifts by the List 水题
D. Gifts by the List 题目连接: http://www.codeforces.com/contest/681/problem/D Description Sasha lives i ...
- Codeforces Round #357 (Div. 2) C. Heap Operations 模拟
C. Heap Operations 题目连接: http://www.codeforces.com/contest/681/problem/C Description Petya has recen ...
- Codeforces Round #357 (Div. 2) B. Economy Game 水题
B. Economy Game 题目连接: http://www.codeforces.com/contest/681/problem/B Description Kolya is developin ...
- Codeforces Round #357 (Div. 2) A. A Good Contest 水题
A. A Good Contest 题目连接: http://www.codeforces.com/contest/681/problem/A Description Codeforces user' ...
- Codeforces Round #357 (Div. 2) A
A. A Good Contest time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces Round #357 (Div. 2) E 计算几何
传说中做cf不补题等于没做 于是第一次补...这次的cf没有做出来DE D题的描述神奇 到现在也没有看懂 于是只补了E 每次div2都是hack前2~3题 终于打出一次hack后的三题了...希望以后 ...
- Codeforces Round #357 (Div. 2) 优先队列+模拟
C. Heap Operations time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces Round #357 (Div. 2) C
C. Heap Operations time limit per test 1 second memory limit per test 256 megabytes input standard i ...
随机推荐
- 使用ghost硬盘对拷备份系统
公司有台server装了OA系统.要备份数据.同一时候假设系统出错之后可以及时回复.所以有买了块同型号硬盘. 用ghost的硬盘对拷功能,将原硬盘的系统和数据拷到新硬盘上.新硬盘挂到server上.当 ...
- VMThread占CPU高基本上是JVM在频繁GC导致,原因基本上是冰法下短时间内创建了大量对象堆积造成频繁GC。
今天线上一个java进程cpu负载100%.按以下步骤查出原因. 1.执行top -c命令,找到cpu最高的进程的id 2.执行top -H -p pid,这个命令就能显示刚刚找到的进程的所有线程的资 ...
- js引入script
引入再删除,节省资源. <!DOCTYPE html> <html> <head lang="en"> <meta charset=&qu ...
- 滚动监听: bootstrap 的scrollspy
滚动监听 bootstrap 的scrollspy,需要借助.nav样式,活动的部分是加 .active类.本身导航没有position:fixed,需要自己加入 滚动监听.只有滚动和监听,只有默认锚 ...
- Drupal 8 提供REST服务实例
drupal8 的核心模块已经支持REST服务. 这样的话使用drupal 对外提供web service 变的简单了. 测试一下d8 的webservice : extend 中的 依赖模块:全部启 ...
- SOE不能进入断点调试
一.前言 任何程序开发,如果不能进入断点调试,是非常的痛苦的. 如果有过SOE开发经验的人都知道,SOE开发过程中调试是非常麻烦的.任何在SOE开发模板中的修改都需要重新编译工程,重新生成.soe 文 ...
- WebService 入门
1. 远程调用技术 2. WebService 概述 WebService 是使用 Http 发送 SOAP 协议数据的一种远程调用技术; WebService 需要开发客户端; WebService ...
- Python高级教程-列表生成式
List Comprehensions(列表生成式) 列表生成式,是Python内置的非常简单却强大的可以用来创建list的生成式. 例如,要生成list:[1,2,3,4,5,6,7,8,9,10] ...
- Tornado介绍与其Web应用结构
1.介绍 tornado是一个Python web框架和异步网络库 起初由 FriendFeed 开发. 通过使用非阻塞网络I/O, Tornado 可以支持上万级的连接,处理 长连接, WebSoc ...
- zookeeper3.4.5集群安装
机器配置: 机器 Hostname user 192.168.169.139 node139 hadoop 192.168.169.140 node140 hadoop 192.168.169.141 ...