有一个由很多木棒构成的集合,每个木棒有对应的长度,请问能否用集合中的这些木棒以某个顺序首尾相连构成一个面积大于 0 的简单多边形且所有木棒都要用上,简单多边形即不会自交的多边形。
初始集合是空的,有两种操作,要么给集合添加一个长度为 L 的木棒,要么删去集合中已经有的某个木棒。每次操作结束后你都需要告知是否能用集合中的这些木棒构成一个简单多边形。
有一个由很多木棒构成的集合,每个木棒有对应的长度,请问能否用集合中的这些木棒以某个顺序首尾相连构成一个面积大于 0 的简单多边形且所有木棒都要用上,简单多边形即不会自交的多边形。
初始集合是空的,有两种操作,要么给集合添加一个长度为 L 的木棒,要么删去集合中已经有的某个木棒。每次操作结束后你都需要告知是否能用集合中的这些木棒构成一个简单多边形。
每组测试用例仅包含一组数据,每组数据第一行为一个正整数 n 表示操作的数量(1 ≤ n ≤ 50000) , 接下来有n行,每行第一个整数为操作类型 i (i ∈ {1,2}),第二个整数为一个长度 L(1 ≤ L ≤ 1,000,000,000)。如果 i=1 代表在集合内插入一个长度为 L 的木棒,如果 i=2 代表删去在集合内的一根长度为 L 的木棒。输入数据保证删除时集合中必定存在长度为 L 的木棒,且任意操作后集合都是非空的。
对于每一次操作结束有一次输出,如果集合内的木棒可以构成简单多边形,输出 "Yes" ,否则输出 "No"。
5 1 1 1 1 1 1 2 1 1 2
No No Yes No No
import java.util.ArrayList; import java.util.Scanner; public class DailyNews2 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); int[][] op = new int[n][2]; for (int i = 0; i < n; i++) { op[i][0] = scanner.nextInt(); op[i][1] = scanner.nextInt(); } stickPuzzle(n, op); } } public static void stickPuzzle(int n, int[][] op) { ArrayList<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { if (op[i][0] == 1) { list.add(op[i][1]); } else { list.remove(new Integer(op[i][1])); } if (canFormPoly(list)) { System.out.println("Yes"); } else { System.out.println("No"); } } } //判断能否构成多边形 //list:各边长 public static boolean canFormPoly(ArrayList<Integer> list) { int len = list.size(); for (int i = 0; i < len; i++) { int temp = list.remove(i); int sum = 0; for (int j = 0; j < len - 1; j++) { sum += list.get(j); } if (sum <= temp) { //判断是否任意len-1条边之和大于剩余一条边 list.add(i, temp); return false; } list.add(i, temp); } return true; } }
#include <iostream> #include <string> #include <map> using namespace std; map<int, int, greater<int>> mp; int num = 0; long long sum = 0; string solution(int a, int b) { if (a == 1) { mp[b] ++; num++; sum += b; } else { if (mp[b] == 1) mp.erase(b); else { mp[b] --; } num--; sum -= b; } if (num <= 2) return "No"; map<int, int>::iterator it = mp.begin(); int maxa = it->first; if (maxa < sum - maxa) return "Yes"; else return "No"; } int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { int a, b; cin >> a >> b; cout << solution(a, b) << endl; } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n = sc.nextInt(); ArrayList<Integer> ls= new ArrayList<>(); for (int i=0;i<n;i++){ int op = sc.nextInt(); int length = sc.nextInt(); if (op==1){ ls.add(length); }else{ int idx = ls.indexOf(length); ls.remove(idx); } if (ls.size() < 3){ System.out.println("No"); }else{ Collections.sort(ls,Collections.reverseOrder()); int max = ls.get(0); int sum = 0; for (int j=1;j<ls.size();j++){ sum += ls.get(j); } if(sum>max){ System.out.println("Yes"); }else{ System.out.println("No"); } } } } }平民算法。。。
n = int(input()) r = [] while n>0: op, l = [int(ele) for ele in input().split(' ')] if op==1: r.append(l) else: r.remove(l) if len(r)<=2: print('No') else: if sum(r)>2*max(r): print('Yes') else: print('No') n-=1
木棍长度用long存储,否则会报数组越界 package leetcode.exams.toutiao; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; /* * mind long is very important */ public class StickPuzzle { public static void main(String[] args) { Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int n=sc.nextInt(); long[][] data=new long[n][2]; for(int i=0;i<n;i++){ data[i][0]=sc.nextLong(); data[i][1]=sc.nextLong(); } dealWith(data); } } public static void dealWith(long[][] data){ List<Long> sticks=new ArrayList<Long>(); for(int i=0;i<data.length;i++){ if(data[i][0]==1) sticks.add(data[i][1]); else sticks.remove(data[i][1]); judge(sticks); } } public static void judge(List<Long> sticks){ if(sticks.size()<3) System.out.println("No"); else{ Collections.sort(sticks); long sum=0; int i=0; for(i=0;i<sticks.size()-1;i++){ sum+=sticks.get(i); } if(sum>sticks.get(i)) System.out.println("Yes"); else System.out.println("No"); } } }
#include <iostream> #include <algorithm> #include <list> using namespace std; int main(){ int n, sumLength; list<int> ls; cin >> n; while(n--){ int i, L; cin >> i >> L; if(i == 1) { sumLength += L; ls.push_back(L); } if(i == 2) { sumLength -= L; list<int>::iterator it = find(ls.begin(), ls.end(), L); // list<int>::iterator it = ls.find(L); ls.erase(it); } // // if(ls.size() < 3) { // cout << "NO" << endl; // continue; // } int maxLength = 0; for(list<int>::iterator it = ls.begin(); it !=ls.end(); it++) { if(maxLength < *it) maxLength = *it; } if(sumLength-maxLength > maxLength) cout << "YES" << endl; else cout << "NO" << endl; // n--; } return 0; } 求教:为什么我的输出只有一行??
import java.security.PublicKey; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); int num = 0; int j = 0; int max = 0; List<Integer> list = new ArrayList<>(); while (j++ < n) { int i = sc.nextInt(); int l = sc.nextInt(); sc.nextLine(); if (i == 1) { list.add(l); num += l; } else { list.remove(list.indexOf(l)); num -= l; } Collections.sort(list); if(list.size() > 0){ max = list.get(list.size()-1); } if (num - max <= max) { System.out.println("No"); } else { System.out.println("Yes"); } } } }
#include <bits/stdc++.h> using namespace std; int main() { int n, op, len; long long sum = 0; cin >> n; multiset<int> st; while (n--) { scanf("%d%d", &op, &len); if (op == 2) { st.erase(st.find(len)); sum -= len; } else { st.insert(len); sum += len; } if (st.empty()) cout << "No\n"; else { int t = *st.rbegin(); if (sum-t > t && st.size() > 2) cout << "Yes\n"; else cout << "No\n"; } } }
container = [] n = int(input()) while n > 0: i, L = list(map(int, input().split())) if i == 1: container.append(L) if len(container) <= 2: print("No") else: if max(container) < sum(container) - max(container): print("Yes") else: print("No") else: container.remove(L) if len(container) <= 2: print("No") else: if max(container) < sum(container) - max(container): print("Yes") else: print("No") n -= 1
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Comparator<Integer> cmp = new Comparator<Integer>() { public int compare(Integer e1, Integer e2) { return e2 - e1; } }; PriorityQueue<Integer> queue = new PriorityQueue<>(cmp); while (in.hasNext()){ int n = in.nextInt(); int shape =0; for (int i = 0; i < n ; i++) { int just = in.nextInt(); int l = in.nextInt(); if(just == 1){ queue.add(l); }else { queue.remove(l); } shape = queue.peek(); List<Integer> list = new ArrayList<>(queue); if(list.size()>=3){ int len = 0; for (int j = 0; j < list.size() ; j++) { len+= list.get(j); } if((len-shape)>shape){ System.out.println("Yes"); }else { System.out.println("No"); } }else { System.out.println("No"); } } } } }
把这个多边形看成一个三角形;把集合里长度最大的拿出来,再把剩下的所有边相加,只要这些边的长度之和大于最长的那边,就可以组成一个三角形;多边形是由三边或三边以上的边构成的。
ops=int(input()) sticks=[] act=[] maxlen=0 totallen=0 flag=True for i in range(ops): Strinput =input() act =Strinput.split() #操作部分 ifact[0]=='1': sticks.append(int(act[1])) totallen+=int(act[1]) ifact[0]=='2': sticks.remove(int(act[1])) totallen-=int(act[1]) maxlen=max(sticks) #判断部分 ifmaxlen<totallen-maxlen: flag=True else: flag=False iflen(sticks)<3: flag=False ifflag==True: print('Yes') else:
print('No')
用蟒蛇的时候偶尔偷偷懒用用自带函数也挺好的
package com.itshw;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class test02 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
List<Integer> list=new ArrayList<>();
int i=0;
while (n>0){
int op=sc.nextInt();
int l=sc.nextInt();
if(op==1){
list.add(l);
canForm(list);
}
if(!list.isEmpty()&&op==2&&list.contains(l)){
int index=list.indexOf(l);
list.remove(index);
canForm(list);
}
n--;
}
}
private static void canForm(List<Integer> list) {
if(list.isEmpty()||list.size()<3){
System.out.println("No");
return;
}
int sum=0;
int max=list.get(0);
for(int i=0;i<list.size();i++){
int temp=list.get(i);
sum+=temp;
if(temp>max){
max=temp;
}
}
if(max<sum-max){
System.out.println("Yes");
}
else{
System.out.println("No");
}
}
}
利用c++的红黑树map
#include <iostream>
#include <map>
using namespace std;
int main() {
int n = 0;
scanf("%d", &n);
map<long long, long long> m;
long long sum = 0;
int count = 0;
for (int i = 0; i < n; i++) {
int choice;
int len;
scanf("%d %d", &choice, &len);
if (choice == 1) {
m[len]++;
count++;
sum += len;
} else {
count--;
if (--m[len] <= 0) {
m.erase(len);
}
sum -= len;
}
if (count >= 3 && m.rbegin()->first < sum - m.rbegin()->first) {
printf("Yes\n");
} else {
printf("No\n");
}
}
}
堆优化
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;
priority_queue<long long int> pq;
long long int sum, l;
int a;
long long int b;
map<long long int, int> countt;
int nowcount;
int main() {
//freopen("input.txt", "r", stdin);
int n;
cin >> n;
sum = 0;
nowcount = 0;
while(n -- ) {
cin >> a >> b;
if (a == 1) {
pq.push(b);
sum += b;
countt[b] ++;
nowcount ++;
} else {
nowcount --;
countt[b] --;
sum -= b;
}
if (nowcount < 3) {
puts("No");
continue;
}
long long int temp = pq.top();
while(countt[temp] == 0) {
pq.pop();
temp = pq.top();
}
if (sum - temp > temp) {
puts("Yes");
} else {
puts("No");
}
}
return 0;
}