十六届CB组暴力
第三题
10分
#include<iostream>
using namespace std;
int main() {
int n, ret = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
if (x != 1) ret++;
}
cout << ret;
return 0;
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int ret = 0;
for (int i = 1; i <= n; i++) {
int x = sc.nextInt();
if (x != 1) ret++;
}
System.out.println(ret);
}
}
import sys
input = sys.stdin.readline
n = int(input())
arr = list(map(int, input().split()))
ret = 0
for x in arr:
if x != 1:
ret += 1
print(ret)
第四题
5分
#include<bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--) {
int A, B, C, K;
cin >> A >> B >> C >> K;
while (K--) {
int nA = (B + C) / 2;
int nB = (A + C) / 2;
int nC = (A + B) / 2;
A = nA;
B = nB;
C = nC;
}
cout << A << " " << B << " " << C << "\n";
}
return 0;
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while (T-- > 0) {
int A = sc.nextInt();
int B = sc.nextInt();
int C = sc.nextInt();
int K = sc.nextInt();
while (K-- > 0) {
int nA = (B + C) / 2;
int nB = (A + C) / 2;
int nC = (A + B) / 2;
A = nA;
B = nB;
C = nC;
}
System.out.println(A + " " + B + " " + C);
}
}
}
import sys
input = sys.stdin.readline
T = int(input())
while T:
T -= 1
A, B, C, K = map(int, input().split())
while K:
K -= 1
nA = (B + C) // 2
nB = (A + C) // 2
nC = (A + B) // 2
A, B, C = nA, nB, nC
print(A, B, C)
10分
#include<bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--) {
int A, B, C, K;
cin >> A >> B >> C >> K;
while (K--) {
int nA = (B + C) / 2;
int nB = (A + C) / 2;
int nC = (A + B) / 2;
A = nA;
B = nB;
C = nC;
if (A == B && B == C) break;
}
cout << A << " " << B << " " << C << "\n";
}
return 0;
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while (T-- > 0) {
int A = sc.nextInt();
int B = sc.nextInt();
int C = sc.nextInt();
int K = sc.nextInt();
while (K-- > 0) {
int nA = (B + C) / 2;
int nB = (A + C) / 2;
int nC = (A + B) / 2;
A = nA;
B = nB;
C = nC;
if (A == B && B == C) break;
}
System.out.println(A + " " + B + " " + C);
}
}
}
import sys
input = sys.stdin.readline
T = int(input())
while T:
T -= 1
A, B, C, K = map(int, input().split())
while K:
K -= 1
nA = (B + C) // 2
nB = (A + C) // 2
nC = (A + B) // 2
A, B, C = nA, nB, nC
if A == B and B == C:
break
print(A, B, C)
第五题
15分
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef long long ll;
int a[N],n,m;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
sort(a+1,a+n+1);
ll ans=1e18;
for(int l=1;l+m-1<=n;l++){
int r=l+m-1;
ans=min(ans,1ll*a[r]*a[r]-1ll*a[l]*a[l]);
}
cout<<ans;
return 0;
}
import java.util.*;
public class Main {
static final int N = 100010;
static long[] a = new long[N];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
for(int i = 1; i <= n; i++) a[i] = sc.nextLong();
Arrays.sort(a, 1, n + 1);
long ans = (long)1e18;
for(int l = 1; l + m - 1 <= n; l++){
int r = l + m - 1;
ans = Math.min(ans, a[r] * a[r] - a[l] * a[l]);
}
System.out.println(ans);
}
}
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
ans = 10**18
for l in range(0, n - m + 1):
r = l + m - 1
ans = min(ans, a[r] * a[r] - a[l] * a[l])
print(ans)
第八题
10分
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10,mod=1e9+7;
typedef long long ll;
int a[N],s[N];
int n;
int ans=0;
void dfs(int u){
if(u>=n){
stack<ll> stk1,stk2;
stk1.push(a[1]);
for(int i=1;i<u;i++){
if(s[i]==1||s[i]==2){
stk2.push(s[i]);
stk1.push(a[i+1]);
}else{
int t1=stk1.top();
stk1.pop();
stk1.push(t1^a[i+1]);
}
}
while(!stk2.empty()){
int t=stk2.top(); stk2.pop();
int t1=stk1.top(); stk1.pop();
int t2=stk1.top(); stk1.pop();
if(t==1) stk1.push(t1+t2);
else stk1.push(t2-t1);
}
ans=(ans+stk1.top())%mod;
return;
}
s[u]=1; dfs(u+1);
s[u]=2; dfs(u+1);
s[u]=3; dfs(u+1);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
dfs(1);
cout<<ans;
return 0;
}
import java.util.*;
public class Main {
static final int N = 100010;
static final int mod = 1000000007;
static int[] a = new int[N];
static int[] s = new int[N];
static int n;
static long ans = 0;
static void dfs(int u){
if(u >= n){
Stack<Long> stk1 = new Stack<>();
Stack<Integer> stk2 = new Stack<>();
stk1.push((long)a[1]);
for(int i=1;i<u;i++){
if(s[i]==1||s[i]==2){
stk2.push(s[i]);
stk1.push((long)a[i+1]);
}else{
long t1=stk1.pop();
stk1.push(t1^a[i+1]);
}
}
while(!stk2.isEmpty()){
int t=stk2.pop();
long t1=stk1.pop();
long t2=stk1.pop();
if(t==1) stk1.push(t1+t2);
else stk1.push(t2-t1);
}
ans=(ans+stk1.peek())%mod;
return;
}
s[u]=1; dfs(u+1);
s[u]=2; dfs(u+1);
s[u]=3; dfs(u+1);
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
for(int i=1;i<=n;i++) a[i]=sc.nextInt();
dfs(1);
System.out.println(ans);
}
}
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
mod = 10**9+7
n = int(input())
a = [0] + list(map(int,input().split()))
s = [0]*(n+1)
ans = 0
def dfs(u):
global ans
if u >= n:
stk1 = [a[1]]
stk2 = []
for i in range(1,u):
if s[i]==1 or s[i]==2:
stk2.append(s[i])
stk1.append(a[i+1])
else:
t1 = stk1.pop()
stk1.append(t1 ^ a[i+1])
while stk2:
t = stk2.pop()
t1 = stk1.pop()
t2 = stk1.pop()
if t==1:
stk1.append(t1+t2)
else:
stk1.append(t2-t1)
ans = (ans + stk1[-1]) % mod
return
s[u]=1; dfs(u+1)
s[u]=2; dfs(u+1)
s[u]=3; dfs(u+1)
dfs(1)
print(ans)