依图
1.地铁进站出站,计算车费
理论上a[n-1],b[0]应该对车费没影响的,但是数据不知道为什么,是有影响的。。。
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
using namespace std;
int n;
int in[1001];
int out[1001];
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&in[i]);
}
for(int i=0;i<n;i++){
scanf("%d",&out[i]);
}
int ans=0,tem=0;
for(int i=0;i<n;i++){
ans=ans+tem;
tem=tem-out[i];
tem=tem+in[i];
}
cout<<ans<<endl;
return 0;
}
2.Dijkstra求最短路径
用邻接矩阵存储的,过了0.6
应该用邻接表
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<set>
#include<cstring>
using namespace std;
const int maxn=1005;
const int INF= 1<<30;
int n,m;
int st,ed;
int G[maxn][maxn];
int cake[maxn];
int d[maxn];
int c[maxn];
bool vis[maxn]={false};
void Dijkstra(int s){
fill(d, d+maxn, INF);
memset(c, 0, sizeof(c));
d[s]=0;
c[s]=cake[s];
for(int i=1;i<=n;i++){
int u=-1, MIN=INF;
for(int j=1;j<=n;j++){
if(vis[j]==false&&d[j]<MIN){
u=j;
MIN=d[j];
}
}
if(u==-1) return;
vis[u]=true;
for(int v=1;v<=n;v++){
if(vis[v]==false&&G[u][v]!=INF){
if(d[u]+G[u][v]<d[v]){
d[v]=d[u]+G[u][v];
c[v]=c[u]+cake[v];
}else if(d[u]+G[u][v]==d[v]){
if(c[u]+cake[v]>c[v]){
c[v]=c[u]+cake[v];
}
}
}
}
}
}
int main(){
scanf("%d %d %d %d", &n,&m, &st,&ed);
for(int i=1;i<=n;i++){
scanf("%d", &cake[i]);
}
fill(G[0],G[0]+maxn*maxn,INF);
for(int j=0;j<m;j++){
int u,v,time;
scanf("%d %d %d",&u,&v,&time);
G[u][v]=G[v][u]=time;
}
Dijkstra(st);
printf("%d %d\n",d[ed], c[ed]);
return 0;
}
3.组合成3的倍数
这道题可以不用存储数组元素,一遍遍历就可以做
如果k=0,统计元素为3的倍数的个数ans=n3;
统计元素除3余1的个数n1,除3余2的个数n2,这两种数两两组合就是3的倍数,
如果k<min(n1,n2),ans=n3+k;
如果k>=min(n1,n2),先计算ans=n3+min(n1,n2),
剩下的元素要么全部都是除3余1的,要么就全部都是除3余2的,他们都是要3个相加才是3的倍数,
k=k-min(n1,n2),k=k/2,假设剩下的是除3余1的,n1=n1-n2,n1=n1/3,
然后ans=ans+min(k,n1)
代码如下:
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
int n,k;
vector<int> shu;
int main(){
scanf("%d %d",&n,&k);
int t;
for(int i=0;i<n;i++){
scanf("%d",&t);
shu.push_back(t);
}
int ans=0,n1=0,n2=0;
for(int i=0;i<n;i++){
if(shu[i]%3==0)
ans++;
else{
if(shu[i]%3==1)
n1++;
else
n2++;
}
}
int minn=min(n1,n2);
if(k<=minn)
ans=ans+k;
else{
ans=ans+minn;
k=(k-minn)/2;
if(n1<n2)
minn=n2-minn;
else
minn=n1-minn;
minn=minn/3;
ans=ans+min(k,minn);
}
cout<<ans<<endl;
return 0;
}
python版本:(waple)
from collections import defaultdict
n, k = map(int, input().strip().split())
a = list(map(int, input().strip().split()))
d = defaultdict(int)
for num in a:
d[num%3] += 1
res = d[0]
while k > 0 and (d[1] + d[2] > 1):
if d[1] > 0 and d[2] > 0:
d[1] -= 1
d[2] -= 1
res += 1
elif d[1] > 1:
d[1] -= 2
d[2] += 1
elif d[2] > 1:
d[2] -= 2
d[1] += 1
else:
break
k -= 1
print(res)
4.博物馆的墙
神仙题。。。。。。
京东
京东的题有点难顶
1.消消乐
上面样例输出是3(消掉1,下落之后消掉2,最后剩余3个数)
python代码如下:(Waple)
from copy import deepcopy
b = []
for _ in range(5):
b.append(list(map(int, input().strip().split())))
ans = 25
def dfs(b, i, j, visited):
res = 1
visited[i][j] = False
for m, n in ((0, 1), (1, 0), (-1, 0), (0, -1)):
new_i, new_j = i+m, j+n
if 0 <= new_i <= 4 and 0 <= new_j <= 4 and b[new_i][new_j] == b[i][j] and visited[new_i][new_j]:
visited[new_i][new_j] = False
res += dfs(b, new_i, new_j, visited)
return res
def op_dfs(b, i, j, visited):
visited[i][j] = False
tmp = b[i][j]
b[i][j] = 'x'
for m, n in ((0, 1), (1, 0), (-1, 0), (0, -1)):
new_i, new_j = i+m, j+n
if 0 <= new_i <= 4 and 0 <= new_j <= 4 and b[new_i][new_j] == tmp and visited[new_i][new_j]:
op_dfs(b, new_i, new_j, visited)
def sch(b):
candi = []
visited = [[False if b[i][j] == 'x' else True for j in range(5)] for i in range(5)]
for i in range(5):
for j in range(5):
if visited[i][j]:
length = dfs(b, i, j, visited)
if length > 2:
candi.append((i, j))
return candi
def calculate(b):
global ans
res = 0
for line in b:
for num in line:
if num != 'x':
res += 1
ans = min(ans, res)
def drop(b):
ans = []
new_a = []
for j in range(5):
res = []
for i in range(4, -1, -1):
if b[i][j] != 'x':
res.append(b[i][j])
res += ['x'] * (5 - len(res))
new_a.append(res[::-1])
for line in zip(*new_a):
ans.append(list(line))
return ans
def operate(b):
candidates = sch(b)
for i, j in candidates:
visited = [[False if b[i][j] == 'x' else True for j in range(5)] for i in range(5)]
new_a = deepcopy(b)
op_dfs(new_a, i, j, visited)
calculate(new_a)
new_a = drop(new_a)
operate(new_a)
operate(b)
print(ans)
2.拼接迷宫
过了0.73
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
int t;
int m,n;
char map[301][301];
int vis[901][901];
bool findp(int i,int j){
if(i<=0||j<=0||i>=3*m-1||j>=3*m-1)
return true;
if(map[i%m][j%n]=='#'||vis[i][j]==1)
return false;
vis[i][j]=1;
return findp(i+1,j)||findp(i,j+1)||findp(i-1,j)||findp(i,j-1);
}
int main(){
cin>>t;
int sj,sk;
for(int i=0;i<t;i++){
cin>>m>>n;
for(int j=0;j<m;j++){
scanf("%s",&map[j]);
for(int k=0;k<n;k++)
if(map[j][k]=='S'){
sj=j;
sk=k;
}
}
//cout<<sj<<sk;
memset(vis,0,sizeof(vis));
if(findp(m+sj,n+sk))
printf("Yes");
else
printf("No");
}
return 0;
}
/*
2
2 2
.#
#S
3 3
...
###
#S#
*/
面试
给定一个字符串和一个模式,判断字符串属不属于这个模式
比如模式为abcc,那么gdff,这个就符合这个模式,tyui就不符合
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
using namespace std;
bool fuhe(string s, string t) {
if(s.size()!=t.size())
return false;
vector<int> nums(256, 0);
vector<int> count(256, 0);
for(int i = 0; i < s.size(); ++i) {
if(nums[s[i]] == 0 && count[t[i]] == 0) {
nums[s[i]] = t[i];
count[t[i]] = 1;
}
else if(nums[s[i]] != t[i]) {
return false;
}
}
return true;
}
int main(){
string s1,s2;
cin>>s1>>s2;
if(fuhe(s1,s2)){
printf("true");
}
else{
printf("false");
}
return 0;
}
Kick Start——谷歌
1.Cherries Mesh
Problem
Your friend is recently done with cooking class and now he wants to boast in front of his school friends by making a nice dessert. He has come up with an amazing dessert called Cherries Mesh. To make the dish, he has already collected cherries numbered 1 to N. He has also decided to connect each distinct and unordered pair of cherries with a sweet strand, made of sugar. Sweet strands are either red or black, depending on the sugar content in them. Each black strand contains one units of sugar, and each red strand contains two units of sugar.
But it turns out that the dessert is now too sweet, and these days his school friends are dieting and they usually like dishes with less sugar. He is really confused now and comes to your rescue. Can you help him find out which all sweet strands he should remove such that each pair of cherries is connected directly or indirectly via a sugar strand, and the dish has the minimum possible sugar content?
Input
The first line of input gives the number of test cases, T.
Each test case begins with a line containing two integers N and M, the number of cherries and the number of black sweet strands, respectively.
Then M lines follow, each describing a pair of cherries connected to a black strand. The i-th line contains cherries numbered Ci and Di, it indicates that Ci and Di cherry are connected with a black strand of sugar.
Note: Any other pair of cherries not present in the input means that they are connected by a red strand.
Output
For each test case, output one line containing Case #x: y
, where x
is the test case number (starting from 1) and y
is minimum possible sugar content.
Limits
Time limit: 15 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100
M ≤ N*(N-1)/2
1 ≤ Ci ≤ N, for all i.
1 ≤ Di ≤ N, for all i.
Ci ≠ Di, for all i.
Every {Ci, Di} is distinct.
Test set 1 (Visible)
1 ≤ N ≤ 100.
0 ≤ M ≤ 100.
Test set 2 (Hidden)
For at least 90% of the test cases:
1 ≤ N ≤ 1000.
0 ≤ M ≤ 1000.
For all test cases:
1 ≤ N ≤ 105.
0 ≤ M ≤ 105.
Sample
Input | Output |
| |
In the first sample case, there are two cherries and they are connected with a black strand. Removing any of the strand causes cherries to get disconnected. Hence, the minimum sugar content is 1.
In the second sample case, we can keep the black strand between cherry numbered 2 and cherry numbered 3, and remove any of the red strands, which leads to a minimum sugar content of 3.
主要就是并查集的应用
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
int t;
int m,n;
int che[100001];
int find(int x){
return che[x]==x?x:che[x]=find(che[x]);
}
int main(){
scanf("%d",&t);
for(int k=0;k<t;k++){
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++){
che[i]=i;
}
int c,d;
int ans=0;
int tn=n;
for(int j=0;j<m;j++){
scanf("%d %d",&c,&d);
int x=find(c);
int y=find(d);
ans=ans+1;
if(x!=y){
tn--;
}
else{
che[x]=y;
}
}
ans=ans+2*(tn-1);
printf("Case #%d: %d\n",k+1,ans);
}
return 0;
}
/*
2
2 1
1 2
3 1
2 3
*/
2.Code-Eat Switcher
Problem
Umon is a foodie coder. Do you know what two activities that he loves the most? Of course, coding and eating! He always spends the whole day doing only those two activities. However, he thinks that some times of the day are better spent coding, and others are better spent eating.
To illustrate this problem, Umon divides his day into S time slots. During the i-th time slot, if Umon codes 100% of the time, he will achieve Ci units of coding. On the other hand, if he eats 100% of the time, he will achieve Eiunits of eating. But of course, Umon can also use only a fraction of the time for coding, and the remaining for eating. Formally, he will choose a real number f (0 ≤ f ≤ 1), code for f of the time, and use the remaining (1 - f) time to eat. This way, he will achieve f × Ci units of coding and (1 - f) × Ei units of eating. The total amount of coding Umon achieves for the day is simply the sum of all units of coding he achieved in each of the time slots. The total amount of eating is calculated in a similar way.
Umon needs to plan his schedule for the next D days. On the i-th day, he needs to achieve at least a total amount of Ai units of coding and Bi units of eating. For each day, determine whether there is a way for Umon to achieve his target.
Input
The first line of input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing two integers D and S, the number of days and the number of time slots in a day, respectively.
Then S lines follow, each describing a time slot. The i-th line contains two integers Ci and Ei, the amount of coding units achieved if Umon codes for 100% of the time slot, and the amount of eating units achieved if he eats for 100% of the time slot, respectively.
Then D lines follow, each describing a day. The i-th line contains two integers Ai and Bi, the minimal total amount of coding and eating that needs to be achieved on that day.
Output
For each test case, output one line containing Case #x: y
, where x
is the test case number (starting from 1) and y
is a string with D characters, where the i-th character is Y
if there exists a schedule that can fulfill the target for the i-th day, otherwise it should be N
.
Limits
Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
1 ≤ Ci ≤ 104, for all i.
1 ≤ Ei ≤ 104, for all i.
0 ≤ Ai ≤ 108, for all i.
0 ≤ Bi ≤ 108, for all i.
Test set 1 (Visible)
1 ≤ S ≤ 2.
1 ≤ D ≤ 10.
Test set 2 (Hidden)
For at least 90% of the test cases:
1 ≤ S ≤ 103.
1 ≤ D ≤ 103.
For all test cases:
1 ≤ S ≤ 105.
1 ≤ D ≤ 105.
Sample
Input | Output |
| |
In the first sample case, there are 4 days and 2 time slots for each day.
- For day 1, Umon can just eat 100% for both time slots, and therefore achieving a total of 0 units of coding and 8 + 10 = 18 units of eating, thus reaching the target.
- For day 2, Umon can eat 100% of the time for the first time slot, and use 50% of the second time slot for coding and 50% for eating, achieving a total of 0 × 3 + 0.5 × 6 = 3 units of coding, and 1 × 8 + 0.5 × 10 = 13 units of eating, thus reaching the target.
- For day 3, it is impossible to get a total of 10 units of coding.
- For day 4, there are an infinite amount of ways to achieve the target. One possible strategy is to code 42% (and eat 58%) in the first time slot, then code 98.76% (and eat 1.24%) in the second time slot. That strategy yields a total of 0.42 × 3 + 0.9876 × 6 = 7.1856 units of coding, and 0.58 × 8 + 0.0124 × 10 = 4.764 units of eating.
Thus, the answer should be YYNY.
In the second sample case, note that the value of characteristics for the time slots may not necessarily be different from each other.
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
bool piisort(pii a, pii b) {
// a.first / a.second > b.first / b.second
return a.first * (ll)b.second > a.second * (ll)b.first;
}
void solve() {
int q, n;
cin >> q >> n;
vector<pii> v;
while(n--) {
int a, b;
cin >> a >> b;
v.push_back(make_pair(a,b));
}
sort(v.begin(), v.end(), piisort);
vector<ll> aPref, bPref;
aPref.push_back(0);
bPref.push_back(0);
for(int i = 0; i < v.size(); i++) {
aPref.push_back(aPref.back() + v[i].first);
bPref.push_back(bPref.back() + v[v.size()-1-i].second);
}
while(q--) {
int aWant, bWant;
cin >> aWant >> bWant;
int lhs = 0;
int rhs = aPref.size()-1;
while(lhs != rhs) {
int mid = (lhs+rhs+1)/2;
if(aPref[mid] < aWant) lhs = mid;
else rhs = mid-1;
}
if(lhs == aPref.size()-1) {
if(aPref[lhs] >= aWant && bPref[0] >= bWant) cout << "Y";
else cout << "N";
}
else {
int idx = lhs;
double aCurr = aPref[idx];
double bCurr = bPref[v.size()-1-idx];
double aNeed = (aWant - aCurr) / v[idx].first;
bCurr += (1-aNeed) * v[idx].second;
if(bCurr + 1e-6 >= bWant) {
cout << "Y";
}
else {
cout << "N";
}
}
}
cout << "\n";
}
void casesolve() {
int t;
cin >> t;
for(int casenum = 1; casenum <= t; casenum++) {
cout << "Case #" << casenum << ": ";
solve();
}
}
int main() {
casesolve();
}
3.Street Checkers
Problem
Alice and Bob are playing a new virtual reality team game - Street Checkers. The game is set on a very long street divided into tiles which are numbered from 0 to 109(inclusive of both). At the start of the game, Alice and Bob are standing on tile number 0 and are given a random number X in range [L, R] (both ends are inclusive). Alice only jumps to odd numbered tiles, while Bob only jumps to even numbered tiles. If the number on the tile divides X, then the player landing on it has to color it with their favorite color. The game is over after tile X has been colored.
A game is considered interesting by both the players if the absolute difference between the number of tiles painted by each is not greater than 2. Help Alice and Bob find how many numbers in the interval [L, R] could make for an interesting game.
Input
The first line of the input gives the number of test cases, T. T lines follow each containing two integers L and R, the start and end of the interval used to generate the random number X.
Output
For each test case, output one line containing Case #x: y
, where x
is the test case number (starting from 1) and y
is the count of numbers in interval [L, R] which results in an interesting game for Alice and Bob.
Limits
Time limit: 40 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
0 ≤ R - L ≤ 105.
Test set 1 (Visible)
1 ≤ L ≤ R ≤ 106.
Test set 2 (Hidden)
1 ≤ L ≤ R ≤ 109.
Sample
Input | Output |
| |
For the first sample case, let us look at all the possible number in range [5, 10]:
- 5 - Alice would paint 2 tiles : {1, 5}, and Bob would not paint any tile. The game would be interesting since the absolute difference is 2.
- 6 - Alice would paint 2 tiles : {1, 3}, and Bob would paint 2 tiles : {2, 6}. The game would be interesting since the absolute difference is 0.
- 7 - Alice would paint 2 tiles : {1, 7}, and Bob would not paint any tile. The game would be interesting since the absolute difference is 2.
- 8 - Alice would paint 1 tile : {1}, and Bob would paint 3 tiles : {2, 4, 8}. The game would be interesting since the absolute difference is 2.
- 9 - Alice would paint 2 tiles : {1, 3, 9}, and Bob would not paint any tile. The game would not be interesting since the absolute difference is greater than 2.
- 10 - Alice would paint 2 tiles : {1, 5}, and Bob would paint 2 tiles : {2, 10}. The game would be interesting since the absolute difference is 0.
Thus, the answer for this test case is 5.
In the second sample case, we have only one number 102. Alice would paint 4 tiles : {1, 3, 17, 51} while Bob would paint 4 tiles : {2, 6, 34, 102}. The game would be interesting since the absolute difference is 0.
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
void genDivs(vector<int> v, int lhs, int rhs, vector<int>& allDivs) {
assert(v.size() == allDivs.size());
for(int i = 0; i < allDivs.size(); i++) allDivs[i] = 1;
for(int i = 2; i * i <= rhs; i++) {
int x = (lhs/i) * i;
while(x <= rhs) {
if(x >= lhs) {
int add = 1;
while(v[x-lhs] % i == 0) {
add++;
v[x-lhs] /= i;
}
allDivs[x-lhs] *= add;
}
x += i;
}
}
for(int i = 0; i < v.size(); i++) {
if(v[i] > 1) allDivs[i] *= 2;
}
}
void solve() {
int lhs, rhs;
cin >> lhs >> rhs;
vector<int> v;
vector<int> allDivs;
for(int i = lhs; i <= rhs; i++) {
v.push_back(i);
}
allDivs.resize(v.size());
genDivs(v, lhs, rhs, allDivs);
for(int i = 0; i < v.size(); i++) {
while(v[i]%2 == 0) v[i] /= 2;
}
vector<int> evenDivs;
evenDivs.resize(v.size());
genDivs(v, lhs, rhs, evenDivs);
int ret = 0;
for(int i = 0; i < evenDivs.size(); i++) {
int odd = evenDivs[i];
int even = allDivs[i] - evenDivs[i];
// cerr << i << ": " << even << " " << odd << endl;
if(abs(even - odd) <= 2) ret++;
}
cout << ret << "\n";
}
void casesolve() {
int t;
cin >> t;
for(int casenum = 1; casenum <= t; casenum++) {
cout << "Case #" << casenum << ": ";
solve();
}
}
int main() {
casesolve();
}
快手
1.
(by Waple)
N = int(input().strip())
M = int(input().strip())
a = []
pic = []
st = -1
for index in range(M):
tmp = input().strip()
cat, i = tmp.split('_')
if cat == 'V':
a.append(tmp)
else:
if st == -1:
st = index
pic.append(tmp)
res = []
m = st
last = 0
while m > 0:
res.append(a.pop(0))
m -= 1
while a and pic:
if pic:
res.append(pic.pop(0))
last = 0
for _ in range(N-1):
if a:
res.append(a.pop(0))
last += 1
#last
if pic and last >= N-1:
res.append(pic.pop(0))
while a:
res.append(a.pop(0))
print(len(res))
for i in range(len(res)):
print(res[i])
2.
3.健身问题
不知道为啥,通过0%
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
int n;
int dis[100001];
int e[100001];
vector<pair<int,int> > pii;
int main(){
scanf("%d",&n);
for(int k=0;k<n;k++){
scanf("%d",&dis[k]);
}
for(int k=0;k<n;k++){
scanf("%d",&e[k]);
}
for(int k=0;k<n;k++){
int t=2*dis[k]+e[k];
pii.push_back(make_pair(t,k));
}
sort(pii.begin(),pii.end());
int ans=0;
int maxd=0;
for(int k=n-1;k>=0;k--){
if(k==n-1){
ans=pii[k].first;
maxd=dis[pii[k].second];
}
else{
ans=ans+e[pii[k].second];
if(maxd<dis[pii[k].second]){
ans=ans+2*(dis[pii[k].second]-maxd);
maxd=dis[pii[k].second];
}
}
cout<<ans<<endl;
}
return 0;
}
/*
5
1 2 3 4 5
1 1 1 1 1
5
1 2 5 3 4
12 1 1 1 1
*/
4.
python大法好(by Waple)
res = []
s = input()
for i in range(10000):
tmp = s.replace('X', str(i))
eval1, eval2 = tmp.split('=')
ans1, ans2 = eval(eval1), eval(eval2)
if ans1 == ans2:
res.append(i)
if len(res) == 0 or len(res) > 1:
print(-1)
else:
print(res[0])