Codeforces Div3 #498 A-F
A. Adjacent Replacements
执行1e9次命令,输出的最后数组的样子
一个奇数执行两次命令 会变回原来的数字
一个偶数只会执行一次命令 变成比自身小1的数
#include<bits/stdc++.h>
#define int long long
#define MAX(a,b,c) max(a,max(b,c))
#define MIN(a,b,c) min(a,min(b,c))
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
typedef unsigned long long uLL;
using namespace std;
const int maxn=1e6+;
const int INF=1e15;
int a[maxn];
int32_t main()
{
int n; cin>>n;
for(int i=;i<=n;i++) cin>>a[i];
for(int i=;i<=n;i++)
{
if(a[i]%==) cout<<a[i]<<" ";
else cout<<a[i]-<<" ";
}
}
A.cpp
B. Polycarp's Practice
给出 数组n k 将数组分成k段(每段至少1) 求k段每段中最大数的和的最大值
排序一下 最大的n个数就是数字最大的
将k个最大数标记 我用的map 标记
然后扫一遍输出 第一个标记前的非标记的个数和自身 标记和标记间的个数 最后一个标记和标记间还有标记后的个数
例子
1 2 3 2 1 4 1 5 1 3
mp[3]=1; mp[2]=1; mp[5]=1; 这是被标记的
分成的为 1 2 3 2 1 4 1 5 1 3 三段
#include<bits/stdc++.h>
#define int long long
#define MAX(a,b,c) max(a,max(b,c))
#define MIN(a,b,c) min(a,min(b,c))
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
typedef unsigned long long uLL;
using namespace std;
const int maxn=1e6+;
const int INF=1e15;
int a[maxn];
int b[maxn];
map<int,int> mp;
int32_t main()
{
int n,k; cin>>n>>k;
for(int i=;i<=n;i++) {cin>>a[i]; b[i]=a[i];}
sort(a+,a++n);
int ans=; int num=k;
for(int i=n;i>=;i--)
{
if(num) {ans+=a[i]; num--; mp[a[i]]++; }
}
cout<<ans<<endl;
int t=;
for(int i=;i<=n;i++)
{
if(mp[b[i]]&&k!=)
{
cout<<t+<<" "; t=; mp[b[i]]--; k--;
}
else
{
t++;
}
}
cout<<t<<" ";
}
B.cpp
C. Three Parts of the Array
给你一个数组 你分成3段(长度可以为0) 是第一段和最后一段的和相等 求怎么样分让第一段和最大
定义 num1=num3=0;
一个从前往后找 一个从后往前找 谁少谁加 一样就记录答案 都加一个数
#include<bits/stdc++.h>
#define int long long
#define MAX(a,b,c) max(a,max(b,c))
#define MIN(a,b,c) min(a,min(b,c))
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
typedef unsigned long long uLL;
using namespace std;
const int maxn=1e6+;
const int INF=1e15;
int a[maxn];
int32_t main()
{
int n; cin>>n;
for(int i=;i<=n;i++) scanf("%d",&a[i]);
int num1=;
int num3=;
int ans=;
int i=;
int j=n+;
while(i+!=j)
{
if(num1==num3)
{
ans=max(num1,num3); if(i+==j) break;
i++;j--; num1+=a[i]; num3+=a[j];
}
else if(num1<num3)
{
i++; num1+=a[i];
}
else if(num1>num3)
{
j--; num3+=a[j];
}
}
if(num1==num3) ans=num1;
cout<<ans<<endl;
}
C.cpp
D. Two Strings Swaps
给你两个字符串 长度一样 修改第一个字符串 再通过变换 让两个字符串一样
变换规则
- Choose any index ii (1≤i≤n1≤i≤n) and swap characters aiai and bibi;
- Choose any index ii (1≤i≤n1≤i≤n) and swap characters aiai and an−i+1an−i+1;
- Choose any index ii (1≤i≤n1≤i≤n) and swap characters bibi and bn−i+1bn−i+1;
找 ai a(n-i) bi ,b(n-i) 中最大有多少对字符一样
当ai=a(n-i) 时特判 bi!=b(n-1) 要修改两个
#include<bits/stdc++.h>
#define int long long
#define MAX(a,b,c) max(a,max(b,c))
#define MIN(a,b,c) min(a,min(b,c))
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
typedef unsigned long long uLL;
using namespace std;
const int maxn=1e6+;
const int INF=1e15;
int32_t main()
{
int n; cin>>n;
string ss;cin>>ss;
string tt;cin>>tt;
int ans=; for(int i=;i<n/;i++)
{ int t=;
if(ss[i]==ss[n--i]) t++;
if(tt[i]==tt[n--i]) t++;
else t--;
int w=;
if(ss[i]==tt[i]) w++;
if(ss[n--i]==tt[n--i]) w++;
int k=;
if(ss[i]==tt[n--i]) k++;
if(ss[n-i-]==tt[i]) k++;
int num=MAX(t,w,k);
if(num==) ans+=;
if(num==) ans+=;
}
if(n%==)
{
if(ss[n/]!=tt[n/]) ans++;
}
cout<<ans<<endl; }
D.cpp
E. Military Problem
树不会很会 表述(大家多多见谅)
有一个树
q次询问 问节点 x 是否有 y 个子节点 没有输出 -1 有输出第y个字节点上的书
#include<bits/stdc++.h>
#define int long long
#define MAX(a,b,c) max(a,max(b,c))
#define MIN(a,b,c) min(a,min(b,c))
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
typedef unsigned long long uLL;
using namespace std;
const int maxn=1e6+;
const int INF=1e15;
vector<int> tree[maxn];
int vis[maxn];
int sum[maxn];
int s[maxn];
int cnt=;
int dfs(int id)
{
int ans=;
vis[id]=cnt;
s[cnt]=id;
cnt++;
for(int i=;i<tree[id].size();i++)
ans+=dfs(tree[id][i]);
return sum[id]+=ans;
}
int32_t main()
{
int n,p; cin>>n>>p;
for(int i=;i<=n;i++)
{
int a; cin>>a; tree[a].pb(i);
}
dfs();
while(p--)
{
int x,y;
cin>>x>>y;
if(sum[x]<y) cout<<"-1"<<endl;
else
cout<<s[vis[x]+y-]<<endl;
}
}
E.cpp
F. Xor-Paths
从(1,1)到 (n, m) 找路径中所有数的异或为k的道路条数 (只能下 右)
直接dfs会超时 用两端dfs
#include<bits/stdc++.h>
#define int long long
#define MAX(a,b,c) max(a,max(b,c))
#define MIN(a,b,c) min(a,min(b,c))
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
typedef unsigned long long uLL;
using namespace std;
const int maxn=+;
const int INF=0x3f3f3f3f;
int a[][];
map<int,int> mp[][];
int n,m,k;
int MAX;
int ans;
void dfs1(int x,int y,int num)
{
num=num^a[x][y];
if(x+y==MAX)
{
mp[x][y][num]++;
}
else
{
int x1=x+;
int y1=y;
if(x1>= && x1<=n && x1>= && y1<=m)
{
dfs1(x1,y1,num);
}
int x2=x;
int y2=y+;
if(x2>= && x2<=n && x2>= && y2<=m)
{
dfs1(x2,y2,num);
}
}
}
void dfs2(int x,int y,int num)
{
if(x+y==MAX)
{
ans+= mp[x][y][num];
}
else
{
num=num^a[x][y];
int x1=x-;
int y1=y;
if(x1>= && x1<=n && x1>= && y1<=m)
{
dfs2(x1,y1,num);
}
int x2=x;
int y2=y-;
if(x2>= && x2<=n && x2>= && y2<=m)
{
dfs2(x2,y2,num);
}
}
}
int32_t main()
{
cin>>n>>m>>k;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
cin>>a[i][j];
}
}
if(n==&&m==)
{
if(k==a[][]) cout<<<<endl;
else cout<<<<endl;
return ;
} MAX=max(n,m);
dfs1(,,);
dfs2(n,m,k);
cout<<ans<<endl;
}
F.cpp
Codeforces Div3 #498 A-F的更多相关文章
- Codeforces Div3 #501 A-E(2) F以后补
感觉自己有点强迫症 不都写出来就找理由不写题解 http://codeforces.com/contest/1015 题目链接 A. Points in Segments 题目意思 n个线段 ...
- CodeForces Round #498 div3
A: 题目没读, 啥也不会的室友帮我写的. #include<bits/stdc++.h> using namespace std; #define Fopen freopen(" ...
- Codeforces Round #535 (Div. 3) [codeforces div3 难度测评]
hhhh感觉我真的太久没有接触过OI了 大约是前天听到JK他们约着一起刷codeforces,假期里觉得有些颓废的我忽然也心血来潮来看看题目 今天看codeforces才知道居然有div3了,感觉应该 ...
- Codeforces Gym 100500F Problem F. Door Lock 二分
Problem F. Door LockTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/at ...
- Codeforces Gym 100002 Problem F "Folding" 区间DP
Problem F "Folding" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/ ...
- 树形DP ---- Codeforces Global Round 2 F. Niyaz and Small Degrees引发的一场血案
Aspirations:没有结果,没有成绩,acm是否有意义?它最大的意义就是让我培养快速理解和应用一个个未知知识点的能力. ————————————————————————————————————— ...
- Codeforces Round #498 (Div. 3) 简要题解
[比赛链接] https://codeforces.com/contest/1006 [题解] Problem A. Adjacent Replacements [算法] 将序列中的所有 ...
- codeforces Educational Codeforces Round 24 (A~F)
题目链接:http://codeforces.com/contest/818 A. Diplomas and Certificates 题解:水题 #include <iostream> ...
- Codeforces Gym 100286F Problem F. Fibonacci System 数位DP
Problem F. Fibonacci SystemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudg ...
随机推荐
- Oracle.PL/SQL高级
一.匿名块 .使用returning ... INTO 保存增删改表数据时的一些列的值 ()增加数据时保存数据 DECLARE v_ename emp.ename%TYPE; v_sal emp.sa ...
- Oracle.练习题
2018-07-31 ---练习3 ---创建sporter表 create table sporter( sporterid ) constraint sport_id primary key, s ...
- js中有哪几种函数?
匿名函数,回调函数,递归函数,构造函数
- unity中鼠标按下加速漫游,鼠标抬起减速漫游。
private bool IsMouseUpOrDown=true; //一开始默认是鼠标抬起状态 if (Input.GetMouseButtonDown(1)) //鼠标按下的瞬间状态 { IsM ...
- 每天CSS学习之text-overflow
text-overflow是CSS3的一个属性,其作用是当文本溢出包含它的元素时,应该裁剪还是将多余的字符用省略号来表示. 该属性一般和overflow:hidden属性一起使用. text-over ...
- mybatis动态sql #和$的区别
$和#都支持动态sql:就是你传什么它就是什么 区别: 1.#可以防止sql注入在sql执行时显示 '?' 比$安全 SELECT * FROM table WHERE id = ? 2.在使用#传入 ...
- PM2报错‘Spawning PM2 daemon with pm2_home...’的解决方案
问题 在某次因为SRE升级域名问题,导致了Node服务器代码死循环了,产生的504(Gateway timeout)错误. 登录到机器上看,正在用pm2查问题的原因中,突然发现错误从504变成的502 ...
- 11.Python-第三方库requests详解(三)
Response对象 使用requests方法后,会返回一个response对象,其存储了服务器响应的内容,如上实例中已经提到的 r.text.r.status_code……获取文本方式的响应体实例: ...
- mvc项目中实现备份数据库(sqlserver2005)
功能要求:mvc项目,实现数据库备份(bak文件) 实现步骤:<a id="backupDB" href="javascript:" >数据库备份& ...
- Zookeeper与Paxos
初识Zookeeper zookeeper为分布式应用提供了高效且可靠的分布式协调服务,提供了诸如统一命名服务.配置管理和分布式锁等分布式的基础服务. 在解决分布式数据一致性方面,zk没有直接采用Pa ...