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 ...
随机推荐
- Domino Web中隐藏附件选择框
只要在Web Form的最后加入以下代码,并选择Pass-Tru HTML,即可隐藏默认的附件选择框. <div id=attach Style="display:none" ...
- Java RSA加密以及验签
签名加密以及验签工具类: 一般秘钥分为3个key 1.自己生成的私钥, 2.通过私钥生成的公钥1 3.通过提交公钥1给某宝,获取的公钥2. RSA公钥加密算法简介 非对称加密算法.只有短的RSA钥匙才 ...
- 巨蟒python全栈开发-第14天 内置函数2 递归 二分查找
一.今日内容总览 1.内置函数补充 repr() 显示出字符串的官方表示形式 chr() arscii码中的字,转换成位置 ord() arscii码中的位置,转换成字2.递归 自己调用自己 两个口: ...
- AVCaptureInput和AVCaptureOutput子类
1.AVCaptureInput AVCaptureDeviceInput:用于从AVCaptureDevice对象捕获数据. AVCaptureScreenInput:从macOS屏幕上录制的一种捕 ...
- iOS核心动画详解(一)
前言 这篇文章主要是针对核心动画(Core Animation)的讲解,不涉及UIView的动画.因为内容较多,这篇文章会分为几个章节来进行介绍.本文主要是介绍核心动画的几个类之间的关系和CAAnim ...
- linux7开机自启动东方通tongweb
自启动服务: 可以通过把TongWeb设置为系统服务来实现. 具体实现: 以root用户进行操作,在/etc/init.d目录下编写TongWeb的服务脚本tongweb,用来控制TongWeb的启动 ...
- Compilation failed: internal java compiler error
在Idea中编译时出现这个错误:Error:java: Compilation failed: internal java compiler error. Information:Using java ...
- MySQL中的共享锁与排他锁
MySQL中的共享锁与排他锁 在MySQL中的行级锁,表级锁,页级锁中介绍过,行级锁是Mysql中锁定粒度最细的一种锁,行级锁能大大减少数据库操作的冲突.行级锁分为共享锁和排他锁两种,本文将详细介绍共 ...
- CNI Proposal 摘要
原文连接:https://github.com/containernetworking/cni/blob/master/SPEC.md General consideration CNI的想法是先让容 ...
- Swift学习笔记四:数组和字典
Swift 提供两种集合类型来存储集合,数组和字典. 数组是一个同类型的序列化列表集合.字典是一个能够使用相似于键的唯一标识符来获取值的非序列化集合.也就是说数组是有序的.字典是无序的. 一. 数 ...