Codeforces #402
Codeforces #402
Codeforces 779A Pupils Redistribution
链接:http://codeforces.com/contest/779/problem/A
题意:有A组和B组,每组有n个人,A组里面每个人的分值为\(a_i\),B组里面每个人的分值为\(b_i\),\(1\le a_i ,b_i\le5\),至少双方要交换多少人才能使使A组里面每种分值的人要和B组里面一样多。
思路:如果两个在\(i\)这个分值上面人数之差为奇数,就不行,如果为偶数,假如A比B多,那么要A跟B交换差的一半,如果B的在\(j\)这个分值上面比A多,那么刚好就可以跟\(i\)这个分值的交换,所以答案就是他们之间差的一半。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
using namespace std;
int cnta[10],cntb[10];
int main(){
int n;
while(scanf("%d",&n)!=EOF){
memset(cnta,0,sizeof(cnta));
memset(cntb,0,sizeof(cntb));
int x;
for(int i=1;i<=n;i++){
scanf("%d",&x);
cnta[x]++;
}
for(int i=1;i<=n;i++){
scanf("%d",&x);
cntb[x]++;
}
bool flag=true;
int cnt=0;
for(int i=1;i<=5;i++){
int num=abs(cnta[i]-cntb[i]);
if(num>0){
if(num%2==1) {
flag=false;
break;
}
else cnt+=num/2;
}
}
if(flag){
printf("%d\n",cnt/2);
}
else {
printf("-1\n");
}
}
}
Codeforces 779B Weird Rounding
链接:http://codeforces.com/contest/779/problem/B
题意:有两个数\(n,k\),最少要删掉\(n\)里面多少个数字,才能使得\(n\)整除\(10^k\)。30020如果删掉2就为3000,如果为1000,删掉1,为000,全部删掉为0,有多个0要删到只剩1个0才算0。
思路:贪心,肯定是从后面开始删,每次删完判断一次,不行继续,如果删到只剩下0了但是不止一个0,这个时候就可以直接删掉当前数的位数-1就只剩下一个0了。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
using namespace std;
int a[20];
int cnt;
long long mod;
void change(long long tmp){
cnt=0;
while(tmp){
a[cnt++]=tmp%10;
tmp/=10;
}
}
long long changenum(int num[],int len){
long long tmp=0;
long long dig=1;
for(int i=0;i<len;i++){
tmp+=num[i]*dig;
dig*=10;
}
return tmp;
}
int ans;
int main(){
long long num;
int k;
while(scanf("%I64d",&num)!=EOF){
scanf("%d",&k);
mod=1;
for(int i=1;i<=k;i++){
mod*=10;
}
ans=0;
if(num%mod==0) {
printf("0\n");
continue;
}
else {
bool flag=false;
while(!flag){
if(num==0&&cnt!=1){
ans+=(cnt-2);
}
if(num%mod==0){
flag=true;
break;
}
change(num);
for(int i=0;i<cnt;i++){
if(a[i]!=0){
for(int j=i+1;j<cnt;j++){
a[j-1]=a[j];
}
ans++;
break;
}
}
num=changenum(a,cnt-1);
}
printf("%d\n",ans);
}
}
return 0;
}
Codeforces 779C Dishonest Sellers
链接:http://codeforces.com/contest/779/problem/C
题意:有\(n\)个商品,每个商品有个当前价格\(a_i\),和一个星期之后的折扣价格\(b_i\),现在一定要在当前至少买\(k\)个物品,问怎么样购买才能使买完\(n\)个商品花费最低
思路:原价=\(a1+...+a_n\),折后价=\(b_1+...+b_k+...+a_n\),原价-折后价=\(\sum_{i=1}^{k}(a_i-b_i)\),所以要想折后价低,就要选择两者差价尽量大的,也就是差价小的买原价。注意如果当折后价比原价贵,那么就可以直接选择原价。
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int MAXN=2e5+10;
int n,k;
int a[MAXN],b[MAXN];
bool flag[MAXN];
struct node{
int id;
int num;
};
node c[MAXN];
bool cmp(node a,node b){
return a.num<b.num;
}
int main(){
while(scanf("%d %d",&n,&k)!=EOF){
memset(flag,false,sizeof(flag));
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++){
scanf("%d",&b[i]);
}
for(int i=1;i<=n;i++){
c[i].num=a[i]-b[i];
c[i].id=i;
if(c[i].num<=0) flag[c[i].id]=true;
}
sort(c+1,c+1+n,cmp);
long long ans=0;
for(int i=1;i<=k;i++) flag[c[i].id]=true;
for(int i=1;i<=n;i++){
if(!flag[i]) ans+=b[i];
else ans+=a[i];
}
printf("%lld\n",ans);
}
return 0;
}
Codeforces 779D String Game
链接:http://codeforces.com/contest/779/problem/D
题意:现在有一个字符串原串\(t\),有一个模式串\(p\),现在保证在原串里面删除一些字母,\(p\)仍然存在于\(t\)里面(子序列),给出删除的顺序\(a_i\),问最多能删到哪一步\(p\)里面还包含\(t\)。
思路:二分答案,O(n)判定子序列
Codeforces #402的更多相关文章
- codeforces 402 D. Upgrading Array(数论+贪心)
题目链接:http://codeforces.com/contest/402/problem/D 题意:给出一个a串和素数串b .f(1) = 0; p为s的最小素因子如果p不属于b , 否则 . a ...
- CodeForces 402 E Strictly Positive Matrix
Strictly Positive Matrix 题解: 如果原来的 a[i][j] = 0, 现要 a[i][j] = 1, 那么等于 sum{a[i][k] + a[k][j]} > 1. ...
- Codeforces Round #402 (Div. 2)
Codeforces Round #402 (Div. 2) A. 日常沙比提 #include<iostream> #include<cstdio> #include< ...
- Codeforces Round #402 (Div. 2) A+B+C+D
Codeforces Round #402 (Div. 2) A. Pupils Redistribution 模拟大法好.两个数列分别含有n个数x(1<=x<=5) .现在要求交换一些数 ...
- Codeforces Round #402 (Div. 2) D. String Game
D. String Game time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...
- Codeforces Round #402 (Div. 2) 阵亡记
好长时间没有打Codeforces了,今天被ysf拉过去打了一场. lrd也来参(nian)加(ya)比(zhong)赛(sheng) Problem A: 我去,这不SB题吗.. 用桶统计一下每个数 ...
- CodeForces Round #402 (Div.2) A-E
2017.2.26 CF D2 402 这次状态还算能忍吧……一路不紧不慢切了前ABC(不紧不慢已经是在作死了),卡在D,然后跑去看E和F——卧槽怎么还有F,早知道前面做快点了…… F看了看,不会,弃 ...
- Diary of Codeforces Round #402 (Div. 2)
这一场的表现可以用"全程智障"4个字,生动传神地描述出来. About 写题: A. 写了一堆if比较大小, 这很勤勉.(绝对值君对自己の存在感为0表示很难过.) B. 题,直接读 ...
- http://codeforces.com/contest/402/problem/E
E. Strictly Positive Matrix time limit per test 1 second memory limit per test 256 megabytes input s ...
随机推荐
- Flutter 即学即用系列博客——06 超实用 Widget 集锦
本篇文章我们来讲讲一些比较常用的 Widget. 大家验证的时候使用下面的代码替换 main.dart 代码,然后在 //TODO 语句返回下面常用 Widget 示例的代码. import 'pac ...
- AngularJs with Webpackv1 升級到 Webpack4
本篇記錄一下升級的血淚過程 請注意升級前請先創一個新目錄將升級應用與舊應用隔離 1. 需要將相關的套件做統一升級的動作,已確認需要升級所有舊的loaders 其它應用的套件可先不做升級的動作 (如果編 ...
- kali linux 2019.1 替换burpsuite pro 1.7.37
下面全部实操: 先切换JDK版本为1.8.执行 update-alternatives --config java 然后会显示多个jdk版本,1.8选3 输入java –version会显示jdk为1 ...
- footer固定在页面底部的实现方法总结
方法一:footer高度固定+绝对定位 HTML代码: <body> <header>头部</header> <main>中间内容</main&g ...
- 林业资源遥感航拍监测GIS系统
航拍监测.遥感监测在林业有害生物(松材线虫病监测).森林防火监测.森林滥砍滥伐.林地侵占.林地违规开发监测等方面应用,将大大提升林业资源监测水平, 针对已有森林资源大量流失,滥砍滥伐现象普遍存在的事实 ...
- vue的组件化运用(数据在两个组件互传,小问题总结)
一.vue的组件化应用 首先,知道有哪些相关的属性需要用到,再慢慢去理解,运用. 1.两个vue页面 2. slot占位符(可用可不用) 3.props内置属性 4.watch监听函数 5.impor ...
- deepin linux 学习笔记(二)——文本编辑器
目录 deepin linux 学习笔记(二)--文本编辑器 前言 nano 小巧的命令行编辑器 通用 编辑 定位 排版 配置 vim 思路独特的超级编辑器 命令模式 插入模式 底线模式(末行模式) ...
- Java中字符串相加和字符串常量相加区别
有一道这样的程序: public class TestStringDemo { public static void main(String[] args) { String s1 = "P ...
- springCloud feign使用/优化总结
基于springCloud Dalston.SR3版本 1.当接口参数是多个的时候 需要指定@RequestParam 中的value来明确一下. /** * 用户互扫 * @param uid 被扫 ...
- golang语言示例
package main import "fmt" /* my first programmer in go */ func fib(n int) int{ if n<2{ ...